import { CodeSegment } from "./CodeSegment"; import { CodeEnvironment, CodeSnippet, SnippetApiResponse, SnippetSchema, } from "./types"; import { usePascalCaseContractName } from "@3rdweb-sdk/react"; import { Flex, Spinner, Stack } from "@chakra-ui/react"; import { useWallet } from "@solana/wallet-adapter-react"; import { useQuery } from "@tanstack/react-query"; import { useAddress, useContract } from "@thirdweb-dev/react"; import { Abi } from "@thirdweb-dev/sdk"; import { CodeOverview } from "contract-ui/tabs/code/components/code-overview"; import { constants } from "ethers"; import { useCallback, useMemo, useState } from "react"; import { IoDocumentOutline } from "react-icons/io5"; import { Card, CodeBlock, Heading, LinkButton, Text } from "tw-components"; function replaceVariablesInCodeSnippet( snippet: CodeSnippet, contractAddress?: string, walletAddress?: string, chainName?: string, ): CodeSnippet { const envs = Object.keys(snippet) as CodeEnvironment[]; for (const env of envs) { if (contractAddress) { snippet[env] = snippet[env] ?.replace(/{{contract_address}}/gm, contractAddress) ?.replace(/{{program_address}}/gm, contractAddress) ?.replace(/{{chainName}}/gm, chainName || "goerli") .replace(//gm, contractAddress); } if (walletAddress) { snippet[env] = snippet[env]?.replace( /{{wallet_address}}/gm, walletAddress, ); } } return snippet; } interface ContractCodeProps { contractAddress?: string | undefined; contractType: string; ecosystem: "evm" | "solana"; } const INSTALL_COMMANDS = { evm: { typescript: "npm install @thirdweb-dev/sdk ethers@5", javascript: "npm install @thirdweb-dev/sdk ethers@5", react: "npm install @thirdweb-dev/sdk @thirdweb-dev/react ethers@5", "react-native": "npm install 'ethers@5' node-libs-browser react-native-crypto react-native-randombytes react-native-get-random-values react-native-svg react-native-mmkv@2.5.1 @react-native-async-storage/async-storage @thirdweb-dev/react-native @thirdweb-dev/react-native-compat", python: "pip install thirdweb-sdk", go: "go get github.com/thirdweb-dev/go-sdk/thirdweb", unity: ``, }, solana: { typescript: "npm install @thirdweb-dev/sdk", javascript: "npm install @thirdweb-dev/sdk", react: "npm install @thirdweb-dev/sdk @thirdweb-dev/react @solana/wallet-adapter-wallets @solana/wallet-adapter-react", "react-native": "", python: "pip install thirdweb-sdk", go: "go get github.com/thirdweb-dev/go-sdk/thirdweb", unity: ``, }, }; const CREATE_APP_COMMANDS = { evm: "npx thirdweb create app --evm", solana: "npx thirdweb create app --solana", }; export const ContractCode: React.FC = ({ contractAddress = constants.AddressZero, contractType, ecosystem, }) => { const { data, isLoading } = useContractCodeSnippetQuery(ecosystem); // TODO jonas - bring this back when we figure out what the SDK inputs are going to be for this const chainName = ""; const contractName = usePascalCaseContractName(contractType); const scopedData = useMemo(() => { return getContractSnippets(data, contractName); }, [data, contractName]); const evmAddress = useAddress(); const solanaAddress = useWallet().publicKey?.toBase58(); const address = evmAddress || solanaAddress; const [environment, setEnvironment] = useState("javascript"); const replaceSnippetVars = useCallback( (snip: Partial>) => replaceVariablesInCodeSnippet(snip, contractAddress, address, chainName), [address, contractAddress, chainName], ); const { contract } = useContract(contractAddress); if (isLoading) { return ( Loading... ); } if (!scopedData && contract) { return ( ); } 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 ( {environment.includes("react") ? ( <> Create a new Project Get up and running in seconds using a template React project ) : ( <> 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); })} ); }; function getContractSnippets( snippets?: SnippetApiResponse, contractName?: string | null, ): SnippetSchema | null { return contractName && snippets ? snippets[contractName] : null; } function useContractCodeSnippetQuery(ecosystem: "evm" | "solana") { return useQuery(["code-snippet"], async () => { const filename = ecosystem === "evm" ? "snippets" : "snippets_solana"; const res = await fetch( `https://raw.githubusercontent.com/thirdweb-dev/docs/main/docs/${filename}.json`, ); return (await res.json()) as SnippetApiResponse; }); }