[Truffle-v5] Wrong return type for functions with multiple named return values
See original GitHub issueAdding the following function to the MetaCoin
contract in examples/truffle-v5
:
function foo() public pure returns (uint bar, address bam) {
bar = 42;
bam = address(0);
}
generates the following type bindings:
foo(txDetails?: Truffle.TransactionDetails): Promise<[BN, string]>;
Therefore, accessing these values in a test like this compiles fine:
it('should allow calling foo', async () => {
const metaCoinInstance = await MetaCoin.new();
let [bar, bam] = await metaCoinInstance.foo();
assert.equal(bar.toNumber(), 42);
});
However, running the test crashes with the following Type error:
- Contract: MetaCoin should allow calling foo: TypeError: (intermediate value) is not iterable at Context.<anonymous> (test/metacoin.ts:43:22) at processTicksAndRejections (internal/process/task_queues.js:97:5)
I believe the reason for this is that truffle v5 uses web3 v1 under the hood and thus the function above should actually compile to a Promise of a named struct rather than a Promise of a tuple. In this case the method should have the following signature:
foo(txDetails?: Truffle.TransactionDetails): Promise<{
bar: BN;
bam: string;
0: BN;
1: string;
}>;
(which is already the case when using --target=web3-v1
)
As a temporary work-around, accessing the field by index compiles fine and does not crash (since the named struct also has a field with name 0 & 1):
let result = await metaCoinInstance.foo();
assert.equal(result[0], 42);
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Return multiple values from function - Stack Overflow
I'd like to add that one of the main use-cases for multiple return values in Go is error ...
Read more >Multiple return values · Issue #68 · dart-lang/language - GitHub
A partner team has requested support for multiple return values for functions, for example: Future lat, long = mapService.
Read more >Named return values in Go - Exploring Software
It says that the function will return an int and an error; Declares two variables - i of type int and e of...
Read more >Function return values - Learn web development | MDN
When the function completes (finishes running), it returns a value, which is a new string with the replacement made. In the code above,...
Read more >Multiple Return Values - Go by Example
This feature is used often in idiomatic Go, for example to return both result and error values from a function. package main. import...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I might have a similar issue. I created a repo that shows it here
TL;DR When I call a function, that is supposed to return multiple values I have a problem destructuring them:
This doesn’t work:
…but these work:
Released: https://github.com/ethereum-ts/TypeChain/releases/tag/typechain%404.0.1