1

I want want to be able to send an ERC20 token to a smart contract and be able to track which address sent the ERC20 token and for what amount. For example, an address sends X tokens to a smart contract and once they make the transfer the address can now run a function in the smart contract to say register a domain name. Said function would not be able to run without the deposit of the ERC20 token. Note there are two smart contract calls, one to transfer the tokens, and one on the smart contract receiving the ERC20 token.

When I read the ERC20 Solidity smart contract code I see a mapping of balances. I do not understand how I can track who sent what amounts.

1
  • what you mean is that you have a contract and you want the contract to receive payments, right?. Your issue is then that when someone sends tokens your contract cannot know because your contract does not receive any notification. Is that your question?
    – Jaime
    Jun 9, 2018 at 22:42

1 Answer 1

0

Upon a successful transfer of an ERC20 token, an event containing the parties' addresses and the value amount transfered is emitted. The Transfer event looks like:

event Transfer(address indexed _from, address indexed _to, uint _value);

Your contract can listen to the token contract for Transfer events where your contract is the _to parameter - you can do this off-chain using the web3 api watch event.

You may find these related questions helpful:

Detect token transaction to a contract

Call a smart contract payable function sending erc20 token

2
  • Thank you, the first link you posted had the answer I was looking for. "If you want your Solidity contract code to respond to an ERC 20 transfer then you're probably out of luck;" I am now wondering if there is a way to store all transactions of a token on chain as a data structure rather than posting events so other contracts can read current and past transactions, but I will be doing more research on that before asking another question. Jun 10, 2018 at 17:07
  • Hi @Paulmullins were you able to find a solution for this? Dec 23, 2020 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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