transfer-sol.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. new anchor.BN(transferAmount) // amount in lamports
  33. )
  34. .accounts({
  35. sender: wallet.publicKey,
  36. recipient: recipient.publicKey
  37. })
  38. .rpc();
  39. await getBalances(wallet.publicKey, recipient.publicKey, "Resulting");
  40. console.log("Your transaction signature", tx);
  41. });
  42. it("Transfer SOL to program owned account", async () => {
  43. await getBalances(wallet.publicKey, dataAccount.publicKey, "Beginning");
  44. const tx = await program.methods
  45. .transferSolWithCpi(
  46. new anchor.BN(transferAmount) // amount in lamports
  47. )
  48. .accounts({
  49. sender: wallet.publicKey,
  50. recipient: dataAccount.publicKey,
  51. })
  52. .rpc();
  53. await getBalances(wallet.publicKey, dataAccount.publicKey, "Resulting");
  54. console.log("Your transaction signature", tx);
  55. });
  56. it("Transfer SOL from program owned account", async () => {
  57. await getBalances(dataAccount.publicKey, wallet.publicKey, "Beginning");
  58. const tx = await program.methods
  59. .transferSolWithProgram(
  60. new anchor.BN(transferAmount) // amount in lamports
  61. )
  62. .accounts({
  63. sender: dataAccount.publicKey,
  64. recipient: wallet.publicKey,
  65. })
  66. .rpc();
  67. await getBalances(dataAccount.publicKey, wallet.publicKey, "Resulting");
  68. console.log("Your transaction signature", tx);
  69. });
  70. // Helper function to get balances and log them to the console
  71. async function getBalances(
  72. payerPubkey: PublicKey,
  73. recipientPubkey: PublicKey,
  74. timeframe: string
  75. ) {
  76. let payerBalance = await connection.getBalance(payerPubkey);
  77. let recipientBalance = await connection.getBalance(recipientPubkey);
  78. console.log(`${timeframe} balances:`);
  79. console.log(` Payer: ${payerBalance / anchor.web3.LAMPORTS_PER_SOL}`); // convert lamports to SOL
  80. console.log(
  81. ` Recipient: ${recipientBalance / anchor.web3.LAMPORTS_PER_SOL}` // convert lamports to SOL
  82. );
  83. }
  84. });