Skip to content

Commit

Permalink
solana/transfer: use hook helper to transfer tokens
Browse files Browse the repository at this point in the history
TODO: the old token program still works -- is this secure? I think so
but double check
  • Loading branch information
kcsongor committed Apr 17, 2024
1 parent 508af61 commit 1123458
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anchor_lang::prelude::*;
use anchor_spl::token_interface;
use ntt_messages::mode::Mode;
use spl_token_2022::onchain;

use crate::{
config::*,
Expand Down Expand Up @@ -119,8 +120,8 @@ pub struct ReleaseInboundUnlock<'info> {
/// Setting this flag to `false` is useful when bundling this instruction
/// together with [`crate::instructions::redeem`], so that the unlocking
/// is attempted optimistically.
pub fn release_inbound_unlock(
ctx: Context<ReleaseInboundUnlock>,
pub fn release_inbound_unlock<'info>(
ctx: Context<'_, '_, '_, 'info, ReleaseInboundUnlock<'info>>,
args: ReleaseInboundArgs,
) -> Result<()> {
let inbox_item = &mut ctx.accounts.common.inbox_item;
Expand All @@ -138,22 +139,22 @@ pub fn release_inbound_unlock(
assert!(inbox_item.release_status == ReleaseStatus::Released);
match ctx.accounts.common.config.mode {
Mode::Burning => Err(NTTError::InvalidMode.into()),
Mode::Locking => token_interface::transfer_checked(
CpiContext::new_with_signer(
ctx.accounts.common.token_program.to_account_info(),
token_interface::TransferChecked {
from: ctx.accounts.custody.to_account_info(),
to: ctx.accounts.common.recipient.to_account_info(),
authority: ctx.accounts.common.token_authority.clone(),
mint: ctx.accounts.common.mint.to_account_info(),
},
Mode::Locking => {
onchain::invoke_transfer_checked(
&ctx.accounts.common.token_program.key(),
ctx.accounts.custody.to_account_info(),
ctx.accounts.common.mint.to_account_info(),
ctx.accounts.common.recipient.to_account_info(),
ctx.accounts.common.token_authority.clone(),
ctx.remaining_accounts,
inbox_item.amount,
ctx.accounts.common.mint.decimals,
&[&[
crate::TOKEN_AUTHORITY_SEED,
&[ctx.bumps.common.token_authority],
]],
),
inbox_item.amount,
ctx.accounts.common.mint.decimals,
),
)?;
Ok(())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use anchor_lang::prelude::*;
use anchor_spl::token_interface;
use ntt_messages::{chain_id::ChainId, mode::Mode, trimmed_amount::TrimmedAmount};
use spl_token_2022::onchain;

use crate::{
bitmap::Bitmap,
Expand Down Expand Up @@ -215,7 +216,10 @@ pub struct TransferLock<'info> {
pub custody: InterfaceAccount<'info, token_interface::TokenAccount>,
}

pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<()> {
pub fn transfer_lock<'info>(
ctx: Context<'_, '_, '_, 'info, TransferLock<'info>>,
args: TransferArgs,
) -> Result<()> {
require_eq!(
ctx.accounts.common.config.mode,
Mode::Locking,
Expand All @@ -240,24 +244,21 @@ pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<(

let before = accs.custody.amount;

token_interface::transfer_checked(
CpiContext::new_with_signer(
accs.common.token_program.to_account_info(),
token_interface::TransferChecked {
from: accs.common.from.to_account_info(),
to: accs.custody.to_account_info(),
authority: accs.session_authority.to_account_info(),
mint: accs.common.mint.to_account_info(),
},
&[&[
crate::SESSION_AUTHORITY_SEED,
accs.common.from.owner.as_ref(),
args.keccak256().as_ref(),
&[ctx.bumps.session_authority],
]],
),
onchain::invoke_transfer_checked(
&accs.common.token_program.key(),
accs.common.from.to_account_info(),
accs.common.mint.to_account_info(),
accs.custody.to_account_info(),
accs.session_authority.to_account_info(),
ctx.remaining_accounts,
amount,
accs.common.mint.decimals,
&[&[
crate::SESSION_AUTHORITY_SEED,
accs.common.from.owner.as_ref(),
args.keccak256().as_ref(),
&[ctx.bumps.session_authority],
]],
)?;

accs.custody.reload()?;
Expand Down
9 changes: 6 additions & 3 deletions solana/programs/example-native-token-transfers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ pub mod example_native_token_transfers {
instructions::transfer_burn(ctx, args)
}

pub fn transfer_lock(ctx: Context<TransferLock>, args: TransferArgs) -> Result<()> {
pub fn transfer_lock<'info>(
ctx: Context<'_, '_, '_, 'info, TransferLock<'info>>,
args: TransferArgs,
) -> Result<()> {
instructions::transfer_lock(ctx, args)
}

Expand All @@ -96,8 +99,8 @@ pub mod example_native_token_transfers {
instructions::release_inbound_mint(ctx, args)
}

pub fn release_inbound_unlock(
ctx: Context<ReleaseInboundUnlock>,
pub fn release_inbound_unlock<'info>(
ctx: Context<'_, '_, '_, 'info, ReleaseInboundUnlock<'info>>,
args: ReleaseInboundArgs,
) -> Result<()> {
instructions::release_inbound_unlock(ctx, args)
Expand Down

0 comments on commit 1123458

Please sign in to comment.