Solidity – How to Call a Smart Contract Payable Function Sending ERC20 Tokens

erc-20payablesoliditytokens

We can send a er20 token to a smart contract.

To track it we can use a function ( msg.value is always wei )

But how do i verify if the erc20 token is actually sent ?

For example i want to set map true if the person sent > 5 vbcoins

function sendVBCoins(uint256 _howmuch){
  require(_howmuch >= 5);
  goodperson[msg.sender] = true;
}

Anyone can send 1 wei and call that function and become goodperson.

How do i solve it ?

Best Answer

This sort of transfer can be done in two stages.

The person who aspires to be good needs to first call vbToken.approve(<your contract's address>, 5) (or rather 5 * 10**decimals). This authorizes your contract to take 5 vbcoins from them.

They then call sendVBCoins(5) (or, again, more likely 5 * 10**decimals). Inside sendVBCoins, you will call vbToken.transferFrom(msg.sender, _howmuch). If that succeeds, then you've received those vbcoins from them, and you can consider them to be a "good person."

Related Topic