"use client"; import { Tab, TabGroup, TabList, TabPanel, TabPanels, Field, Label, } from "@headlessui/react"; import { ArrowPathIcon } from "@heroicons/react/24/outline"; import PythAbi from "@pythnetwork/pyth-sdk-solidity/abis/IPyth.json"; import PythErrorsAbi from "@pythnetwork/pyth-sdk-solidity/abis/PythErrors.json"; import { ChainIcon } from "connectkit"; import { type Dispatch, type SetStateAction, type ComponentProps, type ElementType, type SVGAttributes, useState, useCallback, useMemo, } from "react"; import Markdown from "react-markdown"; import { useSwitchChain, useChainId, useConfig } from "wagmi"; import { readContract } from "wagmi/actions"; import { getContractAddress } from "./networks"; import type { Parameter } from "./parameter"; import { ParameterInput } from "./parameter-input"; import { type EvmApiType, RunButton } from "./run-button"; import { getLogger } from "../../browser-logger"; import { MARKDOWN_COMPONENTS } from "../../markdown-components"; import { useIsMounted } from "../../use-is-mounted"; import { type SupportedLanguage, Code } from "../Code"; import { ErrorTooltip } from "../ErrorTooltip"; import { InlineLink } from "../InlineLink"; import { Select } from "../Select"; export { ParameterType } from "./parameter"; export { EvmApiType } from "./run-button"; const abi = [...PythAbi, ...PythErrorsAbi] as const; type Props = | ReadApi | WriteApi; type Common = { name: (typeof PythAbi)[number]["name"]; summary: string; description: string; parameters: Parameter[]; examples: Example[]; code: CodeSample[]; }; export type ReadApi = Common & { type: EvmApiType.Read; }; export type WriteApi = Common & { type: EvmApiType.Write; valueParam: ParameterName; }; type Example = { name: string; icon?: ElementType>; parameters: ValueOrFunctionOrAsyncFunction>; }; type ValueOrFunctionOrAsyncFunction = | T | ((ctx: ContractContext) => T) | ((ctx: ContractContext) => Promise); type ContractContext = { readContract: (functionName: string, args: unknown[]) => Promise; }; export enum Language { Solidity, EthersJSV6, } type CodeSample = { language: Language; dimRange: ComponentProps["dimRange"]; code: ( network: NetworkInfo, params: Partial>, ) => string; }; export type NetworkInfo = { name: string; rpcUrl: string; contractAddress: string; }; export const EvmApi = ({ name, summary, description, parameters, code, examples, ...props }: Props) => { const [paramValues, setParamValues] = useState< Partial> >({}); const chainId = useChainId(); const { chains, switchChain } = useSwitchChain(); const isMounted = useIsMounted(); const currentChain = useMemo(() => { const chain = isMounted ? chains.find((chain) => chain.id === chainId) : chains[0]; if (chain === undefined) { throw new Error(`Invalid current chain id: ${chainId.toString()}`); } return chain; }, [chainId, chains, isMounted]); return (

{name}

{summary}

Description

{description}

Arguments

{parameters.length > 0 ? (
    {parameters.map((parameter) => (
  • ))}
) : (
This API takes no arguments
)}
{examples.length > 0 && (

Examples

    {examples.map((example) => (
  • ))}
)}