create.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 { ReallocInstruction } from './instruction';
  9. export class Create {
  10. instruction: ReallocInstruction;
  11. name: string;
  12. house_number: number;
  13. street: string;
  14. city: string;
  15. constructor(props: {
  16. instruction: ReallocInstruction,
  17. name: string,
  18. house_number: number,
  19. street: string,
  20. city: string,
  21. }) {
  22. this.instruction = props.instruction;
  23. this.name = props.name;
  24. this.house_number = props.house_number;
  25. this.street = props.street;
  26. this.city = props.city;
  27. }
  28. toBuffer() {
  29. return Buffer.from(borsh.serialize(CreateSchema, this))
  30. };
  31. static fromBuffer(buffer: Buffer) {
  32. return borsh.deserialize(CreateSchema, Create, buffer);
  33. };
  34. };
  35. export const CreateSchema = new Map([
  36. [ Create, {
  37. kind: 'struct',
  38. fields: [
  39. ['instruction', 'u8'],
  40. ['name', 'string'],
  41. ['house_number', 'u8'],
  42. ['street', 'string'],
  43. ['city', 'string'],
  44. ],
  45. }]
  46. ]);
  47. export function createCreateInstruction(
  48. target: PublicKey,
  49. payer: PublicKey,
  50. programId: PublicKey,
  51. name: string,
  52. house_number: number,
  53. street: string,
  54. city: string,
  55. ): TransactionInstruction {
  56. const instructionObject = new Create({
  57. instruction: ReallocInstruction.Create,
  58. name,
  59. house_number,
  60. street,
  61. city,
  62. });
  63. const ix = new TransactionInstruction({
  64. keys: [
  65. {pubkey: target, isSigner: false, isWritable: true},
  66. {pubkey: payer, isSigner: true, isWritable: true},
  67. {pubkey: SystemProgram.programId, isSigner: false, isWritable: false}
  68. ],
  69. programId: programId,
  70. data: instructionObject.toBuffer(),
  71. });
  72. return ix;
  73. }