test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { TransferSol } from "../target/types/transfer_sol";
  3. describe("transfer-sol", () => {
  4. const provider = anchor.AnchorProvider.env();
  5. anchor.setProvider(provider);
  6. const payer = provider.wallet as anchor.Wallet;
  7. const program = anchor.workspace.TransferSol as anchor.Program<TransferSol>;
  8. const transferAmount = 1 * anchor.web3.LAMPORTS_PER_SOL;
  9. const test1Recipient = anchor.web3.Keypair.generate();
  10. const test2Recipient1 = anchor.web3.Keypair.generate();
  11. const test2Recipient2 = anchor.web3.Keypair.generate();
  12. it("Transfer between accounts using the system program", async () => {
  13. await getBalances(payer.publicKey, test1Recipient.publicKey, "Beginning");
  14. await program.methods
  15. .transferSolWithCpi(new anchor.BN(transferAmount))
  16. .accounts({
  17. payer: payer.publicKey,
  18. recipient: test1Recipient.publicKey,
  19. systemProgram: anchor.web3.SystemProgram.programId,
  20. })
  21. .signers([payer.payer])
  22. .rpc();
  23. await getBalances(payer.publicKey, test1Recipient.publicKey, "Resulting");
  24. });
  25. it("Create two accounts for the following test", async () => {
  26. const ix = (pubkey: anchor.web3.PublicKey) => {
  27. return anchor.web3.SystemProgram.createAccount({
  28. fromPubkey: payer.publicKey,
  29. newAccountPubkey: pubkey,
  30. space: 0,
  31. lamports: 2 * anchor.web3.LAMPORTS_PER_SOL,
  32. programId: program.programId,
  33. });
  34. };
  35. await anchor.web3.sendAndConfirmTransaction(
  36. provider.connection,
  37. new anchor.web3.Transaction()
  38. .add(ix(test2Recipient1.publicKey))
  39. .add(ix(test2Recipient2.publicKey)),
  40. [payer.payer, test2Recipient1, test2Recipient2]
  41. );
  42. });
  43. it("Transfer between accounts using our program", async () => {
  44. await getBalances(
  45. test2Recipient1.publicKey,
  46. test2Recipient2.publicKey,
  47. "Beginning"
  48. );
  49. await program.methods
  50. .transferSolWithProgram(new anchor.BN(transferAmount))
  51. .accounts({
  52. payer: test2Recipient1.publicKey,
  53. recipient: test2Recipient2.publicKey,
  54. systemProgram: anchor.web3.SystemProgram.programId,
  55. })
  56. .rpc();
  57. await getBalances(
  58. test2Recipient1.publicKey,
  59. test2Recipient2.publicKey,
  60. "Resulting"
  61. );
  62. });
  63. async function getBalances(
  64. payerPubkey: anchor.web3.PublicKey,
  65. recipientPubkey: anchor.web3.PublicKey,
  66. timeframe: string
  67. ) {
  68. let payerBalance = await provider.connection.getBalance(payerPubkey);
  69. let recipientBalance = await provider.connection.getBalance(
  70. recipientPubkey
  71. );
  72. console.log(`${timeframe} balances:`);
  73. console.log(` Payer: ${payerBalance}`);
  74. console.log(` Recipient: ${recipientBalance}`);
  75. }
  76. });