import { Box, Flex, Select } from "@chakra-ui/react";
import { SmartContract } from "@thirdweb-dev/sdk/dist/declarations/src/evm/contracts/smart-contract";
import { useContractFunctions } from "components/contract-components/hooks";
import { CodeSegment } from "components/contract-tabs/code/CodeSegment";
import { Environment } from "components/contract-tabs/code/types";
import { useSingleQueryParam } from "hooks/useQueryParam";
import { useMemo, useState } from "react";
import { Card, Heading, Text } from "tw-components";
import { SupportedNetwork } from "utils/network";
interface CodeOverviewProps {
contract: SmartContract | null;
}
const COMMANDS = {
install: {
javascript: "npm install @thirdweb-dev/sdk ethers",
react: "npm install @thirdweb-dev/react @thirdweb-dev/sdk ethers",
web3button: "npm install @thirdweb-dev/react @thirdweb-dev/sdk ethers",
python: "pip install thirdweb-sdk",
go: "go get github.com/thirdweb-dev/go-sdk/thirdweb",
},
setup: {
javascript: `import { ThirdwebSDK } from "@thirdweb-dev/sdk/evm";
const sdk = new ThirdwebSDK("{{chainName}}");
const contract = await sdk.getContract("{{contract_address}}");`,
react: `import { useContract } from "@thirdweb-dev/react";
export default function Component() {
const { contract } = useContract("{{contract_address}}");
// Now you can use the contract in the rest of the component
}`,
web3button: `import { Web3Button } from "@thirdweb-dev/react";
export default function Component() {
return (
{
// Any action with your contract
}}
>
Do something
)
}`,
python: `from thirdweb import ThirdwebSDK
sdk = ThirdwebSDK("{{chainName}}")
contract = sdk.get_contract("{{contract_address}}")`,
go: `import "github.com/thirdweb-dev/go-sdk/thirdweb"
sdk, err := thirdweb.NewThirdwebSDK("{{chainName}}")
contract, err := 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}})
}`,
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}} ]);
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}})`,
},
};
interface SnippetOptions {
contractAddress?: string;
fn?: string;
args?: string[];
chainName?: string;
}
function formatSnippet(
snippet: Record,
{ contractAddress, fn, args, chainName }: SnippetOptions,
) {
const code = { ...snippet };
for (const key of Object.keys(code)) {
const env = key as Environment;
code[env] = code[env]
?.replace(/{{contract_address}}/gm, contractAddress)
?.replace(/{{chainName}}/gm, chainName || "goerli")
?.replace(/{{function}}/gm, fn || "");
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 = ({ contract }) => {
const chainName = useSingleQueryParam("networkOrAddress");
const [environment, setEnvironment] = useState("react");
const functions = useContractFunctions(contract);
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].name : "",
);
const [write, setWrite] = useState(
writeFunctions && writeFunctions.length > 0 ? writeFunctions[0].name : "",
);
return (
<>
Getting Started
First, install the latest version of the SDK.
Follow along below to get started using this contract in your code.
Reading Data
Once you setup your contract, you can read data from contract
functions as follows:
f.name === read)
?.inputs?.map((i) => i.name),
chainName,
})}
/>
Writing Data
And you can also make write function calls with the following setup:
f.name === write)
?.inputs?.map((i, index) => i.name || `arg${index}`),
chainName,
})}
/>
>
);
};