test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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("Checking accounts", 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. "AE653DEBtNWr2VcU3FhVtFPc7rUf4z2Km8s5TnSwiiaW"
  20. );
  21. // We'll create this ahead of time.
  22. // Our program will try to modify it.
  23. const accountToChange = Keypair.generate();
  24. // Our program will create this.
  25. const accountToCreate = Keypair.generate();
  26. it("Create an account owned by our program", async () => {
  27. let ix = SystemProgram.createAccount({
  28. fromPubkey: payer.publicKey,
  29. newAccountPubkey: accountToChange.publicKey,
  30. lamports: await connection.getMinimumBalanceForRentExemption(0),
  31. space: 0,
  32. programId: PROGRAM_ID, // Our program
  33. });
  34. await sendAndConfirmTransaction(
  35. connection,
  36. new Transaction().add(ix),
  37. [payer, accountToChange]
  38. );
  39. });
  40. it("Check accounts", async () => {
  41. let ix = new TransactionInstruction({
  42. keys: [
  43. {pubkey: payer.publicKey, isSigner: true, isWritable: true},
  44. {pubkey: accountToCreate.publicKey, isSigner: true, isWritable: true},
  45. {pubkey: accountToChange.publicKey, isSigner: true, isWritable: true},
  46. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  47. ],
  48. programId: PROGRAM_ID,
  49. data: Buffer.alloc(0),
  50. });
  51. await sendAndConfirmTransaction(
  52. connection,
  53. new Transaction().add(ix),
  54. [payer, accountToCreate, accountToChange]
  55. );
  56. });
  57. });