0
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

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

interface IPancakeRouter01 {
    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
}

interface ISwapRouter {
    struct ExactInputSingleParams {
        address tokenIn;
        address tokenOut;
        uint24 fee;
        address recipient;
        uint256 deadline;
        uint256 amountIn;
        uint256 amountOutMinimum;
        uint160 sqrtPriceLimitX96;
    }

    function exactInputSingle(ExactInputSingleParams calldata params) external returns (uint256 amountOut);
}

contract NeoNobleCrypto is ERC20, ERC20Burnable, Ownable {
    IPancakeRouter01 public pancakeRouter;
    ISwapRouter public uniswapRouter;
    address public WBNB;
    address public USDT;
    address public BTC;
    address public ETH;
    address public SOL;

    constructor() ERC20("NeoNoble Crypto", "NEO") Ownable(msg.sender) {
        uint8 _decimals = 10;
        uint256 _supply = 800_000_000 * 10 ** uint256(_decimals);
        address tokenOwner = 0x051913a7290421e3E9c26c3ac62440d424ff2a7B;
        
        _mint(tokenOwner, _supply);

        pancakeRouter = IPancakeRouter01(0x10ED43C718714eb63d5aA57B78B54704E256024E); // PancakeSwap Router V2 Address
        uniswapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); // Uniswap V3 Router Address
        WBNB = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c; // Wrapped BNB Token Address
        USDT = 0x55d398326f99059fF775485246999027B3197955; // USDT Token Address on BSC
        BTC = 0x321162Cd933E2Be498Cd2267a90534A804051b11; // BTC Token Address on BSC
        ETH = 0x2170Ed0880ac9A755fd29B2688956BD959F933F8; // ETH Token Address on BSC
        SOL = 0x570A5D26f7765Ecb712C0924E4De545B89fD43dF; // SOL Token Address on BSC
    }

    function swapToETH(uint256 amount) external {
        _swap(pancakeRouter, amount, ETH);
    }

    function swapToBNB(uint256 amount) external {
        _swap(pancakeRouter, amount, WBNB);
    }

    function swapToBitcoin(uint256 amount) external {
        _swap(pancakeRouter, amount, BTC);
    }

    function swapToSolana(uint256 amount) external {
        _swap(pancakeRouter, amount, SOL);
    }

    function swapToUSDT(uint256 amount) external {
        _swap(pancakeRouter, amount, USDT);
    }

    function _swap(IPancakeRouter01 router, uint256 amount, address toToken) internal {
        require(balanceOf(msg.sender) >= amount, "Insufficient balance");
        _transfer(msg.sender, address(this), amount);
        _approve(address(this), address(router), amount);

        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = toToken;

        router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
            amount,
            0, // accept any amount of tokens
            path,
            msg.sender,
            block.timestamp + 300 // 5 minutes
        );
    }

    function swapToETHOnUniswap(uint256 amount) external {
        _swapOnUniswap(amount, ETH);
    }

    function swapToUSDTOnUniswap(uint256 amount) external {
        _swapOnUniswap(amount, USDT);
    }

    function _swapOnUniswap(uint256 amount, address toToken) internal {
        require(balanceOf(msg.sender) >= amount, "Insufficient balance");
        _transfer(msg.sender, address(this), amount);
        _approve(address(this), address(uniswapRouter), amount);

        ISwapRouter.ExactInputSingleParams memory params =
            ISwapRouter.ExactInputSingleParams({
                tokenIn: address(this),
                tokenOut: toToken,
                fee: 3000,
                recipient: msg.sender,
                deadline: block.timestamp + 300,
                amountIn: amount,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        uniswapRouter.exactInputSingle(params);
    }
}

1 Answer 1

0

It looks ok, but it's probably better to test it on a testnet first. The only bug I'm seeing is that you're not setting decimals correctly, so I suggest you change it to 18 (default).

Also remember to add liquidity before trying to swap.

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.