I am developing an application that automatically converts eth, usdt ... to my token when they transfer money to my contract address. With eth, I can do it, but how to detect other erc tokens, including my token transferred to the contract address. I have read through some articles, describing the use of approve and then transferFrom. But this will cause the transferor to pay for 2 times . Are there any functions similar to payable of eth for tokens only?
2 Answers
While there are many proposals, e.g. ERC223, generally the two-step process is the most basic approach to start with.
Have the user approve the contract to transferFrom, and then have the contract do it. A UI can choreograph that for the user so it looks like one move. EIP-1102 smooths out permissions from a user perspective so they aren't constantly interrupted.
This tutorial explains implementing EIP-1102, how it silences those transaction approval interruptions, and why wallet apps are recommending adoption. https://status.im/ru/developer_tools/run_on_status/eip-1102.html
Hope it helps.
You need to call approve from the original contract, then use the transferFrom function and upon the transfer of whatever the balance is, add that to a user balance and work from there contract side.
interface tokenToTransfer {
function transferFrom(address from, address to, uint256 value) external;
}
//Contract B, below calls Contract A's interface above
Contract B {
tokenToTransfer public transferToken;
function Add_Token(address selectedToken, uint256 amount) public {
transferToken = tokenToTransfer(selectedToken);
transferToken.transferFrom(msg.sender, 0x032b993595f295b45c11171AdEea3812F03A07E3, amount);
***user balance struct/whatever*** = amount
}
}