Skip to content

Instantly share code, notes, and snippets.

@gregfromstl
Created May 1, 2024 06:56
Show Gist options
  • Save gregfromstl/36389da96a6c3b420cc3a5c9aa537fc8 to your computer and use it in GitHub Desktop.
Save gregfromstl/36389da96a6c3b420cc3a5c9aa537fc8 to your computer and use it in GitHub Desktop.
Bulk update token metadata
import {
createThirdwebClient,
type Address,
getContract,
prepareContractCall,
sendAndConfirmTransaction,
} from "thirdweb";
import { privateKeyToAccount } from "thirdweb/wallets";
import { upload } from "thirdweb/storage";
import { sepolia } from "thirdweb/chains";
const client = createThirdwebClient({
secretKey: YOUR_SECRET_KEY, // get it from https://thirdweb.com/dashboard/settings/api-keys
});
// This should be the admin account on your contract
const eoa = privateKeyToAccount({
privateKey: process.env.PRIVATE_KEY as Address,
client,
});
// Update this to return the desired metadata based on the token ID
const generateMetadata = (i: number) => ({
name: `Token ${i}`,
description: `Description for token ${i}`,
image: `https://images.unsplash.com/photo-1711994872181-1e112e5e18e0?q=80`,
});
// Update this to however many token IDs you have
const files = [...Array(10).keys()].map(
(i) => new File([JSON.stringify(generateMetadata(i))], `${i}`)
);
const uris = await upload({
client,
files,
});
const contract = getContract({
client,
address: "0x123",
chain: sepolia,
});
await Promise.all(
uris.map(async (uri, tokenId) => {
const tx = prepareContractCall({
contract,
method: "function setTokenURI(uint256 tokenId, string memory uri)",
params: [BigInt(tokenId), uri],
});
try {
await sendAndConfirmTransaction({
transaction: tx,
account: eoa,
});
} catch (error) {
console.error(error);
}
})
);
console.log("Done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment