1

there are arguments

      "args": [
        {
          "name": "decimals",
          "type": "u8"
        },
        {
          "name": "mintAuthority",
          "type": "publicKey"
        },
        {
          "name": "freezeAuthority",
          "type": {
            "defined": "COption<Pubkey>"
          }
        }
      ]

How to make the correct schema to be used in borsh.js?

I tried something like this

const schema = {
  struct: {
    decimals: "u8",
    mintAuthority: "string",
    freezeAuthority: "string",
  },
};

or

class MyArgs {
  decimals: u8;
  mintAuthority: PublicKey;
  freezeAuthority: COption<Pubkey> | null;

  constructor(decimals: u8, mintAuthority: PublicKey, freezeAuthority: COption<Pubkey> | null) {
    this.decimals = decimals;
    this.mintAuthority = mintAuthority;
    this.freezeAuthority = freezeAuthority;
  }
}

const MyArgsSchema = new Map([
  [
    MyArgs,
    {
      kind: 'struct',
      fields: [
        ['decimals', 'u8'],
        ['mintAuthority', 'publicKey'],
        ['freezeAuthority', { kind: 'option', type: 'publicKey' }],
      ],
    },
  ],
]);

const data = "bT1LdJiCtzLAHMaiFmacn9H5oEZbRNsD6un3YxFXLYX6aixPPcaebNgLjwnMxzsEQ7uHaVmG3f5MPax7VEPAbJXYY1Q";

const resultDecode = deserialize(
  MyArgsSchema, // MyArgsSchema or schema
  Buffer.from(bs58.decode(data)),
);
console.log(resultDecode); // Error Invalid schema....

but it threw an error

Error: Invalid schema: {} expected option, enum, array, set, map, struct or u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, f32, f64, bool, string

How to correctly specify the type for publicKey and

"type": {
   "defined": "COption<Pubkey>"
 }

i`m use "borsh": "^2.0.0" version, "bs58": "^5.0.0",

1 Answer 1

2

Pubkey is not part of the borsh specification. It can be represented as a [u8; 32] instead. Should look something like:

class MyArgs {
  decimals: u8;
  mintAuthority: PublicKey;
  freezeAuthority: Pubkey | null;

  constructor(decimals: u8, mintAuthority: Uint8Array, freezeAuthority: Uint8Array | null) {
    this.decimals = decimals;
    this.mintAuthority = new PublicKey(mintAuthority);
    this.freezeAuthority = freezeAuthority !== null ? new PublicKey(mintAuthority) : null ;
  }
}

const MyArgsSchema = new Map([
  [
    MyArgs,
    {
      kind: 'struct',
      fields: [
        ['decimals', 'u8'],
        ['mintAuthority', '[32]'],
        ['freezeAuthority', { kind: 'option', type: '[32]' }],
      ],
    },
  ],
]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.