Skip to content

Commit

Permalink
solana: Let redeem code return error instead of try_release
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsaigle committed Apr 2, 2024
1 parent 8fa15bc commit e87c404
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct ReleaseInbound<'info> {

#[derive(AnchorDeserialize, AnchorSerialize)]
pub struct ReleaseInboundArgs {
pub revert_on_delay: bool,
pub revert_when_not_ready: bool,
}

// Burn/mint
Expand All @@ -54,8 +54,8 @@ pub struct ReleaseInboundMint<'info> {
}

/// Release an inbound transfer and mint the tokens to the recipient.
/// When `revert_on_delay` is true, the transaction will revert if the
/// release timestamp has not been reached. When `revert_on_delay` is false, the
/// When `revert_when_not_ready` is true, the transaction will revert if the
/// release timestamp has not been reached. When `revert_when_not_ready` is false, the
/// transaction succeeds, but the minting is not performed.
/// Setting this flag to `false` is useful when bundling this instruction
/// together with [`crate::instructions::redeem`] in a transaction, so that the minting
Expand All @@ -69,8 +69,14 @@ pub fn release_inbound_mint(
let released = inbox_item.try_release()?;

if !released {
if args.revert_on_delay {
return Err(NTTError::CantReleaseYet.into());
if args.revert_when_not_ready {
match inbox_item.release_status {
ReleaseStatus::NotApproved => return Err(NTTError::TransferNotApproved.into()),
ReleaseStatus::ReleaseAfter(_) => return Err(NTTError::CantReleaseYet.into()),
// Unreachable: if released, [`InboxItem::try_release`] will return an Error immediately
// rather than Ok(bool).
ReleaseStatus::Released => return Err(NTTError::TransferAlreadyRedeemed.into()),
}
} else {
return Ok(());
}
Expand Down Expand Up @@ -112,8 +118,8 @@ pub struct ReleaseInboundUnlock<'info> {
}

/// Release an inbound transfer and unlock the tokens to the recipient.
/// When `revert_on_delay` is true, the transaction will revert if the
/// release timestamp has not been reached. When `revert_on_delay` is false, the
/// When `revert_when_not_ready` is true, the transaction will revert if the
/// release timestamp has not been reached. When `revert_when_not_ready` is false, the
/// transaction succeeds, but the unlocking is not performed.
/// Setting this flag to `false` is useful when bundling this instruction
/// together with [`crate::instructions::redeem`], so that the unlocking
Expand All @@ -127,8 +133,14 @@ pub fn release_inbound_unlock(
let released = inbox_item.try_release()?;

if !released {
if args.revert_on_delay {
return Err(NTTError::CantReleaseYet.into());
if args.revert_when_not_ready {
match inbox_item.release_status {
ReleaseStatus::NotApproved => return Err(NTTError::TransferNotApproved.into()),
ReleaseStatus::ReleaseAfter(_) => return Err(NTTError::CantReleaseYet.into()),
// Unreachable: if released, [`InboxItem::try_release`] will return an Error immediately
// rather than Ok(bool).
ReleaseStatus::Released => return Err(NTTError::TransferAlreadyRedeemed.into()),
}
} else {
return Ok(());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl InboxItem {
let now = current_timestamp();

match self.release_status {
ReleaseStatus::NotApproved => Err(NTTError::TransferNotApproved.into()),
ReleaseStatus::NotApproved => Ok(false),
ReleaseStatus::ReleaseAfter(release_timestamp) => {
if release_timestamp > now {
return Ok(false);
Expand Down

0 comments on commit e87c404

Please sign in to comment.