2

I am writing code where I want to call other contracts within my current contract.

Bank.sol

    contract Bank {

    address owner;

    mapping (address => uint) balances;


    function init() returns(bool){
        owner = tx.origin;
        return true;
    }

    // This will take the value of the transaction and add to the senders account.
    function deposit(address customer,uint value) returns (bool res) {
        // If the amount they send is 0, return false.
        balances[customer] += value;
        return true;
    }

    // Attempt to withdraw the given 'amount' of Ether from the account.
    function withdraw(address customer, uint amount) returns (bool res) {
        // Skip if someone tries to withdraw 0 or if they don't have
        // enough Ether to make the withdrawal.
        if (balances[customer] < amount || amount == 0){
            return false;
        }
        balances[customer] -= amount;

        return true;
    }

    function getBalanceOf(address customer) constant returns(uint){
        return balances[customer];
    }

}

FundManager.sol

    import "Bank";
contract FundManager {

    address owner;
    Bank bank;

    function init(address bank) returns (bool){
        owner = tx.origin;
        bank = Bank(bank);
        return true;
    }


    function deposit(uint value) returns (bool res) {

        bool success = bank.deposit(tx.origin,value);
        return success;
    }


    function withdraw(uint amount) returns (bool res) {

        bool success = bank.withdraw(msg.sender, amount);
        return success;
    }

}

I am using hydrachain. when I call the deposit or withdraw functions from the bank's contractInstance everything works fine and I get the correct balance but when I call.The FundManager's deposit and withdraw functions the same changes dont take effect. I am using truffle to deploy contract and using a node script to test it out.

var Web3 = require('web3');
const assert = require('assert');
var web3;
if(typeof web3 !== 'undefined')
    web3 = new Web3(web3.currentProvider);
else
 // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:4000/"));

var coinbase = web3.eth.coinbase;
var bankContract=web3.eth.contract([{ "constant": false, "inputs": [{ "name": "customer", "type": "address" }, { "name": "value", "type": "uint256" }], "name": "deposit", "outputs": [{ "name": "res", "type": "bool" }], "type": "function" }, { "constant": true, "inputs": [{ "name": "customer", "type": "address" }], "name": "getBalanceOf", "outputs": [{ "name": "", "type": "uint256" }], "type": "function" }, { "constant": false, "inputs": [], "name": "init", "outputs": [{ "name": "", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "customer", "type": "address" }, { "name": "amount", "type": "uint256" }], "name": "withdraw", "outputs": [{ "name": "res", "type": "bool" }], "type": "function" }]);
var bankContractInstance = bankContract.at('0x0e7e4e2b408adff6daf54260cefca330056a185c');
var fundmanagerContract = web3.eth.contract([{ "constant": false, "inputs": [{ "name": "bankAddress", "type": "address" }], "name": "init", "outputs": [{ "name": "", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "amount", "type": "uint256" }], "name": "withdraw", "outputs": [{ "name": "res", "type": "bool" }], "type": "function" }, { "constant": false, "inputs": [{ "name": "value", "type": "uint256" }], "name": "deposit", "outputs": [{ "name": "res", "type": "bool" }], "type": "function" }]);
var fundmanagerContractInstance = fundmanagerContract.at('0xd7b96da8f3c5005cfa8eb6db3674e89e056d342c');
var accounts = web3.eth.accounts.slice(1,11);

var fundmanager = accounts[0];
var bank = accounts[1]
var alice = accounts[2];
var bob = accounts[3];

bankContractInstance.init({from:bank});
fundmanagerContractInstance.init(bankContractInstance.address,{from:fundmanager});
bankContractInstance.deposit(alice,3475,{from:bank});
bankContractInstance.withdraw(alice,1000,{from:bank});

fundmanagerContractInstance.deposit(1000,{from:alice});
fundmanagerContractInstance.withdraw(500,{from:alice});
console.log("The money in alice account is:"+bankContractInstance.getBalanceOf(alice));
1
  • I would like to follow up on this question. When deploying Bank and FundManager, does it count as one transaction with the gasUsed in each contract combined? If Bank uses 2141592 gas, and FundManager uses 1141592 gas, will the contracts not deploy from gas limit problems?
    – shiso
    Jan 29, 2016 at 14:34

2 Answers 2

6

In the FundManager, you did not properly attach the address to where the Bank contract is.

bank = Bank(bank);

Should be:

bank = Bank(bankAddress);
3
  • Simon de la Rouviere actually the issue is that if you put bank = Bank(bank); and then if you somewhere check address(this); you will 0x00000...00 address. But if you put bank = Bank(bankAddress); then you will get the correct address. The two variable names have to be separate. Jan 28, 2016 at 12:40
  • Before you edited it, your code was: function init(address bankAddress) returns (bool){ So, it still makes sense. Jan 28, 2016 at 13:06
  • Yes thats true but because I used in my init function ` function init(address bank) returns (bool){ owner = tx.origin; bank = Bank(bank); return true; }` The same variable name i.e bank in the function parameter and my contract variable , I was getting the address 0x000 as address of my bank contract. Jan 29, 2016 at 14:11
3

actually the issue is that if you put

bank = Bank(bank); 

and then if you somewhere check

address(bank);

you will get a 0x00000...00 address. But if you put

bank = Bank(bankAddress); 

then you will get the correct address. The two variable names have to be separate.

2
  • That's part of the issue. The real issue was, was that (before you edited it), the parameter was called "bankAddress". And you didn't use that in your code. Jan 28, 2016 at 13:10
  • Yeah thats true !! Jan 29, 2016 at 14:05

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.