Access variables through auto-generated getters from a different contract

0 votes

I am storing the variables of my token outside of the main contract in a contract called Storage and, therefore, need to access the auto generated getters of the publicly declared variables from a different contract than the one in which they are declared.

contract Storage {

    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

}

contract TokenA {

    address public storageAddress;

    function getAllowance(address _owner, address _spender) public returns (uint256) {
        return Storage(storageAddress). allowance( /** ? */);
    }

}

How could I access the variable allowance without specifically defining a getter inside the contract Storage?

Oct 3, 2018 in Blockchain by digger
• 26,600 points
23 views

1 answer to this question.

0 votes

You just need to pass it in as if you were calling a function:

function getAllowance(address _owner, address _spender) public view returns (uint256) {
    Storage s = Storage(storageAddress);
    return s.allowance(_owner, _sender);
}

Be careful of the order. You may have to swap it depending on how you're handling storage.

answered Oct 3, 2018 by slayer
• 29,240 points

Related Questions In Blockchain

0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,090 points
63 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,810 points
182 views
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,620 points
176 views
0 votes
1 answer

Is it possible to send eth to a contract through its constructor from another contract?

I found the solution for this: pragma solidity ...READ MORE

answered Sep 26, 2018 in Blockchain by slayer
• 29,240 points
111 views
0 votes
1 answer