首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Smart Contracts与@usedapp和@ethersproject/contracts一起使用时出现奇怪的错误

将Smart Contracts与@usedapp和@ethersproject/contracts一起使用时出现奇怪的错误
EN

Stack Overflow用户
提问于 2021-11-09 15:10:09
回答 1查看 420关注 0票数 2

你好,我正在尝试使用一份合同,该合同是在可靠的基础上使用React和typescript与brownie一起部署在我的前端。同样使用使用的框架,正如文档here所说的那样,为了与约定功能交互,我应该创建一个新的约定,提供地址和ABI。下面是我的代码:

代码语言:javascript
复制
import {useContractFunction, useEthers} from '@usedapp/core'
import TokenFarm from "../chain-info/contracts/TokenFarm.json"
import ERC20 from "../chain-info/contracts/MockERC20.json"
import networkMapping from "../chain-info/deployments/map.json"
import {constants, utils} from "ethers"
import {Contract} from '@ethersproject/contracts'

export const useStakeTokens = (tokenAddress: string) => {
    // chainId 
    const {chainId} = useEthers()
    // abi
    const {abi} = TokenFarm
    // address
    // const dappTokenAddress = chainId ? networkMapping[String(chainId)]["DappToken"][0] : constants.AddressZero
    const tokenFarmAddress = chainId ? networkMapping[String(chainId)]["TokenFarm"][0] : constants.AddressZero
    // approve
    const tokenFarmInterface = new utils.Interface(abi)
    const tokenFarmContract = new Contract(tokenFarmAddress, tokenFarmInterface)

    const erc20ABI = ERC20.abi
    const erc20Interface = new utils.Interface(erc20ABI)
    const erc20Contract = new Contract(tokenAddress, erc20Interface)
    // approve
    const { send: approveErc20Send, state: approveAndStakeErc20State } =
        useContractFunction(erc20Contract, "approve", {
            transactionName: "Approve ERC20 transfer",
        })

}

在使用erc20ContractuseContractFunction上出现错误

Vscode error

这是完整的错误消息

代码语言:javascript
复制
Argument of type 'import("/home/cromewar/Solidity-Projects/full_defi_app/dev/front_end/node_modules/@ethersproject/contracts/lib/index").Contract' is not assignable to parameter of type 'import("/home/cromewar/Solidity-Projects/full_defi_app/dev/front_end/node_modules/@usedapp/core/node_modules/@ethersproject/contracts/lib/index").Contract'.
  Types of property '_runningEvents' are incompatible.
    Type '{ [eventTag: string]: RunningEvent; }' is not assignable to type '{ [eventTag: string]: RunningEvent; }'. Two different types with this name exist, but they are unrelated.
      'string' index signatures are incompatible.
        Type 'RunningEvent' is not assignable to type 'RunningEvent'. Two different types with this name exist, but they are unrelated.
          Types have separate declarations of a private property '_listeners'.  TS2345

    23 |     // approve
    24 |     const { send: approveErc20Send, state: approveAndStakeErc20State } =
  > 25 |         useContractFunction(erc20Contract, "approve", {
       |                             ^
    26 |             transactionName: "Approve ERC20 transfer",
    27 |         })
    28 |
代码语言:javascript
复制
Argument of type 'import("/home/cromewar/Solidity-Projects/full_defi_app/dev/front_end/node_modules/@ethersproject/contracts/lib/index").Contract' is not assignable to parameter of type 'import("/home/cromewar/Solidity-Projects/full_defi_app/dev/front_end/node_modules/@usedapp/core/node_modules/@ethersproject/contracts/lib/index").Contract'.
  Types of property '_runningEvents' are incompatible.
    Type '{ [eventTag: string]: RunningEvent; }' is not assignable to type '{ [eventTag: string]: RunningEvent; }'. Two different types with this name exist, but they are unrelated.
      'string' index signatures are incompatible.
        Type 'RunningEvent' is not assignable to type 'RunningEvent'. Two different types with this name exist, but they are unrelated.
          Types have separate declarations of a private property '_listeners'.  TS2345

它说类型是不兼容的,但它们实际上是完全相同的,有人知道发生了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-09 22:55:33

解决方案:

替换为:

代码语言:javascript
复制
import {Contract} from '@ethersproject/contracts'

为此:

代码语言:javascript
复制
import {Contract} from '@usedapp/core/node_modules/@ethersproject/contracts'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69900547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档