Skip to content

Push proceeds to users in FinalizeSettle#62

Merged
fedgiac merged 48 commits into
mainfrom
push-funds-to-user-processing-in-finalize-settle
Jul 23, 2026
Merged

Push proceeds to users in FinalizeSettle#62
fedgiac merged 48 commits into
mainfrom
push-funds-to-user-processing-in-finalize-settle

Conversation

@fedgiac

@fedgiac fedgiac commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Push each order's proceeds out of the buffers in FinalizeSettle.

This is the final part of the settlement push flow. The previous PR taught BeginSettle to pair each settled order with exactly one push and check the push pays the order's buy token account, but nothing moved yet. Here FinalizeSettle actually performs the transfers.

What push_funds does

BeginSettle already guarantees, for the paired finalize, that the push count matches the order count and that each push's destination is the right buy token account, and the counterpart check guarantees that begin ran.
So the only things left for FinalizeSettle to check is that:

  • each push draws from the canonical buffer for its destination's mint;
  • the buy token is a valid token account.

And naturally, it needs to actually send funds.

How to test

finalize_settle_pushes.rs moves from asserting that pushes merely parse to asserting they actually pay: single-order, several orders sharing one buffer, and several orders drawing from different buffers, each checking both the destinations' credited balances and the buffers' debited balances. It also covers new edge cases.

@fedgiac
fedgiac requested a review from a team as a code owner July 6, 2026 21:29
@fedgiac fedgiac mentioned this pull request Jul 6, 2026
Comment thread programs/settlement/src/settle/finalize.rs Outdated
Comment thread programs/settlement/src/settle/finalize.rs Outdated
Comment thread programs/settlement/tests/finalize_settle_pushes.rs
Comment thread programs/settlement/tests/finalize_settle_pushes.rs Outdated
Comment thread programs/settlement/tests/finalize_settle_pushes.rs
Comment thread programs/settlement/tests/finalize_settle_pushes.rs
Comment thread programs/settlement/tests/finalize_settle_pushes.rs
Comment thread programs/settlement/tests/finalize_settle_pushes.rs Outdated
Comment thread programs/settlement/tests/finalize_settle_pushes.rs Outdated
kaze-cow and others added 23 commits July 14, 2026 17:36
…h-funds-to-user-processing-in-finalize-settle
…h-funds-to-user-processing-in-finalize-settle
Base automatically changed from push-funds-to-user-processing-in-begin-settle to main July 15, 2026 15:06

@tilacog tilacog left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +74 to +77
let (state_pda, state_bump) = Address::find_program_address(&state_pda_seeds(), program_id);
if state_pda_account.address() != &state_pda {
return Err(SettlementError::StateAccountMismatch.into());
}

@tilacog tilacog Jul 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked this fn a lot.

I believe after #49 it will use create_program_address instead of find_program_address, is that right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, then there won't be any reason to run the search loop!

Comment on lines +52 to +61
/// Validate that `token_program_account` is the legacy SPL Token program, which
/// every settlement transfer is issued against.
#[must_use = "ignoring the output may lead to an unintended on-chain state"]
fn validate_token_account(token_program_account: &AccountView) -> ProgramResult {
if token_program_account.address() != &SPL_TOKEN_PROGRAM_ID {
return Err(ProgramError::IncorrectProgramId);
}
Ok(())
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing critical, but I this looks like a good spot to call pinocchio_token::state::Account::from_account_view, which also validates the account data.

I assume any invalid token account would still cause a revert during the transfer call anyway, so we'd just be catching the error earlier.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed this function to stress that this validates only the token program address: bf1b683.

The from_account_view already existed in the current code, so I implemented the suggestion by moving the check from the order processing to this function: 4192b1e.
The price for this is that we go through all orders twice instead of only once, but maybe it isn't too bad.
I didn't include in the PR because I wasn't sure this is the change you intended. WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I don't have a strong opinion about where the validation takes place.
Ideally we'd iterate only once, but I agree it's not a huge gain.

@fedgiac
fedgiac merged commit 5aab53f into main Jul 23, 2026
12 checks passed
@fedgiac
fedgiac deleted the push-funds-to-user-processing-in-finalize-settle branch July 23, 2026 08:11
@fedgiac
fedgiac restored the push-funds-to-user-processing-in-finalize-settle branch July 23, 2026 08:12
@fedgiac
fedgiac deleted the push-funds-to-user-processing-in-finalize-settle branch July 23, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants