test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import {
  2. Connection,
  3. Keypair, LAMPORTS_PER_SOL,
  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 via a cross program invocation", 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. it("Create the account via direct call to system program", async () => {
  36. const newKeypair = Keypair.generate();
  37. const ix = SystemProgram.createAccount({
  38. fromPubkey: payer.publicKey,
  39. newAccountPubkey: newKeypair.publicKey,
  40. lamports: LAMPORTS_PER_SOL,
  41. space: 0,
  42. programId: SystemProgram.programId
  43. })
  44. await sendAndConfirmTransaction(connection,
  45. new Transaction().add(ix),
  46. [payer, newKeypair]);
  47. });
  48. });