|
| 1 | +use anchor_lang::prelude::*; |
| 2 | +use anchor_spl::token_interface; |
| 3 | +use ntt_messages::mode::Mode; |
| 4 | +use spl_token_2022::onchain; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + config::*, |
| 8 | + error::NTTError, |
| 9 | + queue::inbox::{InboxItem, ReleaseStatus}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Accounts)] |
| 13 | +pub struct ReleaseInboundMultisig<'info> { |
| 14 | + #[account(mut)] |
| 15 | + pub payer: Signer<'info>, |
| 16 | + |
| 17 | + pub config: NotPausedConfig<'info>, |
| 18 | + |
| 19 | + #[account(mut)] |
| 20 | + pub inbox_item: Account<'info, InboxItem>, |
| 21 | + |
| 22 | + #[account( |
| 23 | + mut, |
| 24 | + associated_token::authority = inbox_item.recipient_address, |
| 25 | + associated_token::mint = mint, |
| 26 | + associated_token::token_program = token_program, |
| 27 | + )] |
| 28 | + pub recipient: InterfaceAccount<'info, token_interface::TokenAccount>, |
| 29 | + |
| 30 | + /// CHECK: multisig account should be mint authority |
| 31 | + #[account(constraint = mint.mint_authority.unwrap() == multisig.key())] |
| 32 | + pub multisig: UncheckedAccount<'info>, |
| 33 | + |
| 34 | + #[account( |
| 35 | + seeds = [crate::TOKEN_AUTHORITY_SEED], |
| 36 | + bump, |
| 37 | + )] |
| 38 | + /// CHECK The seeds constraint ensures that this is the correct address |
| 39 | + pub token_authority: UncheckedAccount<'info>, |
| 40 | + |
| 41 | + #[account( |
| 42 | + mut, |
| 43 | + address = config.mint, |
| 44 | + )] |
| 45 | + /// CHECK: the mint address matches the config |
| 46 | + pub mint: InterfaceAccount<'info, token_interface::Mint>, |
| 47 | + |
| 48 | + pub token_program: Interface<'info, token_interface::TokenInterface>, |
| 49 | + |
| 50 | + /// CHECK: the token program checks if this indeed the right authority for the mint |
| 51 | + #[account( |
| 52 | + mut, |
| 53 | + address = config.custody |
| 54 | + )] |
| 55 | + pub custody: InterfaceAccount<'info, token_interface::TokenAccount>, |
| 56 | +} |
| 57 | + |
| 58 | +#[derive(AnchorDeserialize, AnchorSerialize)] |
| 59 | +pub struct ReleaseInboundMultisigArgs { |
| 60 | + pub revert_on_delay: bool, |
| 61 | +} |
| 62 | + |
| 63 | +// Burn/mint |
| 64 | + |
| 65 | +#[derive(Accounts)] |
| 66 | +pub struct ReleaseInboundMultisigMint<'info> { |
| 67 | + #[account( |
| 68 | + constraint = common.config.mode == Mode::Burning @ NTTError::InvalidMode, |
| 69 | + )] |
| 70 | + common: ReleaseInboundMultisig<'info>, |
| 71 | +} |
| 72 | + |
| 73 | +/// Release an inbound transfer and mint the tokens to the recipient. |
| 74 | +/// When `revert_on_error` is true, the transaction will revert if the |
| 75 | +/// release timestamp has not been reached. When `revert_on_error` is false, the |
| 76 | +/// transaction succeeds, but the minting is not performed. |
| 77 | +/// Setting this flag to `false` is useful when bundling this instruction |
| 78 | +/// together with [`crate::instructions::redeem`] in a transaction, so that the minting |
| 79 | +/// is attempted optimistically. |
| 80 | +pub fn release_inbound_multisig_mint<'info>( |
| 81 | + ctx: Context<'_, '_, '_, 'info, ReleaseInboundMultisigMint<'info>>, |
| 82 | + args: ReleaseInboundMultisigArgs, |
| 83 | +) -> Result<()> { |
| 84 | + let inbox_item = &mut ctx.accounts.common.inbox_item; |
| 85 | + |
| 86 | + let released = inbox_item.try_release()?; |
| 87 | + |
| 88 | + if !released { |
| 89 | + if args.revert_on_delay { |
| 90 | + return Err(NTTError::CantReleaseYet.into()); |
| 91 | + } else { |
| 92 | + return Ok(()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + assert!(inbox_item.release_status == ReleaseStatus::Released); |
| 97 | + |
| 98 | + // NOTE: minting tokens is a two-step process: |
| 99 | + // 1. Mint tokens to the custody account |
| 100 | + // 2. Transfer the tokens from the custody account to the recipient |
| 101 | + // |
| 102 | + // This is done to ensure that if the token has a transfer hook defined, it |
| 103 | + // will be called after the tokens are minted. |
| 104 | + // Unfortunately the Token2022 program doesn't trigger transfer hooks when |
| 105 | + // minting tokens, so we have to do it "manually" via a transfer. |
| 106 | + // |
| 107 | + // If we didn't do this, transfer hooks could be bypassed by transferring |
| 108 | + // the tokens out through NTT first, then back in to the intended recipient. |
| 109 | + // |
| 110 | + // The [`transfer_burn`] function operates in a similar way |
| 111 | + // (transfer to custody from sender, *then* burn). |
| 112 | + |
| 113 | + // Step 1: mint tokens to the custody account |
| 114 | + let ix = spl_token_2022::instruction::mint_to( |
| 115 | + &ctx.accounts.common.token_program.key(), |
| 116 | + &ctx.accounts.common.mint.key(), |
| 117 | + &ctx.accounts.common.custody.key(), |
| 118 | + &ctx.accounts.common.multisig.key(), |
| 119 | + &[&ctx.accounts.common.token_authority.key()], |
| 120 | + inbox_item.amount, |
| 121 | + )?; |
| 122 | + solana_program::program::invoke_signed( |
| 123 | + &ix, |
| 124 | + &[ |
| 125 | + ctx.accounts.common.custody.to_account_info(), |
| 126 | + ctx.accounts.common.mint.to_account_info(), |
| 127 | + ctx.accounts.common.token_authority.to_account_info(), |
| 128 | + ctx.accounts.common.multisig.to_account_info(), |
| 129 | + ], |
| 130 | + &[&[ |
| 131 | + crate::TOKEN_AUTHORITY_SEED, |
| 132 | + &[ctx.bumps.common.token_authority], |
| 133 | + ]], |
| 134 | + )?; |
| 135 | + |
| 136 | + // Step 2: transfer the tokens from the custody account to the recipient |
| 137 | + onchain::invoke_transfer_checked( |
| 138 | + &ctx.accounts.common.token_program.key(), |
| 139 | + ctx.accounts.common.custody.to_account_info(), |
| 140 | + ctx.accounts.common.mint.to_account_info(), |
| 141 | + ctx.accounts.common.recipient.to_account_info(), |
| 142 | + ctx.accounts.common.token_authority.to_account_info(), |
| 143 | + ctx.remaining_accounts, |
| 144 | + inbox_item.amount, |
| 145 | + ctx.accounts.common.mint.decimals, |
| 146 | + &[&[ |
| 147 | + crate::TOKEN_AUTHORITY_SEED, |
| 148 | + &[ctx.bumps.common.token_authority], |
| 149 | + ]], |
| 150 | + )?; |
| 151 | + Ok(()) |
| 152 | +} |
0 commit comments