I made a contract that swap has two methods, one to swap from wbnb to token and one from token to wbnb. I am noticing differences in the gas consumed, even if I reduce it to the bone with everything hard-coded and leave only the transfer and swap functions when I swap from wbnb to token I use 109k of gas and if I do token to wbnb 83k. How is this difference possible by calling the same functions?

Mar 9, 2022, 12:35 PM
function sellZeroFeeToken(address _tokenToSell) external onlyOwner {
IERC20 tSell = IERC20(token);
uint112 balance = uint112(tSell.balanceOf(address(this)));
require(balance > 0, "Fail with error B");

IPancakePair pair = IPancakePair(dexFactory.getPair(WETHAdd, token));
(address token0, ) = sortTokens(WETHAdd, token);
tSell.transfer(address(pair), balance);
pair.swap(
token == token0 ? 0 : 900000000000000,
token == token0 ? 900000000000000 : 0,
address(this),
new bytes(0)
);
}

function buyTokenLight() external onlyOwner {
IPancakePair pair = IPancakePair(dexFactory.getPair(WETHAdd, token));
WETH.transfer(address(pair), 1000000000000000);
(address token0, ) = sortTokens(WETHAdd, token);
(uint112 amount0Out, uint112 amount1Out) = WETHAdd == token0
? (uint112(0), uint112(1309453737266632200))
: (uint112(1309453737266632200), uint112(0));
pair.swap(amount0Out, amount1Out, address(this), new bytes(0));
}
The inputs are hard-coded to see only the cost of the swap, buy 109k sell 81k. How is it possible?
Mar 9, 2022, 12:52 PM

© 2024 Draquery.com All rights reserved.