Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/thirdweb/src/extensions/erc1155/drop1155.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-co
import { resolvePromisedValue } from "../../utils/promise/resolve-promised-value.js";
import { toEther } from "../../utils/units.js";
import { generateMerkleTreeInfoERC1155 } from "../airdrop/write/merkleInfoERC1155.js";
import { name } from "../common/read/name.js";
import { deployERC20Contract } from "../prebuilts/deploy-erc20.js";
import { deployERC1155Contract } from "../prebuilts/deploy-erc1155.js";
import { balanceOf } from "./__generated__/IERC1155/read/balanceOf.js";
Expand Down Expand Up @@ -45,12 +46,22 @@ describe.runIf(process.env.TW_SECRET_KEY)(
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
params: {
name: "Test DropERC1155",
name: "EditionDrop",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can You still pass a contract uri here? Was useful to skip the ipsy upload

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added back

contractURI: TEST_CONTRACT_URI,
},
type: "DropERC1155",
});

expect(contractAddress).toBeDefined();
const deployedName = await name({
contract: getContract({
client: TEST_CLIENT,
chain: ANVIL_CHAIN,
address: contractAddress,
}),
});
expect(deployedName).toBe("EditionDrop");

contract = getContract({
address: contractAddress,
chain: ANVIL_CHAIN,
Expand Down
13 changes: 12 additions & 1 deletion packages/thirdweb/src/extensions/erc1155/token1155.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { resolveContractAbi } from "../../contract/actions/resolve-abi.js";
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
import { name } from "../common/read/name.js";
import { deployERC1155Contract } from "../prebuilts/deploy-erc1155.js";
import { balanceOf } from "./__generated__/IERC1155/read/balanceOf.js";
import { totalSupply } from "./__generated__/IERC1155/read/totalSupply.js";
Expand All @@ -37,12 +38,22 @@ describe.runIf(process.env.TW_SECRET_KEY)("TokenERC1155", () => {
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
params: {
name: "Test TokenERC1155",
name: "Edition",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Also this tests the exact same thing no? Doesn't actually help coverage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added back

contractURI: TEST_CONTRACT_URI,
},
type: "TokenERC1155",
});

expect(contractAddress).toBeDefined();
const deployedName = await name({
contract: getContract({
client: TEST_CLIENT,
chain: ANVIL_CHAIN,
address: contractAddress,
}),
});
expect(deployedName).toBe("Edition");

contract = getContract({
address: contractAddress,
chain: ANVIL_CHAIN,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {} from "viem";
import { describe, expect, it } from "vitest";
import { TEST_ACCOUNT_C } from "~test/test-wallets.js";
import { optimizeMintBatchContent } from "./mintAdditionalSupplyToBatch.js";
Expand Down
13 changes: 11 additions & 2 deletions packages/thirdweb/src/extensions/erc20/drop20.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { beforeAll, describe, expect, it } from "vitest";
import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js";
import { VITALIK_WALLET } from "../../../test/src/addresses.js";
import { ANVIL_CHAIN } from "../../../test/src/chains.js";
import { TEST_CLIENT } from "../../../test/src/test-clients.js";
Expand All @@ -13,6 +12,7 @@ import { type ThirdwebContract, getContract } from "../../contract/contract.js";
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
import { resolvePromisedValue } from "../../utils/promise/resolve-promised-value.js";
import { toEther } from "../../utils/units.js";
import { name } from "../common/read/name.js";
import { deployERC20Contract } from "../prebuilts/deploy-erc20.js";
import { getClaimConditions } from "./drops/read/getClaimConditions.js";
import { claimTo } from "./drops/write/claimTo.js";
Expand All @@ -35,11 +35,20 @@ describe.runIf(process.env.TW_SECRET_KEY)(
client: TEST_CLIENT,
params: {
name: "Test DropERC20",
contractURI: TEST_CONTRACT_URI,
},
type: "DropERC20",
});

expect(contractAddress).toBeDefined();
const deployedName = await name({
contract: getContract({
client: TEST_CLIENT,
chain: ANVIL_CHAIN,
address: contractAddress,
}),
});
expect(deployedName).toBe("Test DropERC20");

contract = getContract({
address: contractAddress,
chain: ANVIL_CHAIN,
Expand Down
17 changes: 10 additions & 7 deletions packages/thirdweb/src/extensions/erc721/drop721.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { type ThirdwebContract, getContract } from "../../contract/contract.js";
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
import { resolvePromisedValue } from "../../utils/promise/resolve-promised-value.js";
import { toEther } from "../../utils/units.js";
import { name } from "../common/read/name.js";
import { deployERC20Contract } from "../prebuilts/deploy-erc20.js";
import { deployERC721Contract } from "../prebuilts/deploy-erc721.js";
import { balanceOf } from "./__generated__/IERC721A/read/balanceOf.js";
Expand Down Expand Up @@ -40,11 +41,19 @@ describe.runIf(process.env.TW_SECRET_KEY)(
client: TEST_CLIENT,
params: {
name: "Test DropERC721",
contractURI: TEST_CONTRACT_URI,
},
type: "DropERC721",
});

contract = getContract({
address: contractAddress,
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
});

const deployedName = await name({ contract });
expect(deployedName).toBe("Test DropERC721");

const erc20ContractAddress = await deployERC20Contract({
account: TEST_ACCOUNT_A,
chain: ANVIL_CHAIN,
Expand All @@ -61,12 +70,6 @@ describe.runIf(process.env.TW_SECRET_KEY)(
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
});

contract = getContract({
address: contractAddress,
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
});
// this deploys a contract, it may take some time
}, 120_000);

Expand Down
67 changes: 0 additions & 67 deletions packages/thirdweb/src/extensions/erc721/read/getNFT.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
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 { DOODLES_CONTRACT } from "~test/test-contracts.js";
import { TEST_ACCOUNT_A } from "~test/test-wallets.js";
import { getContract } from "../../../contract/contract.js";
import { deployERC721Contract } from "../../../extensions/prebuilts/deploy-erc721.js";
import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js";
import { parseNFT } from "../../../utils/nft/parseNft.js";
import { setTokenURI } from "../__generated__/INFTMetadata/write/setTokenURI.js";
import { mintTo } from "../write/mintTo.js";
import { getNFT } from "./getNFT.js";

describe.runIf(process.env.TW_SECRET_KEY)("erc721.getNFT", () => {
Expand Down Expand Up @@ -99,60 +88,4 @@ describe.runIf(process.env.TW_SECRET_KEY)("erc721.getNFT", () => {
}
`);
});

it("should return a default value if the URI of the token doesn't exist", async () => {
/**
* To create this test scenario, we first deploy an NFTCollection/Edition contract,
* mint a token, then purposefully change that token's URI to an empty string, using setTokenURI
*/
const contract = getContract({
address: await deployERC721Contract({
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
account: TEST_ACCOUNT_A,
type: "TokenERC721",
params: {
name: "",
contractURI: TEST_CONTRACT_URI,
},
}),
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
});

await sendAndConfirmTransaction({
transaction: mintTo({
contract,
nft: { name: "token 0" },
to: TEST_ACCOUNT_A.address,
}),
account: TEST_ACCOUNT_A,
});

await sendAndConfirmTransaction({
transaction: setTokenURI({
contract,
tokenId: 0n,
// Need to have some spaces because NFTMetadata.sol does not allow to update an empty valud
uri: " ",
}),
account: TEST_ACCOUNT_A,
});

expect(await getNFT({ contract, tokenId: 0n })).toStrictEqual(
parseNFT(
{
id: 0n,
type: "ERC721",
uri: "",
},
{
tokenId: 0n,
tokenUri: "",
type: "ERC721",
owner: null,
},
),
);
});
});
32 changes: 0 additions & 32 deletions packages/thirdweb/src/extensions/erc721/read/getNFTs.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
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 {
DOODLES_CONTRACT,
UNISWAPV3_FACTORY_CONTRACT,
} from "~test/test-contracts.js";
import { TEST_ACCOUNT_B } from "~test/test-wallets.js";
import { resolveContractAbi } from "../../../contract/actions/resolve-abi.js";
import { getContract } from "../../../contract/contract.js";
import { deployERC721Contract } from "../../../extensions/prebuilts/deploy-erc721.js";
import { isGetNFTSupported } from "./getNFT.js";
import { getNFTs } from "./getNFTs.js";

describe.runIf(process.env.TW_SECRET_KEY)("erc721.getNFTs", () => {
Expand Down Expand Up @@ -197,29 +188,6 @@ describe.runIf(process.env.TW_SECRET_KEY)("erc721.getNFTs", () => {
// TODO find a contract that we can use that has "1 indexed" NFTs, then re-enable this test
});

it("isGetNFTsSupported should work", async () => {
const contract = getContract({
address: await deployERC721Contract({
chain: ANVIL_CHAIN,
client: TEST_CLIENT,
type: "TokenERC721",
params: {
name: "",
contractURI: TEST_CONTRACT_URI,
},
account: TEST_ACCOUNT_B,
}),
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(isGetNFTSupported(selectors)).toBe(true);
});

it("should throw error if totalSupply and nextTokenIdToMint are not supported", async () => {
await expect(() =>
getNFTs({ contract: UNISWAPV3_FACTORY_CONTRACT }),
Expand Down
115 changes: 115 additions & 0 deletions packages/thirdweb/src/extensions/erc721/token721.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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_B } from "~test/test-wallets.js";
import { resolveContractAbi } from "../../contract/actions/resolve-abi.js";
import { type ThirdwebContract, getContract } from "../../contract/contract.js";
import { name } from "../../extensions/common/read/name.js";
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
import { parseNFT } from "../../utils/nft/parseNft.js";
import { deployERC721Contract } from "../prebuilts/deploy-erc721.js";
import { tokenURI } from "./__generated__/IERC721A/read/tokenURI.js";
import { setTokenURI } from "./__generated__/INFTMetadata/write/setTokenURI.js";
import { getNFT, isGetNFTSupported } from "./read/getNFT.js";
import { mintTo } from "./write/mintTo.js";

const client = TEST_CLIENT;
const chain = ANVIL_CHAIN;
const account = TEST_ACCOUNT_B;

let token721Contract: ThirdwebContract;

describe.runIf(process.env.TW_SECRET_KEY)("deployERC721", () => {
it("should deploy ERC721 token", async () => {
const address = await deployERC721Contract({
client,
chain,
account,
type: "TokenERC721",
params: {
name: "NFTCollection",
contractURI: TEST_CONTRACT_URI,
},
});
expect(address).toBeDefined();
token721Contract = getContract({
client: TEST_CLIENT,
chain: ANVIL_CHAIN,
address,
});
const deployedName = await name({ contract: token721Contract });
expect(deployedName).toBe("NFTCollection");
});

it("should mint an nft when passed an object", async () => {
const transaction = mintTo({
contract: token721Contract,
nft: { name: "token 0" },
to: account.address,
});
await sendAndConfirmTransaction({
transaction,
account,
});

const nft = await getNFT({ contract: token721Contract, tokenId: 0n });
expect(nft.metadata.name).toBe("token 0");
});

it("should mint an nft when passed a string", async () => {
const transaction = mintTo({
contract: token721Contract,
nft: "ipfs://fake-token-uri",
to: account.address,
});
await sendAndConfirmTransaction({
transaction,
account,
});
const _uri = await tokenURI({ contract: token721Contract, tokenId: 1n });
expect(_uri).toBe("ipfs://fake-token-uri");
});

it("isGetNFTsSupported should work", async () => {
const abi = await resolveContractAbi<Abi>(token721Contract);
const selectors = abi
.filter((f) => f.type === "function")
.map((f) => toFunctionSelector(f));
expect(isGetNFTSupported(selectors)).toBe(true);
});

it("should return a default value if the URI of the token doesn't exist", async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the value of moving these tests around? Feels like unnecessary work. Would you rather focus on actually writing new tests on areas that aren't covered.

You can check the uncovered areas on codecov, any file/folder with low coverage is what you should focus on.

All of react for example is particularly low

Copy link
Contributor Author

@kien-ngo kien-ngo Nov 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to put the tests for token standards into the following structures:
|- erc20
|-- drop20.test.ts
|-- token20.test.ts
|
|- erc721
|-- drop721.test.ts
|-- token721.test.ts
|
|- erc1155
|-- drop1155.test.ts
|-- token1155.test.ts

to speed up the tests in general and (somehow) avoid the issue with out-of-gas + timeout

/**
* mint a token, then purposefully change that token's URI to an empty string, using setTokenURI
*/
await sendAndConfirmTransaction({
transaction: setTokenURI({
contract: token721Contract,
tokenId: 1n,
// Need to have some spaces because NFTMetadata.sol does not allow to update an empty value
uri: " ",
}),
account,
});

expect(
await getNFT({ contract: token721Contract, tokenId: 1n }),
).toStrictEqual(
parseNFT(
{
id: 1n,
type: "ERC721",
uri: "",
},
{
tokenId: 1n,
tokenUri: "",
type: "ERC721",
owner: null,
},
),
);
});
});
Loading
Loading