setup reclaim order#53
Conversation
| /// 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) | ||
| } | ||
|
|
There was a problem hiding this comment.
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. 😢
prevents potential unexpected behavior anywhere else
…cowprotocol/solana-programs into kaze/sc-150-close-order-account
fedgiac
left a comment
There was a problem hiding this comment.
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.
| let ix = CreateOrder { | ||
| program_id: *program_id, | ||
| owner: owner.pubkey(), | ||
| created_by: owner.pubkey(), | ||
| order_pda: pda, | ||
| intent_bytes: encoded, | ||
| }; |
There was a problem hiding this comment.
Nit: it would be nicer to use the client's CreateOrder instead so you don't need to encode the order data manually. But then you wouldn't have the order PDA.
There was a problem hiding this comment.
doesn't seem clear cut atm, so leaving as is for the moment.
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
Description
Adds the
ReclaimOrderinstruction. 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 theRuntimeAccounttype 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 functionget_timestampthat 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
Defaulttrait 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 likesample_intentwhich 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:
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.