1

How is data from instance variables in contracts actually stored in the blockchain? Say I have a simple contract like the example Token contract:

contract MyToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyToken(
        uint256 initialSupply
        ) {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
    }

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
    }
}

Where is the data in balanceOf stored? How might I be able to get this information without adding a function to the Contract just to return say the balance for an address?

2

1-the data is stored in the contract's storage (which is located in the blockchain) every contract has its own storage think of it as an associated disk.

2-you can get the data using whether public e.g

 mapping (address => uint256) public balanceOf;

so every time you could get the result without the need of calling a function (solidity set a getter for you).

enter image description here

or you could set a getter (without declaring your variable public) to return the balance of any address you provide :

    function balance_get(address add) returns (uint256)
{
return balanceOf[ add];
}

Your Answer

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

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