test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import {
  2. Connection,
  3. Keypair,
  4. LAMPORTS_PER_SOL,
  5. PublicKey,
  6. sendAndConfirmTransaction,
  7. SystemProgram,
  8. Transaction,
  9. } from '@solana/web3.js';
  10. import { createTransferInstruction, InstructionType } from './instruction';
  11. function createKeypairFromFile(path: string): Keypair {
  12. return Keypair.fromSecretKey(
  13. Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
  14. )
  15. };
  16. describe("transfer-sol", () => {
  17. const connection = new Connection(`http://localhost:8899`, 'confirmed');
  18. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  19. const program = createKeypairFromFile('./program/target/so/program-keypair.json');
  20. const transferAmount = 1 * LAMPORTS_PER_SOL;
  21. const test1Recipient = Keypair.generate();
  22. const test2Recipient1 = Keypair.generate();
  23. const test2Recipient2 = Keypair.generate();
  24. it("Transfer between accounts using the system program", async () => {
  25. await getBalances(payer.publicKey, test1Recipient.publicKey, "Beginning");
  26. let ix = createTransferInstruction(
  27. payer.publicKey,
  28. test1Recipient.publicKey,
  29. program.publicKey,
  30. InstructionType.CpiTransfer,
  31. transferAmount
  32. );
  33. await sendAndConfirmTransaction(
  34. connection,
  35. new Transaction().add(ix),
  36. [payer]
  37. );
  38. await getBalances(payer.publicKey, test1Recipient.publicKey, "Resulting");
  39. });
  40. it("Create two accounts for the following test", async () => {
  41. const ix = (pubkey: PublicKey) => {
  42. return SystemProgram.createAccount({
  43. fromPubkey: payer.publicKey,
  44. newAccountPubkey: pubkey,
  45. space: 0,
  46. lamports: 2 * LAMPORTS_PER_SOL,
  47. programId: program.publicKey,
  48. })
  49. };
  50. await sendAndConfirmTransaction(
  51. connection,
  52. new Transaction()
  53. .add(ix(test2Recipient1.publicKey))
  54. .add(ix(test2Recipient2.publicKey))
  55. ,
  56. [payer, test2Recipient1, test2Recipient2]
  57. );
  58. });
  59. it("Transfer between accounts using our program", async () => {
  60. await getBalances(test2Recipient1.publicKey, test2Recipient2.publicKey, "Beginning");
  61. let ix = createTransferInstruction(
  62. test2Recipient1.publicKey,
  63. test2Recipient2.publicKey,
  64. program.publicKey,
  65. InstructionType.ProgramTransfer,
  66. transferAmount
  67. );
  68. await sendAndConfirmTransaction(
  69. connection,
  70. new Transaction().add(ix),
  71. [payer, test2Recipient1]
  72. );
  73. await getBalances(test2Recipient1.publicKey, test2Recipient2.publicKey, "Resulting");
  74. });
  75. async function getBalances(payerPubkey: PublicKey, recipientPubkey: PublicKey, timeframe: string) {
  76. let payerBalance = await connection.getBalance(payerPubkey);
  77. let recipientBalance = await connection.getBalance(recipientPubkey);
  78. console.log(`${timeframe} balances:`);
  79. console.log(` Payer: ${payerBalance}`);
  80. console.log(` Recipient: ${recipientBalance}`);
  81. };
  82. });