fix(escrow): waterfall-distribute funds among collaborators on escrow release#273
Open
macan88 wants to merge 2 commits intodavedumto:mainfrom
Open
fix(escrow): waterfall-distribute funds among collaborators on escrow release#273macan88 wants to merge 2 commits intodavedumto:mainfrom
macan88 wants to merge 2 commits intodavedumto:mainfrom
Conversation
|
@macan88 is attempting to deploy a commit to the david's projects Team on Vercel. A member of the Team first needs to authorize it. |
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an invoice with revenue-split collaborators had its escrow released, the full escrow amount was sent directly to the freelancer's wallet, completely bypassing collaborator shares. This caused conflicting Stellar transactions, left funds stuck in escrow, and broke the revenue-split contract between the freelancer and their sub-contractors.
Root Cause
The escrow release route (
app/api/routes-d/escrow/release/route.ts) queried the invoice withoutinclude: { collaborators: true }, so collaborator data was never available. Even if it had been fetched, the code unconditionally calledsendStellarPaymentonce with 100% of the escrow amount to the invoice owner — there was no waterfall logic at all.Solution
collaborators: { select: { id, email, walletAddress, revenueSharePercent } }to thefindUniquecall so split data is always present.walletAddressand a validrevenueSharePercent, and that the combined share is < 100%. This is checked before the on-chain escrow release and before the DB transaction, so a bad configuration cannot leave the system in a partially-released state.floor(percent/100 * total * 1e7) / 1e7(Stellar's 7-decimal precision). The freelancer receives the exact remainder, ensuring no dust is lost.escrowEventrow atomically with the status flip so operations can replay any Stellar broadcast failure without re-releasing the escrow.Edge Cases Handled
updateManyguard (count !== 1 → throw ESCROW_RELEASE_CONFLICT) is preserved and still protects against concurrent requests.paymentErrorsso the caller and monitoring systems can react without needing server log access. The queuedescrowEventrows enable manual replay.Testing
Closes #269