test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Buffer } from 'node:buffer';
  2. import { Connection, Keypair, SystemProgram, Transaction, TransactionInstruction, sendAndConfirmTransaction } from '@solana/web3.js';
  3. import * as borsh from 'borsh';
  4. function createKeypairFromFile(path: string): Keypair {
  5. return Keypair.fromSecretKey(Buffer.from(JSON.parse(require('node:fs').readFileSync(path, 'utf-8'))));
  6. }
  7. describe('CPI Example', () => {
  8. const connection = new Connection('http://localhost:8899', 'confirmed');
  9. const payer = createKeypairFromFile(`${require('node:os').homedir()}/.config/solana/id.json`);
  10. const hand = createKeypairFromFile('./target/so/hand-keypair.json');
  11. const lever = createKeypairFromFile('./target/so/lever-keypair.json');
  12. class Assignable {
  13. constructor(properties) {
  14. for (const [key, value] of Object.entries(properties)) {
  15. this[key] = value;
  16. }
  17. }
  18. }
  19. class PowerStatus extends Assignable {
  20. toBuffer() {
  21. return Buffer.from(borsh.serialize(PowerStatusSchema, this));
  22. }
  23. }
  24. const PowerStatusSchema = new Map([[PowerStatus, { kind: 'struct', fields: [['is_on', 'u8']] }]]);
  25. class SetPowerStatus extends Assignable {
  26. toBuffer() {
  27. return Buffer.from(borsh.serialize(SetPowerStatusSchema, this));
  28. }
  29. }
  30. const SetPowerStatusSchema = new Map([[SetPowerStatus, { kind: 'struct', fields: [['name', 'string']] }]]);
  31. const powerAccount = Keypair.generate();
  32. it('Initialize the lever!', async () => {
  33. const ix = new TransactionInstruction({
  34. keys: [
  35. { pubkey: powerAccount.publicKey, isSigner: true, isWritable: true },
  36. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  37. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  38. ],
  39. programId: lever.publicKey,
  40. data: new PowerStatus({ is_on: true }).toBuffer(),
  41. });
  42. await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer, powerAccount]);
  43. });
  44. it('Pull the lever!', async () => {
  45. const ix = new TransactionInstruction({
  46. keys: [
  47. { pubkey: powerAccount.publicKey, isSigner: false, isWritable: true },
  48. { pubkey: lever.publicKey, isSigner: false, isWritable: false },
  49. ],
  50. programId: hand.publicKey,
  51. data: new SetPowerStatus({ name: 'Chris' }).toBuffer(),
  52. });
  53. await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]);
  54. });
  55. it('Pull it again!', async () => {
  56. const ix = new TransactionInstruction({
  57. keys: [
  58. { pubkey: powerAccount.publicKey, isSigner: false, isWritable: true },
  59. { pubkey: lever.publicKey, isSigner: false, isWritable: false },
  60. ],
  61. programId: hand.publicKey,
  62. data: new SetPowerStatus({ name: 'Ashley' }).toBuffer(),
  63. });
  64. await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]);
  65. });
  66. });