Set correct mutability on Transfer Native

Change-Id: I50a34645de34cd97b801015748331cfba7cdfac9
This commit is contained in:
Hendrik Hofstadt 2021-07-26 19:49:45 +02:00
parent 42c3040de1
commit f6932bebca
4 changed files with 184 additions and 14 deletions

View File

@ -11,6 +11,7 @@ use crate::{
};
use bridge::{
api::{PostMessage, PostMessageData},
types::ConsistencyLevel,
vaa::SerializePayload,
};
use primitive_types::U256;
@ -36,33 +37,36 @@ use std::ops::{Deref, DerefMut};
#[derive(FromAccounts)]
pub struct TransferNative<'b> {
pub payer: Signer<AccountInfo<'b>>,
pub config: ConfigAccount<'b, { AccountState::Initialized }>,
pub payer: Mut<Signer<AccountInfo<'b>>>,
pub from: Data<'b, SplAccount, { AccountState::Initialized }>,
pub mint: Data<'b, SplMint, { AccountState::Initialized }>,
pub config: Mut<ConfigAccount<'b, { AccountState::Initialized }>>,
pub custody: CustodyAccount<'b, { AccountState::MaybeInitialized }>,
pub from: Mut<Data<'b, SplAccount, { AccountState::Initialized }>>,
pub mint: Mut<Data<'b, SplMint, { AccountState::Initialized }>>,
pub custody: Mut<CustodyAccount<'b, { AccountState::MaybeInitialized }>>,
// This could allow someone to race someone else's tx if they do the approval in a separate tx.
// Therefore the approval must be set in the same tx
// Therefore the approval must be set in the same tx.
pub authority_signer: AuthoritySigner<'b>,
pub custody_signer: CustodySigner<'b>,
/// CPI Context
pub bridge: Info<'b>,
pub bridge: Mut<Info<'b>>,
/// Account to store the posted message
pub message: Info<'b>,
pub message: Mut<Info<'b>>,
/// Emitter of the VAA
pub emitter: EmitterAccount<'b>,
/// Tracker for the emitter sequence
pub sequence: Info<'b>,
pub sequence: Mut<Info<'b>>,
/// Account to collect tx fee
pub fee_collector: Info<'b>,
pub fee_collector: Mut<Info<'b>>,
pub clock: Sysvar<'b, Clock>,
}
@ -151,14 +155,14 @@ pub fn transfer_native(
let params = (bridge::instruction::Instruction::PostMessage, PostMessageData {
nonce: data.nonce,
payload: payload.try_to_vec()?,
persist: true,
consistency_level: ConsistencyLevel::Confirmed,
});
let ix = Instruction::new_with_bytes(
accs.config.wormhole_bridge,
params.try_to_vec()?.as_slice(),
vec![
AccountMeta::new_readonly(*accs.bridge.key, false),
AccountMeta::new(*accs.bridge.key, false),
AccountMeta::new(*accs.message.key, false),
AccountMeta::new_readonly(*accs.emitter.key, true),
AccountMeta::new(*accs.sequence.key, false),
@ -275,7 +279,7 @@ pub fn transfer_wrapped(
let params = (bridge::instruction::Instruction::PostMessage, PostMessageData {
nonce: data.nonce,
payload: payload.try_to_vec()?,
persist: true,
consistency_level: ConsistencyLevel::Confirmed,
});
let ix = Instruction::new_with_bytes(

View File

@ -316,7 +316,7 @@ pub fn transfer_native(
AccountMeta::new_readonly(emitter_key, false),
AccountMeta::new(sequence_key, false),
AccountMeta::new(fee_collector_key, false),
AccountMeta::new(solana_program::sysvar::clock::id(), false),
AccountMeta::new_readonly(solana_program::sysvar::clock::id(), false),
// Dependencies
AccountMeta::new(solana_program::sysvar::rent::id(), false),
AccountMeta::new(solana_program::system_program::id(), false),

View File

@ -100,6 +100,7 @@ pub fn execute(
}
mod helpers {
use bridge::types::PostedMessage;
use token_bridge::{CompleteNativeData, TransferNativeData};
use super::*;
@ -171,6 +172,98 @@ mod helpers {
)
}
pub fn transfer(
client: &RpcClient,
from: &Keypair,
to: &Pubkey,
lamports: u64,
) -> Result<Signature, ClientError> {
execute(
client,
from,
&[from],
&[system_instruction::transfer(&from.pubkey(), to, lamports)],
CommitmentConfig::processed(),
)
}
pub fn initialize(
client: &RpcClient,
program: &Pubkey,
payer: &Keypair,
bridge: &Pubkey,
) -> Result<Signature, ClientError> {
execute(
client,
payer,
&[payer],
&[instructions::initialize(*program, payer.pubkey(), *bridge).unwrap()],
CommitmentConfig::processed(),
)
}
pub fn attest(
client: &RpcClient,
program: &Pubkey,
bridge: &Pubkey,
payer: &Keypair,
mint: Pubkey,
mint_meta: Pubkey,
nonce: u32,
) -> Result<Signature, ClientError> {
let mint_data = Mint::unpack(
&client.get_account(&mint)?.data
).expect("Could not unpack Mint");
execute(
client,
payer,
&[payer],
&[instructions::attest(
*program,
*bridge,
payer.pubkey(),
mint,
mint_data,
mint_meta,
nonce,
)
.unwrap()],
CommitmentConfig::processed(),
)
}
pub fn transfer_native(
client: &RpcClient,
program: &Pubkey,
bridge: &Pubkey,
payer: &Keypair,
from: &Keypair,
mint: Pubkey,
) -> Result<Signature, ClientError> {
execute(
client,
payer,
&[payer],
&[instructions::transfer_native(
*program,
*bridge,
payer.pubkey(),
from.pubkey(),
mint,
TransferNativeData {
nonce: 0,
amount: 0,
fee: 0,
target_address: [0u8; 32],
target_chain: 1,
},
)
.unwrap()],
CommitmentConfig::processed(),
)
}
pub fn create_mint(
client: &RpcClient,
payer: &Keypair,

View File

@ -181,6 +181,79 @@ fn run_integration_tests() {
// Initialize the bridge and verify the bridges state.
test_initialize(&mut context);
test_transfer_native(&mut context);
test_attest(&mut context);
test_complete_native(&mut context);
//test_transfer_wrapped(&mut context);
//test_complete_wrapped(&mut context);
//test_register_chain(&mut context);
//test_create_wrapped(&mut context);
}
fn test_attest(context: &mut Context) -> () {
println!("Attest");
use token_bridge::{
accounts::ConfigAccount,
types::{
Config,
FeeStructure,
},
};
let Context {
ref payer,
ref client,
ref bridge,
ref token_bridge,
ref mint_authority,
ref mint,
ref mint_meta,
..
} = context;
common::attest(
client,
token_bridge,
bridge,
payer,
mint.pubkey(),
mint_meta.pubkey(),
0,
)
.unwrap();
}
fn test_transfer_native(context: &mut Context) -> () {
println!("Transfer Native");
use token_bridge::{
accounts::ConfigAccount,
types::{
Config,
FeeStructure,
},
};
let Context {
ref payer,
ref client,
ref bridge,
ref token_bridge,
ref mint_authority,
ref mint,
ref mint_meta,
ref token_account,
..
} = context;
common::transfer_native(
client,
token_bridge,
bridge,
payer,
token_account,
mint.pubkey(),
)
.unwrap();
}
fn test_initialize(context: &mut Context) {