Is there a way to receive X amount of ERC20 tokens to transact certain methods inside my smart contracts? So let's say a mint method is payable and receives ether as a value instead, it requires the msg.sender to send X amount of ERC20 tokens to the smart contract in order to perform the transaction.

1 Answer 1


A token is nohting more than a variable (a mapping from an address to a uint256) in the smart contract, so in principle you could code something like this.

   function Whatever() public returns (something) {
       balanceOf[msg.sender] -= (some thokens)
       ... execute the rest of the function

But you should be aware of taking care of the total amount of tokens of the contract. If the msg.sender is "spending" some tokens, it must be transfered to someone else. So the better solutions is to call the tranfer function inside the Whatever function

   function Whatever() public returns (...) {
        Transfer(msg.sender, some addreess, amount)
        ... the rest of the code

You just need to think the logic of your contract, the case is that tokens are a variable that can be manipulated by the contract the way you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.