integration.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. #![allow(warnings)]
  2. use borsh::BorshSerialize;
  3. use byteorder::{
  4. BigEndian,
  5. WriteBytesExt,
  6. };
  7. use hex_literal::hex;
  8. use rand::Rng;
  9. use secp256k1::{
  10. Message as Secp256k1Message,
  11. PublicKey,
  12. SecretKey,
  13. };
  14. use sha3::Digest;
  15. use solana_client::rpc_client::RpcClient;
  16. use solana_program::{
  17. borsh::try_from_slice_unchecked,
  18. hash,
  19. instruction::{
  20. AccountMeta,
  21. Instruction,
  22. },
  23. program_pack::Pack,
  24. pubkey::Pubkey,
  25. system_instruction::{
  26. self,
  27. create_account,
  28. },
  29. system_program,
  30. sysvar,
  31. };
  32. use solana_sdk::{
  33. commitment_config::CommitmentConfig,
  34. signature::{
  35. read_keypair_file,
  36. Keypair,
  37. Signer,
  38. },
  39. transaction::Transaction,
  40. };
  41. use solitaire::{
  42. processors::seeded::Seeded,
  43. AccountState,
  44. };
  45. use spl_token::state::{Account, Mint};
  46. use std::{
  47. convert::TryInto,
  48. io::{
  49. Cursor,
  50. Write,
  51. },
  52. time::{
  53. Duration,
  54. SystemTime,
  55. },
  56. };
  57. use bridge::{
  58. accounts::{
  59. Bridge,
  60. FeeCollector,
  61. GuardianSet,
  62. GuardianSetDerivationData,
  63. PostedVAA,
  64. PostedVAADerivationData,
  65. SignatureSet,
  66. },
  67. instruction,
  68. types::{
  69. BridgeConfig,
  70. BridgeData,
  71. GovernancePayloadGuardianSetChange,
  72. GovernancePayloadSetMessageFee,
  73. GovernancePayloadTransferFees,
  74. GuardianSetData,
  75. MessageData,
  76. PostedVAAData,
  77. SequenceTracker,
  78. SignatureSet as SignatureSetData,
  79. },
  80. Initialize,
  81. PostVAA,
  82. PostVAAData,
  83. SerializePayload,
  84. DeserializePayload,
  85. Signature,
  86. };
  87. use primitive_types::U256;
  88. use std::{
  89. collections::HashMap,
  90. str::FromStr,
  91. time::UNIX_EPOCH,
  92. };
  93. use token_bridge::{
  94. accounts::{
  95. EmitterAccount,
  96. MintSigner,
  97. WrappedDerivationData,
  98. WrappedMint,
  99. },
  100. api::EXTERNAL_MINTS,
  101. messages::{
  102. PayloadAssetMeta,
  103. PayloadGovernanceRegisterChain,
  104. PayloadTransfer,
  105. },
  106. types::Address,
  107. };
  108. mod common;
  109. const GOVERNANCE_KEY: [u8; 64] = [
  110. 240, 133, 120, 113, 30, 67, 38, 184, 197, 72, 234, 99, 241, 21, 58, 225, 41, 157, 171, 44, 196,
  111. 163, 134, 236, 92, 148, 110, 68, 127, 114, 177, 0, 173, 253, 199, 9, 242, 142, 201, 174, 108,
  112. 197, 18, 102, 115, 0, 31, 205, 127, 188, 191, 56, 171, 228, 20, 247, 149, 170, 141, 231, 147,
  113. 88, 97, 199,
  114. ];
  115. struct Context {
  116. /// Address of the core bridge contract.
  117. bridge: Pubkey,
  118. /// Shared RPC client for tests to make transactions with.
  119. client: RpcClient,
  120. /// Payer key with a ton of lamports to ease testing with.
  121. payer: Keypair,
  122. /// Track nonces throughout the tests.
  123. seq: Sequencer,
  124. /// Address of the token bridge itself that we wish to test.
  125. token_bridge: Pubkey,
  126. /// Keypairs for mint information, required in multiple tests.
  127. mint_authority: Keypair,
  128. mint: Keypair,
  129. mint_meta: Pubkey,
  130. /// Keypairs for test token accounts.
  131. token_authority: Keypair,
  132. token_account: Keypair,
  133. external_token_account: Keypair,
  134. metadata_account: Pubkey,
  135. }
  136. /// Small helper to track and provide sequences during tests. This is in particular needed for
  137. /// guardian operations that require them for derivations.
  138. struct Sequencer {
  139. sequences: HashMap<[u8; 32], u64>,
  140. }
  141. impl Sequencer {
  142. fn next(&mut self, emitter: [u8; 32]) -> u64 {
  143. let entry = self.sequences.entry(emitter).or_insert(0);
  144. *entry += 1;
  145. *entry - 1
  146. }
  147. fn peek(&mut self, emitter: [u8; 32]) -> u64 {
  148. *self.sequences.entry(emitter).or_insert(0)
  149. }
  150. }
  151. #[test]
  152. fn run_integration_tests() {
  153. let (payer, client, bridge, token_bridge) = common::setup();
  154. // Setup a Bridge to test against.
  155. println!("Bridge: {}", bridge);
  156. common::initialize_bridge(&client, &bridge, &payer);
  157. // Context for test environment.
  158. let mint = Keypair::new();
  159. let mint_pubkey = mint.pubkey();
  160. let metadata_pubkey = Pubkey::from_str("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s").unwrap();
  161. // SPL Token Meta
  162. let metadata_seeds = &[
  163. "metadata".as_bytes(),
  164. metadata_pubkey.as_ref(),
  165. mint_pubkey.as_ref(),
  166. ];
  167. let (metadata_key, metadata_bump_seed) = Pubkey::find_program_address(
  168. metadata_seeds,
  169. &Pubkey::from_str("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s").unwrap(),
  170. );
  171. // Token Bridge Meta
  172. use token_bridge::accounts::WrappedTokenMeta;
  173. let metadata_account = WrappedTokenMeta::<'_, { AccountState::Uninitialized }>::key(
  174. &token_bridge::accounts::WrappedMetaDerivationData {
  175. mint_key: mint_pubkey.clone(),
  176. },
  177. &token_bridge,
  178. );
  179. let mut context = Context {
  180. seq: Sequencer {
  181. sequences: HashMap::new(),
  182. },
  183. bridge,
  184. client,
  185. payer,
  186. token_bridge,
  187. mint_authority: Keypair::new(),
  188. mint,
  189. mint_meta: metadata_account,
  190. token_account: Keypair::new(),
  191. external_token_account: Keypair::new(),
  192. token_authority: Keypair::new(),
  193. metadata_account: metadata_key,
  194. };
  195. // Create a mint for use within tests.
  196. common::create_mint(
  197. &context.client,
  198. &context.payer,
  199. &context.mint_authority.pubkey(),
  200. &context.mint,
  201. )
  202. .unwrap();
  203. // Create Token accounts for use within tests.
  204. common::create_token_account(
  205. &context.client,
  206. &context.payer,
  207. &context.token_account,
  208. context.token_authority.pubkey(),
  209. context.mint.pubkey(),
  210. )
  211. .unwrap();
  212. // Mint tokens
  213. common::mint_tokens(
  214. &context.client,
  215. &context.payer,
  216. &context.mint_authority,
  217. &context.mint,
  218. &context.token_account.pubkey(),
  219. 1000,
  220. )
  221. .unwrap();
  222. // Initialize the bridge and verify the bridges state.
  223. test_initialize(&mut context);
  224. test_transfer_native(&mut context);
  225. test_attest(&mut context);
  226. test_register_chain(&mut context);
  227. test_transfer_native_in(&mut context);
  228. // Create an SPL Metadata account to test attestations for wrapped tokens.
  229. common::create_spl_metadata(
  230. &context.client,
  231. &context.payer,
  232. &context.metadata_account,
  233. &context.mint_authority,
  234. &context.mint,
  235. &context.payer.pubkey(),
  236. "BTC".to_string(),
  237. "Bitcoin".to_string(),
  238. )
  239. .unwrap();
  240. let wrapped = test_create_wrapped(&mut context);
  241. let wrapped_acc = Keypair::new();
  242. common::create_token_account(
  243. &context.client,
  244. &context.payer,
  245. &wrapped_acc,
  246. context.token_authority.pubkey(),
  247. wrapped,
  248. )
  249. .unwrap();
  250. test_transfer_wrapped_in(&mut context, wrapped_acc.pubkey());
  251. test_transfer_wrapped(&mut context, wrapped_acc.pubkey());
  252. // Sollet specific tests
  253. test_create_wrapped_preexisting(&mut context);
  254. test_transfer_in_wrapped_preexisting(&mut context);
  255. test_transfer_in_wrapped_preexisting_invalid(&mut context);
  256. test_transfer_wrapped_preexisting(&mut context);
  257. }
  258. fn test_attest(context: &mut Context) -> () {
  259. println!("Attest");
  260. use token_bridge::{
  261. accounts::ConfigAccount,
  262. types::Config,
  263. };
  264. let Context {
  265. ref payer,
  266. ref client,
  267. ref bridge,
  268. ref token_bridge,
  269. ref mint_authority,
  270. ref mint,
  271. ref mint_meta,
  272. ref metadata_account,
  273. ..
  274. } = context;
  275. let message = &Keypair::new();
  276. common::attest(
  277. client,
  278. token_bridge,
  279. bridge,
  280. payer,
  281. message,
  282. mint.pubkey(),
  283. 0,
  284. )
  285. .unwrap();
  286. let emitter_key = EmitterAccount::key(None, &token_bridge);
  287. let mint_data = Mint::unpack(
  288. &client
  289. .get_account_with_commitment(&mint.pubkey(), CommitmentConfig::processed())
  290. .unwrap()
  291. .value
  292. .unwrap()
  293. .data,
  294. )
  295. .unwrap();
  296. let payload = PayloadAssetMeta {
  297. token_address: mint.pubkey().to_bytes(),
  298. token_chain: 1,
  299. decimals: mint_data.decimals,
  300. symbol: "USD".to_string(),
  301. name: "Bitcoin".to_string(),
  302. };
  303. let payload = payload.try_to_vec().unwrap();
  304. }
  305. fn test_transfer_native(context: &mut Context) -> () {
  306. println!("Transfer Native");
  307. use token_bridge::{
  308. accounts::ConfigAccount,
  309. types::Config,
  310. };
  311. let Context {
  312. ref payer,
  313. ref client,
  314. ref bridge,
  315. ref token_bridge,
  316. ref mint_authority,
  317. ref mint,
  318. ref mint_meta,
  319. ref token_account,
  320. ref token_authority,
  321. ..
  322. } = context;
  323. let message = &Keypair::new();
  324. common::transfer_native(
  325. client,
  326. token_bridge,
  327. bridge,
  328. payer,
  329. message,
  330. token_account,
  331. token_authority,
  332. mint.pubkey(),
  333. 100,
  334. )
  335. .unwrap();
  336. }
  337. fn test_transfer_wrapped(context: &mut Context, token_account: Pubkey) -> () {
  338. println!("TransferWrapped");
  339. use token_bridge::{
  340. accounts::ConfigAccount,
  341. types::Config,
  342. };
  343. let Context {
  344. ref payer,
  345. ref client,
  346. ref bridge,
  347. ref token_bridge,
  348. ref mint_authority,
  349. ref token_authority,
  350. ..
  351. } = context;
  352. let message = &Keypair::new();
  353. common::transfer_wrapped(
  354. client,
  355. token_bridge,
  356. bridge,
  357. payer,
  358. message,
  359. token_account,
  360. token_authority,
  361. 2,
  362. [1u8; 32],
  363. 10000000,
  364. )
  365. .unwrap();
  366. }
  367. fn test_register_chain(context: &mut Context) -> () {
  368. println!("Register Chain");
  369. use token_bridge::{
  370. accounts::ConfigAccount,
  371. types::Config,
  372. };
  373. let Context {
  374. ref payer,
  375. ref client,
  376. ref bridge,
  377. ref token_bridge,
  378. ref mint_authority,
  379. ref mint,
  380. ref mint_meta,
  381. ref token_account,
  382. ref token_authority,
  383. ..
  384. } = context;
  385. let nonce = rand::thread_rng().gen();
  386. let emitter = Keypair::from_bytes(&GOVERNANCE_KEY).unwrap();
  387. let payload = PayloadGovernanceRegisterChain {
  388. chain: 2,
  389. endpoint_address: [0u8; 32],
  390. };
  391. let message = payload.try_to_vec().unwrap();
  392. let (vaa, _, _) = common::generate_vaa(emitter.pubkey().to_bytes(), 1, message, nonce, 0);
  393. common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
  394. let mut msg_derivation_data = &PostedVAADerivationData {
  395. payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
  396. };
  397. let message_key =
  398. PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
  399. common::register_chain(
  400. client,
  401. token_bridge,
  402. bridge,
  403. &message_key,
  404. vaa,
  405. payload,
  406. payer,
  407. )
  408. .unwrap();
  409. }
  410. fn test_transfer_native_in(context: &mut Context) -> () {
  411. println!("TransferNativeIn");
  412. use token_bridge::{
  413. accounts::ConfigAccount,
  414. types::Config,
  415. };
  416. let Context {
  417. ref payer,
  418. ref client,
  419. ref bridge,
  420. ref token_bridge,
  421. ref mint_authority,
  422. ref mint,
  423. ref mint_meta,
  424. ref token_account,
  425. ref token_authority,
  426. ..
  427. } = context;
  428. let nonce = rand::thread_rng().gen();
  429. let payload = PayloadTransfer {
  430. amount: U256::from(100),
  431. token_address: mint.pubkey().to_bytes(),
  432. token_chain: 1,
  433. to: token_account.pubkey().to_bytes(),
  434. to_chain: 1,
  435. fee: U256::from(0),
  436. };
  437. let message = payload.try_to_vec().unwrap();
  438. let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 1);
  439. common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
  440. let mut msg_derivation_data = &PostedVAADerivationData {
  441. payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
  442. };
  443. let message_key =
  444. PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
  445. common::complete_native(
  446. client,
  447. token_bridge,
  448. bridge,
  449. &message_key,
  450. vaa,
  451. payload,
  452. payer,
  453. )
  454. .unwrap();
  455. }
  456. fn test_transfer_wrapped_in(context: &mut Context, to: Pubkey) -> () {
  457. println!("TransferWrappedIn");
  458. use token_bridge::{
  459. accounts::ConfigAccount,
  460. types::Config,
  461. };
  462. let Context {
  463. ref payer,
  464. ref client,
  465. ref bridge,
  466. ref token_bridge,
  467. ref mint_authority,
  468. ref mint,
  469. ref mint_meta,
  470. ref token_account,
  471. ref token_authority,
  472. ..
  473. } = context;
  474. let nonce = rand::thread_rng().gen();
  475. let payload = PayloadTransfer {
  476. amount: U256::from(100000000),
  477. token_address: [1u8; 32],
  478. token_chain: 2,
  479. to: to.to_bytes(),
  480. to_chain: 1,
  481. fee: U256::from(0),
  482. };
  483. let message = payload.try_to_vec().unwrap();
  484. let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, rand::thread_rng().gen());
  485. common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
  486. let mut msg_derivation_data = &PostedVAADerivationData {
  487. payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
  488. };
  489. let message_key =
  490. PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
  491. common::complete_transfer_wrapped(
  492. client,
  493. token_bridge,
  494. bridge,
  495. &message_key,
  496. vaa,
  497. payload,
  498. payer,
  499. )
  500. .unwrap();
  501. }
  502. fn test_create_wrapped(context: &mut Context) -> (Pubkey) {
  503. println!("CreateWrapped");
  504. use token_bridge::{
  505. accounts::ConfigAccount,
  506. types::Config,
  507. };
  508. let Context {
  509. ref payer,
  510. ref client,
  511. ref bridge,
  512. ref token_bridge,
  513. ref mint_authority,
  514. ref mint,
  515. ref mint_meta,
  516. ref token_account,
  517. ref token_authority,
  518. ..
  519. } = context;
  520. let nonce = rand::thread_rng().gen();
  521. let payload = PayloadAssetMeta {
  522. token_address: [1u8; 32],
  523. token_chain: 2,
  524. decimals: 7,
  525. symbol: "".to_string(),
  526. name: "".to_string(),
  527. };
  528. let message = payload.try_to_vec().unwrap();
  529. let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 2);
  530. common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
  531. let mut msg_derivation_data = &PostedVAADerivationData {
  532. payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
  533. };
  534. let message_key =
  535. PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
  536. common::create_wrapped(
  537. client,
  538. token_bridge,
  539. bridge,
  540. &message_key,
  541. vaa,
  542. payload,
  543. payer,
  544. )
  545. .unwrap();
  546. return WrappedMint::<'_, { AccountState::Initialized }>::key(
  547. &WrappedDerivationData {
  548. token_chain: 2,
  549. token_address: [1u8; 32],
  550. },
  551. token_bridge,
  552. );
  553. }
  554. fn test_create_wrapped_preexisting(context: &mut Context) -> (Pubkey) {
  555. println!("CreateWrappedPreexisting");
  556. use token_bridge::{
  557. accounts::ConfigAccount,
  558. types::Config,
  559. };
  560. let Context {
  561. ref payer,
  562. ref client,
  563. ref bridge,
  564. ref token_bridge,
  565. ref mint_authority,
  566. ref mint,
  567. ref mint_meta,
  568. ref token_account,
  569. ref token_authority,
  570. ..
  571. } = context;
  572. // FDhdMYh3KsF64Jxzh8tnx9rJXQTcN461rguUK9z9zm64
  573. let mint_keypair = Keypair::from_bytes(&[
  574. 78, 244, 23, 240, 92, 61, 31, 184, 188, 176, 28, 188, 143, 230, 185, 139, 23, 32, 60, 221,
  575. 166, 209, 15, 175, 243, 160, 174, 226, 190, 8, 124, 115, 211, 68, 134, 6, 252, 30, 9, 108,
  576. 54, 236, 74, 254, 5, 8, 178, 146, 14, 182, 243, 214, 1, 108, 184, 93, 66, 224, 100, 135,
  577. 16, 120, 69, 93,
  578. ])
  579. .unwrap();
  580. // Create a wrapped mint
  581. common::create_mint(
  582. client,
  583. payer,
  584. &MintSigner::key(None, token_bridge),
  585. &mint_keypair,
  586. );
  587. let nonce = rand::thread_rng().gen();
  588. println!("{}", hex::encode([0xaau8; 32]));
  589. println!("{:?}", EXTERNAL_MINTS);
  590. println!("{:?}", EXTERNAL_MINTS.get(hex::encode([0xaau8; 32]).as_str()));
  591. let payload = PayloadAssetMeta {
  592. token_address: [0xaau8; 32],
  593. token_chain: 2,
  594. decimals: 7,
  595. symbol: "".to_string(),
  596. name: "".to_string(),
  597. };
  598. let message = payload.try_to_vec().unwrap();
  599. let (vaa, _, _) = common::generate_vaa([0u8; 32], 2, message, nonce, 120);
  600. common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
  601. let mut msg_derivation_data = &PostedVAADerivationData {
  602. payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
  603. };
  604. let message_key =
  605. PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
  606. common::create_wrapped(
  607. client,
  608. token_bridge,
  609. bridge,
  610. &message_key,
  611. vaa,
  612. payload,
  613. payer,
  614. )
  615. .unwrap();
  616. return WrappedMint::<'_, { AccountState::Initialized }>::key(
  617. &WrappedDerivationData {
  618. token_chain: 2,
  619. token_address: [0xaau8; 32],
  620. },
  621. token_bridge,
  622. );
  623. }
  624. fn test_initialize(context: &mut Context) {
  625. println!("Initialize");
  626. use token_bridge::{
  627. accounts::ConfigAccount,
  628. types::Config,
  629. };
  630. let Context {
  631. ref payer,
  632. ref client,
  633. ref bridge,
  634. ref token_bridge,
  635. ..
  636. } = context;
  637. common::initialize(client, token_bridge, payer, &bridge).unwrap();
  638. // Verify Token Bridge State
  639. let config_key = ConfigAccount::<'_, { AccountState::Uninitialized }>::key(None, &token_bridge);
  640. let config: Config = common::get_account_data(client, &config_key).unwrap();
  641. assert_eq!(config.wormhole_bridge, *bridge);
  642. }
  643. fn test_transfer_in_wrapped_preexisting(context: &mut Context) {
  644. println!("TransferInPreexisting");
  645. use token_bridge::{
  646. accounts::ConfigAccount,
  647. types::Config,
  648. };
  649. let Context {
  650. ref payer,
  651. ref client,
  652. ref bridge,
  653. ref token_bridge,
  654. ref mint_authority,
  655. ref mint,
  656. ref mint_meta,
  657. ref external_token_account,
  658. ref token_authority,
  659. ..
  660. } = context;
  661. // FDhdMYh3KsF64Jxzh8tnx9rJXQTcN461rguUK9z9zm64
  662. let mint_keypair = Keypair::from_bytes(&[
  663. 78, 244, 23, 240, 92, 61, 31, 184, 188, 176, 28, 188, 143, 230, 185, 139, 23, 32, 60, 221,
  664. 166, 209, 15, 175, 243, 160, 174, 226, 190, 8, 124, 115, 211, 68, 134, 6, 252, 30, 9, 108,
  665. 54, 236, 74, 254, 5, 8, 178, 146, 14, 182, 243, 214, 1, 108, 184, 93, 66, 224, 100, 135,
  666. 16, 120, 69, 93,
  667. ])
  668. .unwrap();
  669. // Create Token accounts for use within tests.
  670. common::create_token_account(
  671. &context.client,
  672. &context.payer,
  673. &external_token_account,
  674. context.token_authority.pubkey(),
  675. mint_keypair.pubkey(),
  676. )
  677. .unwrap();
  678. let nonce = rand::thread_rng().gen();
  679. let payload = PayloadTransfer {
  680. amount: U256::from(10000),
  681. token_address: [0xaau8; 32],
  682. token_chain: 2,
  683. to: external_token_account.pubkey().to_bytes(),
  684. to_chain: 1,
  685. fee: U256::from(0),
  686. };
  687. let message = payload.try_to_vec().unwrap();
  688. let (vaa, _, _) = common::generate_vaa(
  689. [0u8; 32],
  690. 2,
  691. message,
  692. nonce,
  693. 0
  694. );
  695. common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
  696. let mut msg_derivation_data = &PostedVAADerivationData {
  697. payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
  698. };
  699. let message_key =
  700. PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
  701. common::complete_transfer_wrapped(
  702. client,
  703. token_bridge,
  704. bridge,
  705. &message_key,
  706. vaa,
  707. payload,
  708. payer,
  709. )
  710. .unwrap();
  711. let account_data = Account::unpack(
  712. &client
  713. .get_account_with_commitment(&external_token_account.pubkey(), CommitmentConfig::processed())
  714. .unwrap()
  715. .value
  716. .unwrap()
  717. .data,
  718. )
  719. .unwrap();
  720. // Check truncation for external asset shrinks by the correct amount.
  721. assert_eq!(1000, account_data.amount);
  722. }
  723. fn test_transfer_wrapped_preexisting(context: &mut Context) {
  724. println!("TransferOutPreexisting");
  725. use token_bridge::{
  726. accounts::ConfigAccount,
  727. types::Config,
  728. };
  729. let Context {
  730. ref payer,
  731. ref client,
  732. ref bridge,
  733. ref token_bridge,
  734. ref mint_authority,
  735. ref mint,
  736. ref mint_meta,
  737. ref external_token_account,
  738. ref token_authority,
  739. ..
  740. } = context;
  741. // FDhdMYh3KsF64Jxzh8tnx9rJXQTcN461rguUK9z9zm64
  742. let mint_keypair = Keypair::from_bytes(&[
  743. 78, 244, 23, 240, 92, 61, 31, 184, 188, 176, 28, 188, 143, 230, 185, 139, 23, 32, 60, 221,
  744. 166, 209, 15, 175, 243, 160, 174, 226, 190, 8, 124, 115, 211, 68, 134, 6, 252, 30, 9, 108,
  745. 54, 236, 74, 254, 5, 8, 178, 146, 14, 182, 243, 214, 1, 108, 184, 93, 66, 224, 100, 135,
  746. 16, 120, 69, 93,
  747. ])
  748. .unwrap();
  749. let message = &Keypair::new();
  750. common::transfer_wrapped(
  751. client,
  752. token_bridge,
  753. bridge,
  754. payer,
  755. message,
  756. external_token_account.pubkey(),
  757. token_authority,
  758. 2,
  759. [0xaau8; 32],
  760. 1000,
  761. );
  762. let account_data = Account::unpack(
  763. &client
  764. .get_account_with_commitment(&external_token_account.pubkey(), CommitmentConfig::processed())
  765. .unwrap()
  766. .value
  767. .unwrap()
  768. .data,
  769. )
  770. .unwrap();
  771. // Check truncation for external asset shrinks by the correct amount.
  772. assert_eq!(0, account_data.amount);
  773. let posted_message: PostedVAAData = common::get_account_data(client, &message.pubkey()).unwrap();
  774. let mut transfer = posted_message.0.payload.clone();
  775. let transfer = &mut &transfer[0..];
  776. let transfer: PayloadTransfer = PayloadTransfer::deserialize(transfer).unwrap();
  777. assert_eq!(transfer.amount, U256::from(10000));
  778. }
  779. fn test_transfer_in_wrapped_preexisting_invalid(context: &mut Context) {
  780. println!("TransferInPreexisting");
  781. use token_bridge::{
  782. accounts::ConfigAccount,
  783. types::Config,
  784. };
  785. let Context {
  786. ref payer,
  787. ref client,
  788. ref bridge,
  789. ref token_bridge,
  790. ref mint_authority,
  791. ref mint,
  792. ref mint_meta,
  793. ref external_token_account,
  794. ref token_authority,
  795. ..
  796. } = context;
  797. // FDhdMYh3KsF64Jxzh8tnx9rJXQTcN461rguUK9z9zm64
  798. let mint_keypair = Keypair::from_bytes(&[
  799. 78, 244, 23, 240, 92, 61, 31, 184, 188, 176, 28, 188, 143, 230, 185, 139, 23, 32, 60, 221,
  800. 166, 209, 15, 175, 243, 160, 174, 226, 190, 8, 124, 115, 211, 68, 134, 6, 252, 30, 9, 108,
  801. 54, 236, 74, 254, 5, 8, 178, 146, 14, 182, 243, 214, 1, 108, 184, 93, 66, 224, 100, 135,
  802. 16, 120, 69, 93,
  803. ])
  804. .unwrap();
  805. let nonce = rand::thread_rng().gen();
  806. let payload = PayloadTransfer {
  807. amount: U256::from(10000),
  808. token_address: [0xabu8; 32],
  809. token_chain: 2,
  810. to: external_token_account.pubkey().to_bytes(),
  811. to_chain: 1,
  812. fee: U256::from(0),
  813. };
  814. let message = payload.try_to_vec().unwrap();
  815. let (vaa, _, _) = common::generate_vaa(
  816. [0u8; 32],
  817. 2,
  818. message,
  819. nonce,
  820. 0
  821. );
  822. common::post_vaa(client, bridge, payer, vaa.clone()).unwrap();
  823. let mut msg_derivation_data = &PostedVAADerivationData {
  824. payload_hash: bridge::instructions::hash_vaa(&vaa).to_vec(),
  825. };
  826. let message_key =
  827. PostedVAA::<'_, { AccountState::MaybeInitialized }>::key(&msg_derivation_data, &bridge);
  828. assert!(common::complete_transfer_wrapped(
  829. client,
  830. token_bridge,
  831. bridge,
  832. &message_key,
  833. vaa,
  834. payload,
  835. payer,
  836. ).is_err());
  837. }