Let's say I have a token A and a smart contract B. A user sent some amount of A to to the contract B by direct transfer through his wallet. In the fallback function of the smart contract B, how will I get the amount of token A that was sent?
msg.value did'nt give me anything since it's not Eth that was sent.
When your contract receives ERC-20 tokens, the fallback()
function is not invoked - nor any other function. Meaning, your contract does not get notified about incoming ERC-20 transfers unless you pull them with transferFrom()
.
contract YourContract {
function pullTokens() external {
// need to have prior approval
tokenContract.transferFrom(msg.sender, address(this), amount);
}
}
Note: Some other fungible token standards define these notification functions for recipient contracts - for example ERC-777 and its function tokensReceived()
.