Skip to content

Instantly share code, notes, and snippets.

@davoclavo
Created October 1, 2021 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davoclavo/dda7e18e732b61c56e590fd98119ef35 to your computer and use it in GitHub Desktop.
Save davoclavo/dda7e18e732b61c56e590fd98119ef35 to your computer and use it in GitHub Desktop.
Proposal: Account Codec Program

Given that it is kind of tricky to know how to read data from accounts, it would be really useful to have an "Account Codec Program" as a central repository of Program Accounts type schemas.

I'm thinking with this program you could register Program Account schemas to know what format to use to Decode them. As an analogy, its like storing the Anchor's IDL on chain, or also akin to how the Metaplex Metadata Token program is the standard where you define metadata for an NFT Mint

E.g.

Some program's account:

declare_id!("SomeProgramPubkey987654321")
struct Person {
  name: [u8; 100],
  age: u8,
}

struct Pet {
  legs: u8,
  owner: Pubkey
}

The owner of the private key of that program address could

  1. Register a list of Account names

PDA seed = ["SomeProgramPubkey987654321", "AccountCodecProgramPubkey1234567890"]

Encoded data:

[
    "Person",
    "Animal"
]
  1. The owner of the private key of that program address could register multiple associated PDAs for each account using the Codec program

PDA seed = ["Person", "SomeProgramPubkey987654321", "AccountCodecProgramPubkey1234567890"]

Encoded data:

[
    Field {name: "name", type: "[u8; 100]"},
    Field {name: "age", type: "u8"},
]

PDA seed = ["Animal", "SomeProgramPubkey987654321", "AccountCodecProgramPubkey1234567890"]

Encoded data:

[
    Field {name: "legs", type: "u8"},
    Field {name: "owner", type: "u32"},
]

Additional thoughts

  • The encoded data would have to support nested data structures
  • Perhaps account codecs could be registered by the community, instead of just allowing program signers to be able to do so, to increase its usage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment