I want to implement the possibility to receive token after having sent eth to the contract. How can i do? Maybe i have to modify this function?
function () public payable {
revert();
}
You have left out one important detail: Is the token a different contract?
If we suppose that the token is in a separate contract and want to send 10 tokens for each ether contributted (I'm assuming the token has 18 decimals like ether)
ERC20 token; // <-- Token address assigned on the constructor for example.
function () public payable {
// Received amount of ether
uint amount = msg.value;
// Convert to tokens
uint tokens = amount * 10;
// Transfer tokens assigned to this contract to sender
token.transfer(msg.sender, tokens);
}
For the ERC20 contract interface you can use the OpenZeppelin implementation.
I'd recommend to take a look a the other contracts implemented by the OpenZeppelin team so you have a better idea how to program other things.