我正在尝试创建一个 bep20 代币,其中一定比例的交易量转换为 BNB,然后发送到指定的慈善钱包以存储 BNB 以备将来捐赠。该代币的另一个特点是交易的另一部分也被交换为 BNB,并与流动性池中的代币锁定在一起。最后一个功能正在运行,但是,我设置为慈善钱包的钱包没有接收 BNB。我已经用测试代币和少量 BNB 建立了一个流动性池,这就是我如何了解到它没有按照它的设计目的进行的。这个代币是 YUMMY 的一个分支,完整的合约可以在这里查看:https://bscscan.com/address/0x05f2df7b3d612a23fe12162a6c996447dce728a5#code
function sendBNBToCharity(uint256 amount) private {
swapTokensForEth(amount);
_charityWalletAddress.transfer(address(this).balance);
}
function _setCharityWallet(address payable charityWalletAddress) external onlyOwner() {
_charityWalletAddress = charityWalletAddress;
}
function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
// split the contract balance into thirds
uint256 halfOfLiquify = contractTokenBalance.div(4);
uint256 otherHalfOfLiquify = contractTokenBalance.div(4);
uint256 portionForFees = contractTokenBalance.sub(halfOfLiquify).sub(otherHalfOfLiquify);
// 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;
// swap tokens for ETH
swapTokensForEth(halfOfLiquify); // <- this breaks the ETH -> HATE swap when swap+liquify is triggered
swapTokensForEth(portionForFees);
// how much ETH did we just swap into?
uint256 newBalance = address(this).balance.sub(initialBalance);
// add liquidity to uniswap
addLiquidity(otherHalfOfLiquify, newBalance);
sendBNBToCharity(portionForFees);
emit SwapAndLiquify(halfOfLiquify, newBalance, otherHalfOfLiquify);
}
function swapTokensForEth(uint256 tokenAmount) private {
// generate the uniswap pair path of token -> weth
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = uniswapV2Router.WETH();
_approve(address(this), address(uniswapV2Router), tokenAmount);
// make the swap
uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
tokenAmount,
0, // accept any amount of ETH
path,
address(this),
block.timestamp
);
}
答案 0 :(得分:1)
您的 swapTokensForEth
函数正在交换为 WETH,而不是 ETH。 Wrapped ETH 是一种持有 ETH 的币,使其具有 ERC20 代币的作用,因此可以在 Uniswap 等地方使用。
要从您的 WETH 中取出 ETH,您需要致电 WETH.withdraw(amount)
,这会将您的 WETH 转换回 ETH。