Skip to content

Commit a6e9188

Browse files
committed
solana/transfer: use hook helper to transfer tokens
TODO: the old token program still works -- is this secure? I think so but double check
1 parent 5b81cb0 commit a6e9188

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

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

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anchor_lang::prelude::*;
22
use anchor_spl::token_interface;
33
use ntt_messages::mode::Mode;
4+
use spl_token_2022::onchain;
45

56
use crate::{
67
config::*,
@@ -119,8 +120,8 @@ pub struct ReleaseInboundUnlock<'info> {
119120
/// Setting this flag to `false` is useful when bundling this instruction
120121
/// together with [`crate::instructions::redeem`], so that the unlocking
121122
/// is attempted optimistically.
122-
pub fn release_inbound_unlock(
123-
ctx: Context<ReleaseInboundUnlock>,
123+
pub fn release_inbound_unlock<'info>(
124+
ctx: Context<'_, '_, '_, 'info, ReleaseInboundUnlock<'info>>,
124125
args: ReleaseInboundArgs,
125126
) -> Result<()> {
126127
let inbox_item = &mut ctx.accounts.common.inbox_item;
@@ -138,22 +139,22 @@ pub fn release_inbound_unlock(
138139
assert!(inbox_item.release_status == ReleaseStatus::Released);
139140
match ctx.accounts.common.config.mode {
140141
Mode::Burning => Err(NTTError::InvalidMode.into()),
141-
Mode::Locking => token_interface::transfer_checked(
142-
CpiContext::new_with_signer(
143-
ctx.accounts.common.token_program.to_account_info(),
144-
token_interface::TransferChecked {
145-
from: ctx.accounts.custody.to_account_info(),
146-
to: ctx.accounts.common.recipient.to_account_info(),
147-
authority: ctx.accounts.common.token_authority.clone(),
148-
mint: ctx.accounts.common.mint.to_account_info(),
149-
},
142+
Mode::Locking => {
143+
onchain::invoke_transfer_checked(
144+
&ctx.accounts.common.token_program.key(),
145+
ctx.accounts.custody.to_account_info(),
146+
ctx.accounts.common.mint.to_account_info(),
147+
ctx.accounts.common.recipient.to_account_info(),
148+
ctx.accounts.common.token_authority.clone(),
149+
ctx.remaining_accounts,
150+
inbox_item.amount,
151+
ctx.accounts.common.mint.decimals,
150152
&[&[
151153
crate::TOKEN_AUTHORITY_SEED,
152154
&[ctx.bumps.common.token_authority],
153155
]],
154-
),
155-
inbox_item.amount,
156-
ctx.accounts.common.mint.decimals,
157-
),
156+
)?;
157+
Ok(())
158+
}
158159
}
159160
}

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

+18-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use anchor_lang::prelude::*;
33
use anchor_spl::token_interface;
44
use ntt_messages::{chain_id::ChainId, mode::Mode, trimmed_amount::TrimmedAmount};
5+
use spl_token_2022::onchain;
56

67
use crate::{
78
bitmap::Bitmap,
@@ -215,7 +216,10 @@ pub struct TransferLock<'info> {
215216
pub custody: InterfaceAccount<'info, token_interface::TokenAccount>,
216217
}
217218

218-
pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<()> {
219+
pub fn transfer_lock<'info>(
220+
ctx: Context<'_, '_, '_, 'info, TransferLock<'info>>,
221+
args: TransferArgs,
222+
) -> Result<()> {
219223
require_eq!(
220224
ctx.accounts.common.config.mode,
221225
Mode::Locking,
@@ -240,24 +244,21 @@ pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<(
240244

241245
let before = accs.custody.amount;
242246

243-
token_interface::transfer_checked(
244-
CpiContext::new_with_signer(
245-
accs.common.token_program.to_account_info(),
246-
token_interface::TransferChecked {
247-
from: accs.common.from.to_account_info(),
248-
to: accs.custody.to_account_info(),
249-
authority: accs.session_authority.to_account_info(),
250-
mint: accs.common.mint.to_account_info(),
251-
},
252-
&[&[
253-
crate::SESSION_AUTHORITY_SEED,
254-
accs.common.from.owner.as_ref(),
255-
args.keccak256().as_ref(),
256-
&[ctx.bumps.session_authority],
257-
]],
258-
),
247+
onchain::invoke_transfer_checked(
248+
&accs.common.token_program.key(),
249+
accs.common.from.to_account_info(),
250+
accs.common.mint.to_account_info(),
251+
accs.custody.to_account_info(),
252+
accs.session_authority.to_account_info(),
253+
ctx.remaining_accounts,
259254
amount,
260255
accs.common.mint.decimals,
256+
&[&[
257+
crate::SESSION_AUTHORITY_SEED,
258+
accs.common.from.owner.as_ref(),
259+
args.keccak256().as_ref(),
260+
&[ctx.bumps.session_authority],
261+
]],
261262
)?;
262263

263264
accs.custody.reload()?;

solana/programs/example-native-token-transfers/src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ pub mod example_native_token_transfers {
8181
instructions::transfer_burn(ctx, args)
8282
}
8383

84-
pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<()> {
84+
pub fn transfer_lock<'info>(
85+
ctx: Context<'_, '_, '_, 'info, TransferLock<'info>>,
86+
args: TransferArgs,
87+
) -> Result<()> {
8588
instructions::transfer_lock(ctx, args)
8689
}
8790

@@ -96,8 +99,8 @@ pub mod example_native_token_transfers {
9699
instructions::release_inbound_mint(ctx, args)
97100
}
98101

99-
pub fn release_inbound_unlock(
100-
ctx: Context<ReleaseInboundUnlock>,
102+
pub fn release_inbound_unlock<'info>(
103+
ctx: Context<'_, '_, '_, 'info, ReleaseInboundUnlock<'info>>,
101104
args: ReleaseInboundArgs,
102105
) -> Result<()> {
103106
instructions::release_inbound_unlock(ctx, args)

0 commit comments

Comments
 (0)