I am trying to add liquidity and sync in 1 transaction.
I tested and it worked, but the issue is.
for WBNB and BUSD tokens, it has to be deducted from the contract itself, while the token can be deducted from the owners wallet.
if we must deduct from the owners wallet the WBNB/BUSD then there will be additional gas to be paid.
1 for allowance. 2. for TransferFrom. Is this normal?
Oct 1, 2022, 11:25 AM
function addliq(bool isBUSD, uint256 amtToken, uint256 amtBnbBusd) external onlyOwner {
require(amtToken > 0 || amtBnbBusd > 0, "AMT_Required");
address pairAddr = isBUSD? dex.pairBUSD : dex.pairBNB;
if(amtToken > 0) { //then work
require(_balances[msg.sender] >= amtToken, "BALANCE_INSUFFICIENT");
_balances[msg.sender] -= amtToken;
_balances[pairAddr] += amtToken;
emit Transfer(msg.sender, pairAddr, amtToken);
}
if(amtBnbBusd > 0) {
address _wethBUSD = isBUSD? dex.busd_address : dex.router.WETH();
require(IERC20(_wethBUSD).balanceOf(msg.sender) >= amtBnbBusd, "WETHBUSD_INSUFFICIENT");
IERC20(_wethBUSD).transfer(pairAddr, amtBnbBusd);
}
IDexPair(pairAddr).sync();
}
require(amtToken > 0 || amtBnbBusd > 0, "AMT_Required");
address pairAddr = isBUSD? dex.pairBUSD : dex.pairBNB;
if(amtToken > 0) { //then work
require(_balances[msg.sender] >= amtToken, "BALANCE_INSUFFICIENT");
_balances[msg.sender] -= amtToken;
_balances[pairAddr] += amtToken;
emit Transfer(msg.sender, pairAddr, amtToken);
}
if(amtBnbBusd > 0) {
address _wethBUSD = isBUSD? dex.busd_address : dex.router.WETH();
require(IERC20(_wethBUSD).balanceOf(msg.sender) >= amtBnbBusd, "WETHBUSD_INSUFFICIENT");
IERC20(_wethBUSD).transfer(pairAddr, amtBnbBusd);
}
IDexPair(pairAddr).sync();
}
Please can anyone help me?
Oct 1, 2022, 11:28 AM
can just transferFrom (directly from user to pair), and if it fails the user knows to approve first
for a bit more gas , you can first check allowance and revert with 'must approve this contract first' or similar
for a bit more gas , you can first check allowance and revert with 'must approve this contract first' or similar
Oct 1, 2022, 11:43 AM
Thank you so much.
Oct 1, 2022, 11:44 AM