Erc20 Token Presale Swap Problem Remix Ethereum Solidity

  Kiến thức lập trình
// SPDX-License-Identifier: MITpragma solidity ^0.8.0;



import "@openzeppelin/contracts/token/ERC20/IERC20.sol";import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";



contract Presale {


address public usdtAddress;address public tokenAddress;address public owner;


using SafeERC20 for IERC20;  constructor(address _usdtAddress, address _tokenAddress) {     usdtAddress = _usdtAddress;     tokenAddress = _tokenAddress;     owner = msg.sender; }  modifier onlyOwner() { require(msg.sender == owner, "Only contract owner can call this function"); _;

}


function approveUSDT(uint256 amount) external {     IERC20 usdt = IERC20(usdtAddress);     IERC20 token = IERC20(tokenAddress);    uint256 tokenAmount = amount * 10**6 / 1000; // 1 USDT = 1000 token, 6 ondalık     usdt.approve(address(this), amount);     token.approve(msg.sender, tokenAmount); }  function approveToken(uint256 amount) external onlyOwner { IERC20 token = IERC20(tokenAddress); token.approve(address(this), amount);


}

function buyTokens(uint256 usdtAmount) external  {

    IERC20 usdt = IERC20(usdtAddress);
    IERC20 token = IERC20(tokenAddress);
    
    uint256 tokenAmount = usdtAmount * 10**6 / 1000; // 1 USDT = 1000 token, 6 ondalık
     require(usdt.allowance(msg.sender, address(this)) >= usdtAmount, "Not enough allowance for USDT");
    usdt.transferFrom(msg.sender, address(this), usdtAmount);
    
    token.transfer(msg.sender, tokenAmount);

}

    function transferTokensToContract(uint256 amount) external onlyOwner{
        IERC20 token = IERC20(tokenAddress);
     token.transferFrom(msg.sender, address(this), amount);
    
    }

}

Can you help me with a code that is very important to me? I am trying to write a contract that buys tokens with USDT. While distributing the code, I enter a 6-decimal token contract instead of USDT with Remix Ethereum, then I enter my 18-decimal token and distribute it. I authorize the number of tokens I send with the approvetoken process. I call approveUSDT before each purchase step, then I call the buytoken function, but it does not work.

New contributor

Buğ Oğ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT