¿Cómo usar payable para recibir ethers?

Soy nuevo en los contratos inteligentes. Quiero crear un contrato que acepte ether y transferirá los tokens equivalentes a la dirección de ether del remitente (venta colectiva). después de unos días de investigación, escribí este código. pero no acepta los éteres (en ropsten testnet). cada vez que envio un ether a este contrato me resulta fallido.

contrato TuTokenToken {

string public constant name = "YOURCOIN";
string public constant symbol = "YRC";
uint8 public constant decimals = 8;  // 8 decimal places.
uint256 public constant tokenCreationRate = 1500;
uint256 public constant tokenCreationCap = 10000 ether * tokenCreationRate;
uint256 public constant tokenCreationMin = 1000 ether * tokenCreationRate;
address public coinOwner; // Receives ETH and its own YRC endowment.
uint256 totalTokens; // The current total token supply.
mapping (address => uint256) balances;

event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Refund(address indexed _from, uint256 _value);

function YourTokenToken() {
    coinOwner = msg.sender;
}

// Crowdfunding:

function create() payable external {

    if (msg.value == 0) throw;
    if (msg.value > (tokenCreationCap - totalTokens) / tokenCreationRate)
        throw;

    var numTokens = msg.value * tokenCreationRate;
    totalTokens += numTokens;
    // Assign new tokens to the sender
    balances[msg.sender] += numTokens;
    // Log token creation event
    Transfer(0, msg.sender, numTokens);
}

}

Respuestas (1)

Si desea recibir ether por contrato utilizando la dirección del contrato, debe implementar una función anónima con una palabra clave de pago.

// Anonymous Function or Fallback Function
function() paybale{
if (msg.value == 0) throw;
if (msg.value > (tokenCreationCap - totalTokens) / tokenCreationRate)
    throw;

var numTokens = msg.value * tokenCreationRate;
totalTokens += numTokens;
// Assign new tokens to the sender
balances[msg.sender] += numTokens;
// Log token creation event
Transfer(0, msg.sender, numTokens);
}

Para más información funciones anónimas .