6

Hello I'm pretty new to ERC20 and i try to research something but maybe concept in my head is not correct. I'm trying to create smart contract that is receiving some amount of ERC20, validating the input amount and if it's ok it continue with logic below. Smart contract looks something like this:

contract Orders {
 uint256 public counter;

 function deposit() public payable {
   // Here we validate if sended USDT for example is higher than 50, and if so we increment the counter

  counter = counter + 1
 }
}

How to validate sended amount of ERC20 for each individual deposit

2 Answers 2

10

Try this:

//SPDX-License-Identifier: MIT
pragma solidity 0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Orders is Ownable {
  uint256 public counter;
  address token;

  constructor(address _token) {
    token = _token;
  }

  function deposit(uint _amount) public payable {
    // Set the minimum amount to 1 token (in this case I'm using LINK token)
    uint _minAmount = 1*(10**18);
    // Here we validate if sended USDT for example is higher than 50, and if so we increment the counter
    require(_amount >= _minAmount, "Amount less than minimum amount");
    // I call the function of IERC20 contract to transfer the token from the user (that he's interacting with the contract) to
    // the smart contract  
    IERC20(token).transferFrom(msg.sender, address(this), _amount);
    counter = counter + 1;
  }

  // This function allow you to see how many tokens have the smart contract 
  function getContractBalance() public onlyOwner view returns(uint){
    return IERC20(token).balanceOf(address(this));
  }
}

NOTE: Remember to approve the smart contract to spend your tokens.

5
  • amazing man i will give a try, one more question is it good to check if user that is sending these tokens actually owns them? Mar 9, 2022 at 16:12
  • Yes, it is another control that you can do. You can use the same balanceOf() function (contained into IERC20). It is the same implementation of getContractBalance() function but you must call IERC20(token).balanceOf([address_user]). Mar 9, 2022 at 17:10
  • love you brotha!!!!!!!!!!!! Mar 9, 2022 at 22:03
  • Just one last question really, so if i send the tokens to address that is owned by the smart contract i need to approve it in order to spend otherwise they will. stay locked in the smart contract? Also do i need to do approve if i send from 1 wallet address to another user wallet address? Mar 9, 2022 at 22:04
  • because the contract does not need to accept ETH so I think we don't need to use payable
    – 0xh8h
    Apr 9, 2023 at 10:10
2

ERC20 doesn't notify the receiver when it receives some tokens. The token itself handles the amount for each address. When someone sends your contract some amount of this ERC20 token, it will update the state of the token contract but nothing will happen in your contract.

What you can do (depending on what is the purpose of your contract) is when you call some kind of function that needs to know how many tokens the contract holds, just check the ERC20(token).balanceOf(address(this)) and handle it from there

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.