test.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {
  2. Connection,
  3. Keypair,
  4. sendAndConfirmTransaction,
  5. SystemProgram,
  6. Transaction,
  7. TransactionInstruction,
  8. } from '@solana/web3.js';
  9. function createKeypairFromFile(path: string): Keypair {
  10. return Keypair.fromSecretKey(
  11. Buffer.from(JSON.parse(require('fs').readFileSync(path, "utf-8")))
  12. )
  13. };
  14. describe("Create a system account", async () => {
  15. const connection = new Connection(`http://localhost:8899`, 'confirmed');
  16. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  17. const program = createKeypairFromFile('./program/target/so/program-keypair.json');
  18. it("Create the account", async () => {
  19. const newKeypair = Keypair.generate();
  20. let ix = new TransactionInstruction({
  21. keys: [
  22. {pubkey: payer.publicKey, isSigner: true, isWritable: true},
  23. {pubkey: newKeypair.publicKey, isSigner: true, isWritable: true},
  24. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  25. ],
  26. programId: program.publicKey,
  27. data: Buffer.alloc(0),
  28. });
  29. await sendAndConfirmTransaction(
  30. connection,
  31. new Transaction().add(ix),
  32. [payer, newKeypair]
  33. );
  34. });
  35. });