remove transfer specific methods

Change-Id: I017c9946aaf0f23a56cfc27b7871dbf22f19957b
This commit is contained in:
Hendrik Hofstadt 2021-04-15 11:36:29 +02:00
parent 6a6ba06c0c
commit aad19ff8f8
1 changed files with 1 additions and 271 deletions

View File

@ -902,69 +902,6 @@ impl Bridge {
Ok(())
}
/// Processes a VAA transfer in
pub fn process_vaa_transfer(
program_id: &Pubkey,
accounts: &[AccountInfo],
account_info_iter: &mut Iter<AccountInfo>,
bridge_info: &AccountInfo,
bridge: &Bridge,
b: &BodyTransfer,
) -> ProgramResult {
next_account_info(account_info_iter)?; // Token program
let mint_info = next_account_info(account_info_iter)?;
let destination_info = next_account_info(account_info_iter)?;
let destination = Self::token_account_deserialize(destination_info)?;
if destination.mint != *mint_info.key {
return Err(Error::TokenMintMismatch.into());
}
if b.asset.chain == CHAIN_ID_SOLANA {
let custody_info = next_account_info(account_info_iter)?;
let expected_custody_id =
Bridge::derive_custody_id(program_id, bridge_info.key, mint_info.key)?;
if expected_custody_id != *custody_info.key {
return Err(Error::InvalidDerivedAccount.into());
}
// Native Solana asset, transfer from custody
Bridge::token_transfer_custody(
program_id,
accounts,
&bridge.config.token_program,
custody_info.key,
destination_info.key,
b.amount,
)?;
} else {
// Foreign chain asset, mint wrapped asset
let expected_mint_address = Bridge::derive_wrapped_asset_id(
program_id,
bridge_info.key,
b.asset.chain,
b.asset.decimals,
b.asset.address,
)?;
if expected_mint_address != *mint_info.key {
return Err(Error::InvalidDerivedAccount.into());
}
// This automatically asserts that the mint was created by this account by using
// derivated keys
Bridge::wrapped_mint_to(
program_id,
accounts,
&bridge.config.token_program,
mint_info.key,
destination_info.key,
b.amount,
)?;
}
Ok(())
}
/// Processes a VAA post for data availability (for Solana -> foreign transfers)
pub fn process_vaa_transfer_post(
program_id: &Pubkey,
@ -1031,72 +968,7 @@ impl Bridge {
bridge_info.key,
bridge_info.key,
);
Self::invoke_as_bridge(program_id, &upgrade_ix, accounts);
Ok(())
}
/// Creates a new wrapped asset
pub fn process_create_wrapped(
program_id: &Pubkey,
accounts: &[AccountInfo],
a: &AssetMeta,
) -> ProgramResult {
msg!("create wrapped");
let account_info_iter = &mut accounts.iter();
next_account_info(account_info_iter)?; // System program
next_account_info(account_info_iter)?; // Token program
next_account_info(account_info_iter)?; // Rent sysvar
let bridge_info = Self::next_account_info_with_owner(account_info_iter, program_id)?;
let payer_info = next_account_info(account_info_iter)?;
let mint_info = next_account_info(account_info_iter)?;
let wrapped_meta_info = next_account_info(account_info_iter)?;
let bridge_data = bridge_info.data.try_borrow().map_err(|_| ProgramError::AccountBorrowFailed)?;
let bridge: &Bridge = Self::unpack_immutable(&bridge_data)?;
// Foreign chain asset, mint wrapped asset
let expected_mint_address = Bridge::derive_wrapped_asset_id(
program_id,
bridge_info.key,
a.chain,
a.decimals,
a.address,
)?;
if expected_mint_address != *mint_info.key {
return Err(Error::InvalidDerivedAccount.into());
}
// Create wrapped mint
Self::create_wrapped_mint(
program_id,
accounts,
&bridge.config.token_program,
mint_info.key,
bridge_info.key,
payer_info,
&a,
a.decimals,
None,
)?;
// Check and create wrapped asset meta to allow reverse resolution of info
let wrapped_meta_seeds = Bridge::derive_wrapped_meta_seeds(bridge_info.key, mint_info.key);
Bridge::check_and_create_account::<WrappedAssetMeta>(
program_id,
accounts,
wrapped_meta_info.key,
payer_info,
program_id,
&wrapped_meta_seeds,
None,
)?;
let mut wrapped_meta_data = wrapped_meta_info.try_borrow_mut_data()?;
let wrapped_meta: &mut WrappedAssetMeta = Bridge::unpack(&mut wrapped_meta_data)?;
wrapped_meta.address = a.address;
wrapped_meta.chain = a.chain;
Self::invoke_as_bridge(program_id, &upgrade_ix, accounts)?;
Ok(())
}
@ -1104,148 +976,6 @@ impl Bridge {
/// Implementation of actions
impl Bridge {
/// Burn a wrapped asset from account
pub fn wrapped_burn(
program_id: &Pubkey,
accounts: &[AccountInfo],
token_program_id: &Pubkey,
token_account: &Pubkey,
mint_account: &Pubkey,
amount: U256,
) -> Result<(), ProgramError> {
let ix = spl_token::instruction::burn(
token_program_id,
token_account,
mint_account,
&Self::derive_bridge_id(program_id)?,
&[],
amount.as_u64(),
)?;
Self::invoke_as_bridge(program_id, &ix, accounts)
}
/// Mint a wrapped asset to account
pub fn wrapped_mint_to(
program_id: &Pubkey,
accounts: &[AccountInfo],
token_program_id: &Pubkey,
mint: &Pubkey,
destination: &Pubkey,
amount: U256,
) -> Result<(), ProgramError> {
let ix = spl_token::instruction::mint_to(
token_program_id,
mint,
destination,
&Self::derive_bridge_id(program_id)?,
&[],
amount.as_u64(),
)?;
Self::invoke_as_bridge(program_id, &ix, accounts)
}
/// Transfer tokens from a caller
pub fn token_transfer_caller(
program_id: &Pubkey,
accounts: &[AccountInfo],
token_program_id: &Pubkey,
source: &Pubkey,
destination: &Pubkey,
authority: &Pubkey,
amount: U256,
) -> Result<(), ProgramError> {
let ix = spl_token::instruction::transfer(
token_program_id,
source,
destination,
authority,
&[],
amount.as_u64(),
)?;
Self::invoke_as_bridge(program_id, &ix, accounts)
}
/// Transfer tokens from a custody account
pub fn token_transfer_custody(
program_id: &Pubkey,
accounts: &[AccountInfo],
token_program_id: &Pubkey,
source: &Pubkey,
destination: &Pubkey,
amount: U256,
) -> Result<(), ProgramError> {
let ix = spl_token::instruction::transfer(
token_program_id,
source,
destination,
&Self::derive_bridge_id(program_id)?,
&[],
amount.as_u64(),
)?;
Self::invoke_as_bridge(program_id, &ix, accounts)
}
/// Create a new account
pub fn create_custody_account(
program_id: &Pubkey,
accounts: &[AccountInfo],
token_program: &Pubkey,
bridge: &Pubkey,
account: &Pubkey,
mint: &Pubkey,
payer: &AccountInfo,
subsidizer: Option<&AccountInfo>,
) -> Result<(), ProgramError> {
Self::check_and_create_account::<[u8; spl_token::state::Account::LEN]>(
program_id,
accounts,
account,
payer,
token_program,
&Self::derive_custody_seeds(bridge, mint),
subsidizer,
)?;
msg!(token_program.to_string().as_str());
let ix = spl_token::instruction::initialize_account(
token_program,
account,
mint,
&Self::derive_bridge_id(program_id)?,
)?;
invoke_signed(&ix, accounts, &[])
}
/// Create a mint for a wrapped asset
pub fn create_wrapped_mint(
program_id: &Pubkey,
accounts: &[AccountInfo],
token_program: &Pubkey,
mint: &Pubkey,
bridge: &Pubkey,
payer: &AccountInfo,
asset: &AssetMeta,
decimals: u8,
subsidizer: Option<&AccountInfo>,
) -> Result<(), ProgramError> {
Self::check_and_create_account::<[u8; spl_token::state::Mint::LEN]>(
program_id,
accounts,
mint,
payer,
token_program,
&Self::derive_wrapped_asset_seeds(bridge, asset.chain, asset.decimals, asset.address),
subsidizer,
)?;
let ix = spl_token::instruction::initialize_mint(
token_program,
mint,
&Self::derive_bridge_id(program_id)?,
None,
decimals,
)?;
invoke_signed(&ix, accounts, &[])
}
pub fn invoke_as_bridge<'a>(
program_id: &Pubkey,
instruction: &Instruction,