i am new to blockchain and solidity.

im trying to understand the behaviour of the 'new' keyword in solidity.

I have a deployed contract Coin and in the coin contract you have the ability to transfer value from one address to another.

once you create the contract it will reward the first user with some value.

I dont know how to call the getBalance() from another contract. If i create a variable with new Coin(), doesn't that mean i have created a brand new instance of Coin?

How do i call methods on a contract that is already deployed from another contract.

pragma solidity ^0.4.17;

contract Coin {
    mapping (address => uint) balances;

    function Coin() public {
        balances[msg.sender] = 100;

    function sendCoin(address receiver, uint amount) public returns (bool) {
        require(balances[msg.sender] >= amount);
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        return true;

    function getBalance() public view returns (uint) {
        return balances[msg.sender];

2 Answers 2


In the other contract you will first need to import the Coin contract with import "./Coin.sol"; below the pragma solidity ^0.4.*;. Both contracts will need to be in the same folder for this import to work. Then you need to cast the address of the deployed Coin contract to an instance of type Coin as Imran has previously suggested with Coin coinInstance = Coin(coin_address); then you can access functions on it like you would outside of Solidity eg coinInstance.getBalance(msg.sender);

  • does this mean if i lose the address of the instance, i could lose the instance completely? would that mean i would have to have a contract that will store the value?
    – X0r0N
    Jan 18, 2018 at 15:26
  • Once you deploy a contract to the blockchain it will be there forever, unless you've included a function which calls selfdestruct() inside that contract. The calling contract can store the address so you don't loose it, or better yet, the coinInstance could just be saved as a global variable in the calling contracts constructor.
    – willjgriff
    Jan 18, 2018 at 15:33

Modify getBalance by

function getBalance(address _address) public view returns (uint) { return balances[_address]; }

From another contract's function

Coin c = Coin(deployed_contract_address); return c.getBalance(msg.sender);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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