5 改造safemoon的代码,将流动池由xxx-ETH-LP改为xxx-USDT-LP后,打开swap开关,转账时总会报错TransfeHelper:TRANSFER_FROM_FAILED

1 safemoon的逻辑是转账时token合约自己收取手续费,然后将手续费添加进流动性池。 2 改造后的swapAndLiquidity函数实现如下

function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
        // split the contract balance into halves
        uint256 half = contractTokenBalance.div(2);
        uint256 otherHalf = contractTokenBalance.sub(half);

        // capture the contract's current ETH balance.
        // this is so that we can capture exactly the amount of ETH that the
        // swap creates, and not make the liquidity event include any ETH that
        // has been manually sent to the contract
        // uint256 initialBalance = address(this).balance;
        uint256 initialBalance = usdtContract.balanceOf(address(this));

        // swap tokens for ETH // <- this breaks the ETH -> HATE swap when swap+liquify is triggered
        swapTokensForToken(half); 

        // how much ETH did we just swap into?
        // uint256 newBalance = address(this).balance.sub(initialBalance);

        uint256 newBalance = usdtContract.balanceOf(address(this)).sub(initialBalance);
        require(usdtContract.balanceOf(address(this)) > 0, "this Balance 0");
        require(newBalance > 0, "newBalance 0");
        require(otherHalf > 0, "otherHalf 0");
        // add liquidity to uniswap
        addLiquidity(otherHalf, newBalance);

        emit SwapAndLiquify(half, newBalance, otherHalf);
    }

3 一篇相关资料https://www.followchain.org/transfer-from-failed-pancakeswap/,但没有解决方案。

请先 登录 后评论

3 个回答

zoie.zhang
请先 登录 后评论
Tiny熊 - 布道者
  擅长:智能合约,以太坊
请先 登录 后评论
czl
请先 登录 后评论
  • 3 关注
  • 0 收藏,125 浏览
  • alba 提出于 2021-07-22 11:20