【问题标题】:useDapp's useContractCall throws "Invalid contract call" in ReactuseDapp 的 useContractCall 在 React 中抛出“无效的合约调用”
【发布时间】:2021-10-22 14:14:02
【问题描述】:

React 组件中,我想读取部署在 Rinkeby 测试网上的合约中“暂停”的功能。使用 useDapp (https://usedapp.io/) 钩子“useContractCall”,我在自定义钩子中调用合约函数:

import { ethers } from "ethers";
import { useContractCall } from "@usedapp/core";
import { contractAddress } from '../../index.js'

const iface = new ethers.Interface([
  "function paused()"
]);

export function usePaused() {
  const [paused] = useContractCall({
    abi: iface,
    address: contractAddress,
    method: 'paused',
    args: [],
  }) ?? [];
  return paused;
}

它会抛出一个错误TypeError: ethers__WEBPACK_IMPORTED_MODULE_0__.ethers.utils.Interface is not a constructor

什么时候代替

const iface = new ethers.Interface([
  "function paused()"
]);

我使用const iface = new ethers.Interface(abi);,我的自定义钩子仍然不起作用,它会引发警告:

Invalid contract call: address=0x...contract_address... method=paused args= 
    at RenderButton (http://localhost:3001/static/js/main.chunk.js:1961:70)

我做错了什么?我按照https://usedapp.readthedocs.io/en/latest/guide.html#custom-hooks 上的说明进行操作。

【问题讨论】:

    标签: node.js reactjs solidity web3 ethers.js


    【解决方案1】:

    我遇到过这个问题。

    在我的例子中,react Webapp 设置为 Mainnet,但浏览器钱包位于 Rinkeby。 钱包网络改为主网后,问题就解决了。

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2017-09-21
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      相关资源
      最近更新 更多