2

I am trying to send ether from one contract to another by getting the input address. Here A has a balance of 100 ether.

1) contract B is created first and I have the address 2) contract A is created next 3) invoking fundTransfer() function with B's address and 50 ether as amount.

Transfer isn't happening. what am i missing here. Sorry if it is a basic question.

pragma solidity ^0.4.8;

contract A {

    function A () payable {

    }

    event balanc(uint a);

    function fundTransfer(address b,uint amt) {
       bool ret = b.send(amt);
    }    

    function getBalance() {
        balanc(this.balance);
    }
}

contract B {

    event balanc(uint a);

    function getBalance() {
        balanc(this.balance);
    }
}

2 Answers 2

2

The following code will work:

pragma solidity ^0.4.8;

contract A {
    function A() payable {
    }

    function fundTransfer(address b, uint256 amt) {
       bool ret = b.send(amt);
    }    

    function getBalance() constant returns (uint256 balance) {
        balance = this.balance;
    }
}

contract B {
    function getBalance() constant returns (uint256 balance) {
        balance = this.balance;
    }

    function () payable {
    }
}

The main difference to your original source code is that I have added the fallback () function marked with the payable modifier. Your events work, but I have just replaced your events with constant functions.

Following are the screens that show you how to deploy it using the JavaScript VM with Browser Solidity (or Remix).

The first screen below shows the deployment of contract B to the address 0xde6a66562c299052b1cfd24abc1dc639d429e1d6. Note that Value is set to 0.

enter image description here

In the next screen, I have set Value to 100 (ethers) and deployed contract A. Note that A.getBalance() returns an error - this is because a value of 100 ethers is sent when invoking A.getBalance() and this function is not marked with the payable modifier.

enter image description here

In the next screen, I have set Value to 0 and clicked on getBalance() and the returned result shows that contract A has been successfully deployed with 100 ethers.

enter image description here

In the next screen, I will execute A.fundTransfer(...) with the parameters "0xde6a66562c299052b1cfd24abc1dc639d429e1d6","50000000000000000000". Note that the "50000000000000000000" number will need to be quoted or the number will cause an error.

enter image description here

And the next screen shows the successful transfer of 50 ethers from A to B. You can also check that A.getBalance() returns a value of 50 ethers.

enter image description here

If you remove the function () payable { } from contract B, the exercise above will fail.

1

I think, you should check this. It seems related.

The balance shall get deducted from the contract, not A's account.

for deducting the amount from contract, you have to pass some ethers in value field, while calling the function. e.g.

myContractInstance.myMethod(arg1,arg2,...,{value:web3.fromWei((1 ether), gas: 600000 }, function(err, result){...});

Hope this helps.

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.