test.ts 2.2 KB

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