From d96754f771a8918f4b454b35469c95ab2b42f1b9 Mon Sep 17 00:00:00 2001 From: Joaquin Gonzalez Date: Fri, 22 Jul 2022 11:33:35 -0300 Subject: [PATCH 1/3] getNftContractMetadata logic added --- README.md | 17 +++++++++++++++++ src/alchemy-apis/types.ts | 14 ++++++++++++++ src/index.ts | 13 +++++++++++++ 3 files changed, 44 insertions(+) diff --git a/README.md b/README.md index 8cb3578..6f6f289 100644 --- a/README.md +++ b/README.md @@ -413,6 +413,23 @@ An object with the following fields: - `attributes`: (Optional) An array of attributes from the NFT metadata. Each attribute is a dictionary with unknown keys and values, as they depend directly on the contract. - `timeLastUpdated`: ISO timestamp of the last cache refresh for the information returned in the metadata field. +### `web3.alchemy.getContractMetadata(contractAddress)` + +**Parameters:** + +- `contractAddress`: The contract address for the NFT collection + +**Returns:** + +An object with the following fields: + +- `address`: The hex string of the contract address for the queried NFT collection +- `contractMetadata`: + - `name`: (string) The NFT contract name . + - `symbol`: (string) The NFT contract symbol abbreviation. + - `totalSupply`: (string) total number of NFTs in a given NFT collection. + - `tokenType`: (string) "erc721" or "erc1155" NFT token type. + ### `web3.alchemy.getTransactionReceipts({blockNumber | blockHash})` Fetches all transaction receipts for a block number or a block hash. diff --git a/src/alchemy-apis/types.ts b/src/alchemy-apis/types.ts index dc5b25c..b0f79ec 100644 --- a/src/alchemy-apis/types.ts +++ b/src/alchemy-apis/types.ts @@ -86,6 +86,13 @@ export interface NftMetadata extends Record { attributes?: Array>; } +export interface NftContractMetadata extends Record { + name?: string; + symbol?: string; + totalSupply?: string; + tokenType?: string; +} + export interface TokenUri { raw: string; gateway: string; @@ -93,6 +100,7 @@ export interface TokenUri { export interface NftContract { address: string; + contractMetadata?: NftContractMetadata; } export interface NftId { @@ -110,8 +118,14 @@ export interface GetNftMetadataParams { tokenType?: "erc721" | "erc1155"; } +export interface GetNftContractMetadataParams { + contractAddress: string; +} + export type GetNftMetadataResponse = Nft; +export type GetNftContractMetadataResponse = NftContract; + export interface Nft extends BaseNft { title: string; description: string; diff --git a/src/index.ts b/src/index.ts index fbc1834..8693138 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,8 @@ import { toHex } from "web3-utils"; import { AssetTransfersParams, AssetTransfersResponse, + GetNftContractMetadataParams, + GetNftContractMetadataResponse, GetNftMetadataParams, GetNftMetadataResponse, GetNftsParams, @@ -74,6 +76,10 @@ export interface AlchemyMethods { params: GetNftMetadataParams, callback?: Web3Callback, ): Promise; + getNftContractMetadata( + params: GetNftContractMetadataParams, + callback?: Web3Callback, + ): Promise; getNfts( params: GetNftsParamsWithoutMetadata, callback?: Web3Callback, @@ -251,6 +257,13 @@ export function createAlchemyWeb3( params, path: "/v1/getNFTMetadata/", }), + getNftContractMetadata: (params: GetNftContractMetadataParams, callback) => + callAlchemyRestEndpoint({ + restSender, + callback, + params, + path: "/v1/getContractMetadata/", + }), getNfts, getTransactionReceipts: (params: TransactionReceiptsParams, callback) => callAlchemyJsonRpcMethod({ From 34ef03444c932d4e0796d691e59cb19622b5ae9f Mon Sep 17 00:00:00 2001 From: Joaquin Gonzalez Date: Fri, 22 Jul 2022 12:19:21 -0300 Subject: [PATCH 2/3] self review --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6f6f289..6aa2b4d 100644 --- a/README.md +++ b/README.md @@ -413,7 +413,7 @@ An object with the following fields: - `attributes`: (Optional) An array of attributes from the NFT metadata. Each attribute is a dictionary with unknown keys and values, as they depend directly on the contract. - `timeLastUpdated`: ISO timestamp of the last cache refresh for the information returned in the metadata field. -### `web3.alchemy.getContractMetadata(contractAddress)` +### `web3.alchemy.getNftContractMetadata(contractAddress)` **Parameters:** From 1544a92cc68b44988a4339ab4d28e8f2a62b0ad9 Mon Sep 17 00:00:00 2001 From: Joaquin Gonzalez Date: Fri, 22 Jul 2022 15:51:12 -0300 Subject: [PATCH 3/3] support v2 nft servicies --- src/index.ts | 6 +++--- src/web3-adapter/alchemyContext.ts | 2 +- src/web3-adapter/sendRestPayload.ts | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8693138..9cfe51a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -204,7 +204,7 @@ export function createAlchemyWeb3( restSender, callback, params, - path: "/v1/getNFTs/", + path: "/getNFTs/", }); } @@ -255,14 +255,14 @@ export function createAlchemyWeb3( restSender, callback, params, - path: "/v1/getNFTMetadata/", + path: "/getNFTMetadata/", }), getNftContractMetadata: (params: GetNftContractMetadataParams, callback) => callAlchemyRestEndpoint({ restSender, callback, params, - path: "/v1/getContractMetadata/", + path: "/getContractMetadata/", }), getNfts, getTransactionReceipts: (params: TransactionReceiptsParams, callback) => diff --git a/src/web3-adapter/alchemyContext.ts b/src/web3-adapter/alchemyContext.ts index 1fcdede..9c81b40 100644 --- a/src/web3-adapter/alchemyContext.ts +++ b/src/web3-adapter/alchemyContext.ts @@ -66,7 +66,7 @@ export function makeAlchemyContext( return { provider, jsonRpcSenders, restSender, setWriteProvider }; } else { throw new Error( - `Alchemy URL protocol must be one of http, https, ws, or wss. Recieved: ${url}`, + `Alchemy URL protocol must be one of http, https, ws, or wss. Received: ${url}`, ); } } diff --git a/src/web3-adapter/sendRestPayload.ts b/src/web3-adapter/sendRestPayload.ts index 5077fa8..28f68ea 100644 --- a/src/web3-adapter/sendRestPayload.ts +++ b/src/web3-adapter/sendRestPayload.ts @@ -15,11 +15,15 @@ export type SendRestPayloadFunction = ( export interface RestPayloadConfig { url: string; config: FullConfig; + entity?: string; + version?: string; } export function makeRestPayloadSender({ url, config, + entity = "nft", + version = "v2", }: RestPayloadConfig): RestPayloadSender { // The rest payload sender only works for alchemy.com http endpoints. let error: string | undefined; @@ -54,7 +58,7 @@ export function makeRestPayloadSender({ if (origin && apiKey) { const endpoint = new URI(origin) .search(payload) - .path(apiKey + path) + .path(`/${entity}/${version}/${apiKey}${path}`) .toString(); for (let i = 0; i < maxRetries + 1; i++) { const response = await fetch(endpoint);