create.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import * as borsh from "borsh";
  2. import { Buffer } from "buffer";
  3. import {
  4. PublicKey,
  5. SystemProgram,
  6. TransactionInstruction
  7. } from '@solana/web3.js';
  8. import { MyInstruction } from ".";
  9. export class Create {
  10. instruction: MyInstruction;
  11. name: string;
  12. constructor(props: {
  13. instruction: MyInstruction,
  14. name: string,
  15. }) {
  16. this.instruction = props.instruction;
  17. this.name = props.name;
  18. }
  19. toBuffer() {
  20. return Buffer.from(borsh.serialize(CreateSchema, this))
  21. };
  22. static fromBuffer(buffer: Buffer) {
  23. return borsh.deserialize(CreateSchema, Create, buffer);
  24. };
  25. };
  26. export const CreateSchema = new Map([
  27. [ Create, {
  28. kind: 'struct',
  29. fields: [
  30. ['instruction', 'u8'],
  31. ['name', 'string'],
  32. ],
  33. }]
  34. ]);
  35. export function createCreateUserInstruction(
  36. target: PublicKey,
  37. payer: PublicKey,
  38. programId: PublicKey,
  39. name: string,
  40. ): TransactionInstruction {
  41. const instructionObject = new Create({
  42. instruction: MyInstruction.CreateUser,
  43. name,
  44. });
  45. const ix = new TransactionInstruction({
  46. keys: [
  47. {pubkey: target, isSigner: false, isWritable: true},
  48. {pubkey: payer, isSigner: true, isWritable: true},
  49. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  50. ],
  51. programId: programId,
  52. data: instructionObject.toBuffer(),
  53. });
  54. return ix;
  55. }