Skip to content

Commit 71aa257

Browse files
committed
[SDK] Consolidate tests to avoid excessive deployERC1155Contract (#5135)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR primarily focuses on refactoring and enhancing the test coverage for the `erc1155` extension of the `thirdweb` package. It removes outdated tests, introduces new tests, and updates existing ones to improve functionality verification. ### Detailed summary - Removed multiple test files related to `erc1155` functions. - Updated `lazyMint.test.ts` to use `DROP1155_CONTRACT`. - Added a test for `isGetNFTsSupported` in `drop1155.test.ts`. - Simplified and enhanced tests in `mintAdditionalSupplyToBatch.test.ts`. - Introduced new tests for `updateTokenURI`, minting, and `getOwnedTokenIds` in `token1155.test.ts`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent c32576f commit 71aa257

File tree

10 files changed

+198
-464
lines changed

10 files changed

+198
-464
lines changed

packages/thirdweb/src/extensions/erc1155/drop1155.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type Abi, toFunctionSelector } from "viem";
12
import { beforeAll, describe, expect, it } from "vitest";
23
import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js";
34
import { VITALIK_WALLET } from "../../../test/src/addresses.js";
@@ -8,6 +9,7 @@ import {
89
TEST_ACCOUNT_B,
910
TEST_ACCOUNT_D,
1011
} from "../../../test/src/test-wallets.js";
12+
import { resolveContractAbi } from "../../contract/actions/resolve-abi.js";
1113
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
1214
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
1315
import { resolvePromisedValue } from "../../utils/promise/resolve-promised-value.js";
@@ -20,6 +22,7 @@ import { claimTo } from "./drops/write/claimTo.js";
2022
import { resetClaimEligibility } from "./drops/write/resetClaimEligibility.js";
2123
import { setClaimConditions } from "./drops/write/setClaimConditions.js";
2224
import { getNFT } from "./read/getNFT.js";
25+
import { isGetNFTsSupported } from "./read/getNFTs.js";
2326
import { lazyMint } from "./write/lazyMint.js";
2427

2528
describe.runIf(process.env.TW_SECRET_KEY)(
@@ -398,5 +401,13 @@ describe.runIf(process.env.TW_SECRET_KEY)(
398401
balanceOf({ tokenId: 6n, contract, owner: TEST_ACCOUNT_D.address }),
399402
).resolves.toBe(2n);
400403
});
404+
405+
it("isGetNFTsSupported should work with our Edition Drop contracts", async () => {
406+
const abi = await resolveContractAbi<Abi>(contract);
407+
const selectors = abi
408+
.filter((f) => f.type === "function")
409+
.map((f) => toFunctionSelector(f));
410+
expect(isGetNFTsSupported(selectors)).toBe(true);
411+
});
401412
},
402413
);

packages/thirdweb/src/extensions/erc1155/read/getNFTs.test.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

packages/thirdweb/src/extensions/erc1155/read/getOwnedNFTs.test.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

packages/thirdweb/src/extensions/erc1155/read/getOwnedTokenIds.test.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

