Erc-20
如何在智能合約中接收 ERC20
您好,我對 ERC20 很陌生,我嘗試研究一些東西,但我腦海中的概念可能不正確。我正在嘗試創建接收一定數量 ERC20 的智能合約,驗證輸入數量,如果沒問題,它會繼續下面的邏輯。智能合約看起來像這樣:
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 } }
如何驗證每筆存款的 ERC20 發送金額
試試這個:
//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)); } }
**注意:**請記住批准智能合約來使用您的代幣。