I'm writing a smart contract that will do something as long as the user requesting that feature has paid the correct amount. If the amount is in ETH, I can easily do that with a simple require statement like this:
function doSomething() public payable {
require(msg.value == 1 ether); // make sure they paid
// if they paid, proceed with the action
}
However, if I want to accept other ERC-20 tokens instead of ETH, I am not sure how I would be able to do that inside my contract (i.e. without running my own separate server listening to the blockchain and teasing out individual transactions).
Since all ERC-20 token transfers happen inside the contract for the specific token (and not inside the methods of my smart contracts), how would my contract know or validate that:
- The correct amount has been transferred.
- The amount was transferred from address A.
- The amount was transferred to address B.
Where A is my smart contract, and B is the user.