packages/thirdweb/src/extensions/erc1155/token1155.test.ts

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
import { type Abi, toFunctionSelector } from "viem";
12
import { beforeAll, describe, expect, it } from "vitest";
23
import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js";
34
import { ANVIL_CHAIN } from "../../../test/src/chains.js";
45
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
56
import {
67
TEST_ACCOUNT_A,
78
TEST_ACCOUNT_B,
9+
TEST_ACCOUNT_C,
810
} from "../../../test/src/test-wallets.js";
11+
import { resolveContractAbi } from "../../contract/actions/resolve-abi.js";
912
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
1013
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
1114
import { deployERC1155Contract } from "../prebuilts/deploy-erc1155.js";
1215
import { balanceOf } from "./__generated__/IERC1155/read/balanceOf.js";
1316
import { totalSupply } from "./__generated__/IERC1155/read/totalSupply.js";
17+
import { uri } from "./__generated__/IERC1155/read/uri.js";
1418
import { nextTokenIdToMint } from "./__generated__/IERC1155Enumerable/read/nextTokenIdToMint.js";
1519
import { getNFT } from "./read/getNFT.js";
16-
import { getNFTs } from "./read/getNFTs.js";
17-
import { mintAdditionalSupplyTo } from "./write/mintAdditionalSupplyTo.js";
20+
import { getNFTs, isGetNFTsSupported } from "./read/getNFTs.js";
21+
import { getOwnedTokenIds } from "./read/getOwnedTokenIds.js";
22+
import {
23+
isMintAdditionalSupplyToSupported,
24+
mintAdditionalSupplyTo,
25+
} from "./write/mintAdditionalSupplyTo.js";
26+
import { mintAdditionalSupplyToBatch } from "./write/mintAdditionalSupplyToBatch.js";
1827
import { mintTo } from "./write/mintTo.js";
28+
import { mintToBatch } from "./write/mintToBatch.js";
29+
import { updateTokenURI } from "./write/updateTokenURI.js";
1930

