0

I am working on the ethernaut game to learn about solidity and the ethereum ecosystem. I solved the Fallback problem, but don't understand the interaction below.

The basic question is: the contract has a function that returns a uint, the interaction shows it as returning an array. Why? And how do I map the array to the uint?

The relevant part of the contract is

  function contribute() public payable {
    require(msg.value < 0.001 ether);
    contributions[msg.sender] += msg.value;
    if(contributions[msg.sender] > contributions[owner]) {
      owner = msg.sender;
    }
  }

  function getContribution() public view returns (uint) {
    return contributions[msg.sender];
  }

The interaction is (addresses and tx ids edited out, though I think b/c it is the testnet there is little reason to)

>> await contract.getContribution()
Object { negative: 0, words: (2) […], length: 1, red: null }
length: 1
negative: 0
red: null
words: Array [ 0, <1 empty slot> ]
0: 0
length: 2
​​...
>> await contract.contribute({from: "0x<XXX>", value: web3.utils.toWei('0.0001', 'ether')})
Object { tx: "0x<XXX>", receipt: {…}, logs: [] }

>> await contract.getContribution()
Object { negative: 0, words: (3) […], length: 2, red: null }
length: 2
negative: 0
red: null
words: Array(3) [ 8011776, 1490116, <1 empty slot> ]
0: 8011776
1: 1490116
length: 3
​​...

1 Answer 1

1
>> Number(await contract.getContribution())
1
  • I see. The local contract doesn't know the return type, and interprets it as an array. Reading that data appropriately (with Number()) does get me the expected value.
    – kasterma
    Jan 23 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.