Web3BB

    • Register
    • Login
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    How to Receive ERC20 In smart Contract

    Solidity
    1
    1
    1716
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      web3master last edited by web3master

      //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));
        }
      }
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post