Contract Function Doesn't Transfer Ether


#1

Hello,

I have the following sample smart contract below. I can successfully call the Function “transfer” from the API, however every transfer is 0 wei no matter how much I send to the API call. I get the same 0 wei result from the API and the API Documentation Test page.

Both accounts have enough ether to make the transfer. What am I doing wrong here?

Best,
Aaron

contract VerifyForToken {
    /* Public variables of the token */
    string public standard = 'Token 0.1';
    string public name;
    string public symbol;
    uint8 public decimals;
    uint256 public initialSupply;
    uint256 public totalSupply;

    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

  
    /* Initializes contract with initial supply tokens to the creator of the contract */
    function VerifyForToken() {

         initialSupply = 500;
         name ="proofcoin";
        decimals = 2;
         symbol = "^";
        
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
        totalSupply = initialSupply;                        // Update total supply
                                   
    }

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
    }
    
    /* This unnamed function is called whenever someone tries to send ether to it */
    function () {
        throw;     // Prevents accidental sending of ether
    }
}

Here is a screenshot of my Blockapps dashboard after triggering the functions via API.


#2

Hi Aaron,

The transfer function needs the keyword payable. You cannot send ether to a function if it doesnt have the payable modifier. This was introduced in Solidity 0.4.0 (I think).

So your function should read

function transfer(address _to, uint256 _value) payable {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
    }