Skip to content

Instantly share code, notes, and snippets.

@warengonzaga
Last active May 17, 2024 07:12
Show Gist options
  • Save warengonzaga/0b516203d9ebd65cc200ca359675f997 to your computer and use it in GitHub Desktop.
Save warengonzaga/0b516203d9ebd65cc200ca359675f997 to your computer and use it in GitHub Desktop.
method to add contract to dashboard
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 = 84532;
const walletAddress = "0x21d514c90ee4E4e4Cd16Ce9185BF01F0F1eE4A04";
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
const address = "0x90Ad6ACb44A36227EEFDB12AA2D2f254F9eD968B"; // contract address to add
/**
* Add a contract to the Thirdweb dashboard
* @param thirdwebClient - Thirdweb client
* @param contractAddressToAdd - Contract address to add
* @param walletAddresstoAdd - Wallet address to add the contract to
* @param contractChainId - Chain ID of the contract
* @param account - Account to send the transaction. Account means the wallet that is sending the transaction
*
* @returns - Transaction receipt
*
* @remarks - This function adds a contract to the Thirdweb dashboard
*
*/
async function addContractToThirdwebDashboard(
thirdwebClient: any,
contractAddressToAdd: string,
walletAddresstoAdd: string,
contractChainId: number,
account: any,
) {
const thirdwebMultichainRegistry = getContract({
client: thirdwebClient,
chain: defineChain(137), // don't touch this
address: "0xcdAD8FA86e18538aC207872E8ff3536501431B73", // don't touch this
});
const contractToAdd = getContract({
client: thirdwebClient,
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment