【发布时间】: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