0

This is the contract:

pragma solidity ^0.4.15;

contract Test {

    address public owner = msg.sender;

    function sendWeis(address _receiver, uint256 _amount) returns (bool) {

        _receiver.transfer(_amount);
        return true;

    }

    function getBalance(address _owner) constant returns (uint256) {

        return _owner.balance;
    }
}

I'm trying to send ether from account A(owner of the contract) to B.

I get this error in solidity browser:

Exception during execution. (invalid opcode). Please debug the transaction for more information.

If I use send() instead of transfer() I don't get any errors, but ethers are not transferred, even though the transaction is executed successfully.

Seems that I'm doing something fundamentally wrong since it doesn't work either in testrpc or geth. Thanks.

1 Answer 1

1

You have a misunderstand to transfer, you must have ethers if would like to transfer. when you deployed smart contract, it's balance is zero, so transfer to other account(contract or account) will failed. At first, you should deposite some ethers, and then transfer, as follows:

pragma solidity ^0.4.13;

contract Test {

    //address public owner = msg.sender;

    //deposite some ethers
    function deposite() payable{

    }

    function sendWeis(address _receiver,uint256 _amount) returns (bool) {
        require(this.balance >= _amount);
        _receiver.transfer(_amount);
        return true;
    }

    function getBalance(address _owner) constant returns (uint256) {
        return _owner.balance;
    }

}

================The second=====================

calling sendWeis method with etheres, as follows:

pragma solidity ^0.4.13;

contract Test {

    //address public owner = msg.sender;

    //deposite some ethers
    function deposite() payable{

    }

    //the sencond transfer method
    function sendWeis(address _receiver) payable returns (bool) {
        _receiver.transfer(msg.value);
        return true;
    }

    function getBalance(address _owner) constant returns (uint256) {
        return _owner.balance;
    }

}

and the result is :

enter image description here

Hope is helps~

1
  • Thanks! That what it. I was a little confused. I'll also stick to transfer method, I don't see a case to use "send" anymore.
    – John Smith
    Commented Aug 20, 2017 at 8:30

Your Answer

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

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