import { ABICopyButton } from "./ABICopyButton"; import { CodeSegment } from "./CodeSegment"; import { CodeSnippet, Environment, SnippetApiResponse, SnippetSchema, } from "./types"; import { useContractName, useWeb3 } from "@3rdweb-sdk/react"; import { Flex, Spinner, Stack } from "@chakra-ui/react"; import { ValidContractInstance } from "@thirdweb-dev/sdk"; import { useCallback, useMemo, useState } from "react"; import { IoDocumentOutline } from "react-icons/io5"; import { useQuery } from "react-query"; import { Card, CodeBlock, Heading, LinkButton, Text } from "tw-components"; function replaceVariablesInCodeSnippet( snippet: CodeSnippet, contractAddress?: string, walletAddress?: string, ): CodeSnippet { const envs = Object.keys(snippet) as Environment[]; for (const env of envs) { if (contractAddress) { snippet[env] = snippet[env] ?.replace(/{{contract_address}}/gm, contractAddress) .replace(//gm, contractAddress); } if (walletAddress) { snippet[env] = snippet[env]?.replace( /{{wallet_address}}/gm, walletAddress, ); } } return snippet; } interface IContractCode { contract?: ValidContractInstance; } const INSTALL_COMMANDS = { typescript: "npm install @thirdweb-dev/sdk", javascript: "npm install @thirdweb-dev/sdk", react: "npm install @thirdweb-dev/react", python: "pip install thirdweb-sdk", go: "go get github.com/thirdweb-dev/go-sdk/thirdweb", }; export const ContractCode: React.FC = ({ contract }) => { const { data, isLoading } = useContractCodeSnippetQuery(); const contractName = useContractName(contract); const scopedData = useMemo(() => { return getContractSnippets(data, contractName); }, [data, contractName]); const { address } = useWeb3(); const [environment, setEnvironment] = useState("javascript"); const replaceSnippetVars = useCallback( (snip: Partial>) => replaceVariablesInCodeSnippet(snip, contract?.getAddress(), address), [address, contract], ); if (isLoading) { return ( Loading... ); } if (!scopedData) { return ( Code snippets for this contract are not yet available. Please check back for updates over the next couple of days. ); } const snippetCard = (snippet: SnippetSchema) => ( {snippet.summary} {snippet.remarks && {snippet.remarks}} {snippet.reference[environment as string] && ( } isExternal noIcon href={snippet.reference[environment as string]} variant="outline" size="sm" > Documentation )} ); return ( Getting Started First, install the latest version of the SDK. Follow along below to get started using this contract in your code. {scopedData.methods?.map((snippet) => { return snippetCard(snippet); })} {scopedData.properties?.map((snippet) => { return snippetCard(snippet); })} Contract ABI If you need the underlying contract ABI for this contract you can copy it from here. {contract && ( )} ); }; function getContractSnippets( snippets?: SnippetApiResponse, contractName?: string | null, ): SnippetSchema | null { return contractName && snippets ? snippets[contractName] : null; } function useContractCodeSnippetQuery() { return useQuery(["code-snippet"], async () => { const res = await fetch( `https://raw.githubusercontent.com/thirdweb-dev/docs/main/docs/snippets.json`, ); return (await res.json()) as SnippetApiResponse; }); }