metaplex-foundation / solita

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

feat: exporting account providers #68

Closed thlorenz closed 2 years ago

thlorenz commented 2 years ago

Summary

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

Therefore solita now exports accountProviders similar to the below (example from mpl_token_metadata):

import { UseAuthorityRecord } from './UseAuthorityRecord'
import { CollectionAuthorityRecord } from './CollectionAuthorityRecord'
import { Metadata } from './Metadata'
import { MasterEditionV2 } from './MasterEditionV2'
import { MasterEditionV1 } from './MasterEditionV1'
import { Edition } from './Edition'
import { ReservationListV2 } from './ReservationListV2'
import { ReservationListV1 } from './ReservationListV1'
import { EditionMarker } from './EditionMarker'

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

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

const {
  accountProviders: candyProviders,
  PROGRAM_ADDRESS: CandyAddress,
} = require("@metaplex-foundation/mpl-candy-machine");
const {
  accountProviders: 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.

FIXES: #67

Details