英文:
In ethers.js, why does the return value of a function return an array containing the value instead of the value?
问题
以下是要翻译的内容:
The assertion for the following test fails:
以下测试的断言失败:
it('should access MAX_COUNT', async () => {
const maxCount = await myContract.functions.MAX_COUNT();
expect(maxCount).to equal(64);
});
With the following error:
出现以下错误:
AssertionError: expected [ BigNumber { value: "64" } ] to equal 64
This is the solidity code for (the concise version of) the smart contract being tested:
这是被测试的智能合约(简化版本)的Solidity代码:
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;
contract MyContract
{
uint256 public constant MAX_COUNT = 64;
}
Why is the return value [ BigNumber { value: "64" } ]
instead of just BigNumber { value: "64" }
?
为什么返回值是 [ BigNumber { value: "64" } ]
而不只是 BigNumber { value: "64" }
?
英文:
The assertion for the following test fails:
it('should access MAX_COUNT', async () => {
const maxCount = await myContract.functions.MAX_COUNT();
expect(maxCount).to.equal(64);
});
With the following error:
AssertionError: expected [ BigNumber { value: "64" } ] to equal 64
This is the solidity code for (the concise version of) the smart contract being tested:
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.17;
contract MyContract
{
uint256 public constant MAX_COUNT = 64;
}
Why is the return value [ BigNumber { value: "64" } ]
instead of just BigNumber { value: "64" }
?
For context, this question was originally created while trying to figure out this one:
How to correctly import @nomicfoundation/hardhat-chai-matchers
into hardhat project? ...
However turns out to be completely unrelated.
答案1
得分: 2
这个测试应该失败:
it('应该访问MAX_COUNT', async () => {
const maxCount = await multiSend.functions.MAX_COUNT();
expect(maxCount).to.equal(64);
});
然而,这个应该通过:
it('应该访问MAX_COUNT', async () => {
const maxCount = await multiSend.MAX_COUNT();
expect(maxCount).to.equal(64);
});
而且这个测试也应该通过:
it('应该访问MAX_COUNT', async () => {
const [maxCount] = await multiSend.functions.MAX_COUNT();
expect(maxCount).to.equal(64);
});
线索在文档中,具体来说是通过Contract.functions.XYZ()
与Contract.XYZ()
调用时返回类型之间的差异:
总结一下,Contract.XYZ()
返回的是Promise<any>
或Promise<Result>
;而Contract.functions.XYZ()
始终返回Promise<Result>
Result
对象设计用于可以返回多个值等的函数,这就是为什么需要对其进行数组解构。
英文:
This test should fail:
it('should access MAX_COUNT', async () => {
const maxCount = await multiSend.functions.MAX_COUNT();
expect(maxCount).to.equal(64);
});
However, this should pass:
it('should access MAX_COUNT', async () => {
const maxCount = await multiSend.MAX_COUNT();
expect(maxCount).to.equal(64);
});
And this test should pass too:
it('should access MAX_COUNT', async () => {
const [maxCount] = await multiSend.functions.MAX_COUNT();
expect(maxCount).to.equal(64);
});
The clue is in the documentation,
specifically the difference between the return types
when called via Contract.functions.XYZ()
vs Contract.XYZ()
:
docs for Contract.functions
call
In summary Contract.XYZ()
returns either Promise< any >
or Promise< Result >
;
whereas Contract.functions.XYZ()
always returns Promise< Result >
The Result
object is designed for functions that can return multiple values etc, so that's why it it needs to be array-destructured.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论