test.ts 1.3 KB

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