metaplex-foundation / solita

Genrates an SDK API from solana contract IDL.
Apache License 2.0
140 stars 32 forks source link

[feat]: generate account providers to be used by developer tools for de/serialization #67

Closed thlorenz closed 2 years ago

thlorenz commented 2 years ago

Tools like amman depend on deserializing accounts in order to show their data on the terminal or a UI.

Therefore solita should generate and export such providers similar to this:

import {
  CollectionAuthorityRecord,
  Edition,
  EditionMarker,
  MasterEditionV2,
  Metadata,
  ReservationListV2,
  UseAuthorityRecord,
} from 'path/to/accounts';

export const accountProviders = {
  CollectionAuthorityRecord,
  Edition,
  EditionMarker,
  MasterEditionV2,
  Metadata,
  ReservationListV2,
  UseAuthorityRecord,
};

The tool in question can then import/combine them as follow:

const {
  candyProviders,
  PROGRAM_ADDRESS: CandyAddress,
} = require("@metaplex-foundation/mpl-candy-machine");
const {
  tokenMetadataProviders,
  PROGRAM_ADDRESS: TokenMetadataAddress,
} = require("@metaplex-foundation/mpl-token-metadata");

const accountProviders = {
  [CandyAddress]: candyProviders,
  [TokenMetadataAddress]: tokenMetadataProviders,
};

This allows such tool to encounter the correct de/serializer by applying the ones for the providers of the program that owns a particular account. The fitting account provider can be determined by comparing account size (if the account is fixed size) or just attempting to deserialize an account until it succeeds.

This is the approach that amman currently applies (except that it doesn't yet take the account owner into account).