I created smart contract where I send token from contract to senders wallet. I also want contract to receive tokens and send back ETH. In what way can I find out how much tokens sender sends to smart contract?
1 Answer
When writing an ERC20 smart contract you have to write the transfer()
function. The parameters are
_to
destination of tokens_value
value of tokens sent
This is the implementation part of the SingularDTV contracts
/*
* Read and write storage functions
*/
/// @dev Transfers sender's tokens to a given address. Returns success.
/// @param _to Address of token receiver.
/// @param _value Number of tokens to transfer.
function transfer(address _to, uint256 _value) returns (bool success) {
if (balances[msg.sender] >= _value && _value > 0) {
balances[msg.sender] -= _value;
balances[_to] += _value;
Transfer(msg.sender, _to, _value);
return true;
}
else {
return false;
}
}
The user has to call your transfer()
function. You can add the rest of the logic in the function.
-
This helps, thanks. And how can I send ether from funds wallet to sender? Is it possible to withdraw ether from funds wallet and send it to address that sent tokens?– zmikaMar 22, 2018 at 8:42
-
To send ether from your contract you can
_to.transfer(etherValue)
Mar 22, 2018 at 10:55