test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {
  2. Connection,
  3. Keypair,
  4. PublicKey,
  5. sendAndConfirmTransaction,
  6. SystemProgram,
  7. Transaction,
  8. TransactionInstruction,
  9. } from '@solana/web3.js';
  10. import * as borsh from "borsh";
  11. import { Buffer } from "buffer";
  12. function createKeypairFromFile(path: string): Keypair {
  13. return Keypair.fromSecretKey(
  14. Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
  15. )
  16. };
  17. describe("PDA Rent-Payer", () => {
  18. const connection = new Connection(`http://localhost:8899`, 'confirmed');
  19. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  20. const PROGRAM_ID: PublicKey = createKeypairFromFile('./program/target/deploy/program-keypair.json').publicKey;
  21. class Assignable {
  22. constructor(properties) {
  23. Object.keys(properties).map((key) => {
  24. return (this[key] = properties[key]);
  25. });
  26. };
  27. };
  28. enum MyInstruction {
  29. InitRentVault,
  30. CreateNewAccount,
  31. }
  32. class InitRentVault extends Assignable {
  33. toBuffer() { return Buffer.from(borsh.serialize(InitRentVaultSchema, this)) }
  34. };
  35. const InitRentVaultSchema = new Map([
  36. [ InitRentVault, {
  37. kind: 'struct',
  38. fields: [ ['instruction', 'u8'], ['fund_lamports', 'u64'] ],
  39. }]
  40. ]);
  41. class CreateNewAccount extends Assignable {
  42. toBuffer() { return Buffer.from(borsh.serialize(CreateNewAccountSchema, this)) }
  43. };
  44. const CreateNewAccountSchema = new Map([
  45. [ CreateNewAccount, {
  46. kind: 'struct',
  47. fields: [ ['instruction', 'u8'] ],
  48. }]
  49. ]);
  50. function deriveRentVaultPda() {
  51. const pda = PublicKey.findProgramAddressSync(
  52. [Buffer.from("rent_vault")],
  53. PROGRAM_ID,
  54. )
  55. console.log(`PDA: ${pda[0].toBase58()}`)
  56. return pda
  57. }
  58. it("Initialize the Rent Vault", async () => {
  59. const [rentVaultPda, _] = deriveRentVaultPda();
  60. let ix = new TransactionInstruction({
  61. keys: [
  62. {pubkey: rentVaultPda, isSigner: false, isWritable: true},
  63. {pubkey: payer.publicKey, isSigner: true, isWritable: true},
  64. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  65. ],
  66. programId: PROGRAM_ID,
  67. data: (new InitRentVault({ instruction: MyInstruction.InitRentVault, fund_lamports: 1000000000 })).toBuffer(),
  68. });
  69. await sendAndConfirmTransaction(
  70. connection,
  71. new Transaction().add(ix),
  72. [payer]
  73. );
  74. });
  75. it("Create a new account using the Rent Vault", async () => {
  76. const newAccount = Keypair.generate();
  77. const [rentVaultPda, _] = deriveRentVaultPda();
  78. let ix = new TransactionInstruction({
  79. keys: [
  80. {pubkey: newAccount.publicKey, isSigner: true, isWritable: true},
  81. {pubkey: rentVaultPda, isSigner: false, isWritable: true},
  82. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  83. ],
  84. programId: PROGRAM_ID,
  85. data: new CreateNewAccount({ instruction: MyInstruction.CreateNewAccount }).toBuffer(),
  86. });
  87. await sendAndConfirmTransaction(
  88. connection,
  89. new Transaction().add(ix),
  90. [payer, newAccount]
  91. );
  92. });
  93. });