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));