solana: nft-bridge: Add tests
The integration tests were previously just copied over from the token-bridge directory and not changed at all (they still referenced the token-bridge crate). Clean up the files and add tests for basic functionality like sending and receiving native + wrapped NFTs.
This commit is contained in:
parent
66f031c51b
commit
defd16e084
|
@ -1573,8 +1573,8 @@ dependencies = [
|
|||
"rocksalt",
|
||||
"serde",
|
||||
"sha3",
|
||||
"solana-client",
|
||||
"solana-program",
|
||||
"solana-program-test",
|
||||
"solana-sdk",
|
||||
"solitaire",
|
||||
"solitaire-client",
|
||||
|
|
|
@ -9,11 +9,12 @@ crate-type = ["cdylib", "lib"]
|
|||
name = "nft_bridge"
|
||||
|
||||
[features]
|
||||
no-entrypoint = ["solitaire/no-entrypoint", "rand"]
|
||||
no-entrypoint = ["solitaire/no-entrypoint", "instructions", "rand"]
|
||||
trace = ["solitaire/trace"]
|
||||
wasm = ["no-entrypoint", "wasm-bindgen"]
|
||||
client = ["solitaire-client", "solitaire/client", "no-entrypoint"]
|
||||
cpi = ["no-entrypoint"]
|
||||
instructions = []
|
||||
default = []
|
||||
|
||||
[dependencies]
|
||||
|
@ -26,7 +27,7 @@ solitaire = { path = "../../../solitaire/program" }
|
|||
sha3 = "0.9.1"
|
||||
solana-program = "*"
|
||||
spl-token = { version = "=3.2.0", features = ["no-entrypoint"] }
|
||||
spl-associated-token-account = { version = "1.0.2" }
|
||||
spl-associated-token-account = { version = "1.0.2", features = ["no-entrypoint"] }
|
||||
primitive-types = { version = "0.9.0", default-features = false }
|
||||
solitaire-client = { path = "../../../solitaire/client", optional = true }
|
||||
spl-token-metadata = { path = "../../token_bridge/token-metadata" }
|
||||
|
@ -38,7 +39,8 @@ rand = { version = "0.7.3", optional = true }
|
|||
hex = "*"
|
||||
hex-literal = "0.3.1"
|
||||
libsecp256k1 = { version = "0.6.0", features = [] }
|
||||
solana-client = "=1.9.4"
|
||||
rand = "0.7.3"
|
||||
solana-program-test = "=1.9.4"
|
||||
solana-sdk = "=1.9.4"
|
||||
spl-token = { version = "=3.2.0", features = ["no-entrypoint"] }
|
||||
spl-token-metadata = { path = "../../token_bridge/token-metadata" }
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#![deny(unused_must_use)]
|
||||
// #![cfg(all(target_arch = "bpf", not(feature = "no-entrypoint")))]
|
||||
|
||||
#[cfg(feature = "no-entrypoint")]
|
||||
#[cfg(feature = "instructions")]
|
||||
pub mod instructions;
|
||||
|
||||
#[cfg(feature = "wasm")]
|
||||
|
|
|
@ -9,17 +9,12 @@ use byteorder::{
|
|||
WriteBytesExt,
|
||||
};
|
||||
use hex_literal::hex;
|
||||
use secp256k1::{
|
||||
use libsecp256k1::{
|
||||
Message as Secp256k1Message,
|
||||
PublicKey,
|
||||
SecretKey,
|
||||
};
|
||||
use sha3::Digest;
|
||||
use solana_client::{
|
||||
client_error::ClientError,
|
||||
rpc_client::RpcClient,
|
||||
rpc_config::RpcSendTransactionConfig,
|
||||
};
|
||||
use solana_program::{
|
||||
borsh::try_from_slice_unchecked,
|
||||
hash,
|
||||
|
@ -36,8 +31,12 @@ use solana_program::{
|
|||
system_program,
|
||||
sysvar,
|
||||
};
|
||||
use solana_program_test::{
|
||||
BanksClient,
|
||||
ProgramTest,
|
||||
};
|
||||
use solana_sdk::{
|
||||
commitment_config::CommitmentConfig,
|
||||
commitment_config::CommitmentLevel,
|
||||
rent::Rent,
|
||||
secp256k1_instruction::new_secp256k1_instruction,
|
||||
signature::{
|
||||
|
@ -46,7 +45,13 @@ use solana_sdk::{
|
|||
Signature,
|
||||
Signer,
|
||||
},
|
||||
signers::Signers,
|
||||
transaction::Transaction,
|
||||
transport::TransportError,
|
||||
};
|
||||
use solitaire::{
|
||||
processors::seeded::Seeded,
|
||||
AccountState,
|
||||
};
|
||||
use spl_token::state::Mint;
|
||||
use std::{
|
||||
|
@ -62,7 +67,7 @@ use std::{
|
|||
},
|
||||
};
|
||||
|
||||
use token_bridge::{
|
||||
use nft_bridge::{
|
||||
accounts::*,
|
||||
instruction,
|
||||
instructions,
|
||||
|
@ -70,246 +75,192 @@ use token_bridge::{
|
|||
Initialize,
|
||||
};
|
||||
|
||||
use solitaire::{
|
||||
processors::seeded::Seeded,
|
||||
AccountState,
|
||||
};
|
||||
|
||||
pub use helpers::*;
|
||||
|
||||
/// Simple API wrapper for quickly preparing and sending transactions.
|
||||
pub fn execute(
|
||||
client: &RpcClient,
|
||||
pub async fn execute<T: Signers>(
|
||||
client: &mut BanksClient,
|
||||
payer: &Keypair,
|
||||
signers: &[&Keypair],
|
||||
signers: &T,
|
||||
instructions: &[Instruction],
|
||||
commitment_level: CommitmentConfig,
|
||||
) -> Result<Signature, ClientError> {
|
||||
commitment_level: CommitmentLevel,
|
||||
) -> Result<(), TransportError> {
|
||||
let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey()));
|
||||
let recent_blockhash = client.get_recent_blockhash().unwrap().0;
|
||||
transaction.sign(&signers.to_vec(), recent_blockhash);
|
||||
client.send_and_confirm_transaction_with_spinner_and_config(
|
||||
&transaction,
|
||||
commitment_level,
|
||||
RpcSendTransactionConfig {
|
||||
skip_preflight: true,
|
||||
preflight_commitment: None,
|
||||
encoding: None,
|
||||
},
|
||||
)
|
||||
let recent_blockhash = client.get_latest_blockhash().await?;
|
||||
transaction.sign(signers, recent_blockhash);
|
||||
client
|
||||
.process_transaction_with_commitment(transaction, commitment_level)
|
||||
.await
|
||||
}
|
||||
|
||||
mod helpers {
|
||||
use bridge::types::{
|
||||
ConsistencyLevel,
|
||||
PostedVAAData,
|
||||
};
|
||||
use token_bridge::{
|
||||
CompleteNativeData,
|
||||
CompleteWrappedData,
|
||||
CreateWrappedData,
|
||||
RegisterChainData,
|
||||
TransferNativeData,
|
||||
TransferWrappedData,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
use bridge::{
|
||||
accounts::{
|
||||
FeeCollector,
|
||||
PostedVAADerivationData,
|
||||
},
|
||||
types::ConsistencyLevel,
|
||||
PostVAAData,
|
||||
PostedVAAData,
|
||||
};
|
||||
use std::ops::Add;
|
||||
use token_bridge::messages::{
|
||||
PayloadAssetMeta,
|
||||
use nft_bridge::{
|
||||
CompleteNativeData,
|
||||
CompleteWrappedData,
|
||||
CompleteWrappedMetaData,
|
||||
RegisterChainData,
|
||||
TransferNativeData,
|
||||
TransferWrappedData,
|
||||
};
|
||||
use primitive_types::U256;
|
||||
use solana_program_test::processor;
|
||||
|
||||
use nft_bridge::messages::{
|
||||
PayloadGovernanceRegisterChain,
|
||||
PayloadTransfer,
|
||||
};
|
||||
use std::ops::Add;
|
||||
|
||||
/// Generate `count` secp256k1 private keys, along with their ethereum-styled public key
|
||||
/// encoding: 0x0123456789ABCDEF01234
|
||||
pub fn generate_keys(count: u8) -> (Vec<[u8; 20]>, Vec<SecretKey>) {
|
||||
use rand::Rng;
|
||||
use sha3::Digest;
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
// Generate Guardian Keys
|
||||
let secret_keys: Vec<SecretKey> = std::iter::repeat_with(|| SecretKey::random(&mut rng))
|
||||
.take(count as usize)
|
||||
.collect();
|
||||
|
||||
(
|
||||
secret_keys
|
||||
.iter()
|
||||
.map(|key| {
|
||||
let public_key = PublicKey::from_secret_key(&key);
|
||||
let mut h = sha3::Keccak256::default();
|
||||
h.write(&public_key.serialize()[1..]).unwrap();
|
||||
let key: [u8; 32] = h.finalize().into();
|
||||
let mut address = [0u8; 20];
|
||||
address.copy_from_slice(&key[12..]);
|
||||
address
|
||||
})
|
||||
.collect(),
|
||||
secret_keys,
|
||||
)
|
||||
}
|
||||
|
||||
/// Initialize the test environment, spins up a solana-test-validator in the background so that
|
||||
/// each test has a fresh environment to work within.
|
||||
pub fn setup() -> (Keypair, RpcClient, Pubkey, Pubkey) {
|
||||
let payer = env::var("BRIDGE_PAYER").unwrap_or("./payer.json".to_string());
|
||||
let rpc_address = env::var("BRIDGE_RPC").unwrap_or("http://127.0.0.1:8899".to_string());
|
||||
let payer = read_keypair_file(payer).unwrap();
|
||||
let rpc = RpcClient::new(rpc_address);
|
||||
|
||||
pub async fn setup() -> (BanksClient, Keypair, Pubkey, Pubkey) {
|
||||
let (program, token_program) = (
|
||||
env::var("BRIDGE_PROGRAM")
|
||||
.unwrap_or("Bridge1p5gheXUvJ6jGWGeCsgPKgnE3YgdGKRVCMY9o".to_string())
|
||||
.parse::<Pubkey>()
|
||||
.unwrap(),
|
||||
env::var("TOKEN_BRIDGE_PROGRAM")
|
||||
.unwrap_or("B6RHG3mfcckmrYN1UhmJzyS1XX3fZKbkeUcpJe9Sy3FE".to_string())
|
||||
env::var("NFT_BRIDGE_PROGRAM")
|
||||
.unwrap_or("NFTWqJR8YnRVqPDvTJrYuLrQDitTG5AScqbeghi4zSA".to_string())
|
||||
.parse::<Pubkey>()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
(payer, rpc, program, token_program)
|
||||
}
|
||||
let mut builder = ProgramTest::new("bridge", program, processor!(bridge::solitaire));
|
||||
builder.add_program("spl_token_metadata", spl_token_metadata::id(), None);
|
||||
builder.add_program(
|
||||
"nft_bridge",
|
||||
token_program,
|
||||
processor!(nft_bridge::solitaire),
|
||||
);
|
||||
|
||||
/// Wait for a single transaction to fully finalize, guaranteeing chain state has been
|
||||
/// confirmed. Useful for consistently fetching data during state checks.
|
||||
pub fn sync(client: &RpcClient, payer: &Keypair) {
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[system_instruction::transfer(
|
||||
&payer.pubkey(),
|
||||
&payer.pubkey(),
|
||||
1,
|
||||
)],
|
||||
CommitmentConfig::finalized(),
|
||||
)
|
||||
.unwrap();
|
||||
// Some instructions go over the limit when tracing is enabled but we need that for better
|
||||
// logging. We don't really care about the limit during these tests anyway.
|
||||
builder.set_compute_max_units(u64::MAX);
|
||||
|
||||
let (client, payer, _) = builder.start().await;
|
||||
(client, payer, program, token_program)
|
||||
}
|
||||
|
||||
/// Fetch account data, the loop is there to re-attempt until data is available.
|
||||
pub fn get_account_data<T: BorshDeserialize>(
|
||||
client: &RpcClient,
|
||||
account: &Pubkey,
|
||||
pub async fn get_account_data<T: BorshDeserialize>(
|
||||
client: &mut BanksClient,
|
||||
account: Pubkey,
|
||||
) -> Option<T> {
|
||||
let account = client
|
||||
.get_account_with_commitment(account, CommitmentConfig::processed())
|
||||
.get_account_with_commitment(account, CommitmentLevel::Processed)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
T::try_from_slice(&account.value.unwrap().data).ok()
|
||||
T::try_from_slice(&account.data).ok()
|
||||
}
|
||||
|
||||
pub fn initialize_bridge(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
pub async fn initialize_bridge(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
payer: &Keypair,
|
||||
) -> Result<Signature, ClientError> {
|
||||
let initial_guardians = &[[1u8; 20]];
|
||||
initial_guardians: &[[u8; 20]],
|
||||
) -> Result<(), TransportError> {
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[bridge::instructions::initialize(
|
||||
*program,
|
||||
program,
|
||||
payer.pubkey(),
|
||||
50,
|
||||
2_000_000_000,
|
||||
initial_guardians,
|
||||
)
|
||||
.unwrap()],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
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,
|
||||
pub async fn initialize(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
payer: &Keypair,
|
||||
bridge: &Pubkey,
|
||||
) -> Result<Signature, ClientError> {
|
||||
let instruction = instructions::initialize(*program, payer.pubkey(), *bridge)
|
||||
bridge: Pubkey,
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction = instructions::initialize(program, payer.pubkey(), bridge)
|
||||
.expect("Could not create Initialize instruction");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[instruction],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn attest(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
bridge: &Pubkey,
|
||||
payer: &Keypair,
|
||||
message: &Keypair,
|
||||
mint: Pubkey,
|
||||
nonce: u32,
|
||||
) -> Result<Signature, ClientError> {
|
||||
let mint_data = Mint::unpack(
|
||||
&client
|
||||
.get_account_with_commitment(&mint, CommitmentConfig::processed())?
|
||||
.value
|
||||
.unwrap()
|
||||
.data,
|
||||
)
|
||||
.expect("Could not unpack Mint");
|
||||
|
||||
let instruction = instructions::attest(
|
||||
*program,
|
||||
*bridge,
|
||||
payer.pubkey(),
|
||||
message.pubkey(),
|
||||
mint,
|
||||
nonce,
|
||||
)
|
||||
.expect("Could not create Attest instruction");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer, message],
|
||||
&[instruction],
|
||||
CommitmentConfig::processed(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn transfer_native(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
bridge: &Pubkey,
|
||||
pub async fn transfer_native(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
bridge: Pubkey,
|
||||
payer: &Keypair,
|
||||
message: &Keypair,
|
||||
from: &Keypair,
|
||||
from_owner: &Keypair,
|
||||
mint: Pubkey,
|
||||
amount: u64,
|
||||
) -> Result<Signature, ClientError> {
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction = instructions::transfer_native(
|
||||
*program,
|
||||
*bridge,
|
||||
program,
|
||||
bridge,
|
||||
payer.pubkey(),
|
||||
message.pubkey(),
|
||||
from.pubkey(),
|
||||
mint,
|
||||
TransferNativeData {
|
||||
nonce: 0,
|
||||
amount,
|
||||
fee: 0,
|
||||
target_address: [0u8; 32],
|
||||
target_chain: 2,
|
||||
},
|
||||
)
|
||||
.expect("Could not create Transfer Native");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
|
@ -318,53 +269,49 @@ mod helpers {
|
|||
spl_token::instruction::approve(
|
||||
&spl_token::id(),
|
||||
&from.pubkey(),
|
||||
&token_bridge::accounts::AuthoritySigner::key(None, program),
|
||||
&nft_bridge::accounts::AuthoritySigner::key(None, &program),
|
||||
&from_owner.pubkey(),
|
||||
&[],
|
||||
amount,
|
||||
1,
|
||||
)
|
||||
.unwrap(),
|
||||
instruction,
|
||||
],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn transfer_wrapped(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
bridge: &Pubkey,
|
||||
pub async fn transfer_wrapped(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
bridge: Pubkey,
|
||||
payer: &Keypair,
|
||||
message: &Keypair,
|
||||
from: Pubkey,
|
||||
from_owner: &Keypair,
|
||||
token_chain: u16,
|
||||
token_address: Address,
|
||||
amount: u64,
|
||||
) -> Result<Signature, ClientError> {
|
||||
token_id: U256,
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction = instructions::transfer_wrapped(
|
||||
*program,
|
||||
*bridge,
|
||||
program,
|
||||
bridge,
|
||||
payer.pubkey(),
|
||||
message.pubkey(),
|
||||
from,
|
||||
from_owner.pubkey(),
|
||||
token_chain,
|
||||
token_address,
|
||||
token_id,
|
||||
TransferWrappedData {
|
||||
nonce: 0,
|
||||
amount,
|
||||
fee: 0,
|
||||
target_address: [5u8; 32],
|
||||
target_chain: 2,
|
||||
},
|
||||
)
|
||||
.expect("Could not create Transfer Native");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
|
@ -373,162 +320,151 @@ mod helpers {
|
|||
spl_token::instruction::approve(
|
||||
&spl_token::id(),
|
||||
&from,
|
||||
&token_bridge::accounts::AuthoritySigner::key(None, program),
|
||||
&nft_bridge::accounts::AuthoritySigner::key(None, &program),
|
||||
&from_owner.pubkey(),
|
||||
&[],
|
||||
amount,
|
||||
1,
|
||||
)
|
||||
.unwrap(),
|
||||
instruction,
|
||||
],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn register_chain(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
bridge: &Pubkey,
|
||||
message_acc: &Pubkey,
|
||||
pub async fn register_chain(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
bridge: Pubkey,
|
||||
message_acc: Pubkey,
|
||||
vaa: PostVAAData,
|
||||
payload: PayloadGovernanceRegisterChain,
|
||||
payer: &Keypair,
|
||||
) -> Result<Signature, ClientError> {
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction = instructions::register_chain(
|
||||
*program,
|
||||
*bridge,
|
||||
program,
|
||||
bridge,
|
||||
payer.pubkey(),
|
||||
*message_acc,
|
||||
message_acc,
|
||||
vaa,
|
||||
payload,
|
||||
RegisterChainData {},
|
||||
)
|
||||
.expect("Could not create Transfer Native");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[instruction],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn complete_native(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
bridge: &Pubkey,
|
||||
message_acc: &Pubkey,
|
||||
pub async fn complete_native(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
bridge: Pubkey,
|
||||
message_acc: Pubkey,
|
||||
vaa: PostVAAData,
|
||||
payload: PayloadTransfer,
|
||||
payer: &Keypair,
|
||||
) -> Result<Signature, ClientError> {
|
||||
to_authority: Pubkey,
|
||||
mint: Pubkey,
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction = instructions::complete_native(
|
||||
*program,
|
||||
*bridge,
|
||||
program,
|
||||
bridge,
|
||||
payer.pubkey(),
|
||||
*message_acc,
|
||||
message_acc,
|
||||
vaa,
|
||||
Pubkey::new(&payload.to[..]),
|
||||
None,
|
||||
Pubkey::new(&payload.token_address[..]),
|
||||
to_authority,
|
||||
mint,
|
||||
CompleteNativeData {},
|
||||
)
|
||||
.expect("Could not create Complete Native instruction");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[instruction],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn complete_transfer_wrapped(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
bridge: &Pubkey,
|
||||
message_acc: &Pubkey,
|
||||
pub async fn complete_wrapped(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
bridge: Pubkey,
|
||||
message_acc: Pubkey,
|
||||
vaa: PostVAAData,
|
||||
payload: PayloadTransfer,
|
||||
to: Pubkey,
|
||||
payer: &Keypair,
|
||||
) -> Result<Signature, ClientError> {
|
||||
let to = Pubkey::new(&payload.to[..]);
|
||||
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction = instructions::complete_wrapped(
|
||||
*program,
|
||||
*bridge,
|
||||
program,
|
||||
bridge,
|
||||
payer.pubkey(),
|
||||
*message_acc,
|
||||
message_acc,
|
||||
vaa,
|
||||
payload,
|
||||
to,
|
||||
None,
|
||||
CompleteWrappedData {},
|
||||
)
|
||||
.expect("Could not create Complete Wrapped instruction");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[instruction],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn create_wrapped(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
bridge: &Pubkey,
|
||||
message_acc: &Pubkey,
|
||||
pub async fn complete_wrapped_meta(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
bridge: Pubkey,
|
||||
message_acc: Pubkey,
|
||||
vaa: PostVAAData,
|
||||
payload: PayloadAssetMeta,
|
||||
payload: PayloadTransfer,
|
||||
payer: &Keypair,
|
||||
) -> Result<Signature, ClientError> {
|
||||
let instruction = instructions::create_wrapped(
|
||||
*program,
|
||||
*bridge,
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction = instructions::complete_wrapped_meta(
|
||||
program,
|
||||
bridge,
|
||||
payer.pubkey(),
|
||||
*message_acc,
|
||||
message_acc,
|
||||
vaa,
|
||||
payload,
|
||||
CreateWrappedData {},
|
||||
CompleteWrappedMetaData {},
|
||||
)
|
||||
.expect("Could not create Create Wrapped instruction");
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
.expect("Could not create Complete Wrapped Meta instruction");
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[instruction],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn create_mint(
|
||||
client: &RpcClient,
|
||||
pub async fn create_mint(
|
||||
client: &mut BanksClient,
|
||||
payer: &Keypair,
|
||||
mint_authority: &Pubkey,
|
||||
mint: &Keypair,
|
||||
) -> Result<Signature, ClientError> {
|
||||
) -> Result<(), TransportError> {
|
||||
let mint_key = mint.pubkey();
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
|
@ -536,64 +472,33 @@ mod helpers {
|
|||
&[
|
||||
solana_sdk::system_instruction::create_account(
|
||||
&payer.pubkey(),
|
||||
&mint.pubkey(),
|
||||
&mint_key,
|
||||
Rent::default().minimum_balance(spl_token::state::Mint::LEN),
|
||||
spl_token::state::Mint::LEN as u64,
|
||||
&spl_token::id(),
|
||||
),
|
||||
spl_token::instruction::initialize_mint(
|
||||
&spl_token::id(),
|
||||
&mint.pubkey(),
|
||||
&mint_key,
|
||||
mint_authority,
|
||||
None,
|
||||
0,
|
||||
)
|
||||
.unwrap(),
|
||||
],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn create_spl_metadata(
|
||||
client: &RpcClient,
|
||||
payer: &Keypair,
|
||||
metadata_account: &Pubkey,
|
||||
mint_authority: &Keypair,
|
||||
mint: &Keypair,
|
||||
update_authority: &Pubkey,
|
||||
name: String,
|
||||
symbol: String,
|
||||
) -> Result<Signature, ClientError> {
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer, mint_authority],
|
||||
&[spl_token_metadata::instruction::create_metadata_accounts(
|
||||
spl_token_metadata::id(),
|
||||
*metadata_account,
|
||||
mint.pubkey(),
|
||||
mint_authority.pubkey(),
|
||||
payer.pubkey(),
|
||||
*update_authority,
|
||||
name,
|
||||
symbol,
|
||||
"https://token.org".to_string(),
|
||||
None,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
)],
|
||||
CommitmentConfig::processed(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn create_token_account(
|
||||
client: &RpcClient,
|
||||
pub async fn create_token_account(
|
||||
client: &mut BanksClient,
|
||||
payer: &Keypair,
|
||||
token_acc: &Keypair,
|
||||
token_authority: Pubkey,
|
||||
mint: Pubkey,
|
||||
) -> Result<Signature, ClientError> {
|
||||
token_authority: &Pubkey,
|
||||
mint: &Pubkey,
|
||||
) -> Result<(), TransportError> {
|
||||
let token_key = token_acc.pubkey();
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
|
@ -601,35 +506,71 @@ mod helpers {
|
|||
&[
|
||||
solana_sdk::system_instruction::create_account(
|
||||
&payer.pubkey(),
|
||||
&token_acc.pubkey(),
|
||||
&token_key,
|
||||
Rent::default().minimum_balance(spl_token::state::Account::LEN),
|
||||
spl_token::state::Account::LEN as u64,
|
||||
&spl_token::id(),
|
||||
),
|
||||
spl_token::instruction::initialize_account(
|
||||
&spl_token::id(),
|
||||
&token_acc.pubkey(),
|
||||
&mint,
|
||||
&token_authority,
|
||||
&token_key,
|
||||
mint,
|
||||
token_authority,
|
||||
)
|
||||
.unwrap(),
|
||||
],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn mint_tokens(
|
||||
client: &RpcClient,
|
||||
pub async fn create_spl_metadata(
|
||||
client: &mut BanksClient,
|
||||
payer: &Keypair,
|
||||
metadata_account: Pubkey,
|
||||
mint_authority: &Keypair,
|
||||
mint: Pubkey,
|
||||
update_authority: Pubkey,
|
||||
name: String,
|
||||
symbol: String,
|
||||
uri: String,
|
||||
) -> Result<(), TransportError> {
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer, mint_authority],
|
||||
&[spl_token_metadata::instruction::create_metadata_accounts(
|
||||
spl_token_metadata::id(),
|
||||
metadata_account,
|
||||
mint,
|
||||
mint_authority.pubkey(),
|
||||
payer.pubkey(),
|
||||
update_authority,
|
||||
name,
|
||||
symbol,
|
||||
uri,
|
||||
None,
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
)],
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn mint_tokens(
|
||||
client: &mut BanksClient,
|
||||
payer: &Keypair,
|
||||
mint_authority: &Keypair,
|
||||
mint: &Keypair,
|
||||
token_account: &Pubkey,
|
||||
amount: u64,
|
||||
) -> Result<Signature, ClientError> {
|
||||
) -> Result<(), TransportError> {
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer, &mint_authority],
|
||||
&[payer, mint_authority],
|
||||
&[spl_token::instruction::mint_to(
|
||||
&spl_token::id(),
|
||||
&mint.pubkey(),
|
||||
|
@ -639,15 +580,16 @@ mod helpers {
|
|||
amount,
|
||||
)
|
||||
.unwrap()],
|
||||
CommitmentConfig::processed(),
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Utility function for generating VAA's from message data.
|
||||
pub fn generate_vaa(
|
||||
pub fn generate_vaa<T: Into<Vec<u8>>>(
|
||||
emitter: Address,
|
||||
emitter_chain: u16,
|
||||
data: Vec<u8>,
|
||||
data: T,
|
||||
nonce: u32,
|
||||
sequence: u64,
|
||||
) -> (PostVAAData, [u8; 32], [u8; 32]) {
|
||||
|
@ -659,7 +601,7 @@ mod helpers {
|
|||
emitter_chain,
|
||||
emitter_address: emitter,
|
||||
sequence,
|
||||
payload: data,
|
||||
payload: data.into(),
|
||||
timestamp: SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
|
@ -698,46 +640,81 @@ mod helpers {
|
|||
(vaa, body, body_hash)
|
||||
}
|
||||
|
||||
pub fn post_vaa(
|
||||
client: &RpcClient,
|
||||
pub async fn verify_signatures(
|
||||
client: &mut BanksClient,
|
||||
program: &Pubkey,
|
||||
payer: &Keypair,
|
||||
vaa: PostVAAData,
|
||||
) -> Result<(), ClientError> {
|
||||
let instruction =
|
||||
bridge::instructions::post_vaa(*program, payer.pubkey(), Pubkey::new_unique(), vaa);
|
||||
body: [u8; 32],
|
||||
secret_keys: &[SecretKey],
|
||||
guardian_set_version: u32,
|
||||
) -> Result<Pubkey, TransportError> {
|
||||
let signature_set = Keypair::new();
|
||||
let tx_signers = [payer, &signature_set];
|
||||
// Push Secp256k1 instructions for each signature we want to verify.
|
||||
for (i, key) in secret_keys.iter().enumerate() {
|
||||
// Set this signers signature position as present at 0.
|
||||
let mut signers = [-1; 19];
|
||||
signers[i] = 0;
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&tx_signers,
|
||||
&[
|
||||
new_secp256k1_instruction(key, &body),
|
||||
bridge::instructions::verify_signatures(
|
||||
*program,
|
||||
payer.pubkey(),
|
||||
guardian_set_version,
|
||||
signature_set.pubkey(),
|
||||
bridge::VerifySignaturesData { signers },
|
||||
)
|
||||
.unwrap(),
|
||||
],
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(signature_set.pubkey())
|
||||
}
|
||||
|
||||
pub async fn post_vaa(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
payer: &Keypair,
|
||||
signature_set: Pubkey,
|
||||
vaa: PostVAAData,
|
||||
) -> Result<(), TransportError> {
|
||||
let instruction =
|
||||
bridge::instructions::post_vaa(program, payer.pubkey(), signature_set, vaa);
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
&[payer],
|
||||
&[instruction],
|
||||
CommitmentConfig::processed(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn post_message(
|
||||
client: &RpcClient,
|
||||
program: &Pubkey,
|
||||
pub async fn post_message(
|
||||
client: &mut BanksClient,
|
||||
program: Pubkey,
|
||||
payer: &Keypair,
|
||||
emitter: &Keypair,
|
||||
message: &Keypair,
|
||||
nonce: u32,
|
||||
data: Vec<u8>,
|
||||
fee: u64,
|
||||
) -> Result<(), ClientError> {
|
||||
) -> Result<(), TransportError> {
|
||||
// Transfer money into the fee collector as it needs a balance/must exist.
|
||||
let fee_collector = FeeCollector::<'_>::key(None, program);
|
||||
let fee_collector = FeeCollector::<'_>::key(None, &program);
|
||||
|
||||
// Capture the resulting message, later functions will need this.
|
||||
let instruction = bridge::instructions::post_message(
|
||||
*program,
|
||||
program,
|
||||
payer.pubkey(),
|
||||
emitter.pubkey(),
|
||||
message.pubkey(),
|
||||
|
@ -747,10 +724,6 @@ mod helpers {
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
for account in instruction.accounts.iter().enumerate() {
|
||||
println!("{}: {}", account.0, account.1.pubkey);
|
||||
}
|
||||
|
||||
execute(
|
||||
client,
|
||||
payer,
|
||||
|
@ -759,9 +732,8 @@ mod helpers {
|
|||
system_instruction::transfer(&payer.pubkey(), &fee_collector, fee),
|
||||
instruction,
|
||||
],
|
||||
CommitmentConfig::processed(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
CommitmentLevel::Processed,
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,66 @@
|
|||
#![allow(warnings)]
|
||||
|
||||
use borsh::BorshSerialize;
|
||||
use bridge::{
|
||||
accounts::{
|
||||
Bridge,
|
||||
FeeCollector,
|
||||
GuardianSet,
|
||||
GuardianSetDerivationData,
|
||||
PostedVAA,
|
||||
PostedVAADerivationData,
|
||||
SignatureSet,
|
||||
},
|
||||
instruction,
|
||||
types::{
|
||||
GovernancePayloadGuardianSetChange,
|
||||
GovernancePayloadSetMessageFee,
|
||||
GovernancePayloadTransferFees,
|
||||
},
|
||||
BridgeConfig,
|
||||
BridgeData,
|
||||
GuardianSetData,
|
||||
Initialize,
|
||||
MessageData,
|
||||
PostVAA,
|
||||
PostVAAData,
|
||||
PostedVAAData,
|
||||
SequenceTracker,
|
||||
SerializePayload,
|
||||
Signature,
|
||||
SignatureSet as SignatureSetData,
|
||||
};
|
||||
use byteorder::{
|
||||
BigEndian,
|
||||
WriteBytesExt,
|
||||
};
|
||||
use hex_literal::hex;
|
||||
use rand::Rng;
|
||||
use secp256k1::{
|
||||
use libsecp256k1::{
|
||||
Message as Secp256k1Message,
|
||||
PublicKey,
|
||||
SecretKey,
|
||||
};
|
||||
use nft_bridge::{
|
||||
accounts::{
|
||||
ConfigAccount,
|
||||
EmitterAccount,
|
||||
SplTokenMeta,
|
||||
SplTokenMetaDerivationData,
|
||||
WrappedDerivationData,
|
||||
WrappedMint,
|
||||
},
|
||||
messages::{
|
||||
PayloadGovernanceRegisterChain,
|
||||
PayloadTransfer,
|
||||
},
|
||||
types::{
|
||||
Address,
|
||||
Config,
|
||||
},
|
||||
};
|
||||
use primitive_types::U256;
|
||||
use rand::Rng;
|
||||
use sha3::Digest;
|
||||
use solana_client::rpc_client::RpcClient;
|
||||
use solana_program::{
|
||||
borsh::try_from_slice_unchecked,
|
||||
hash,
|
||||
|
@ -30,14 +77,19 @@ use solana_program::{
|
|||
system_program,
|
||||
sysvar,
|
||||
};
|
||||
use solana_program_test::{
|
||||
tokio,
|
||||
BanksClient,
|
||||
};
|
||||
use solana_sdk::{
|
||||
commitment_config::CommitmentConfig,
|
||||
commitment_config::CommitmentLevel,
|
||||
signature::{
|
||||
read_keypair_file,
|
||||
Keypair,
|
||||
Signer,
|
||||
},
|
||||
transaction::Transaction,
|
||||
transport::TransportError,
|
||||
};
|
||||
use solitaire::{
|
||||
processors::seeded::Seeded,
|
||||
|
@ -45,66 +97,20 @@ use solitaire::{
|
|||
};
|
||||
use spl_token::state::Mint;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
convert::TryInto,
|
||||
io::{
|
||||
Cursor,
|
||||
Write,
|
||||
},
|
||||
str::FromStr,
|
||||
time::{
|
||||
Duration,
|
||||
SystemTime,
|
||||
UNIX_EPOCH,
|
||||
},
|
||||
};
|
||||
|
||||
use bridge::{
|
||||
accounts::{
|
||||
Bridge,
|
||||
FeeCollector,
|
||||
GuardianSet,
|
||||
GuardianSetDerivationData,
|
||||
PostedVAA,
|
||||
PostedVAADerivationData,
|
||||
SignatureSet,
|
||||
},
|
||||
instruction,
|
||||
types::{
|
||||
BridgeConfig,
|
||||
BridgeData,
|
||||
GovernancePayloadGuardianSetChange,
|
||||
GovernancePayloadSetMessageFee,
|
||||
GovernancePayloadTransferFees,
|
||||
GuardianSetData,
|
||||
MessageData,
|
||||
PostedVAAData,
|
||||
SequenceTracker,
|
||||
SignatureSet as SignatureSetData,
|
||||
},
|
||||
Initialize,
|
||||
PostVAA,
|
||||
PostVAAData,
|
||||
SerializePayload,
|
||||
Signature,
|
||||
};
|
||||
use primitive_types::U256;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
str::FromStr,
|
||||
time::UNIX_EPOCH,
|
||||
};
|
||||
use token_bridge::{
|
||||
accounts::{
|
||||
EmitterAccount,
|
||||
WrappedDerivationData,
|
||||
WrappedMint,
|
||||
},
|
||||
messages::{
|
||||
PayloadAssetMeta,
|
||||
PayloadGovernanceRegisterChain,
|
||||
PayloadTransfer,
|
||||
},
|
||||
types::Address,
|
||||
};
|
||||
|
||||
mod common;
|
||||
|
||||
const GOVERNANCE_KEY: [u8; 64] = [
|
||||
|
@ -115,20 +121,23 @@ const GOVERNANCE_KEY: [u8; 64] = [
|
|||
];
|
||||
|
||||
struct Context {
|
||||
/// Guardian public keys.
|
||||
guardians: Vec<[u8; 20]>,
|
||||
|
||||
/// Guardian secret keys.
|
||||
guardian_keys: Vec<SecretKey>,
|
||||
|
||||
/// Address of the core bridge contract.
|
||||
bridge: Pubkey,
|
||||
|
||||
/// Shared RPC client for tests to make transactions with.
|
||||
client: RpcClient,
|
||||
client: BanksClient,
|
||||
|
||||
/// Payer key with a ton of lamports to ease testing with.
|
||||
payer: Keypair,
|
||||
|
||||
/// Track nonces throughout the tests.
|
||||
seq: Sequencer,
|
||||
|
||||
/// Address of the token bridge itself that we wish to test.
|
||||
token_bridge: Pubkey,
|
||||
nft_bridge: Pubkey,
|
||||
|
||||
/// Keypairs for mint information, required in multiple tests.
|
||||
mint_authority: Keypair,
|
||||
|
@ -141,31 +150,13 @@ struct Context {
|
|||
metadata_account: Pubkey,
|
||||
}
|
||||
|
||||
/// Small helper to track and provide sequences during tests. This is in particular needed for
|
||||
/// guardian operations that require them for derivations.
|
||||
struct Sequencer {
|
||||
sequences: HashMap<[u8; 32], u64>,
|
||||
}
|
||||
async fn set_up() -> Result<Context, TransportError> {
|
||||
let (guardians, guardian_keys) = common::generate_keys(6);
|
||||
|
||||
impl Sequencer {
|
||||
fn next(&mut self, emitter: [u8; 32]) -> u64 {
|
||||
let entry = self.sequences.entry(emitter).or_insert(0);
|
||||
*entry += 1;
|
||||
*entry - 1
|
||||
}
|
||||
|
||||
fn peek(&mut self, emitter: [u8; 32]) -> u64 {
|
||||
*self.sequences.entry(emitter).or_insert(0)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_integration_tests() {
|
||||
let (payer, client, bridge, token_bridge) = common::setup();
|
||||
let (mut client, payer, bridge, nft_bridge) = common::setup().await;
|
||||
|
||||
// Setup a Bridge to test against.
|
||||
println!("Bridge: {}", bridge);
|
||||
common::initialize_bridge(&client, &bridge, &payer);
|
||||
common::initialize_bridge(&mut client, bridge, &payer, &guardians).await?;
|
||||
|
||||
// Context for test environment.
|
||||
let mint = Keypair::new();
|
||||
|
@ -185,22 +176,21 @@ fn run_integration_tests() {
|
|||
);
|
||||
|
||||
// Token Bridge Meta
|
||||
use token_bridge::accounts::WrappedTokenMeta;
|
||||
use nft_bridge::accounts::WrappedTokenMeta;
|
||||
let metadata_account = WrappedTokenMeta::<'_, { AccountState::Uninitialized }>::key(
|
||||
&token_bridge::accounts::WrappedMetaDerivationData {
|
||||
&nft_bridge::accounts::WrappedMetaDerivationData {
|
||||
mint_key: mint_pubkey.clone(),
|
||||
},
|
||||
&token_bridge,
|
||||
&nft_bridge,
|
||||
);
|
||||
|
||||
let mut context = Context {
|
||||
seq: Sequencer {
|
||||
sequences: HashMap::new(),
|
||||
},
|
||||
guardians,
|
||||
guardian_keys,
|
||||
bridge,
|
||||
client,
|
||||
payer,
|
||||
token_bridge,
|
||||
nft_bridge,
|
||||
mint_authority: Keypair::new(),
|
||||
mint,
|
||||
mint_meta: metadata_account,
|
||||
|
@ -211,207 +201,110 @@ fn run_integration_tests() {
|
|||
|
||||
// Create a mint for use within tests.
|
||||
common::create_mint(
|
||||
&context.client,
|
||||
&mut context.client,
|
||||
&context.payer,
|
||||
&context.mint_authority.pubkey(),
|
||||
&context.mint,
|
||||
)
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
// Create Token accounts for use within tests.
|
||||
common::create_token_account(
|
||||
&context.client,
|
||||
&mut context.client,
|
||||
&context.payer,
|
||||
&context.token_account,
|
||||
context.token_authority.pubkey(),
|
||||
context.mint.pubkey(),
|
||||
&context.token_authority.pubkey(),
|
||||
&context.mint.pubkey(),
|
||||
)
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
// Mint tokens
|
||||
// Create an SPL metadata account for native NFTs.
|
||||
common::create_spl_metadata(
|
||||
&mut context.client,
|
||||
&context.payer,
|
||||
context.metadata_account,
|
||||
&context.mint_authority,
|
||||
context.mint.pubkey(),
|
||||
context.payer.pubkey(),
|
||||
"Non-Fungible Token".into(),
|
||||
"NFT".into(),
|
||||
"https://example.com".into(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
// Mint an NFT.
|
||||
common::mint_tokens(
|
||||
&context.client,
|
||||
&mut context.client,
|
||||
&context.payer,
|
||||
&context.mint_authority,
|
||||
&context.mint,
|
||||
&context.token_account.pubkey(),
|
||||
1000,
|
||||
1,
|
||||
)
|
||||
.unwrap();
|
||||
.await?;
|
||||
|
||||
// Initialize the bridge and verify the bridges state.
|
||||
test_initialize(&mut context);
|
||||
test_transfer_native(&mut context);
|
||||
test_attest(&mut context);
|
||||
test_register_chain(&mut context);
|
||||
test_transfer_native_in(&mut context);
|
||||
|
||||
// Create an SPL Metadata account to test attestations for wrapped tokens.
|
||||
common::create_spl_metadata(
|
||||
&context.client,
|
||||
// Initialize the nft bridge.
|
||||
common::initialize(
|
||||
&mut context.client,
|
||||
context.nft_bridge,
|
||||
&context.payer,
|
||||
&context.metadata_account,
|
||||
&context.mint_authority,
|
||||
&context.mint,
|
||||
&context.payer.pubkey(),
|
||||
"BTC".to_string(),
|
||||
"Bitcoin".to_string(),
|
||||
context.bridge,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let wrapped = test_create_wrapped(&mut context);
|
||||
let wrapped_acc = Keypair::new();
|
||||
common::create_token_account(
|
||||
&context.client,
|
||||
&context.payer,
|
||||
&wrapped_acc,
|
||||
context.token_authority.pubkey(),
|
||||
wrapped,
|
||||
)
|
||||
.unwrap();
|
||||
test_transfer_wrapped_in(&mut context, wrapped_acc.pubkey());
|
||||
test_transfer_wrapped(&mut context, wrapped_acc.pubkey());
|
||||
// Verify NFT Bridge State
|
||||
let config_key = ConfigAccount::<'_, { AccountState::Uninitialized }>::key(None, &nft_bridge);
|
||||
let config: Config = common::get_account_data(&mut context.client, config_key)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(config.wormhole_bridge, bridge);
|
||||
|
||||
Ok(context)
|
||||
}
|
||||
|
||||
fn test_attest(context: &mut Context) -> () {
|
||||
println!("Attest");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn transfer_native() {
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
ref mint_authority,
|
||||
ref mint,
|
||||
ref mint_meta,
|
||||
ref metadata_account,
|
||||
..
|
||||
} = context;
|
||||
|
||||
let message = &Keypair::new();
|
||||
|
||||
common::attest(
|
||||
client,
|
||||
token_bridge,
|
||||
ref mut client,
|
||||
bridge,
|
||||
payer,
|
||||
message,
|
||||
mint.pubkey(),
|
||||
0,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let emitter_key = EmitterAccount::key(None, &token_bridge);
|
||||
let mint_data = Mint::unpack(
|
||||
&client
|
||||
.get_account_with_commitment(&mint.pubkey(), CommitmentConfig::processed())
|
||||
.unwrap()
|
||||
.value
|
||||
.unwrap()
|
||||
.data,
|
||||
)
|
||||
.unwrap();
|
||||
let payload = PayloadAssetMeta {
|
||||
token_address: mint.pubkey().to_bytes(),
|
||||
token_chain: 1,
|
||||
decimals: mint_data.decimals,
|
||||
symbol: "USD".to_string(),
|
||||
name: "Bitcoin".to_string(),
|
||||
};
|
||||
let payload = payload.try_to_vec().unwrap();
|
||||
}
|
||||
|
||||
fn test_transfer_native(context: &mut Context) -> () {
|
||||
println!("Transfer Native");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
};
|
||||
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
nft_bridge,
|
||||
ref mint_authority,
|
||||
ref mint,
|
||||
ref mint_meta,
|
||||
ref token_account,
|
||||
ref token_authority,
|
||||
..
|
||||
} = context;
|
||||
} = set_up().await.unwrap();
|
||||
|
||||
let message = &Keypair::new();
|
||||
|
||||
common::transfer_native(
|
||||
client,
|
||||
token_bridge,
|
||||
nft_bridge,
|
||||
bridge,
|
||||
payer,
|
||||
message,
|
||||
token_account,
|
||||
token_authority,
|
||||
mint.pubkey(),
|
||||
100,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn test_transfer_wrapped(context: &mut Context, token_account: Pubkey) -> () {
|
||||
println!("TransferWrapped");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
};
|
||||
|
||||
async fn register_chain(context: &mut Context) {
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref mut client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
ref mint_authority,
|
||||
ref token_authority,
|
||||
..
|
||||
} = context;
|
||||
|
||||
let message = &Keypair::new();
|
||||
|
||||
common::transfer_wrapped(
|
||||
client,
|
||||
token_bridge,
|
||||
bridge,
|
||||
payer,
|
||||
message,
|
||||
token_account,
|
||||
token_authority,
|
||||
2,
|
||||
[1u8; 32],
|
||||
10000000,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn test_register_chain(context: &mut Context) -> () {
|
||||
println!("Register Chain");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
};
|
||||
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
ref nft_bridge,
|
||||
ref mint_authority,
|
||||
ref mint,
|
||||
ref mint_meta,
|
||||
ref token_account,
|
||||
ref token_authority,
|
||||
ref guardian_keys,
|
||||
..
|
||||
} = context;
|
||||
|
||||
|
@ -423,209 +316,219 @@ fn test_register_chain(context: &mut Context) -> () {
|
|||
};
|
||||
let message = payload.try_to_vec().unwrap();
|
||||
|
||||
let (vaa, _, _) = common::generate_vaa(emitter.pubkey().to_bytes(), 1, message, nonce, 0);
|
||||
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
|
||||
let (vaa, body, _) = common::generate_vaa(emitter.pubkey().to_bytes(), 1, message, nonce, 0);
|
||||
let signature_set = common::verify_signatures(client, &bridge, payer, body, guardian_keys, 0)
|
||||
.await
|
||||
.unwrap();
|
||||
common::post_vaa(client, *bridge, payer, signature_set, vaa.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut msg_derivation_data = &PostedVAADerivationData {
|
||||
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
|
||||
payload_hash: body.to_vec(),
|
||||
};
|
||||
let message_key =
|
||||
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
|
||||
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, bridge);
|
||||
|
||||
common::register_chain(
|
||||
client,
|
||||
token_bridge,
|
||||
bridge,
|
||||
&message_key,
|
||||
*nft_bridge,
|
||||
*bridge,
|
||||
message_key,
|
||||
vaa,
|
||||
payload,
|
||||
payer,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn test_transfer_native_in(context: &mut Context) -> () {
|
||||
println!("TransferNativeIn");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
};
|
||||
|
||||
#[tokio::test]
|
||||
async fn transfer_native_in() {
|
||||
let mut context = set_up().await.unwrap();
|
||||
register_chain(&mut context).await;
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
ref mut client,
|
||||
bridge,
|
||||
nft_bridge,
|
||||
ref mint_authority,
|
||||
ref mint,
|
||||
ref mint_meta,
|
||||
ref token_account,
|
||||
ref token_authority,
|
||||
ref guardian_keys,
|
||||
..
|
||||
} = context;
|
||||
|
||||
// Do an initial transfer so that the bridge account owns the NFT.
|
||||
let message = &Keypair::new();
|
||||
common::transfer_native(
|
||||
client,
|
||||
nft_bridge,
|
||||
bridge,
|
||||
payer,
|
||||
message,
|
||||
token_account,
|
||||
token_authority,
|
||||
mint.pubkey(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let nonce = rand::thread_rng().gen();
|
||||
|
||||
let token_address = [1u8; 32];
|
||||
let token_chain = 1;
|
||||
let token_id = U256::from_big_endian(&mint.pubkey().to_bytes());
|
||||
|
||||
let associated_addr = spl_associated_token_account::get_associated_token_address(
|
||||
&token_authority.pubkey(),
|
||||
&mint.pubkey(),
|
||||
);
|
||||
|
||||
let payload = PayloadTransfer {
|
||||
amount: U256::from(100),
|
||||
token_address: mint.pubkey().to_bytes(),
|
||||
token_address: [1u8; 32],
|
||||
token_chain: 1,
|
||||
to: token_account.pubkey().to_bytes(),
|
||||
symbol: "NFT".into(),
|
||||
name: "Non-Fungible Token".into(),
|
||||
token_id,
|
||||
uri: "https://example.com".to_string(),
|
||||
to: associated_addr.to_bytes(),
|
||||
to_chain: 1,
|
||||
fee: U256::from(0),
|
||||
};
|
||||
let message = payload.try_to_vec().unwrap();
|
||||
|
||||
let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 1);
|
||||
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
|
||||
let mut msg_derivation_data = &PostedVAADerivationData {
|
||||
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
|
||||
let (vaa, body, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 1);
|
||||
let signature_set = common::verify_signatures(client, &bridge, payer, body, guardian_keys, 0)
|
||||
.await
|
||||
.unwrap();
|
||||
common::post_vaa(client, bridge, payer, signature_set, vaa.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
let msg_derivation_data = &PostedVAADerivationData {
|
||||
payload_hash: body.to_vec(),
|
||||
};
|
||||
let message_key =
|
||||
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
|
||||
|
||||
common::complete_native(
|
||||
client,
|
||||
token_bridge,
|
||||
nft_bridge,
|
||||
bridge,
|
||||
&message_key,
|
||||
message_key,
|
||||
vaa,
|
||||
payload,
|
||||
payer,
|
||||
token_authority.pubkey(),
|
||||
mint.pubkey(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn test_transfer_wrapped_in(context: &mut Context, to: Pubkey) -> () {
|
||||
println!("TransferWrappedIn");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
};
|
||||
#[tokio::test]
|
||||
async fn transfer_wrapped() {
|
||||
let mut context = set_up().await.unwrap();
|
||||
register_chain(&mut context).await;
|
||||
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
ref mut client,
|
||||
bridge,
|
||||
nft_bridge,
|
||||
ref mint_authority,
|
||||
ref mint,
|
||||
ref mint_meta,
|
||||
ref token_account,
|
||||
ref token_authority,
|
||||
ref guardian_keys,
|
||||
metadata_account,
|
||||
..
|
||||
} = context;
|
||||
|
||||
let nonce = rand::thread_rng().gen();
|
||||
|
||||
let payload = PayloadTransfer {
|
||||
amount: U256::from(100000000),
|
||||
token_address: [1u8; 32],
|
||||
token_chain: 2,
|
||||
to: to.to_bytes(),
|
||||
to_chain: 1,
|
||||
fee: U256::from(0),
|
||||
};
|
||||
let message = payload.try_to_vec().unwrap();
|
||||
let token_chain = 2;
|
||||
let token_address = [7u8; 32];
|
||||
let token_id = U256::from_big_endian(&[0x2cu8; 32]);
|
||||
|
||||
let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, rand::thread_rng().gen());
|
||||
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
|
||||
let mut msg_derivation_data = &PostedVAADerivationData {
|
||||
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
|
||||
};
|
||||
let message_key =
|
||||
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
|
||||
|
||||
common::complete_transfer_wrapped(
|
||||
client,
|
||||
token_bridge,
|
||||
bridge,
|
||||
&message_key,
|
||||
vaa,
|
||||
payload,
|
||||
payer,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn test_create_wrapped(context: &mut Context) -> (Pubkey) {
|
||||
println!("CreateWrapped");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
};
|
||||
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
ref mint_authority,
|
||||
ref mint,
|
||||
ref mint_meta,
|
||||
ref token_account,
|
||||
ref token_authority,
|
||||
..
|
||||
} = context;
|
||||
|
||||
let nonce = rand::thread_rng().gen();
|
||||
|
||||
let payload = PayloadAssetMeta {
|
||||
token_address: [1u8; 32],
|
||||
token_chain: 2,
|
||||
decimals: 7,
|
||||
symbol: "".to_string(),
|
||||
name: "".to_string(),
|
||||
};
|
||||
let message = payload.try_to_vec().unwrap();
|
||||
|
||||
let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 2);
|
||||
common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
|
||||
let mut msg_derivation_data = &PostedVAADerivationData {
|
||||
payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
|
||||
};
|
||||
let message_key =
|
||||
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
|
||||
|
||||
common::create_wrapped(
|
||||
client,
|
||||
token_bridge,
|
||||
bridge,
|
||||
&message_key,
|
||||
vaa,
|
||||
payload,
|
||||
payer,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
return WrappedMint::<'_, { AccountState::Initialized }>::key(
|
||||
let wrapped_mint_key = WrappedMint::<'_, { AccountState::Uninitialized }>::key(
|
||||
&WrappedDerivationData {
|
||||
token_chain: 2,
|
||||
token_address: [1u8; 32],
|
||||
token_chain,
|
||||
token_address,
|
||||
token_id,
|
||||
},
|
||||
token_bridge,
|
||||
&nft_bridge,
|
||||
);
|
||||
let associated_addr = spl_associated_token_account::get_associated_token_address(
|
||||
&token_authority.pubkey(),
|
||||
&wrapped_mint_key,
|
||||
);
|
||||
}
|
||||
|
||||
fn test_initialize(context: &mut Context) {
|
||||
println!("Initialize");
|
||||
use token_bridge::{
|
||||
accounts::ConfigAccount,
|
||||
types::Config,
|
||||
let symbol = "UUC";
|
||||
let name = "External Token";
|
||||
let uri = "https://example.com";
|
||||
let payload = PayloadTransfer {
|
||||
token_address,
|
||||
token_chain,
|
||||
symbol: symbol.into(),
|
||||
name: name.into(),
|
||||
token_id,
|
||||
uri: uri.into(),
|
||||
to: associated_addr.to_bytes(),
|
||||
to_chain: 1,
|
||||
};
|
||||
let message = payload.try_to_vec().unwrap();
|
||||
|
||||
let Context {
|
||||
ref payer,
|
||||
ref client,
|
||||
ref bridge,
|
||||
ref token_bridge,
|
||||
..
|
||||
} = context;
|
||||
let (vaa, body, _) =
|
||||
common::generate_vaa([0u8; 32], 2, message, nonce, rand::thread_rng().gen());
|
||||
let signature_set = common::verify_signatures(client, &bridge, payer, body, guardian_keys, 0)
|
||||
.await
|
||||
.unwrap();
|
||||
common::post_vaa(client, bridge, payer, signature_set, vaa.clone())
|
||||
.await
|
||||
.unwrap();
|
||||
let mut msg_derivation_data = &PostedVAADerivationData {
|
||||
payload_hash: body.to_vec(),
|
||||
};
|
||||
let message_key =
|
||||
PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
|
||||
|
||||
common::initialize(client, token_bridge, payer, &bridge).unwrap();
|
||||
common::complete_wrapped(
|
||||
client,
|
||||
nft_bridge,
|
||||
bridge,
|
||||
message_key,
|
||||
vaa.clone(),
|
||||
payload.clone(),
|
||||
token_authority.pubkey(),
|
||||
payer,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Verify Token Bridge State
|
||||
let config_key = ConfigAccount::<'_, { AccountState::Uninitialized }>::key(None, &token_bridge);
|
||||
let config: Config = common::get_account_data(client, &config_key).unwrap();
|
||||
assert_eq!(config.wormhole_bridge, *bridge);
|
||||
// What this actually does is initialize the spl token metadata so that we can then burn the NFT
|
||||
// in the future but of course, we can't call it something useful like
|
||||
// `initialize_wrapped_nft_metadata`.
|
||||
common::complete_wrapped_meta(client, nft_bridge, bridge, message_key, vaa, payload, payer)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Now transfer the wrapped nft back, which will burn it.
|
||||
let message = &Keypair::new();
|
||||
common::transfer_wrapped(
|
||||
client,
|
||||
nft_bridge,
|
||||
bridge,
|
||||
payer,
|
||||
message,
|
||||
associated_addr,
|
||||
token_authority,
|
||||
token_chain,
|
||||
token_address,
|
||||
token_id,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue