根据Bancor官方提供的解决方案,需要更新smart contract中的deposit函数。具体操作如下:
require(amount > 0, "Deposit amount needs to be greater than 0");
require(lpt.balanceOf(address(this)) >= amount, "Not enough liquidity in the pool");
这样可以确保用户不能将0 ETH或负数存入合约中,并检查合约中是否有足够的流动性。
(bool success, ) = msg.sender.call{value: amount}("");
require(success, "Failed to send Ether");
这样可以确保在向合约中添加ETH时,用户地址的余额不会减少。
function liquidityChanged(uint256 ethAmount, uint256 tokenAmount) internal virtual override {
require(ethAmount > 0 && tokenAmount > 0, "Both amounts need to be greater than 0");
uint256 totalSupply = 0;
uint256 poolBalance = 0;
if (address(lpt) == bancor_eth_cross_connector_address) {
(uint256 reserveRatio, ) = bancor_get_ratios();
poolBalance = address(this).balance.sub(ethAmount);
totalSupply = poolBalance.div(reserveRatio);
} else {
totalSupply = lpt.totalSupply();
poolBalance = address(token).balanceOf(address(this));
}
if (lpt.balanceOf(address(this)) < totalSupply) {
lpt.issue(msg.sender, totalSupply.sub(lpt.balanceOf(address(this))));
} else {
lpt.transfer(msg.sender, lpt.balanceOf(address(this)).sub(totalSupply));
}
}
这样,当用户向合约中存入ETH时,会正确计算和转移LP token数量,确保