From 50e359d58ba001666c8b28376b5b800d06909197 Mon Sep 17 00:00:00 2001 From: kien-ngo Date: Sun, 13 Oct 2024 23:30:34 +0000 Subject: [PATCH] [SDK] Improve ERC1155 test coverage (#4985) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem solved Short description of the bug fixed or feature added --- ## PR-Codex overview This PR focuses on enhancing test coverage for the ERC20 and ERC1155 extensions in the `thirdweb` package. It introduces conditional test execution based on the presence of a secret key and adds new tests to verify specific functionalities. ### Detailed summary - Updated `describe` blocks to conditionally run tests based on `process.env.TW_SECRET_KEY` for multiple test files. - Added tests for `isLazyMintSupported` in `erc1155: lazyMint`. - Added tests for `isMintAdditionalSupplyToSupported` in `erc1155: mintAdditionalSupplyTo`. - Added tests for `mintTo` functionality in `erc1155: mintTo`. - Added tests for `isGetNFTsSupported` in `ERC1155 getNFTs`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .../extensions/erc1155/read/getNFTs.test.ts | 44 +++++++++++++++++++ .../extensions/erc1155/write/lazyMint.test.ts | 35 +++++++++++++++ .../write/mintAdditionalSupplyTo.test.ts | 38 ++++++++++++++++ .../extensions/erc1155/write/mintTo.test.ts | 43 ++++++++++++++++++ .../src/extensions/erc20/read/isERC20.test.ts | 2 +- .../erc20/write/transferBatch.test.ts | 2 +- 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 packages/thirdweb/src/extensions/erc1155/read/getNFTs.test.ts create mode 100644 packages/thirdweb/src/extensions/erc1155/write/lazyMint.test.ts create mode 100644 packages/thirdweb/src/extensions/erc1155/write/mintAdditionalSupplyTo.test.ts create mode 100644 packages/thirdweb/src/extensions/erc1155/write/mintTo.test.ts diff --git a/packages/thirdweb/src/extensions/erc1155/read/getNFTs.test.ts b/packages/thirdweb/src/extensions/erc1155/read/getNFTs.test.ts new file mode 100644 index 00000000000..4490c1c1e1e --- /dev/null +++ b/packages/thirdweb/src/extensions/erc1155/read/getNFTs.test.ts @@ -0,0 +1,44 @@ +import { type Abi, toFunctionSelector } from "viem"; +import { describe, expect, it } from "vitest"; +import { ANVIL_CHAIN } from "~test/chains.js"; +import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js"; +import { TEST_CLIENT } from "~test/test-clients.js"; +import { DROP1155_CONTRACT } from "~test/test-contracts.js"; +import { TEST_ACCOUNT_C } from "~test/test-wallets.js"; +import { resolveContractAbi } from "../../../contract/actions/resolve-abi.js"; +import { getContract } from "../../../contract/contract.js"; +import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js"; +import { isGetNFTsSupported } from "./getNFTs.js"; + +describe.runIf(process.env.TW_SECRET_KEY)("ERC1155 getNFTs", () => { + it("isGetNFTsSupported should work with our Edition Drop contracts", async () => { + const abi = await resolveContractAbi(DROP1155_CONTRACT); + const selectors = abi + .filter((f) => f.type === "function") + .map((f) => toFunctionSelector(f)); + expect(isGetNFTsSupported(selectors)).toBe(true); + }); + + it("isGetNFTsSupported should work with our Edition contracts", async () => { + const contract = getContract({ + address: await deployERC1155Contract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + params: { + name: "", + contractURI: TEST_CONTRACT_URI, + }, + type: "TokenERC1155", + account: TEST_ACCOUNT_C, + }), + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + }); + + const abi = await resolveContractAbi(contract); + const selectors = abi + .filter((f) => f.type === "function") + .map((f) => toFunctionSelector(f)); + expect(isGetNFTsSupported(selectors)).toBe(true); + }); +}); diff --git a/packages/thirdweb/src/extensions/erc1155/write/lazyMint.test.ts b/packages/thirdweb/src/extensions/erc1155/write/lazyMint.test.ts new file mode 100644 index 00000000000..a1e64b1d47c --- /dev/null +++ b/packages/thirdweb/src/extensions/erc1155/write/lazyMint.test.ts @@ -0,0 +1,35 @@ +import { type Abi, toFunctionSelector } from "viem"; +import { describe, expect, it } from "vitest"; +import { ANVIL_CHAIN } from "~test/chains.js"; +import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js"; +import { TEST_CLIENT } from "~test/test-clients.js"; +import { TEST_ACCOUNT_C } from "~test/test-wallets.js"; +import { resolveContractAbi } from "../../../contract/actions/resolve-abi.js"; +import { getContract } from "../../../contract/contract.js"; +import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js"; +import { isLazyMintSupported } from "./lazyMint.js"; + +describe.runIf(process.env.TW_SECRET_KEY)("erc1155: lazyMint", () => { + it("`isLazyMintSupported` should work with our Edition Drop contracts", async () => { + const contract = getContract({ + address: await deployERC1155Contract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + params: { + name: "", + contractURI: TEST_CONTRACT_URI, + }, + type: "DropERC1155", + account: TEST_ACCOUNT_C, + }), + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + }); + + const abi = await resolveContractAbi(contract); + const selectors = abi + .filter((f) => f.type === "function") + .map((f) => toFunctionSelector(f)); + expect(isLazyMintSupported(selectors)).toBe(true); + }); +}); diff --git a/packages/thirdweb/src/extensions/erc1155/write/mintAdditionalSupplyTo.test.ts b/packages/thirdweb/src/extensions/erc1155/write/mintAdditionalSupplyTo.test.ts new file mode 100644 index 00000000000..d73eaa7d6e7 --- /dev/null +++ b/packages/thirdweb/src/extensions/erc1155/write/mintAdditionalSupplyTo.test.ts @@ -0,0 +1,38 @@ +import { type Abi, toFunctionSelector } from "viem"; +import { describe, expect, it } from "vitest"; +import { ANVIL_CHAIN } from "~test/chains.js"; +import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js"; +import { TEST_CLIENT } from "~test/test-clients.js"; +import { TEST_ACCOUNT_C } from "~test/test-wallets.js"; +import { resolveContractAbi } from "../../../contract/actions/resolve-abi.js"; +import { getContract } from "../../../contract/contract.js"; +import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js"; +import { isMintAdditionalSupplyToSupported } from "./mintAdditionalSupplyTo.js"; + +describe.runIf(process.env.TW_SECRET_KEY)( + "erc1155: mintAdditionalSupplyTo", + () => { + it("`isMintAdditionalSupplyToSupported` should work with our Edition contracts", async () => { + const contract = getContract({ + address: await deployERC1155Contract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + params: { + name: "", + contractURI: TEST_CONTRACT_URI, + }, + type: "TokenERC1155", + account: TEST_ACCOUNT_C, + }), + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + }); + + const abi = await resolveContractAbi(contract); + const selectors = abi + .filter((f) => f.type === "function") + .map((f) => toFunctionSelector(f)); + expect(isMintAdditionalSupplyToSupported(selectors)).toBe(true); + }); + }, +); diff --git a/packages/thirdweb/src/extensions/erc1155/write/mintTo.test.ts b/packages/thirdweb/src/extensions/erc1155/write/mintTo.test.ts new file mode 100644 index 00000000000..4e21fe158a6 --- /dev/null +++ b/packages/thirdweb/src/extensions/erc1155/write/mintTo.test.ts @@ -0,0 +1,43 @@ +import {} from "viem"; +import { describe, expect, it } from "vitest"; +import { ANVIL_CHAIN } from "~test/chains.js"; +import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js"; +import { TEST_CLIENT } from "~test/test-clients.js"; +import { TEST_ACCOUNT_C } from "~test/test-wallets.js"; +import { getContract } from "../../../contract/contract.js"; +import { deployERC1155Contract } from "../../../extensions/prebuilts/deploy-erc1155.js"; +import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js"; +import { uri } from "../__generated__/IERC1155/read/uri.js"; +import { mintTo } from "./mintTo.js"; + +describe.runIf(process.env.TW_SECRET_KEY)("erc1155: mintTo", () => { + it("should mint with `nft` being declared as a string", async () => { + const contract = getContract({ + address: await deployERC1155Contract({ + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + params: { + name: "", + contractURI: TEST_CONTRACT_URI, + }, + type: "TokenERC1155", + account: TEST_ACCOUNT_C, + }), + chain: ANVIL_CHAIN, + client: TEST_CLIENT, + }); + + await sendAndConfirmTransaction({ + transaction: mintTo({ + contract, + nft: TEST_CONTRACT_URI, + to: TEST_ACCOUNT_C.address, + supply: 1n, + }), + account: TEST_ACCOUNT_C, + }); + + const tokenUri = await uri({ contract, tokenId: 0n }); + expect(tokenUri).toBe(TEST_CONTRACT_URI); + }); +}); diff --git a/packages/thirdweb/src/extensions/erc20/read/isERC20.test.ts b/packages/thirdweb/src/extensions/erc20/read/isERC20.test.ts index ae7814216b8..717b104d226 100644 --- a/packages/thirdweb/src/extensions/erc20/read/isERC20.test.ts +++ b/packages/thirdweb/src/extensions/erc20/read/isERC20.test.ts @@ -8,7 +8,7 @@ import { import { resolveContractAbi } from "../../../contract/actions/resolve-abi.js"; import { isERC20 } from "./isERC20.js"; -describe("isERC20", () => { +describe.runIf(process.env.TW_SECRET_KEY)("isERC20", () => { it("should detect USDT as a valid erc20 contract", async () => { const abi = await resolveContractAbi(USDT_CONTRACT); const selectors = abi diff --git a/packages/thirdweb/src/extensions/erc20/write/transferBatch.test.ts b/packages/thirdweb/src/extensions/erc20/write/transferBatch.test.ts index 739a4a0db52..fd1be09feb0 100644 --- a/packages/thirdweb/src/extensions/erc20/write/transferBatch.test.ts +++ b/packages/thirdweb/src/extensions/erc20/write/transferBatch.test.ts @@ -19,7 +19,7 @@ const chain = ANVIL_CHAIN; const client = TEST_CLIENT; const account = TEST_ACCOUNT_A; -describe("erc20: transferBatch", () => { +describe.runIf(process.env.TW_SECRET_KEY)("erc20: transferBatch", () => { it("should transfer tokens to multiple recipients", async () => { const address = await deployERC20Contract({ type: "TokenERC20",