# Presale Problem

**URL:** <https://forum.openzeppelin.com/t/presale-problem/40264>\
**Category:** Smart Contracts\
**Tags:** erc20\
**Created:** [April 18, 2024, 11:18am UTC](https://forum.openzeppelin.com/t/presale-problem/40264 "2024-04-18T11:18:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bug\_Og](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/bug_og/32/20803_2.png) [@Bug\_Og](https://forum.openzeppelin.com/u/Bug_Og)\
**Post date:** [April 18, 2024, 11:18am UTC](https://forum.openzeppelin.com/t/presale-problem/40264/1 "2024-04-18T11:18:55Z")

</div>

```auto
// SPDX-License-Identifier: MIT
pragma 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 Etherium, 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.

---

<div class="post-metadata">

**Author:** ![warrior\_dragon](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/warrior_dragon/32/16359_2.png) [@warrior\_dragon](https://forum.openzeppelin.com/u/warrior_dragon)\
**Post date:** [July 12, 2024, 1:56pm UTC](https://forum.openzeppelin.com/t/presale-problem/40264/2 "2024-07-12T13:56:56Z")

</div>

Hello  
Could you show me err code message?  
I think token balance of contract is not enough.  
But it is just guess.  
if you provide err message, I can help you to fix that.  
Regards.
