|
| 1 | +import { Commitment, Connection, ConnectionConfig, PublicKey } from "@solana/web3.js"; |
| 2 | +// local imports for the ReadApi types |
| 3 | +import type { |
| 4 | + GetAssetProofRpcInput, |
| 5 | + GetAssetProofRpcResponse, |
| 6 | + GetAssetRpcInput, |
| 7 | + GetAssetsByGroupRpcInput, |
| 8 | + GetAssetsByOwnerRpcInput, |
| 9 | + ReadApiAsset, |
| 10 | + ReadApiAssetList, |
| 11 | +} from "@/ReadApi/types"; |
| 12 | +import type { Metadata, Mint, NftOriginalEdition, SplTokenCurrency } from "@metaplex-foundation/js"; |
| 13 | +// import from the `@metaplex-foundation/js` |
| 14 | +import { MetaplexError, Pda, amount, toBigNumber } from "@metaplex-foundation/js"; |
| 15 | + |
| 16 | +import BN from "bn.js"; |
| 17 | +import { PROGRAM_ID as BUBBLEGUM_PROGRAM_ID } from "@metaplex-foundation/mpl-bubblegum"; |
| 18 | +import { TokenStandard } from "@metaplex-foundation/mpl-token-metadata"; |
| 19 | + |
| 20 | +type JsonRpcParams<ReadApiMethodParams> = { |
| 21 | + method: string; |
| 22 | + id?: string; |
| 23 | + params: ReadApiMethodParams; |
| 24 | +}; |
| 25 | + |
| 26 | +type JsonRpcOutput<ReadApiJsonOutput> = { |
| 27 | + result: ReadApiJsonOutput; |
| 28 | +}; |
| 29 | + |
| 30 | +/** @group Errors */ |
| 31 | +export class ReadApiError extends MetaplexError { |
| 32 | + readonly name: string = "ReadApiError"; |
| 33 | + constructor(message: string, cause?: Error) { |
| 34 | + super(message, "rpc", undefined, cause); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Convert a ReadApi asset (e.g. compressed NFT) into an NftEdition |
| 40 | + */ |
| 41 | +export const toNftEditionFromReadApiAsset = (input: ReadApiAsset): NftOriginalEdition => { |
| 42 | + return { |
| 43 | + model: "nftEdition", |
| 44 | + isOriginal: true, |
| 45 | + address: new PublicKey(input.id), |
| 46 | + supply: toBigNumber(input.supply.print_current_supply), |
| 47 | + maxSupply: toBigNumber(input.supply.print_max_supply), |
| 48 | + }; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Convert a ReadApi asset (e.g. compressed NFT) into an NFT mint |
| 53 | + */ |
| 54 | +export const toMintFromReadApiAsset = (input: ReadApiAsset): Mint => { |
| 55 | + const currency: SplTokenCurrency = { |
| 56 | + symbol: "Token", |
| 57 | + decimals: 0, |
| 58 | + namespace: "spl-token", |
| 59 | + }; |
| 60 | + |
| 61 | + return { |
| 62 | + model: "mint", |
| 63 | + address: new PublicKey(input.id), |
| 64 | + mintAuthorityAddress: new PublicKey(input.id), |
| 65 | + freezeAuthorityAddress: new PublicKey(input.id), |
| 66 | + decimals: 0, |
| 67 | + supply: amount(1, currency), |
| 68 | + isWrappedSol: false, |
| 69 | + currency, |
| 70 | + }; |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * Convert a ReadApi asset's data into standard Metaplex `Metadata` |
| 75 | + */ |
| 76 | +export const toMetadataFromReadApiAsset = (input: ReadApiAsset): Metadata => { |
| 77 | + const updateAuthority = input.authorities?.find(authority => authority.scopes.includes("full")); |
| 78 | + |
| 79 | + const collection = input.grouping.find(({ group_key }) => group_key === "collection"); |
| 80 | + |
| 81 | + return { |
| 82 | + model: "metadata", |
| 83 | + /** |
| 84 | + * We technically don't have a metadata address anymore. |
| 85 | + * So we are using the asset's id as the address |
| 86 | + */ |
| 87 | + address: Pda.find(BUBBLEGUM_PROGRAM_ID, [ |
| 88 | + Buffer.from("asset", "utf-8"), |
| 89 | + new PublicKey(input.compression.tree).toBuffer(), |
| 90 | + Uint8Array.from(new BN(input.compression.leaf_id).toArray("le", 8)), |
| 91 | + ]), |
| 92 | + mintAddress: new PublicKey(input.id), |
| 93 | + updateAuthorityAddress: new PublicKey(updateAuthority!.address), |
| 94 | + |
| 95 | + name: input.content.metadata?.name ?? "", |
| 96 | + symbol: input.content.metadata?.symbol ?? "", |
| 97 | + |
| 98 | + json: input.content.metadata, |
| 99 | + jsonLoaded: true, |
| 100 | + uri: input.content.json_uri, |
| 101 | + isMutable: input.mutable, |
| 102 | + |
| 103 | + primarySaleHappened: input.royalty.primary_sale_happened, |
| 104 | + sellerFeeBasisPoints: input.royalty.basis_points, |
| 105 | + creators: input.creators, |
| 106 | + |
| 107 | + editionNonce: input.supply.edition_nonce, |
| 108 | + tokenStandard: TokenStandard.NonFungible, |
| 109 | + |
| 110 | + collection: collection |
| 111 | + ? { address: new PublicKey(collection.group_value), verified: false } |
| 112 | + : null, |
| 113 | + |
| 114 | + // Current regular `Metadata` does not currently have a `compression` value |
| 115 | + // @ts-ignore |
| 116 | + compression: input.compression, |
| 117 | + |
| 118 | + // Read API doesn't return this info, yet |
| 119 | + collectionDetails: null, |
| 120 | + // Read API doesn't return this info, yet |
| 121 | + uses: null, |
| 122 | + // Read API doesn't return this info, yet |
| 123 | + programmableConfig: null, |
| 124 | + }; |
| 125 | +}; |
| 126 | + |
| 127 | +/** |
| 128 | + * Wrapper class to add additional methods on top the standard Connection from `@solana/web3.js` |
| 129 | + * Specifically, adding the RPC methods used by the Digital Asset Standards (DAS) ReadApi |
| 130 | + * for state compression and compressed NFTs |
| 131 | + */ |
| 132 | +export class WrapperConnection extends Connection { |
| 133 | + constructor(endpoint: string, commitmentOrConfig?: Commitment | ConnectionConfig) { |
| 134 | + super(endpoint, commitmentOrConfig); |
| 135 | + } |
| 136 | + |
| 137 | + private callReadApi = async <ReadApiMethodParams, ReadApiJsonOutput>( |
| 138 | + jsonRpcParams: JsonRpcParams<ReadApiMethodParams>, |
| 139 | + ): Promise<JsonRpcOutput<ReadApiJsonOutput>> => { |
| 140 | + const response = await fetch(this.rpcEndpoint, { |
| 141 | + method: "POST", |
| 142 | + headers: { |
| 143 | + "Content-Type": "application/json", |
| 144 | + }, |
| 145 | + body: JSON.stringify({ |
| 146 | + jsonrpc: "2.0", |
| 147 | + method: jsonRpcParams.method, |
| 148 | + id: jsonRpcParams.id ?? "rpd-op-123", |
| 149 | + params: jsonRpcParams.params, |
| 150 | + }), |
| 151 | + }); |
| 152 | + |
| 153 | + return await response.json() as JsonRpcOutput<ReadApiJsonOutput>; |
| 154 | + }; |
| 155 | + |
| 156 | + // Asset id can be calculated via Bubblegum#getLeafAssetId |
| 157 | + // It is a PDA with the following seeds: ["asset", tree, leafIndex] |
| 158 | + async getAsset(assetId: PublicKey): Promise<ReadApiAsset> { |
| 159 | + const { result: asset } = await this.callReadApi<GetAssetRpcInput, ReadApiAsset>({ |
| 160 | + method: "getAsset", |
| 161 | + params: { |
| 162 | + id: assetId.toBase58(), |
| 163 | + }, |
| 164 | + }); |
| 165 | + |
| 166 | + if (!asset) throw new ReadApiError("No asset returned"); |
| 167 | + |
| 168 | + return asset; |
| 169 | + } |
| 170 | + |
| 171 | + // Asset id can be calculated via Bubblegum#getLeafAssetId |
| 172 | + // It is a PDA with the following seeds: ["asset", tree, leafIndex] |
| 173 | + async getAssetProof(assetId: PublicKey): Promise<GetAssetProofRpcResponse> { |
| 174 | + const { result: proof } = await this.callReadApi< |
| 175 | + GetAssetProofRpcInput, |
| 176 | + GetAssetProofRpcResponse |
| 177 | + >({ |
| 178 | + method: "getAssetProof", |
| 179 | + params: { |
| 180 | + id: assetId.toBase58(), |
| 181 | + }, |
| 182 | + }); |
| 183 | + |
| 184 | + if (!proof) throw new ReadApiError("No asset proof returned"); |
| 185 | + |
| 186 | + return proof; |
| 187 | + } |
| 188 | + |
| 189 | + // |
| 190 | + async getAssetsByGroup({ |
| 191 | + groupKey, |
| 192 | + groupValue, |
| 193 | + page, |
| 194 | + limit, |
| 195 | + sortBy, |
| 196 | + before, |
| 197 | + after, |
| 198 | + }: GetAssetsByGroupRpcInput): Promise<ReadApiAssetList> { |
| 199 | + // `page` cannot be supplied with `before` or `after` |
| 200 | + if (typeof page == "number" && (before || after)) |
| 201 | + throw new ReadApiError( |
| 202 | + "Pagination Error. Only one pagination parameter supported per query.", |
| 203 | + ); |
| 204 | + |
| 205 | + // a pagination method MUST be selected, but we are defaulting to using `page=0` |
| 206 | + |
| 207 | + const { result } = await this.callReadApi<GetAssetsByGroupRpcInput, ReadApiAssetList>({ |
| 208 | + method: "getAssetsByGroup", |
| 209 | + params: { |
| 210 | + groupKey, |
| 211 | + groupValue, |
| 212 | + after: after ?? null, |
| 213 | + before: before ?? null, |
| 214 | + limit: limit ?? null, |
| 215 | + page: page ?? 1, |
| 216 | + sortBy: sortBy ?? null, |
| 217 | + }, |
| 218 | + }); |
| 219 | + |
| 220 | + if (!result) throw new ReadApiError("No results returned"); |
| 221 | + |
| 222 | + return result; |
| 223 | + } |
| 224 | + |
| 225 | + // |
| 226 | + async getAssetsByOwner({ |
| 227 | + ownerAddress, |
| 228 | + page, |
| 229 | + limit, |
| 230 | + sortBy, |
| 231 | + before, |
| 232 | + after, |
| 233 | + }: GetAssetsByOwnerRpcInput): Promise<ReadApiAssetList> { |
| 234 | + // `page` cannot be supplied with `before` or `after` |
| 235 | + if (typeof page == "number" && (before || after)) |
| 236 | + throw new ReadApiError( |
| 237 | + "Pagination Error. Only one pagination parameter supported per query.", |
| 238 | + ); |
| 239 | + |
| 240 | + // a pagination method MUST be selected, but we are defaulting to using `page=0` |
| 241 | + |
| 242 | + const { result } = await this.callReadApi<GetAssetsByOwnerRpcInput, ReadApiAssetList>({ |
| 243 | + method: "getAssetsByOwner", |
| 244 | + params: { |
| 245 | + ownerAddress, |
| 246 | + after: after ?? null, |
| 247 | + before: before ?? null, |
| 248 | + limit: limit ?? null, |
| 249 | + page: page ?? 1, |
| 250 | + sortBy: sortBy ?? null, |
| 251 | + }, |
| 252 | + }); |
| 253 | + |
| 254 | + if (!result) throw new ReadApiError("No results returned"); |
| 255 | + |
| 256 | + return result; |
| 257 | + } |
| 258 | +} |
0 commit comments