1

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 1

0

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.

2
  • 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?
    – zmika
    Mar 22, 2018 at 8:42
  • To send ether from your contract you can _to.transfer(etherValue) Mar 22, 2018 at 10:55

Your Answer

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

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