test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import {
  2. Connection,
  3. Keypair,
  4. sendAndConfirmTransaction,
  5. SystemProgram,
  6. Transaction,
  7. TransactionInstruction,
  8. } from '@solana/web3.js';
  9. import * as borsh from "borsh";
  10. import { Buffer } from "buffer";
  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("CPI Example", () => {
  17. const connection = new Connection(`http://localhost:8899`, 'confirmed');
  18. const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
  19. const hand = createKeypairFromFile('./target/so/hand-keypair.json');
  20. const lever = createKeypairFromFile('./target/so/lever-keypair.json');
  21. class Assignable {
  22. constructor(properties) {
  23. Object.keys(properties).map((key) => {
  24. return (this[key] = properties[key]);
  25. });
  26. };
  27. };
  28. class PowerStatus extends Assignable {
  29. toBuffer() { return Buffer.from(borsh.serialize(PowerStatusSchema, this)) }
  30. };
  31. const PowerStatusSchema = new Map([[ PowerStatus, { kind: 'struct', fields: [ ['is_on', 'u8'] ]} ]]);
  32. class SetPowerStatus extends Assignable {
  33. toBuffer() { return Buffer.from(borsh.serialize(SetPowerStatusSchema, this)) }
  34. };
  35. const SetPowerStatusSchema = new Map([[ SetPowerStatus, { kind: 'struct', fields: [ ['name', 'string'] ]} ]]);
  36. const powerAccount = Keypair.generate();
  37. it("Initialize the lever!", async () => {
  38. let ix = new TransactionInstruction({
  39. keys: [
  40. {pubkey: powerAccount.publicKey, isSigner: true, isWritable: true},
  41. {pubkey: payer.publicKey, isSigner: true, isWritable: true},
  42. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  43. ],
  44. programId: lever.publicKey,
  45. data: (new PowerStatus({is_on: true})).toBuffer(),
  46. });
  47. await sendAndConfirmTransaction(
  48. connection,
  49. new Transaction().add(ix),
  50. [payer, powerAccount]
  51. );
  52. });
  53. it("Pull the lever!", async () => {
  54. let ix = new TransactionInstruction({
  55. keys: [
  56. {pubkey: powerAccount.publicKey, isSigner: false, isWritable: true},
  57. {pubkey: lever.publicKey, isSigner: false, isWritable: false},
  58. ],
  59. programId: hand.publicKey,
  60. data: new SetPowerStatus({name: "Chris"}).toBuffer(),
  61. });
  62. await sendAndConfirmTransaction(
  63. connection,
  64. new Transaction().add(ix),
  65. [payer]
  66. );
  67. });
  68. it("Pull it again!", async () => {
  69. let ix = new TransactionInstruction({
  70. keys: [
  71. {pubkey: powerAccount.publicKey, isSigner: false, isWritable: true},
  72. {pubkey: lever.publicKey, isSigner: false, isWritable: false},
  73. ],
  74. programId: hand.publicKey,
  75. data: new SetPowerStatus({name: "Ashley"}).toBuffer(),
  76. });
  77. await sendAndConfirmTransaction(
  78. connection,
  79. new Transaction().add(ix),
  80. [payer]
  81. );
  82. });
  83. });