Skip to content

Commit e87c404

Browse files
committed
solana: Let redeem code return error instead of try_release
1 parent 8fa15bc commit e87c404

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

solana/programs/example-native-token-transfers/src/instructions/release_inbound.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct ReleaseInbound<'info> {
4343

4444
#[derive(AnchorDeserialize, AnchorSerialize)]
4545
pub struct ReleaseInboundArgs {
46-
pub revert_on_delay: bool,
46+
pub revert_when_not_ready: bool,
4747
}
4848

4949
// Burn/mint
@@ -54,8 +54,8 @@ pub struct ReleaseInboundMint<'info> {
5454
}
5555

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

7171
if !released {
72-
if args.revert_on_delay {
73-
return Err(NTTError::CantReleaseYet.into());
72+
if args.revert_when_not_ready {
73+
match inbox_item.release_status {
74+
ReleaseStatus::NotApproved => return Err(NTTError::TransferNotApproved.into()),
75+
ReleaseStatus::ReleaseAfter(_) => return Err(NTTError::CantReleaseYet.into()),
76+
// Unreachable: if released, [`InboxItem::try_release`] will return an Error immediately
77+
// rather than Ok(bool).
78+
ReleaseStatus::Released => return Err(NTTError::TransferAlreadyRedeemed.into()),
79+
}
7480
} else {
7581
return Ok(());
7682
}
@@ -112,8 +118,8 @@ pub struct ReleaseInboundUnlock<'info> {
112118
}
113119

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

129135
if !released {
130-
if args.revert_on_delay {
131-
return Err(NTTError::CantReleaseYet.into());
136+
if args.revert_when_not_ready {
137+
match inbox_item.release_status {
138+
ReleaseStatus::NotApproved => return Err(NTTError::TransferNotApproved.into()),
139+
ReleaseStatus::ReleaseAfter(_) => return Err(NTTError::CantReleaseYet.into()),
140+
// Unreachable: if released, [`InboxItem::try_release`] will return an Error immediately
141+
// rather than Ok(bool).
142+
ReleaseStatus::Released => return Err(NTTError::TransferAlreadyRedeemed.into()),
143+
}
132144
} else {
133145
return Ok(());
134146
}

solana/programs/example-native-token-transfers/src/queue/inbox.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl InboxItem {
5151
let now = current_timestamp();
5252

5353
match self.release_status {
54-
ReleaseStatus::NotApproved => Err(NTTError::TransferNotApproved.into()),
54+
ReleaseStatus::NotApproved => Ok(false),
5555
ReleaseStatus::ReleaseAfter(release_timestamp) => {
5656
if release_timestamp > now {
5757
return Ok(false);

0 commit comments

Comments
 (0)