如何为我的代币添加流动性,以便我可以将其兑换成以太坊?

问题描述 投票:0回答:1

我正在为以太坊制定一个可靠的智能合约,我希望能够让用户将我的代币交换为以太坊。为了做到这一点,我需要将流动性添加到我的代币中,以便用户可以进行交换。到目前为止我的代码是:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";

contract YourToken is ERC20, Ownable, ReentrancyGuard {
    IUniswapV2Router02 public uniswapRouter;
    address public uniswapPair;

    event TokensSwappedAndLiquidityAdded(uint256 inputAmount, uint256 outputAmount, uint256 liquidityTokens);
    event TokensMinted(address indexed account, uint256 amount);

    constructor(address _uniswapRouter) ERC20("Infuraa Token", "INXT") Ownable(msg.sender) {
        uniswapRouter = IUniswapV2Router02(_uniswapRouter);
        uniswapPair = IUniswapV2Factory(uniswapRouter.factory()).createPair(address(this), uniswapRouter.WETH());
        _mint(msg.sender, 980 * 10**12 * 10**18);
    }

    // Function to swap tokens for ETH and add liquidity
    function swapTokensForEthAndAddLiquidity(uint256 tokenAmount) external nonReentrant {
        require(balanceOf(msg.sender) >= tokenAmount, "Insufficient balance");

        // Approve the router to spend the tokens
        _approve(address(this), address(uniswapRouter), tokenAmount);

        // Perform the swap
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapRouter.WETH();

        uint256 initialEthBalance = address(this).balance;

        uniswapRouter.swapExactTokensForETH(
            tokenAmount,
            0, // accept any amount of ETH
            path,
            msg.sender,
            block.timestamp
        );

        uint256 ethOutput = address(this).balance - initialEthBalance;

        // Add liquidity
 uint256 slippageAmount = tokenAmount * 5 / 100;  // 5% slippage
uniswapRouter.addLiquidityETH{value: ethOutput}(
    address(this),
    tokenAmount,
    tokenAmount - slippageAmount,  // minimum amount of token liquidity
    ethOutput - slippageAmount,   // minimum amount of ETH liquidity
    msg.sender,
    block.timestamp
);


        emit TokensSwappedAndLiquidityAdded(tokenAmount, ethOutput, 0);
    }

    // Function to set the Uniswap router address (in case it needs to be updated)
   function setUniswapRouter(address _uniswapRouter) external onlyOwner {
    uniswapRouter = IUniswapV2Router02(_uniswapRouter);

    // Check if the pair already exists
    address pair = IUniswapV2Factory(uniswapRouter.factory()).getPair(address(this), uniswapRouter.WETH());
    if (pair == address(0)) {
        // Pair does not exist, create it
        uniswapPair = IUniswapV2Factory(uniswapRouter.factory()).createPair(address(this), uniswapRouter.WETH());
    } else {
        // Pair already exists, use the existing pair
        uniswapPair = pair;
    }
}


    // Function to mint additional tokens, only callable by the owner
    function mint(address account, uint256 amount) external onlyOwner {
        _mint(account, amount);
        emit TokensMinted(account, amount);
    }

    receive() external payable {}
}

当我尝试在 Remix 中运行“swapTokensForEthAndAddLiquidity”时,它给我一个错误:

气体估计错误并显示以下消息(见下文)。交易执行可能会失败。您想强制发送吗? 尝试执行智能合约内的函数时发生错误。

然后控制台说流动性不足。如何正确地为我的代码添加流动性,以便用户可以将我的代币交换为以太坊?

solidity smartcontracts
1个回答
0
投票

我不会深入探讨为什么您要在智能合约中与 uniswap 进行交互,因为我认为这超出了本文的范围,但问题是您在添加任何流动性之前尝试交换

uniswapRouter.swapExactTokensForETH(
uniswapRouter.addLiquidityETH{value: ethOutput}(

© www.soinside.com 2019 - 2024. All rights reserved.