如何在 ethers.js ABI 中定义结构数组 return 类型

How to define an array of structs return type in ethers.js ABI

目前我得到了这个能力:

const stakingAbi = [
    'function getStakes(address user) external view returns (Stake[] memory)',
    'function stake7Days(uint128 _amount) external whenNotPaused',
    'function stake14Days(uint128 _amount) external whenNotPaused',
    'function stake30Days(uint128 _amount) external whenNotPaused',
    'function stake90Days(uint128 _amount) external whenNotPaused'
]

Stake 结构如下所示:

struct Stake {
    uint16 bonusPercentage;
    uint40 unlockTimestamp;
    uint128 amount;
    bool withdrawn;
}

目前 getStakes 函数不起作用,因为 Ethers 不知道 Stake[] 是什么,我该如何在 ABI 中定义它?

在 ethers.js 中仍然不支持以 human-readable ABI 格式指定结构。参见 https://github.com/ethers-io/ethers.js/issues/315

您需要使用 JSON ABI 格式:https://docs.ethers.io/v5/api/utils/abi/formats/#abi-formats--solidity

或者,您可以通过返回一个元组数组来解决它:

function getStakes(address user) external view returns (tuple(uint16 bonusPercentage, uint40 unlockTimestamp, uint128 amount, bool withdrawn)[] memory)

或数组元组:

function getStakes(address user) external view returns (uint16[] memory, uint40[] memory, uint128[] memory, bool[] memory)