call_anchor.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import expect from 'expect';
  2. import { AnchorProvider, Program } from '@coral-xyz/anchor';
  3. import {
  4. PublicKey, AccountMeta,
  5. Keypair, Signer,
  6. Connection,
  7. LAMPORTS_PER_SOL,
  8. BpfLoader, Transaction,
  9. BPF_LOADER_PROGRAM_ID, SystemProgram, sendAndConfirmTransaction,
  10. } from '@solana/web3.js';
  11. import * as fs from "fs";
  12. async function newAccountWithLamports(connection: Connection): Promise<Keypair> {
  13. const account = Keypair.generate();
  14. let signature = await connection.requestAirdrop(account.publicKey, 16 * LAMPORTS_PER_SOL);
  15. await connection.confirmTransaction(signature, 'confirmed');
  16. return account;
  17. }
  18. describe('Call Anchor program from Solidity via IDL', () => {
  19. it('call_anchor', async function () {
  20. // This program instantiates an anchor program, calls various functions on it and checks the return values
  21. const connection = new Connection("http://localhost:8899", {
  22. commitment: "confirmed",
  23. confirmTransactionInitialTimeout: 1e6,
  24. });
  25. const payer = await newAccountWithLamports(connection);
  26. const callAnchorProgramId = Keypair.generate();
  27. await BpfLoader.load(connection, payer, callAnchorProgramId, fs.readFileSync("./tests/call_anchor.so"), BPF_LOADER_PROGRAM_ID);
  28. const file_name = "call_anchor";
  29. const idl = JSON.parse(fs.readFileSync("tests/" + file_name + ".json", 'utf8'));
  30. const storage = Keypair.generate();
  31. const provider = AnchorProvider.env();
  32. const data = Keypair.generate();
  33. await create_account(provider, storage, callAnchorProgramId.publicKey, 8192);
  34. const program = new Program(idl, callAnchorProgramId.publicKey, provider);
  35. // create account
  36. await program.methods.new(data.publicKey)
  37. .accounts({ dataAccount: storage.publicKey })
  38. .rpc();
  39. const ret = await program.methods.data().accounts({ dataAccount: storage.publicKey }).view();
  40. expect(ret).toEqual(data.publicKey);
  41. const anchor_program_id = new PublicKey("z7FbDfQDfucxJz5o8jrGLgvSbdoeSqX5VrxBb5TVjHq");
  42. const remainingAccounts: AccountMeta[] = [{
  43. pubkey: data.publicKey,
  44. isSigner: true,
  45. isWritable: true,
  46. }, {
  47. pubkey: payer.publicKey,
  48. isSigner: true,
  49. isWritable: true,
  50. }];
  51. await program.methods.test(payer.publicKey)
  52. .accounts({
  53. dataAccount: storage.publicKey,
  54. anchor_programId: anchor_program_id,
  55. })
  56. .remainingAccounts(remainingAccounts)
  57. .signers([data, payer])
  58. .rpc();
  59. });
  60. });
  61. async function create_account(provider: AnchorProvider, account: Keypair, programId: PublicKey, space: number) {
  62. const lamports = await provider.connection.getMinimumBalanceForRentExemption(space);
  63. const transaction = new Transaction();
  64. transaction.add(
  65. SystemProgram.createAccount({
  66. fromPubkey: provider.wallet.publicKey,
  67. newAccountPubkey: account.publicKey,
  68. lamports,
  69. space,
  70. programId,
  71. }));
  72. await provider.sendAndConfirm(transaction, [account]);
  73. }