ETH交易值始终0

发布于 2025-01-24 02:32:37 字数 1983 浏览 0 评论 0原文

我正在尝试使用uniswap v3为我的项目添加掉期。每当我从前端调用合同中的功能时,值为0 ETH中的MetAmask中,只有支付费用。 我尝试了所有可以想到的方式将输入字符串转换为WEI,GWEI,HEX等,等等在MetAmask交易页面上始终为0。

function transferApprove(uint _amountIn, address _routerAddress) internal
    {
        TransferHelper.safeTransferFrom(
            ETH,
            msg.sender,
            address(this),
            _amountIn
        );

        TransferHelper.safeApprove(WETH9, _routerAddress, _amountIn);
    }

    function swapExactInputSingle(uint _amountIn)  external returns (uint amountOut)
    {
      require(_amountIn > 0, "cannot swap 0 ETH");
      emit value(_amountIn);
        transferApprove(_amountIn, UNISWAP_V3_ROUTER);

        ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
            .ExactInputSingleParams({
                tokenIn: ETH,
                tokenOut: WETH9,
                fee: 3000,
                recipient: msg.sender,
                deadline: block.timestamp,
                amountIn: _amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        amountOut = uniswapRouter.exactInputSingle(params);
    }

///// JAvaScript部分

const sendSwap = async () => {
try{
    if(!window.ethereum) return alert("Please install metamask");
    const parsedAmount = ethers.utils.parseUnits(amount, 18);
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
    const signer = provider.getSigner()
    const swap = new ethers.Contract(SwapAddress.address, SwapAbi.abi, signer)
    
    const accounts = await ethereum.request({method:'eth_requestAccounts'})
    setCurrentAccount(accounts[0])
    setSwap(swap)

    console.log(parsedAmount)
    const transactionHash = await swap.swapExactInputSingle(parsedAmount)
    setIsLoading(true);
    await transactionHash.wait();
    setIsLoading(false);
    console.log({transactionHash});
} catch (error) {
    console.log(error);
}

I am trying to use uniswap v3 to add swaps to my project. Everytime i call the function in my contract from the frontend the value is 0 ETH in metamask and only the fee is paid.
I have attempted all the ways i can think of converting the input string into wei, gwei, hex and so on and its always 0 on the metamask transaction page.

function transferApprove(uint _amountIn, address _routerAddress) internal
    {
        TransferHelper.safeTransferFrom(
            ETH,
            msg.sender,
            address(this),
            _amountIn
        );

        TransferHelper.safeApprove(WETH9, _routerAddress, _amountIn);
    }

    function swapExactInputSingle(uint _amountIn)  external returns (uint amountOut)
    {
      require(_amountIn > 0, "cannot swap 0 ETH");
      emit value(_amountIn);
        transferApprove(_amountIn, UNISWAP_V3_ROUTER);

        ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
            .ExactInputSingleParams({
                tokenIn: ETH,
                tokenOut: WETH9,
                fee: 3000,
                recipient: msg.sender,
                deadline: block.timestamp,
                amountIn: _amountIn,
                amountOutMinimum: 0,
                sqrtPriceLimitX96: 0
            });

        amountOut = uniswapRouter.exactInputSingle(params);
    }

/////JAVASCRIPT SECTION

const sendSwap = async () => {
try{
    if(!window.ethereum) return alert("Please install metamask");
    const parsedAmount = ethers.utils.parseUnits(amount, 18);
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
    const signer = provider.getSigner()
    const swap = new ethers.Contract(SwapAddress.address, SwapAbi.abi, signer)
    
    const accounts = await ethereum.request({method:'eth_requestAccounts'})
    setCurrentAccount(accounts[0])
    setSwap(swap)

    console.log(parsedAmount)
    const transactionHash = await swap.swapExactInputSingle(parsedAmount)
    setIsLoading(true);
    await transactionHash.wait();
    setIsLoading(false);
    console.log({transactionHash});
} catch (error) {
    console.log(error);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文