Skip to content

Commit

Permalink
[SDK] Improve ERC1155 test coverage (#4985)
Browse files Browse the repository at this point in the history
## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## 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}`

<!-- end pr-codex -->
  • Loading branch information
kien-ngo committed Oct 13, 2024
1 parent a1a6574 commit 50e359d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 2 deletions.
44 changes: 44 additions & 0 deletions packages/thirdweb/src/extensions/erc1155/read/getNFTs.test.ts
Original file line number Diff line number Diff line change
@@ -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<Abi>(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<Abi>(contract);
const selectors = abi
.filter((f) => f.type === "function")
.map((f) => toFunctionSelector(f));
expect(isGetNFTsSupported(selectors)).toBe(true);
});
});
35 changes: 35 additions & 0 deletions packages/thirdweb/src/extensions/erc1155/write/lazyMint.test.ts
Original file line number Diff line number Diff line change
@@ -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<Abi>(contract);
const selectors = abi
.filter((f) => f.type === "function")
.map((f) => toFunctionSelector(f));
expect(isLazyMintSupported(selectors)).toBe(true);
});
});
Original file line number Diff line number Diff line change
@@ -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<Abi>(contract);
const selectors = abi
.filter((f) => f.type === "function")
.map((f) => toFunctionSelector(f));
expect(isMintAdditionalSupplyToSupported(selectors)).toBe(true);
});
},
);
43 changes: 43 additions & 0 deletions packages/thirdweb/src/extensions/erc1155/write/mintTo.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<Abi>(USDT_CONTRACT);
const selectors = abi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 50e359d

Please sign in to comment.