transfer-sol.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { TransferSol } from "../target/types/transfer_sol";
  4. import { PublicKey } from "@solana/web3.js";
  5. describe("transfer-sol", () => {
  6. // Configure the client to use the local cluster.
  7. const provider = anchor.AnchorProvider.env();
  8. anchor.setProvider(provider);
  9. // Generate new keypair to use as data account
  10. const dataAccount = anchor.web3.Keypair.generate();
  11. const wallet = provider.wallet;
  12. const connection = provider.connection;
  13. const program = anchor.workspace.TransferSol as Program<TransferSol>;
  14. // Amount to transfer in lamports
  15. const transferAmount = 1 * anchor.web3.LAMPORTS_PER_SOL; // 1 SOL
  16. it("Is initialized!", async () => {
  17. // Create the new data account
  18. // The dataAccount is required by Solang even though it is not use in the transfer instruction
  19. const tx = await program.methods
  20. .new()
  21. .accounts({ dataAccount: dataAccount.publicKey })
  22. .signers([dataAccount])
  23. .rpc();
  24. console.log("Your transaction signature", tx);
  25. });
  26. it("Transfer SOL using CPI to the system program", async () => {
  27. // Generate new keypair to use as recipient for the transfer
  28. const recipient = anchor.web3.Keypair.generate(); // test1 recipient
  29. await getBalances(wallet.publicKey, recipient.publicKey, "Beginning");
  30. const tx = await program.methods
  31. .transferSolWithCpi(
  32. wallet.publicKey, // sender
  33. recipient.publicKey, // recipient
  34. new anchor.BN(transferAmount) // amount in lamports
  35. )
  36. .accounts({ dataAccount: dataAccount.publicKey })
  37. .remainingAccounts([
  38. {
  39. pubkey: wallet.publicKey, // sender
  40. isWritable: true,
  41. isSigner: true,
  42. },
  43. {
  44. pubkey: recipient.publicKey, // recipient
  45. isWritable: true,
  46. isSigner: false,
  47. },
  48. ])
  49. .rpc();
  50. await getBalances(wallet.publicKey, recipient.publicKey, "Resulting");
  51. console.log("Your transaction signature", tx);
  52. });
  53. it("Transfer SOL to program owned account", async () => {
  54. await getBalances(wallet.publicKey, dataAccount.publicKey, "Beginning");
  55. const tx = await program.methods
  56. .transferSolWithCpi(
  57. wallet.publicKey, // sender
  58. dataAccount.publicKey, // recipient
  59. new anchor.BN(transferAmount) // amount in lamports
  60. )
  61. .accounts({ dataAccount: dataAccount.publicKey })
  62. .remainingAccounts([
  63. {
  64. pubkey: wallet.publicKey, // sender
  65. isWritable: true,
  66. isSigner: true,
  67. },
  68. {
  69. pubkey: dataAccount.publicKey, // recipient
  70. isWritable: true,
  71. isSigner: false,
  72. },
  73. ])
  74. .rpc();
  75. await getBalances(wallet.publicKey, dataAccount.publicKey, "Resulting");
  76. console.log("Your transaction signature", tx);
  77. });
  78. it("Transfer SOL from program owned account", async () => {
  79. await getBalances(dataAccount.publicKey, wallet.publicKey, "Beginning");
  80. const tx = await program.methods
  81. .transferSolWithProgram(
  82. new anchor.BN(transferAmount) // amount in lamports
  83. )
  84. .accounts({ dataAccount: dataAccount.publicKey })
  85. .remainingAccounts([
  86. {
  87. pubkey: wallet.publicKey, // recipient
  88. isWritable: true,
  89. isSigner: true,
  90. },
  91. ])
  92. .rpc();
  93. await getBalances(dataAccount.publicKey, wallet.publicKey, "Resulting");
  94. console.log("Your transaction signature", tx);
  95. });
  96. // Helper function to get balances and log them to the console
  97. async function getBalances(
  98. payerPubkey: PublicKey,
  99. recipientPubkey: PublicKey,
  100. timeframe: string
  101. ) {
  102. let payerBalance = await connection.getBalance(payerPubkey);
  103. let recipientBalance = await connection.getBalance(recipientPubkey);
  104. console.log(`${timeframe} balances:`);
  105. console.log(` Payer: ${payerBalance / anchor.web3.LAMPORTS_PER_SOL}`); // convert lamports to SOL
  106. console.log(
  107. ` Recipient: ${recipientBalance / anchor.web3.LAMPORTS_PER_SOL}` // convert lamports to SOL
  108. );
  109. }
  110. });