import { useDashboardEVMChainId } from "@3rdweb-sdk/react"; import { Accordion, AccordionButton, AccordionIcon, AccordionItem, AccordionPanel, Alert, AlertDescription, AlertIcon, AlertTitle, Box, Divider, Flex, GridItem, Image, List, ListItem, Select, SimpleGrid, Tab, TabList, TabPanel, TabPanels, Tabs, useBreakpointValue, } from "@chakra-ui/react"; import { Chain, defaultChains } from "@thirdweb-dev/chains"; import { useAddress } from "@thirdweb-dev/react"; import { Abi, AbiEvent, AbiFunction, FeatureWithEnabled, } from "@thirdweb-dev/sdk"; import { useContractEnabledExtensions, useContractEvents, useContractFunctions, useFeatureContractCodeSnippetQuery, } from "components/contract-components/hooks"; import { CodeSegment } from "components/contract-tabs/code/CodeSegment"; import { CodeEnvironment, SnippetApiResponse, } from "components/contract-tabs/code/types"; import { DASHBOARD_THIRDWEB_CLIENT_ID } from "constants/rpc"; import { constants } from "ethers"; import { useSupportedChain } from "hooks/chains/configureChains"; import { useSingleQueryParam } from "hooks/useQueryParam"; import { useRouter } from "next/router"; import { useMemo, useState } from "react"; import { Button, Card, Heading, Link, Text, TrackedLink } from "tw-components"; interface CodeOverviewProps { abi?: Abi; contractAddress?: string; onlyInstall?: boolean; chain?: Chain; noSidebar?: boolean; } const COMMANDS = { install: { javascript: "npm install @thirdweb-dev/sdk ethers@5", react: "npm install @thirdweb-dev/react @thirdweb-dev/sdk ethers@5", "react-native": "React Native", web3button: "", python: "pip install thirdweb-sdk", go: "go get github.com/thirdweb-dev/go-sdk/thirdweb", unity: `// Download the .unitypackage from the latest release: // https://github.com/thirdweb-dev/unity-sdk/releases // and drag it into your project`, }, setup: { javascript: `import {{chainName}} from "@thirdweb-dev/chains"; import { ThirdwebSDK } from "@thirdweb-dev/sdk"; // If used on the FRONTEND pass your 'clientId' const sdk = new ThirdwebSDK({{chainName}}, { clientId: "YOUR_CLIENT_ID", }); // --- OR --- // If used on the BACKEND pass your 'secretKey' const sdk = new ThirdwebSDK({{chainName}}, { secretKey: "YOUR_SECRET_KEY", }); const contract = await sdk.getContract("{{contract_address}}");`, react: `import {{chainName}} from "@thirdweb-dev/chains"; import { ThirdwebProvider, useContract } from "@thirdweb-dev/react"; function App() { return ( ) } function Component() { const { contract, isLoading } = useContract("{{contract_address}}"); }`, "react-native": `import {{chainName}} from "@thirdweb-dev/chains"; import { ThirdwebProvider, useContract } from "@thirdweb-dev/react-native"; function App() { return ( ) } function Component() { const { contract, isLoading } = useContract("{{contract_address}}"); }`, web3button: ``, python: `from thirdweb import ThirdwebSDK sdk = ThirdwebSDK("{{chainNameOrRpc}}", options=SDKOptions(secret_key="YOUR_SECRET_KEY")) contract = sdk.get_contract("{{contract_address}}")`, go: `import "github.com/thirdweb-dev/go-sdk/thirdweb" sdk, err := thirdweb.NewThirdwebSDK("{{chainNameOrRpc}}", &thirdweb.SDKOptions{ SecretKey: "YOUR_SECRET_KEY", }) contract, err := sdk.GetContract("{{contract_address}}") `, unity: `using Thirdweb; // Reference the SDK var sdk = ThirdwebManager.Instance.SDK; // Get your contract var contract = sdk.GetContract("{{contract_address}}");`, }, read: { javascript: `const data = await contract.call("{{function}}", [{{args}}])`, react: `import { useContract, useContractRead } from "@thirdweb-dev/react"; export default function Component() { const { contract } = useContract("{{contract_address}}"); const { data, isLoading } = useContractRead(contract, "{{function}}", [{{args}}]) }`, "react-native": `import { useContract, useContractRead } from "@thirdweb-dev/react-native"; export default function Component() { const { contract } = useContract("{{contract_address}}"); const { data, isLoading } = useContractRead(contract, "{{function}}", [{{args}}]) }`, python: `data = contract.call("{{function}}", {{args}})`, go: `data, err := contract.Call("{{function}}", {{args}})`, }, write: { javascript: `const data = await contract.call("{{function}}", [{{args}}])`, react: `import { useContract, useContractWrite } from "@thirdweb-dev/react"; export default function Component() { const { contract } = useContract("{{contract_address}}"); const { mutateAsync: {{function}}, isLoading } = useContractWrite(contract, "{{function}}") const call = async () => { try { const data = await {{function}}({ args: [{{args}}] }); console.info("contract call successs", data); } catch (err) { console.error("contract call failure", err); } } }`, "react-native": `import { useContract, useContractWrite } from "@thirdweb-dev/react-native"; export default function Component() { const { contract } = useContract("{{contract_address}}"); const { mutateAsync: {{function}}, isLoading } = useContractWrite(contract, "{{function}}") const call = async () => { try { const data = await {{function}}({ args: [{{args}}] }); console.info("contract call successs", data); } catch (err) { console.error("contract call failure", err); } } }`, web3button: `import { Web3Button } from "@thirdweb-dev/react"; export default function Component() { return ( { contract.call("{{function}}", [{{args}}]) }} > {{function}} ) }`, python: `data = contract.call("{{function}}", {{args}})`, go: `data, err := contract.Call("{{function}}", {{args}})`, }, events: { javascript: `// You can get a specific event const events = await contract.events.getEvents("{{function}}") // All events const allEvents = await contract.events.getAllEvents(); // Or set up a listener for all events const listener = await contract.events.listenToAllEvents();`, react: `import { useContract, useContractEvents } from "@thirdweb-dev/react"; export default function Component() { const { contract } = useContract("{{contract_address}}"); // You can get a specific event const { data: event } = useContractEvents(contract, "{{function}}") // All events const { data: allEvents } = useContractEvents(contract) // By default, you set up a listener for all events, but you can disable it const { data: eventWithoutListener } = useContractEvents(contract, undefined, { subscribe: false }) }`, "react-native": `import { useContract, useContractEvents } from "@thirdweb-dev/react-native"; export default function Component() { const { contract } = useContract("{{contract_address}}"); // You can get a specific event const { data: event } = useContractEvents(contract, "{{function}}") // All events const { data: allEvents } = useContractEvents(contract) // By default, you set up a listener for all events, but you can disable it const { data: eventWithoutListener } = useContractEvents(contract, undefined, { subscribe: false }) }`, python: `events = contract.get_events("{{function}}", {{args}})`, go: `events, err := contract.GetEvents("{{function}}", {{args}})`, }, }; const WALLETS_SNIPPETS = [ { id: "smart-wallet", name: "Account Abstraction", description: "Deploy accounts for your users", iconUrl: "ipfs://QmeAJVqn17aDNQhjEU3kcWVZCFBrfta8LzaDGkS8Egdiyk/smart-wallet.svg", link: "https://portal.thirdweb.com/references/wallets/latest/SmartWallet", supportedLanguages: { javascript: `import {{chainName}} from "@thirdweb-dev/chains"; import { LocalWallet, SmartWallet } from "@thirdweb-dev/wallets"; // First, connect the personal wallet, which can be any wallet (metamask, walletconnect, etc.) // Here we're just generating a new local wallet which can be saved later const personalWallet = new LocalWallet(); await personalWallet.generate(); // Setup the Account Abstraction configuration const config = { chain: {{chainName}}, // the chain where your account will be or is deployed factoryAddress: "{{factory_address}}", // your own deployed account factory address clientId: "YOUR_CLIENT_ID", // or use secretKey for backend/node scripts gasless: true, // enable or disable gasless transactions }; // Then, connect the Account const wallet = new SmartWallet(config); await wallet.connect({ personalWallet, });`, react: `import {{chainName}} from "@thirdweb-dev/chains"; import { ThirdwebProvider, ConnectWallet, smartWallet } from "@thirdweb-dev/react"; export default function App() { return ( ); }`, "react-native": `import {{chainName}} from "@thirdweb-dev/chains"; import { ThirdwebProvider, ConnectWallet, smartWallet } from "@thirdweb-dev/react-native"; export default function App() { return ( ); }`, unity: `using Thirdweb; public async void ConnectWallet() { // Reference to your Thirdweb SDK var sdk = ThirdwebManager.Instance.SDK; // Configure the connection var connection = new WalletConnection( provider: WalletProvider.SmartWallet, // The wallet provider you want to connect to (Required) chainId: 1, // The chain you want to connect to (Required) password: "myEpicPassword", // If using a local wallet as personal wallet (Optional) email: "email@email.com", // If using an email wallet as personal wallet (Optional) personalWallet: WalletProvider.LocalWallet // The personal wallet you want to use with your Account (Optional) ); // Connect the wallet string address = await sdk.wallet.Connect(connection); }`, }, }, ]; function getExportName(slug: string) { let exportName = slug .split("-") .map((s) => s[0].toUpperCase() + s.slice(1)) .join(""); // if chainName starts with a number, prepend an underscore if (exportName.match(/^[0-9]/)) { exportName = `_${exportName}`; } return exportName; } interface SnippetOptions { contractAddress?: string; fn?: string; args?: string[]; chainName?: string; rpcUrl?: string; address?: string; clientId?: string; } function formatSnippet( snippet: Record, { contractAddress, fn, args, chainName, rpcUrl, address, clientId, }: SnippetOptions, ) { const code = { ...snippet }; const preSupportedSlugs = defaultChains.map((chain) => chain.slug); for (const key of Object.keys(code)) { const env = key as CodeEnvironment; code[env] = code[env] ?.replace(/{{contract_address}}/gm, contractAddress || "0x...") ?.replace(/{{factory_address}}/gm, contractAddress || "0x...") ?.replace(/{{wallet_address}}/gm, address) ?.replace("YOUR_CLIENT_ID", clientId || "YOUR_CLIENT_ID") ?.replace( 'import {{chainName}} from "@thirdweb-dev/chains";', preSupportedSlugs.includes(chainName as any) ? "" : `import ${env === "javascript" ? "{ {{chainName}} }" : "{{chainName}}" } from "@thirdweb-dev/chains";`, ) ?.replace( /{{chainName}}/gm, !chainName || chainName?.startsWith("0x") || chainName?.endsWith(".eth") ? '"ethereum"' : preSupportedSlugs.includes(chainName as any) ? `"${chainName}"` : env === "javascript" ? getExportName(chainName) : `{ ${getExportName(chainName)} }`, ) ?.replace(/{{function}}/gm, fn || "") ?.replace( /{{chainNameOrRpc}}/gm, preSupportedSlugs.includes(chainName as any) ? chainName : rpcUrl?.replace( // eslint-disable-next-line no-template-curly-in-string "${THIRDWEB_API_KEY}", DASHBOARD_THIRDWEB_CLIENT_ID, ) || "", ); if (args && args?.some((arg) => arg)) { code[env] = code[env]?.replace(/{{args}}/gm, args?.join(", ") || ""); } else { code[env] = code[env] ?.replace(", {{args}}", "") ?.replace("{{args}}, ", ""); } } return code; } export const CodeOverview: React.FC = ({ abi, contractAddress = constants.AddressZero, onlyInstall = false, chain, noSidebar = false, }) => { const defaultEnvironment = useSingleQueryParam( "environment", ) as CodeEnvironment; const [environment, setEnvironment] = useState( defaultEnvironment || "javascript", ); const router = useRouter(); const [tab, setTab] = useState("write"); const { data } = useFeatureContractCodeSnippetQuery(environment); const enabledExtensions = useContractEnabledExtensions(abi); const address = useAddress(); const isMobile = useBreakpointValue({ base: true, md: false }); const isAccountFactory = enabledExtensions.some( (extension) => extension.name === "AccountFactory", ); const filteredData = useMemo(() => { if (!data) { return {}; } return filterData(data, enabledExtensions); }, [data, enabledExtensions]); const foundExtensions = useMemo(() => { return Object.keys(filteredData || {}).sort(); }, [filteredData]); const chainId = useDashboardEVMChainId(); const chainInfo = useSupportedChain(chainId || -1); const chainName = chain?.slug || chainInfo?.slug; const rpc = chain?.rpc[0] || chainInfo?.rpc[0]; const functions = useContractFunctions(abi as Abi); const events = useContractEvents(abi as Abi); const { readFunctions, writeFunctions } = useMemo(() => { return { readFunctions: functions?.filter( (f) => f.stateMutability === "view" || f.stateMutability === "pure", ), writeFunctions: functions?.filter( (f) => f.stateMutability !== "view" && f.stateMutability !== "pure", ), }; }, [functions]); const [read, setRead] = useState( readFunctions && readFunctions.length > 0 ? readFunctions[0] : undefined, ); const [write, setWrite] = useState( writeFunctions && writeFunctions.length > 0 ? writeFunctions[0] : undefined, ); const [event, setEvent] = useState( events && events.length > 0 ? events[0] : undefined, ); return ( {isAccountFactory && ( Integrate your account factory Account Factory The recommended way to use account factories is to integrate the{" "} Wallet SDK {" "} in your applications. This will ensure account contracts are deployed for your users only when they need it. w.id === "smart-wallet") ?.supportedLanguages || {}) as any, { contractAddress, chainName, address, }, )} hideTabs /> )} {isAccountFactory ? "Direct contract interaction (advanced)" : chain ? `Getting Started with ${chain.name}` : "Getting Started"} {(noSidebar || isMobile) && ( Choose a language: )} {environment === "react-native" || environment === "unity" ? ( Install the latest version of the SDK.
Learn how in the{" "} {environment === "react-native" ? "React Native" : "Unity"}{" "} documentation .
) : ( <> Install the latest version of the SDK: )}
Initialize the SDK and contract on your project: You will need to pass a client ID/secret key to use thirdweb's infrastructure services. If you don't have any API keys yet you can create one for free from the{" "} dashboard settings .
{!onlyInstall ? ( <> {foundExtensions.length > 0 ? ( {foundExtensions.map((extension) => { const extensionData: any[] = filteredData[ extension ] as unknown as any[]; return ( Extension detected Extension {extension} {extensionData?.map((ext) => { return ( {ext.summary} ); })} ); })} ) : null} All Functions & Events {((writeFunctions || []).length > 0 || (readFunctions || []).length > 0) && ( {(writeFunctions || []).length > 0 && ( Write )} {(readFunctions || []).length > 0 && ( Read )} {(events || []).length > 0 && ( Events )} {writeFunctions?.map((fn) => ( ))} {readFunctions?.map((fn) => ( ))} {events?.map((ev) => ( ))} )} f.name === (tab === "read" ? read?.name : tab === "write" ? write?.name : event?.name), ) ?.inputs?.map((i) => i.name), chainName, }, )} /> ) : null}
{noSidebar || isMobile ? null : ( Choose a language: Getting Started {foundExtensions.map((ext) => ( Extension detected {ext} ))} All Functions & Events )}
); }; function filterData( data: SnippetApiResponse, enabledExtensions: FeatureWithEnabled[], ) { const allowedKeys = enabledExtensions .filter((extension) => extension.enabled) .map((extension) => extension.name as keyof SnippetApiResponse); const filteredData: Partial = {}; for (const key in data) { if (allowedKeys.includes(key as keyof SnippetApiResponse)) { filteredData[key as keyof SnippetApiResponse] = data[key]; } } return filteredData; }