How to add a contract to the thirdweb dashboard via code
In this guide, we will learn how to programmatically add a contract to your thidweb dashboard using the TypeScript SDK (v5).
Note: You will require matic on polygon mainnet network to add your contract to the registry
If you don’t have a `secretKey` you can follow this guide to create one
import { config } from "dotenv"; import { createThirdwebClient, getContract, prepareContractCall, resolveMethod, sendAndConfirmTransaction, } from "thirdweb"; import { contractURI } from "thirdweb/extensions/common"; import { defineChain } from "thirdweb/chains"; import { privateKeyToAccount } from "thirdweb/wallets"; config(); const chainId = ; // chain Id for the contract you want to add const walletAddress = ""; // wallet address for which you would like to add this to thirdweb dashboard const address = ""; // contract address to add to the dashboard const main = async () => { if (!process.env.WALLET_PRIVATE_KEY) { throw new Error("No private key found"); } if (!process.env.THIRDWEB_SECRET_KEY) { throw new Error("No THIRDWEB_SECRET_KEY found"); } try { const client = createThirdwebClient({ secretKey: process.env.THIRDWEB_SECRET_KEY, }); const account = privateKeyToAccount({ client, privateKey: process.env.WALLET_PRIVATE_KEY, }); // private key account async function addContractToThirdwebDashboard( thirdwebClient: any, // Update the type to 'any' for now contractAddressToAdd: string, walletAddresstoAdd: string, // Contract will show up to this wallet's dashboard contractChainId: number, account: any, // Update the type to 'any' for now ) { const thirdwebMultichainRegistry = getContract({ client: thirdwebClient, // Update the type to 'any' chain: defineChain(137), address: "0xcdAD8FA86e18538aC207872E8ff3536501431B73", }); const contractToAdd = getContract({ client: thirdwebClient, // Update the type to 'any' chain: defineChain(contractChainId), address: contractAddressToAdd, }); const metadataURI = await contractURI({ contract: contractToAdd, }); const transaction = prepareContractCall({ contract: thirdwebMultichainRegistry, method: resolveMethod("add"), params: [ walletAddresstoAdd, contractAddressToAdd, contractChainId, metadataURI !== undefined ? metadataURI : "", ], }); const receipt = await sendAndConfirmTransaction({ account, transaction, }); return receipt; } // Call the function and pass the 'client' and 'account' variables await addContractToThirdwebDashboard(client, address, walletAddress, chainId, account); } catch (err) { console.error("Something went wrong: ", err); } }; main();
Simply replace the specified values in the code above, add your .env
details and execute it to add the contract to your dashboard!
const chainId = ; // chain Id for the contract you want to add const walletAddress = ""; // wallet address for which you would like to add this to thirdweb dashboard const address = ""; // contract address to add to the dashboard
.env
file details
THIRDWEB_SECRET_KEY=XXX WALLET_PRIVATE_KEY=XXX
That's all there is to it. It's that simple to add a contract to the dashboard using the thirdweb V5 SDK.
Can’t get this working? If you’ve followed the above and still have issues, contact our support team for help.
Did this answer your question?
😞
😐
🤩