[SDK] More tests for NFT extensions (#4470)

<!-- start pr-codex -->

## PR-Codex overview
The focus of this PR is to add tests and functions for updating token URIs in ERC721 and ERC1155 contracts.

### Detailed summary
- Added test for updating metadata in ERC721 contract
- Added test for updating metadata in ERC1155 contract
- Created functions to update token URIs in ERC721 and ERC1155 contracts

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
This commit is contained in:
kien-ngo
2024-09-11 02:09:30 +00:00
parent 4762ca4dd9
commit 02aabbf97d
4 changed files with 142 additions and 0 deletions
@@ -0,0 +1,54 @@
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_A } 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 { getNFT } from "../read/getNFT.js";
import { mintTo } from "./mintTo.js";
import { updateTokenURI } from "./updateTokenURI.js";
const client = TEST_CLIENT;
const chain = ANVIL_CHAIN;
const account = TEST_ACCOUNT_A;
describe.runIf(process.env.TW_SECRET_KEY)("Edition: Update token uri", () => {
it("should update token uri", async () => {
const address = await deployERC1155Contract({
client,
chain,
account,
type: "TokenERC1155",
params: {
name: "Edition",
contractURI: TEST_CONTRACT_URI,
},
});
const contract = getContract({
address,
client,
chain,
});
await sendAndConfirmTransaction({
transaction: mintTo({
contract,
nft: { name: "Test1" },
to: account.address,
supply: 1n,
}),
account,
});
await sendAndConfirmTransaction({
transaction: updateTokenURI({
contract,
newMetadata: { name: "Test1 Updated" },
tokenId: 0n,
}),
account,
});
const nft = await getNFT({ contract, tokenId: 0n });
expect(nft.metadata.name).toBe("Test1 Updated");
});
});
@@ -53,4 +53,33 @@ describe.runIf(process.env.TW_SECRET_KEY)("updateMetadata ERC721", () => {
expect(nfts[1]?.metadata.name).toBe("token 1 - updated");
expect(nfts[2]?.metadata.name).toBe("token 2");
});
it("should throw if no nft uploaded", async () => {
const address = await deployERC721Contract({
client,
chain,
account,
type: "DropERC721",
params: {
name: "NFT Drop",
contractURI: TEST_CONTRACT_URI,
},
});
const contract = getContract({
address,
client,
chain,
});
const updateTx = updateMetadata({
contract,
targetTokenId: 0n,
newMetadata: { name: "token 1 - updated" },
client,
});
await expect(
sendAndConfirmTransaction({ transaction: updateTx, account }),
).rejects.toThrowError(
"No base URI set. Please set a base URI before updating metadata",
);
});
});
@@ -0,0 +1,56 @@
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_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 { getNFT } from "../read/getNFT.js";
import { mintTo } from "./mintTo.js";
import { updateTokenURI } from "./updateTokenURI.js";
const client = TEST_CLIENT;
const chain = ANVIL_CHAIN;
const account = TEST_ACCOUNT_A;
describe.runIf(process.env.TW_SECRET_KEY)(
"NFTCollection: Update token uri",
() => {
it("should update token uri", async () => {
const address = await deployERC721Contract({
client,
chain,
account,
type: "TokenERC721",
params: {
name: "NFT collection",
contractURI: TEST_CONTRACT_URI,
},
});
const contract = getContract({
address,
client,
chain,
});
await sendAndConfirmTransaction({
transaction: mintTo({
contract,
nft: { name: "Test1" },
to: account.address,
}),
account,
});
await sendAndConfirmTransaction({
transaction: updateTokenURI({
contract,
newMetadata: { name: "Test1 Updated" },
tokenId: 0n,
}),
account,
});
const nft = await getNFT({ contract, tokenId: 0n });
expect(nft.metadata.name).toBe("Test1 Updated");
});
},
);
@@ -50,6 +50,9 @@ export function updateTokenURI(
});
}
/**
* @internal
*/
export async function getUpdateTokenParams(
options: BaseTransactionOptions<UpdateTokenURIParams>,
): Promise<SetTokenURIParams> {