I want to map an address to two balances, because in contract, we have two types of token i.e. energy and power. Is following code is correct in solidity program ?

mapping(address => uint256) Enbalances;
mapping(address => uint256) Pwbalances;

Here is function to get balance of energy :

function balanceOfEn(address _owner) constant returns (uint256 Enbalance) {
    return Enbalances[_owner];

and I call this function as follows :

contractInstance.methods.balanceOfEn('0x1c3bcab2050Fbe3A0c4958dd5776558EE9720460').call({ from: '0x1c3bcab2050Fbe3A0c4958dd5776558EE9720460' }, function(error, result) {

P.S. when I want to get a balance, I receive the following error:

> Error: Couldn't decode uint256 from ABI: 0x
    at SolidityTypeUInt.formatOutputUInt (/home/ubuntu/parityewf/node_modules/we                                                                                                             b3-eth-abi/src/formatters.js:174:15)
    at SolidityTypeUInt.SolidityType.decode (/home/ubuntu/parityewf/node_modules                                                                                                             /web3-eth-abi/src/type.js:252:17)
    at /home/ubuntu/parityewf/node_modules/web3-eth-abi/src/index.js:327:49
    at Array.forEach (native)
    at ABICoder.decodeParameters (/home/ubuntu/parityewf/node_modules/web3-eth-a                                                                                                             bi/src/index.js:326:13)
    at Contract._decodeMethodReturn (/home/ubuntu/parityewf/node_modules/web3-et                                                                                                             h-contract/src/index.js:459:22)
    at Method.outputFormatter (/home/ubuntu/parityewf/node_modules/web3-eth-cont                                                                                                             ract/src/index.js:812:46)
    at Method.formatOutput (/home/ubuntu/parityewf/node_modules/web3-core-method                                                                                                             /src/index.js:163:54)
    at sendTxCallback (/home/ubuntu/parityewf/node_modules/web3-core-method/src/                                                                                                             index.js:467:33)
    at /home/ubuntu/parityewf/node_modules/web3-core-requestmanager/src/index.js                                                                                                             :147:9
    at XMLHttpRequest.request.onreadystatechange (/home/ubuntu/parityewf/node_mo                                                                                                             dules/web3-providers-http/src/index.js:77:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/ubuntu/parityewf/node_modu                                                                                                             les/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/ubuntu/parityewf/node_modules/xhr2/l                                                                                                             ib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/home/ubuntu/parityewf/node_modules/xh                                                                                                             r2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/home/ubuntu/parityewf/node_modules/xhr2/lib                                                                                                             /xhr2.js:469:24)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

Is following alternative code for mapping address to two balances is correct?

mapping (address => uint256) Enbalances;
mapping (address => mapping (address => uint256)) Pwbalances;
  • what error do you receive? Apr 15, 2018 at 9:25
  • it looks like it's related to your js code, and not ETH. Plus, if you set your two mappins as public, Solidity will create automatic getters so you don't have to. Apr 15, 2018 at 10:05
  • github.com/ethereum/web3.js/issues/1089
    – Badr Bellaj
    Apr 15, 2018 at 10:35
  • It seems you have the wrong ABI for this contract, maybe an outdated version of your contract?
    – Jaime
    Apr 15, 2018 at 10:35
  • @Eli Drion What is you opinion about mapping (address => uint256) Enbalances; mapping (address => mapping (address => uint256)) Pwbalances; ? it is not better ?
    – Questioner
    Apr 15, 2018 at 12:57

1 Answer 1

  1. Rebuild your artifacts (using truffle compile for example)

  2. You can simplify contractInstance.methods.balanceOfEn('...').call({ from: '...' })

    To contractInstance.balanceOfEn('...', { from: '...' })

    And in fact, since Enbalances is declared public, you can get rid of function balanceOfEn in your contract, and then use contractInstance.Enbalances('...', { from: '...' })

  • What is you opinion about mapping (address => uint256) Enbalances; mapping (address => mapping (address => uint256)) Pwbalances; ? it is not better ?
    – Questioner
    Apr 15, 2018 at 12:57
  • My ABI is generated by remix and also I tested another function and it works. but in case of balanceOfEn I have this problem.
    – Questioner
    Apr 15, 2018 at 13:17
  • @goodvibrationI updated ABI but I have still same problem Must I deploy contract again?
    – Questioner
    Apr 15, 2018 at 14:39

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.