mango-ui-v3/utils/metaplex.ts

125 lines
2.6 KiB
TypeScript

import { StringPublicKey } from '../@types/types'
const METADATA_PREFIX = 'metadata'
const METADATA_PROGRAM = 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'
export enum MetadataKey {
Uninitialized = 0,
MetadataV1 = 4,
EditionV1 = 1,
MasterEditionV1 = 2,
MasterEditionV2 = 6,
EditionMarker = 7,
}
export class Creator {
address: StringPublicKey
verified: boolean
share: number
constructor(args: {
address: StringPublicKey
verified: boolean
share: number
}) {
this.address = args.address
this.verified = args.verified
this.share = args.share
}
}
class Metadata {
key: MetadataKey
updateAuthority: StringPublicKey
mint: StringPublicKey
data: Data
primarySaleHappened: boolean
isMutable: boolean
editionNonce: number | null
// set lazy
masterEdition?: StringPublicKey
edition?: StringPublicKey
constructor(args: {
updateAuthority: StringPublicKey
mint: StringPublicKey
data: Data
primarySaleHappened: boolean
isMutable: boolean
editionNonce: number | null
}) {
this.key = MetadataKey.MetadataV1
this.updateAuthority = args.updateAuthority
this.mint = args.mint
this.data = args.data
this.primarySaleHappened = args.primarySaleHappened
this.isMutable = args.isMutable
this.editionNonce = args.editionNonce ?? null
}
}
export class Data {
name: string
symbol: string
uri: string
sellerFeeBasisPoints: number
creators: Creator[] | null
constructor(args: {
name: string
symbol: string
uri: string
sellerFeeBasisPoints: number
creators: Creator[] | null
}) {
this.name = args.name
this.symbol = args.symbol
this.uri = args.uri
this.sellerFeeBasisPoints = args.sellerFeeBasisPoints
this.creators = args.creators
}
}
const METADATA_SCHEMA = new Map<any, any>([
[
Data,
{
kind: 'struct',
fields: [
['name', 'string'],
['symbol', 'string'],
['uri', 'string'],
['sellerFeeBasisPoints', 'u16'],
['creators', { kind: 'option', type: [Creator] }],
],
},
],
[
Metadata,
{
kind: 'struct',
fields: [
['key', 'u8'],
['updateAuthority', 'pubkey'],
['mint', 'pubkey'],
['data', Data],
['primarySaleHappened', 'u8'], // bool
['isMutable', 'u8'], // bool
],
},
],
[
Creator,
{
kind: 'struct',
fields: [
['address', 'pubkey'],
['verified', 'u8'],
['share', 'u8'],
],
},
],
])
export { METADATA_SCHEMA, METADATA_PREFIX, METADATA_PROGRAM, Metadata }