【问题标题】:Property 'Fund' does not exist on type 'Contract'. Ethersjs ABI typecasting [TypeScript]类型“合同”上不存在属性“基金”。 Ethers Js ABI 类型转换 [TypeScript]
【发布时间】:2022-11-24 04:45:40
【问题描述】:

在跟随 Patrick Collins 课程时。我遇到了 TypeScript 问题。

我在本教程中使用TypeScript,但原文是用 JavaScript 编写的。

下面的代码导致错误。

index.ts


 const transactionResp = await contract.Fund({ value: ethers.utils.parseEther(ethAmount) });

我得到的错误

error TS2339: Property 'Fund' does not exist on type 'Contract'.

index.ts的完整代码

import { ethers } from "./lib/ethers-5.2.esm.min.js";
import { contractAddress, abi } from "./lib/constants.js";

const fundBtn: HTMLButtonElement = document.getElementById("fundButton") as HTMLButtonElement;

fundBtn.onclick = fund;

async function fund() {
    const amount = "0.2";
    
    if (typeof window.ethereum !== "undefined") {
       
        const provider = new ethers.providers.Web3Provider(window.ethereum); 
        const signer = provider.getSigner(); 
        const contract = new ethers.Contract(contractAddress, abi, signer);
        try {
            const transactionResp = await contract.Fund({ value: ethers.utils.parseEther(amount) }); 
           //^ Above is Error line [error TS2339: Property 'Fund' does not exist on type 'Contract'.]
        } catch (e) {
            console.log(e);
        }
      
    } else {
        connectBtn.innerHTML = "Install Metamask";
    }
}

我的 constant.ts 文件有 ABI 定义。

export const contractAddress = "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512";
export const abi = [
    {
        inputs: [
            {
                internalType: "address",
                name: "PriceFeed",
                type: "address",
            },
        ],
        stateMutability: "nonpayable",
        type: "constructor",
    },
    {
        inputs: [],
        name: "FundMe__NotOwner",
        type: "error",
    },
    {
        stateMutability: "payable",
        type: "fallback",
    },
    {
        inputs: [],
        name: "Fund",
        outputs: [],
        stateMutability: "payable",
        type: "function",
    },
    {
        inputs: [],
        name: "MINIMUM_USD",
        outputs: [
            {
                internalType: "uint256",
                name: "",
                type: "uint256",
            },
        ],
        stateMutability: "view",
        type: "function",
    },
    {
        inputs: [],
        name: "cheaperWithdraw",
        outputs: [],
        stateMutability: "payable",
        type: "function",
    },
    {
        inputs: [
            {
                internalType: "address",
                name: "funder",
                type: "address",
            },
        ],
        name: "getAddressToAmountFunded",
        outputs: [
            {
                internalType: "uint256",
                name: "",
                type: "uint256",
            },
        ],
        stateMutability: "view",
        type: "function",
    },
    {
        inputs: [
            {
                internalType: "uint256",
                name: "index",
                type: "uint256",
            },
        ],
        name: "getFunders",
        outputs: [
            {
                internalType: "address",
                name: "",
                type: "address",
            },
        ],
        stateMutability: "view",
        type: "function",
    },
    {
        inputs: [],
        name: "getOwner",
        outputs: [
            {
                internalType: "address",
                name: "",
                type: "address",
            },
        ],
        stateMutability: "view",
        type: "function",
    },
    {
        inputs: [],
        name: "getPriceFeed",
        outputs: [
            {
                internalType: "contract AggregatorV3Interface",
                name: "",
                type: "address",
            },
        ],
        stateMutability: "view",
        type: "function",
    },
    {
        inputs: [],
        name: "withdraw",
        outputs: [],
        stateMutability: "payable",
        type: "function",
    },
    {
        stateMutability: "payable",
        type: "receive",
    },
];

对我来说,它看起来像是类型转换错误,因此打字稿无法将它读取为Fund作为属性。不知道如何解决? 可能我需要为 ABI 声明接口?不确定如何声明它。

【问题讨论】:

    标签: javascript typescript solidity abi ethers.js


    【解决方案1】:

    Contract您使用的类型是通用类型。如果你检查 node_modules 源代码,一些属性

    readonly address: string;
    readonly interface: Interface;
    readonly signer: Signer;
    readonly provider: Provider;
    readonly functions: { [ name: string ]: ContractFunction };
    readonly callStatic: { [ name: string ]: ContractFunction };
    readonly estimateGas: { [ name: string ]: ContractFunction<BigNumber> };
    readonly populateTransaction: { [ name: string ]: ContractFunction<PopulatedTransaction> };
    readonly filters: { [ name: string ]: (...args: Array<any>) => EventFilter };
    

    您需要根据您的 abi 生成类型。您可以使用ethereum-abi-types-generator。你所要做的就是在package.json中写一个脚本

    "genContractType": "abi-types-generator './public/contracts/YourContractName.json' --output='./types' --name=whateverName --provider=ethers_v5",
    

    一旦你运行这个脚本并进入“types”目录,你应该有whateverName.ts文件。此文件中将有很多类型和接口,但主要合约类型将是名称YourContractNameContract类型。

    现在当你写你的合约等式时,你可以投它as YourContractNameContract

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-24
      • 1970-01-01
      • 2023-04-02
      • 2018-08-17
      • 1970-01-01
      • 2023-02-06
      • 2023-01-19
      • 2018-12-02
      相关资源
      最近更新 更多