2031
describe.runIf(process.env.TW_SECRET_KEY)("TokenERC1155", () => {
2132
let contract: ThirdwebContract;
@@ -158,4 +169,161 @@ describe.runIf(process.env.TW_SECRET_KEY)("TokenERC1155", () => {
158169
]
159170
`);
160171
});
172+
173+
it("isGetNFTsSupported should work with our Edition contracts", async () => {
174+
const abi = await resolveContractAbi<Abi>(contract);
175+
const selectors = abi
176+
.filter((f) => f.type === "function")
177+
.map((f) => toFunctionSelector(f));
178+
expect(isGetNFTsSupported(selectors)).toBe(true);
179+
});
180+
181+
// tokenId #0 is updated in this test
182+
it("should update tokenURI", async () => {
183+
await sendAndConfirmTransaction({
184+
transaction: updateTokenURI({
185+
contract,
186+
newMetadata: { name: "Test1 Updated" },
187+
tokenId: 0n,
188+
}),
189+
account: TEST_ACCOUNT_A,
190+
});
191+
const nft = await getNFT({ contract, tokenId: 0n });
192+
expect(nft.metadata.name).toBe("Test1 Updated");
193+
});
194+
195+
it("should mint with `nft` being declared as a string", async () => {
196+
await sendAndConfirmTransaction({
197+
transaction: mintTo({
198+
contract,
199+
nft: TEST_CONTRACT_URI,
200+
to: TEST_ACCOUNT_A.address,
201+
supply: 1n,
202+
}),
203+
account: TEST_ACCOUNT_A,
204+
});
205+
206+
const tokenUri = await uri({ contract, tokenId: 2n });
207+
expect(tokenUri).toBe(TEST_CONTRACT_URI);
208+
});
209+
210+
it("`isMintAdditionalSupplyToSupported` should work with our Edition contracts", async () => {
211+
const abi = await resolveContractAbi<Abi>(contract);
212+
const selectors = abi
213+
.filter((f) => f.type === "function")
214+
.map((f) => toFunctionSelector(f));
215+
expect(isMintAdditionalSupplyToSupported(selectors)).toBe(true);
216+
});
217+
218+
it("should mint multiple tokens in one tx", async () => {
219+
await sendAndConfirmTransaction({
220+
account: TEST_ACCOUNT_A,
221+
transaction: mintToBatch({
222+
contract,
223+
to: TEST_ACCOUNT_C.address,
224+
nfts: [
225+
{ metadata: { name: "batch token 0" }, supply: 1n },
226+
{ metadata: { name: "batch token 1" }, supply: 2n },
227+
{ metadata: { name: "batch token 2" }, supply: 3n },
228+
],
229+
}),
230+
});
231+
232+
const nfts = await getNFTs({ contract });
233+
expect(nfts).toStrictEqual([
234+
{
235+
// Updated tokenURI from the test above
236+
metadata: { name: "Test1 Updated" },
237+
owner: null,
238+
id: 0n,
239+
tokenURI: "ipfs://QmTSyt6YgoFtH8yhWgevC22BxjyrkCKuzdk86nqQJCwrV9/0",
240+
type: "ERC1155",
241+
supply: 15n,
242+
},
243+
{
244+
metadata: { name: "Test NFT 2" },
245+
owner: null,
246+
id: 1n,
247+
tokenURI: "ipfs://QmV6gsfzdiMRtpnh8ay3CgutStVbes7qoF4DKpYE64h8hT/0",
248+
type: "ERC1155",
249+
supply: 5n,
250+
},
251+
{
252+
metadata: {
253+
name: "tw-contract-name",
254+
symbol: "tw-contract-symbol",
255+
description: "tw-contract-description",
256+
},
257+
owner: null,
258+
id: 2n,
259+
// Minted using URI from the test above
260+
tokenURI:
261+
"ipfs://bafybeiewg2e3vehsb5pb34xwk25eyyfwlvajzmgz3rtdrvn3upsxnsbhzi/contractUri.json",
262+
type: "ERC1155",
263+
supply: 1n,
264+
},
265+
{
266+
metadata: { name: "batch token 0" },
267+
owner: null,
268+
id: 3n,
269+
tokenURI: "ipfs://QmPSQhC2J6Wig4pH1Lt5th19FM58J5oukhfLfpc9L1i39Q/0",
270+
type: "ERC1155",
271+
supply: 1n,
272+
},
273+
{
274+
metadata: { name: "batch token 1" },
275+
owner: null,
276+
id: 4n,
277+
tokenURI: "ipfs://QmWRQwLBAeq6Wr65Yj7A4aeYqMD1C7Hs1moz15ncS6EhGu/0",
278+
type: "ERC1155",
279+
supply: 2n,
280+
},
281+
{
282+
metadata: { name: "batch token 2" },
283+
owner: null,
284+
id: 5n,
285+
tokenURI: "ipfs://QmTg1wxKGvdZR4NrdkYZGcfB5YYaBz75DH83td5RwprMRP/0",
286+
type: "ERC1155",
287+
supply: 3n,
288+
},
289+
]);
290+
});
291+
292+
it("getOwnedTokenIds should work", async () => {
293+
const ownedTokenIds = await getOwnedTokenIds({
294+
contract,
295+
address: TEST_ACCOUNT_C.address,
296+
});
297+
expect(ownedTokenIds).toStrictEqual([
298+
{ tokenId: 3n, balance: 1n },
299+
{ tokenId: 4n, balance: 2n },
300+
{ tokenId: 5n, balance: 3n },
301+
]);
302+
});
303+
304+
it("should mint multiple ADDITIONAL tokens in one tx", async () => {
305+
await sendAndConfirmTransaction({
306+
account: TEST_ACCOUNT_A,
307+
transaction: mintAdditionalSupplyToBatch({
308+
contract,
309+
nfts: [
310+
{ tokenId: 3n, supply: 99n, to: TEST_ACCOUNT_C.address },
311+
{ tokenId: 4n, supply: 94n, to: TEST_ACCOUNT_C.address },
312+
{ tokenId: 5n, supply: 97n, to: TEST_ACCOUNT_C.address },
313+
{ tokenId: 3n, supply: 4n, to: TEST_ACCOUNT_C.address },
314+
],
315+
}),
316+
});
317+
318+
const ownedTokenIds = await getOwnedTokenIds({
319+
contract,
320+
address: TEST_ACCOUNT_C.address,
321+
});
322+
323+
expect(ownedTokenIds).toStrictEqual([
324+
{ tokenId: 3n, balance: 104n },
325+
{ tokenId: 4n, balance: 96n },
326+
{ tokenId: 5n, balance: 100n },
327+
]);
328+
});
161329
});

0 commit comments

Comments
 (0)