Skip to content

Commit 5eea9b3

Browse files
committed
solana: Let redeem code return error instead of try_release
1 parent 86f5a74 commit 5eea9b3

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
@@ -44,7 +44,7 @@ pub struct ReleaseInbound<'info> {
4444

4545
#[derive(AnchorDeserialize, AnchorSerialize)]
4646
pub struct ReleaseInboundArgs {
47-
pub revert_on_delay: bool,
47+
pub revert_when_not_ready: bool,
4848
}
4949

5050
// Burn/mint
@@ -55,8 +55,8 @@ pub struct ReleaseInboundMint<'info> {
5555
}
5656

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

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

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

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

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)