Solidity – Can a Smart Contract Identify Which Address Sent an ERC20 Token?

erc-20solidityweb3js

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.

Best Answer

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