2

I have a function which returns address but it gives me error if I try to read it.

I'll explain by a quick example:

Solidity:

contract TestOneReturn
{
    mapping(uint => uint) m_Data;

    constructor()
        public
    {
        m_Data[0] = 111;
        m_Data[1] = 112;
    }

    function getAddress(uint pA)
        external view
        returns(uint)
    {
        return m_Data[pA];
    }
}

contract TestTwoReturns
{
    mapping(uint => mapping(uint => uint)) m_Data;

    constructor()
        public
    {
        m_Data[0][0] = 211;
         m_Data[0][1] = 212;
    }

    function getAddress(
        uint pA,
        uint pB
    )
        external view
        returns(uint)
    {
        return m_Data[pA][pB];
    }
}

TestOneReturn ABI:

[   {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  } ]

TestTwoReturns ABI

[   {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           },          {               "name": "pB",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  } ] 

I mixed both by simply replacing the last "]" of the first and the beginning of the second "[" by "," which gave me:

[   {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  },  {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           },          {               "name": "pB",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  } ]

Contracts created:

TestOneReturn: 0x0c06d42062d5ae6487f6e2dc6a2a7e07f3022fb9
TestTwoReturns: 0x0540d8a5f8dc519f5cbc3286d359587432ea6d9d

Web3JS:

let testAddr = await web3reader.eth.contract(JSON.parse('[  {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  },  {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           },          {               "name": "pB",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  } ]')).at("0x0c06d42062d5ae6487f6e2dc6a2a7e07f3022fb9");
let ret = await promisify(cb => testAddr.getAddress(0, cb));

Result: 111

let testAddr = await web3reader.eth.contract(JSON.parse('[  {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  },  {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           },          {               "name": "pB",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  } ]')).at("0x0540d8a5f8dc519f5cbc3286d359587432ea6d9d");
let ret = await promisify(cb => testAddr.getAddress(0, 0, cb));

Result: 0

Also I made a third contract for test:

contract TestAll
{
    mapping(uint => uint) m_DataA;
    mapping(uint => mapping(uint => uint)) m_DataB;

    constructor()
        public
    {
        m_DataA[0] = 111;
        m_DataA[1] = 112;

        m_DataB[0][0] = 211;
        m_DataB[0][1] = 212;
    }

    function getAddress(uint pA)
        external view
        returns(uint)
    {
        return m_DataA[pA];
    }

    function getAddress(
        uint pA,
        uint pB
    )
        external view
        returns(uint)
    {
        return m_DataB[pA][pB];
    }
}

ABI:

[   {       "inputs": [],       "payable": false,       "stateMutability": "nonpayable",        "type": "constructor"   },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           },          {               "name": "pB",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  },  {       "constant": true,       "inputs": [             {               "name": "pA",               "type": "uint256"           }       ],      "name": "getAddress",       "outputs": [            {               "name": "",                 "type": "uint256"           }       ],      "payable": false,       "stateMutability": "view",      "type": "function"  } ]

await promisify(cb => testAddr.getAddress(0, cb)); gives "Invalid number of arguments to Solidity function"

But

await promisify(cb => testAddr.getAddress(0, 0, cb)); gives valid result 211

But if I read all values manually in Remix, it shows all of them properly!

Did I make a mistake somewhere? Why isn't it working?

(web3js version: 0.20.3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.