Skip to content

setup reclaim order#53

Open
kaze-cow wants to merge 6 commits into
mainfrom
kaze/sc-150-close-order-account
Open

setup reclaim order#53
kaze-cow wants to merge 6 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 on lines +68 to +85
/// Current on-chain unix timestamp.
///
/// Off the Solana target (e.g. host-run unit tests), the `Clock` sysvar isn't
/// available, so this returns a fixed 2026-01-01T00:00:00Z timestamp instead.
#[cfg(target_os = "solana")]
#[inline(always)]
pub fn get_timestamp() -> Result<i64, ProgramError> {
use pinocchio::sysvars::{clock::Clock, Sysvar};

Ok(Clock::get()?.unix_timestamp)
}

#[cfg(not(target_os = "solana"))]
#[inline(always)]
pub fn get_timestamp() -> Result<i64, ProgramError> {
Ok(1_767_225_600)
}

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 don't like this approach: it's a landmine for anyone who wants to use the settlement program outside the solana environment. The implicit assumption here is that this is only used for tests, and even then, we'd be testing with code that does something different from what we want to test for. What if unix_timestamp is in a different format, or if this has some meaningful side effect that we wouldn't know (like, increasing the number of CPIs and changing the logs)?
I see that the syscall invocation when os ≠ solana returns UnsupportedSysvar.
Maybe this unfortunately a good reason to actually end up using integration tests only. 😢

@kaze-cow kaze-cow mentioned this pull request Jul 6, 2026
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.

2 participants