test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {
  2. Connection,
  3. Keypair,
  4. LAMPORTS_PER_SOL,
  5. PublicKey,
  6. sendAndConfirmTransaction,
  7. SystemProgram,
  8. Transaction,
  9. TransactionInstruction,
  10. } from '@solana/web3.js';
  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("Create a system account", async () => {
  17. const connection = new Connection(`http://localhost:8899`, 'confirmed');
  18. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  19. const PROGRAM_ID: PublicKey = new PublicKey(
  20. "Au21huMZuDQrbzu2Ec5ohpW5CKRqhcGV6qLawfydStGs"
  21. );
  22. it("Create the account via a cross program invocation", async () => {
  23. const newKeypair = Keypair.generate();
  24. let ix = new TransactionInstruction({
  25. keys: [
  26. {pubkey: payer.publicKey, isSigner: true, isWritable: true},
  27. {pubkey: newKeypair.publicKey, isSigner: true, isWritable: true},
  28. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  29. ],
  30. programId: PROGRAM_ID,
  31. data: Buffer.alloc(0),
  32. });
  33. await sendAndConfirmTransaction(
  34. connection,
  35. new Transaction().add(ix),
  36. [payer, newKeypair]
  37. );
  38. });
  39. it("Create the account via direct call to system program", async () => {
  40. const newKeypair = Keypair.generate();
  41. const ix = SystemProgram.createAccount({
  42. fromPubkey: payer.publicKey,
  43. newAccountPubkey: newKeypair.publicKey,
  44. lamports: LAMPORTS_PER_SOL,
  45. space: 0,
  46. programId: SystemProgram.programId
  47. })
  48. await sendAndConfirmTransaction(connection,
  49. new Transaction().add(ix),
  50. [payer, newKeypair]);
  51. });
  52. });