I have a contract which has such field:

    mapping (address => int) balance;

It will hold the balance of account for coins. Let's assume that I've deployed this contract, but I want to disable it and deploy new one, however with keeping the balances of accounts. Is it possible for one contract to get return value of getBalance() of another contract by using CALL method or any other way ? Thanks for Help.

EDIT: I am interested in sending balances between contracts in general :)

  • Hi there. Is the context in this case that you're trying to upgrade a contract, or that you want to transfer balances between contracts generally? If the former, then the following should cover it (including transferring balances): ethereum.stackexchange.com/questions/2404/… May 12 '17 at 11:18

In order for another contract to access that variable, it would have to be declared public, or otherwise have a constant getter to return the value.

If neither exist, you're probably out of luck.


Yes it's possible. There's a couple of ways you can do it. The simplest way is to create a reference to the contract you're calling from the calling contract, which you can do with import "./CalledContract.sol"; and put it below pragma solidity ^0.4.* where 'CalledContract' is the name of the contract you want to call .getBalance() on. Note they both have to be in the same folder for the above command to work.

Then in your code you can create a reference to the instance of that contract type with CalledContract calledContract = CalledContract(addressOfDeployedCalledContract) perhaps in the constructor or accept the CalledContract directly in a function arg

CalledContract calledContract;
function Constructor(CalledContract _calledContract) {
    calledContract = _calledContract;

Then you can do calledContract.getBalance(address) to get the balance of that address from the CalledContract. Note this requires that there is a function in the CalledContract called getBalance(address).

Alternatively you can learn how to use call or delegateCall but it's not really necessary for what you're trying to do.

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.