1

I am trying to decode data from accounts.

This is the IDL for the data I want to decode:

{
      "name": "deployment",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "name": "creator",
            "type": "publicKey"
          },
          {
            "name": "limitPerMint",
            "type": "u64"
          },
          {
            "name": "maxNumberOfTokens",
            "type": "u64"
          },
          {
            "name": "numberOfTokensIssued",
            "type": "u64"
          },
          {
            "name": "decimals",
            "type": "u8"
          },
          {
            "name": "useInscriptions",
            "type": "bool"
          },
          {
            "name": "deploymentType",
            "type": "u8"
          },
          {
            "name": "requireCreatorCosign",
            "type": "bool"
          },
          {
            "name": "migratedFromLegacy",
            "type": "bool"
          },
          {
            "name": "escrowNonFungibleCount",
            "type": "u64"
          },
          {
            "name": "ticker",
            "type": "string"
          },
          {
            "name": "deploymentTemplate",
            "type": "string"
          },
          {
            "name": "mintTemplate",
            "type": "string"
          },
          {
            "name": "fungibleMint",
            "type": "publicKey"
          },
          {
            "name": "offchainUrl",
            "type": "string"
          }
        ]
      }
    },

This is how my code looks like:

import { publicKey, u64, bool } from '@solana/buffer-layout-utils';
import { u32, u8, struct} from '@solana/buffer-layout';
import {Connection,PublicKey,} from "@solana/web3.js";

interface DeploymentAccount{
    discriminator: BN;
    creator: PublicKey;
    limitPerMint: BN;
    maxNumberOfTokens: BN;
    numberOfTokensIssued: BN;
    decimals: number;
    useInscriptions: boolean;
    deploymentType:number;
    requireCreatorCosign:boolean;
    migratedFromLegacy:boolean;
    escrowNonFungibleCount:BN;
    ticker: string;
    deploymentTemplate:string;
    mintTemplate:string;
    fungibleMint: PublicKey;
    offchainUrl:string;
}

const DeploymentAccountLayout = struct<DeploymentAccount>([
    u64("discriminator"),
    publicKey("creator"),
    u64("limitPerMint"),
    u64("maxNumberOfTokens"),
    u64("numberOfTokensIssued"),
    u8("decimals"),
    bool("useInscriptions"),
    u8("deploymentType"),
    bool("requireCreatorCosign"),
    bool("migratedFromLegacy"),
    u64("escrowNonFungibleCount"),
    u8("ticker"),
    u32("deploymentTemplate"),
    u32("mintTemplate"),
    publicKey("fungibleMint"),
    u64("offchainUrl"),
])

const connection = new Connection(rpc)
const accounts = await connection.getAccountInfo(new PublicKey('CDoPzZWsuZQ2GrJ4xWSkqomQLkXxK1FM7HUd2jSRTQKn'), "processed" )
const d = DeploymentAccountLayout.decode(accounts.data)

So far everything seems to work but I do not know what kind of bytes an string represent so I do no know what to assign for ticker, deployTemplate, mintTemplate, offchainUrl

This are the limits set on the contract:

pub const TICKER_LIMIT: usize = 200;
pub const TEMPLATE_LIMIT: usize = 1200;
pub const OFFCHAIN_URL_LIMIT: usize = 1200;

2 Answers 2

2

I found the answer.

Instead of using:

import { publicKey, u64, bool, u256, u128 } from '@solana/buffer-layout-utils';
import { u32, u8, struct} from '@solana/buffer-layout';

I used:

import * as borsh from "@coral-xyz/borsh"

And created the schema as follow:

const deploymentAccountSchema = borsh.struct<DeploymentAccount>([
    borsh.u64("discriminator"),
    borsh.publicKey("creator"),
    borsh.u64("limitPerMint"),
    borsh.u64("maxNumberOfTokens"),
    borsh.u64("numberOfTokensIssued"),
    borsh.u8("decimals"),
    borsh.bool("useInscriptions"),
    borsh.u8("deploymentType"),
    borsh.bool("requireCreatorCosign"),
    borsh.bool("migratedFromLegacy"),
    borsh.u64("escrowNonFungibleCount"),
    borsh.str("ticker"),
    borsh.str("deploymentTemplate"),
    borsh.str("mintTemplate"),
    borsh.publicKey("fungibleMint"),
    borsh.str("offchainUrl"),
])

This returns the data as expected

0

If you have an IDL that Anchor generated like export const IDL: MyProgram = { //... and export const IDL: MyProgram = { //... you can automatically generate types like

export type SomeAccount = IdlAccounts<typeof IDL>["someAccount"];

Just make sure someAccount is actually the name in the IDL, and note that it is typically camelCase.

2
  • This is a nice one didn't know you can do that but my problem was creating the schema to decode the data Commented Feb 10 at 14:53
  • If you've done this you can do const someAcc: someAccount = await program.account.someAccount.fetch(someKey);
    – Whiteseal
    Commented Feb 12 at 17:22

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.