Skip to content

Commit

Permalink
fix 1155 read extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges committed Feb 23, 2024
1 parent d8ef2fd commit 01ccc3c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/thirdweb/src/exports/extensions/erc1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export {
} from "../../extensions/erc1155/read/getOwnedNFTs.js";
export { nextTokenIdToMint } from "../../extensions/erc1155/read/nextTokenIdToMint.js";
export {
tokenURI,
uri as tokenURI,
type TokenUriParams,
} from "../../extensions/erc1155/read/tokenURI.js";
} from "../../extensions/erc1155/read/uri.js";
export {
totalSupply,
type TotalSupplyParams,
Expand Down
51 changes: 51 additions & 0 deletions packages/thirdweb/src/extensions/erc1155/read/getNFT.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, it, expect, vi, afterEach } from "vitest";

import { getNFT } from "./getNFT.js";
import { DROP1155_CONTRACT } from "~test/test-contracts.js";

const fetchSpy = vi.spyOn(globalThis, "fetch");

describe("erc1155.getNFT", () => {
afterEach(() => {
fetchSpy.mockClear();
});
it.runIf(process.env.TW_SECRET_KEY)("without owner", async () => {
const nft = await getNFT({
contract: DROP1155_CONTRACT,
tokenId: 2n,
});
expect(nft).toMatchInlineSnapshot(`
{
"id": 2n,
"metadata": {
"animation_url": "ipfs://QmYoM63qaumQznBRx38tQjkY4ewbymeFb2KWBhkfMqNHax/3.mp4",
"attributes": [
{
"trait_type": "Revenue Share",
"value": "15%",
},
{
"trait_type": "Max Supply",
"value": "5000",
},
{
"trait_type": "Max Per Wallet",
"value": "10",
},
],
"background_color": "",
"description": "",
"external_url": "https://auraexchange.org",
"image": "ipfs://QmYoM63qaumQznBRx38tQjkY4ewbymeFb2KWBhkfMqNHax/2.png",
"name": "Aura Platinum",
},
"owner": null,
"supply": 2519n,
"tokenURI": "ipfs://QmbMXdbnNUAuGRoY6c6G792c6T9utfaBGqRUaMaRUf52Cb/2",
"type": "ERC1155",
}
`);
// 2 fetch calls: 1 for RPC, 1 for fetching the tokenUri
expect(fetchSpy).toHaveBeenCalledTimes(2);
});
});
10 changes: 5 additions & 5 deletions packages/thirdweb/src/extensions/erc1155/read/getNFT.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { BaseTransactionOptions } from "../../../transaction/types.js";
import { fetchTokenMetadata } from "../../../utils/nft/fetchTokenMetadata.js";
import { parseNFT, type NFT } from "../../../utils/nft/parseNft.js";
import { tokenURI, type TokenUriParams } from "./tokenURI.js";
import { uri, type TokenUriParams } from "./uri.js";
import { totalSupply } from "./totalSupply.js";

/**
Expand All @@ -26,19 +26,19 @@ export type GetNFTParams = TokenUriParams;
export async function getNFT(
options: BaseTransactionOptions<GetNFTParams>,
): Promise<NFT<"ERC1155">> {
const [uri, supply] = await Promise.all([
tokenURI(options),
const [tokenUri, supply] = await Promise.all([
uri(options),
totalSupply(options),
]);
return parseNFT(
await fetchTokenMetadata({
client: options.contract.client,
tokenId: options.tokenId,
tokenUri: uri,
tokenUri,
}),
{
tokenId: options.tokenId,
tokenUri: uri,
tokenUri,
type: "ERC1155",
owner: null,
supply,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ export type TokenUriParams = { tokenId: bigint };
* @extension ERC1155
* @example
* ```ts
* import { tokenURI } from "thirdweb/extensions/erc155";
* const uri = await tokenURI({ contract, tokenId: 1n });
* import { uri } from "thirdweb/extensions/erc155";
* const tokenUri = await uri({ contract, tokenId: 1n });
* ```
*/
export function tokenURI(
export function uri(
options: BaseTransactionOptions<TokenUriParams>,
): Promise<string> {
return readContract({
...options,
method: "function tokenURI(uint256) returns (string)",
method: "function uri(uint256) returns (string)",
params: [options.tokenId],
});
}
10 changes: 10 additions & 0 deletions packages/thirdweb/test/src/test-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ export const DOODLES_CONTRACT = getContract({
address: DOODLES_ADDRESS,
chain: FORKED_ETHEREUM_CHAIN,
});

// ERC1155

const AURA_ADDRESS = "0x42d3641255C946CC451474295d29D3505173F22A";

export const DROP1155_CONTRACT = getContract({
client: TEST_CLIENT,
address: AURA_ADDRESS,
chain: FORKED_ETHEREUM_CHAIN,
});

0 comments on commit 01ccc3c

Please sign in to comment.