Chain type error on useMoralisWeb3ApiCall options

Code:

  const { native } = useMoralisWeb3Api();
  const { farmAddress, farmAbi } = useFarm();

  const options = {
    chain: "bsc testnet",
    address: farmAddress,
    function_name: "getTokens",
    abi: farmAbi,
  };

  const { fetch, data, error, isLoading } = useMoralisWeb3ApiCall(
    native.runContractFunction,
    options
  );

Error:

TS2345: Argument of type '{ chain: string; address: any; function_name: string; abi: ({ anonymous: boolean; inputs: { indexed: boolean; internalType: string; name: string; type: string; }[]; name: string; type: string; outputs?: undefined; stateMutability?: undefined; } | { ...; } | { ...; })[]; }' is not assignable to parameter of type '{ chain?: "0x4" | "0x3" | "0x5" | "0x1" | "0x89" | "bsc testnet" | "eth" | "ropsten" | "rinkeby" | "goerli" | "kovan" | "0x2a" | "polygon" | "mumbai" | "0x13881" | "bsc" | "0x38" | ... 7 more ... | undefined; subdomain?: string | undefined; providerUrl?: string | undefined; function_name: string; } & { ...; }'.
  Type '{ chain: string; address: any; function_name: string; abi: ({ anonymous: boolean; inputs: { indexed: boolean; internalType: string; name: string; type: string; }[]; name: string; type: string; outputs?: undefined; stateMutability?: undefined; } | { ...; } | { ...; })[]; }' is not assignable to type '{ chain?: "0x4" | "0x3" | "0x5" | "0x1" | "0x89" | "bsc testnet" | "eth" | "ropsten" | "rinkeby" | "goerli" | "kovan" | "0x2a" | "polygon" | "mumbai" | "0x13881" | "bsc" | "0x38" | "0x61" | ... 6 more ... | undefined; subdomain?: string | undefined; providerUrl?: string | undefined; function_name: string; }'.
    Types of property 'chain' are incompatible.
      Type 'string' is not assignable to type '"0x4" | "0x3" | "0x5" | "0x1" | "0x89" | "bsc testnet" | "eth" | "ropsten" | "rinkeby" | "goerli" | "kovan" | "0x2a" | "polygon" | "mumbai" | "0x13881" | "bsc" | "0x38" | "0x61" | ... 6 more ... | undefined'.
    141 |   const { fetch, data, error, isLoading } = useMoralisWeb3ApiCall(
    142 |     native.runContractFunction,
  > 143 |     options
        |     ^^^^^^^
    144 |   );

Looks like you’re using TypeScript, if so, you can try adding types to your options

  const options: {
    chain: "bsc testnet";
    address: string;
    abi: object[];
    function_name: string;
  } = {
    chain: "bsc testnet",
    address: farmAddress,
    function_name: "getTokens",
    abi: farmAbi,
  };
3 Likes

const chain = “bsc testnet”
const option = {
chain: chain as any,
address: farmAddress,
function_name: “getTokens”,
abi: farmAbi,
};
Please try this

chain as any too should work, but regards typescript, it’s better to have the expected type at most.

1 Like