Skip to content

Commit 0d6fe1f

Browse files
committed
solana: Refactor token_authority_sig, simplify release_inbox_item logic, move constraint back to struct
1 parent 8ac99d0 commit 0d6fe1f

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

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

+30-42
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub struct ReleaseInboundArgs {
6262
pub struct ReleaseInboundMint<'info> {
6363
#[account(
6464
constraint = common.config.mode == Mode::Burning @ NTTError::InvalidMode,
65+
constraint = common.mint.mint_authority.unwrap() == common.token_authority.key()
6566
)]
6667
common: ReleaseInbound<'info>,
6768
}
@@ -77,11 +78,6 @@ pub fn release_inbound_mint<'info>(
7778
ctx: Context<'_, '_, '_, 'info, ReleaseInboundMint<'info>>,
7879
args: ReleaseInboundArgs,
7980
) -> Result<()> {
80-
if ctx.accounts.common.mint.mint_authority.unwrap() != ctx.accounts.common.token_authority.key()
81-
{
82-
return Err(NTTError::InvalidMintAuthority.into());
83-
}
84-
8581
let inbox_item = release_inbox_item(&mut ctx.accounts.common.inbox_item, args.revert_on_delay)?;
8682

8783
// NOTE: minting tokens is a two-step process:
@@ -99,6 +95,11 @@ pub fn release_inbound_mint<'info>(
9995
// The [`transfer_burn`] function operates in a similar way
10096
// (transfer to custody from sender, *then* burn).
10197

98+
let token_authority_sig: &[&[&[u8]]] = &[&[
99+
crate::TOKEN_AUTHORITY_SEED,
100+
&[ctx.bumps.common.token_authority],
101+
]];
102+
102103
// Step 1: mint tokens to the custody account
103104
token_interface::mint_to(
104105
CpiContext::new_with_signer(
@@ -108,10 +109,7 @@ pub fn release_inbound_mint<'info>(
108109
to: ctx.accounts.common.custody.to_account_info(),
109110
authority: ctx.accounts.common.token_authority.to_account_info(),
110111
},
111-
&[&[
112-
crate::TOKEN_AUTHORITY_SEED,
113-
&[ctx.bumps.common.token_authority],
114-
]],
112+
token_authority_sig,
115113
),
116114
inbox_item.amount,
117115
)?;
@@ -126,10 +124,7 @@ pub fn release_inbound_mint<'info>(
126124
ctx.remaining_accounts,
127125
inbox_item.amount,
128126
ctx.accounts.common.mint.decimals,
129-
&[&[
130-
crate::TOKEN_AUTHORITY_SEED,
131-
&[ctx.bumps.common.token_authority],
132-
]],
127+
token_authority_sig,
133128
)?;
134129
Ok(())
135130
}
@@ -167,27 +162,28 @@ pub fn release_inbound_mint_multisig<'info>(
167162
// The [`transfer_burn`] function operates in a similar way
168163
// (transfer to custody from sender, *then* burn).
169164

165+
let token_authority_sig: &[&[&[u8]]] = &[&[
166+
crate::TOKEN_AUTHORITY_SEED,
167+
&[ctx.bumps.common.token_authority],
168+
]];
169+
170170
// Step 1: mint tokens to the custody account
171-
let ix = spl_token_2022::instruction::mint_to(
172-
&ctx.accounts.common.token_program.key(),
173-
&ctx.accounts.common.mint.key(),
174-
&ctx.accounts.common.custody.key(),
175-
&ctx.accounts.multisig.key(),
176-
&[&ctx.accounts.common.token_authority.key()],
177-
inbox_item.amount,
178-
)?;
179171
solana_program::program::invoke_signed(
180-
&ix,
172+
&spl_token_2022::instruction::mint_to(
173+
&ctx.accounts.common.token_program.key(),
174+
&ctx.accounts.common.mint.key(),
175+
&ctx.accounts.common.custody.key(),
176+
&ctx.accounts.multisig.key(),
177+
&[&ctx.accounts.common.token_authority.key()],
178+
inbox_item.amount,
179+
)?,
181180
&[
182181
ctx.accounts.common.custody.to_account_info(),
183182
ctx.accounts.common.mint.to_account_info(),
184183
ctx.accounts.common.token_authority.to_account_info(),
185184
ctx.accounts.multisig.to_account_info(),
186185
],
187-
&[&[
188-
crate::TOKEN_AUTHORITY_SEED,
189-
&[ctx.bumps.common.token_authority],
190-
]],
186+
token_authority_sig,
191187
)?;
192188

193189
// Step 2: transfer the tokens from the custody account to the recipient
@@ -200,10 +196,7 @@ pub fn release_inbound_mint_multisig<'info>(
200196
ctx.remaining_accounts,
201197
inbox_item.amount,
202198
ctx.accounts.common.mint.decimals,
203-
&[&[
204-
crate::TOKEN_AUTHORITY_SEED,
205-
&[ctx.bumps.common.token_authority],
206-
]],
199+
token_authority_sig,
207200
)?;
208201
Ok(())
209202
}
@@ -250,17 +243,12 @@ pub fn release_inbound_unlock<'info>(
250243
}
251244

252245
fn release_inbox_item(inbox_item: &mut InboxItem, revert_on_delay: bool) -> Result<&mut InboxItem> {
253-
let released = inbox_item.try_release()?;
254-
255-
if !released {
256-
if revert_on_delay {
257-
return Err(NTTError::CantReleaseYet.into());
258-
} else {
259-
return Ok(inbox_item);
260-
}
246+
if inbox_item.try_release()? {
247+
assert!(inbox_item.release_status == ReleaseStatus::Released);
248+
Ok(inbox_item)
249+
} else if revert_on_delay {
250+
Err(NTTError::CantReleaseYet.into())
251+
} else {
252+
Ok(inbox_item)
261253
}
262-
263-
assert!(inbox_item.release_status == ReleaseStatus::Released);
264-
265-
Ok(inbox_item)
266254
}

0 commit comments

Comments
 (0)