Skip to content

setup reclaim order#53

Merged
kaze-cow merged 32 commits into
mainfrom
kaze/sc-150-close-order-account
Jul 17, 2026
Merged

setup reclaim order#53
kaze-cow merged 32 commits into
mainfrom
kaze/sc-150-close-order-account

Conversation

@kaze-cow

@kaze-cow kaze-cow commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Description

Adds the ReclaimOrder instruction. Also add some more capabilities to the test helpers.

Changes

The ReclaimOrder instruction is fairly simple: anyone can call this instruction once an order has expired to close the account and return the rent to the original order creator.

Besides the addition of the instruction itself, I found it would be very helpful if the unit tests could do more to validate the behavior of the function. So I added a new account initialization fixture, fake_account_with_data, which allows for creating an account that contains data that can subsequently be used to validate more conditions with particular input order PDAs. Unfortunately, due to the unusual dynamic memory layout of the RuntimeAccount type which puts the account data contiguously after the actual data is set, .

Additionally, Clock::get() always returns an error outside of a solana target_os context, so I replaced calls to this with a helper function get_timestamp that is stubbed to a static value (arbitrarily chosen to be 2026/01/01) for unit tests.

Finally, while writing the unit tests, I noticed its not ergonomic to create new order data because Default trait was not in use. So I added it using #[derive] macro. In the future we may want to refactor more of the tests to remove helpers like sample_intent which basically just give you a plain intent. The default pattern is just a lot more ergonomic and standardized.

With these optimizations, testing a function which has an input order PDA turned out to be relatively ergonomic in unit tests:

        let order_data = OrderAccount {
            created_by: Address::new_unique(),
            ..Default::default()
        };

        let order_pda = fake_account_with_data(Address::new_unique(), &EncodedOrderAccount::from(order_data)[..]);

How to test

Check CI. Observe the changes and consider if the general direction. Evaluate if the usage of additional unsafe code is warranted for what it accomplishes.

@kaze-cow
kaze-cow requested a review from a team as a code owner July 1, 2026 06:58
@linear-code

linear-code Bot commented Jul 1, 2026

Copy link
Copy Markdown

Comment thread programs/settlement/src/processor.rs Outdated
@kaze-cow kaze-cow mentioned this pull request Jul 6, 2026
Comment thread interface/src/instruction/mod.rs

@fedgiac fedgiac 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.

Mostly minor comments.
The only notable points are:

  • I think there's a security issue because we don't validate the PDA before clearing it. Should be reasonably easy to address.
  • I still don't like the complexity introduced to handle time in unit tests. I think it's fine to just do the one relevant test as an integration test. (Same if there were more.)
  • I added a suggestion on how to (imho) improve the unsafe block.

Comment thread interface/src/instruction/reclaim_order.rs
Comment thread interface/src/instruction/reclaim_order.rs
Comment thread interface/src/instruction/reclaim_order.rs Outdated
Comment thread programs/settlement/src/reclaim_order.rs Outdated
Comment thread programs/settlement/src/reclaim_order.rs Outdated
Comment thread programs/settlement/tests/reclaim_order.rs Outdated
Comment thread programs/settlement/src/reclaim_order.rs Outdated
Comment thread programs/settlement/tests/reclaim_order.rs
Comment thread programs/settlement/tests/reclaim_order.rs Outdated
Comment thread programs/settlement/tests/reclaim_order.rs Outdated
kaze-cow and others added 13 commits July 8, 2026 13:24
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
…checks from pda

this requires adding bumps to the input data of the reclaimorder function
@kaze-cow
kaze-cow requested a review from fedgiac July 8, 2026 06:53

@fedgiac fedgiac 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.

The logic looks good and the important comments have been addressed, only small details left. Also, please update the PR description with the latest changes before merging!

Comment thread interface/src/data/order.rs
Comment thread interface/src/data/order.rs Outdated
Comment thread interface/src/instruction/mod.rs Outdated
Comment thread interface/src/instruction/reclaim_order.rs
Comment thread interface/src/instruction/reclaim_order.rs Outdated
Comment thread programs/settlement/src/settle/begin.rs
Comment thread interface/src/data/order.rs Outdated
Comment thread programs/settlement/src/reclaim_order.rs Outdated
Comment thread programs/settlement/tests/reclaim_order.rs Outdated
Comment thread programs/settlement/tests/reclaim_order.rs Outdated
Comment on lines +58 to +81
impl OrderAccount {
/// Load and decode the order at the given PDA, and confirm its canonical
pub fn load_from_pda(
order_pda: &AccountView,
program_id: &Address,
bump: u8,
) -> Result<Self, ProgramError> {
let data = order_pda.try_borrow()?;
let bytes: &[u8; EncodedOrderAccount::SIZE] = (&*data)
.try_into()
.map_err(|_| ProgramError::InvalidAccountData)?;
let (account, uid) = EncodedOrderAccount::decode_and_hash(bytes)?;
drop(data);

let expected =
Address::create_program_address(&order_pda_signer_seeds(&uid, &[bump]), program_id)
.map_err(|_| SettlementError::OrderNotCanonical)?;
if &expected != order_pda.address() {
return Err(SettlementError::OrderNotCanonical.into());
}

Ok(account)
}
}

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.

Would it make sense to have traits for account deserialization just like we do for Instruction parsing?

(ref)

@kaze-cow kaze-cow Jul 17, 2026

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.

it would but as of right now this is the only load_from_pda function we have so I didn't want to go too crazy for this PR.

I didn't see any great stdlib traits that would have made sense here either so if we do make a trait it would literally just be defining this function and thats it; and as you mention there are the bump consideration issues.

Comment thread interface/src/data/order.rs Outdated
Comment thread interface/src/data/order.rs Outdated
Comment thread programs/settlement/src/reclaim_order.rs
kaze-cow and others added 9 commits July 17, 2026 16:19
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: tilacog <tilacog@gmail.com>
@kaze-cow
kaze-cow requested review from fedgiac and tilacog July 17, 2026 08:08

@fedgiac fedgiac 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.

Looks good to me!

Comment thread programs/settlement/src/reclaim_order.rs
Comment thread interface/src/data/order.rs Outdated
Comment thread interface/src/data/order.rs
kaze-cow and others added 3 commits July 17, 2026 18:46
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
Co-authored-by: Federico Giacon <58218759+fedgiac@users.noreply.github.com>
@kaze-cow
kaze-cow merged commit 185ae4f into main Jul 17, 2026
12 checks passed
@kaze-cow
kaze-cow deleted the kaze/sc-150-close-order-account branch July 17, 2026 09:54
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