Etherjs:类型错误:contract.XXX 不是函数

tit*_*oto 3 blockchain ethereum web3js ethers.js

我试图从 FTM 网络上的智能合约调用名为“remove_liquidity_one_coin”的函数,但出现以下错误并且无法找出原因:

TypeError: curveContract.remove_liquidity_one_coin is not a function

通常,当我想要调用合约的函数时,我会获取 ABI(合约地址),然后实例化它并可以使用它的函数。

对于下面的合约,它适用于“读取”功能,但不适用于“写入”功能,例如remove_liquidity_one_coin.

这是我使用的简化代码:

let signer = new ethers.Wallet(privateKey, provider)
let contractAddress = "0xa58f16498c288c357e28ee899873ff2b55d7c437"
let contractAbi = [...] // ABI of the contract. In this case: https://api.ftmscan.com/api?module=contract&action=getabi&address=0x3cabd83bca606768939b843f91df8f4963dbc079&format=raw
let curveContract = new ethers.Contract(contractAddress, contractAbi, signer)

// Read function => works
let liquidityToRemove = await curveContract.calc_withdraw_one_coin(
            lpTokenToWidraw, // Amount to withdraw
            0 // Index of the token to withdraw
);

// Write function => doesn't work
let receivedCoins = await curveContract.remove_liquidity_one_coin(
    liquidityToRemove, // Amount to withdraw
    0, // Index of the token to receive
    expectedAmount // Expected amount to withdraw
);   
Run Code Online (Sandbox Code Playgroud)

你知道我错过了什么吗?

编辑 我最终只使用了我想要的功能的 Abi 。例子:

let signer = new ethers.Wallet(privateKey, provider)
let contractAddress = "0xa58f16498c288c357e28ee899873ff2b55d7c437"
let functionAbi = ["function remove_liquidity_one_coin(uint256 burn_amount, int128 i, uint256 min_received) public returns (uint256)"];
let curveContract = new ethers.Contract(contractAddress, functionAbi, signer)

// Write function => works
let receivedCoins = await curveContract.remove_liquidity_one_coin(
    liquidityToRemove, // Amount to withdraw
    0, // Index of the token to receive
    expectedAmount // Expected amount to withdraw
);
Run Code Online (Sandbox Code Playgroud)

小智 7

请参阅ricmoo 的 GitHub 讨论答案

我假设你指的是方法buy

您的 ABI 中有多个定义buy,因此您需要指定您的意思。我强烈建议您缩减 ABI 以仅包含您使用的方法,但如果您希望保留完整的 ABI,则需要使用:

// There are two "buy" methers, so you must specify which you mean:
// - buy(uint256)
// - buy(uint256,bytes)
contract["buy(uint256)"](shareId, overrides)
Run Code Online (Sandbox Code Playgroud)

如果您缩减 ABI 以排除不使用的代码,您将能够拥有更清晰的代码(您已经可以使用的代码),并且您的应用程序大小将减少大约 2 MB,因为其中大部分(大量)ABI 未使用。

简而言之,如果有多个同名但签名不同的方法,就会出现这个错误。