question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Truffle-v5] Wrong return type for functions with multiple named return values

See original GitHub issue

Adding 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:

  1. 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
JustynaBroniszewskacommented, Nov 24, 2020

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:

contract Example {
    uint a = 1;
    uint b = 2;

    function getNumbers() public view returns (uint, uint) {
        return (a, b);
    }
}

This doesn’t work:

  const [a, b] = await contract.getNumbers()

…but these work:

  const { 0: a, 1: b } = await contract.getNumbers()
  const numbers = await contract.getNumbers()
  console.log(numbers[0], numbers[1])
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found