Pancakeswap ZAPInBNBRebalancing function Issue.
Tags: pancakeswap, zaprouter
Published: 2022-10-15 21:47:57

Hello everyone!
I’m fairly new to coding and the forum so I’m hoping I’m posting this in the right place.
On to my question:
As part of an automation project, I’m interacting with ERC20 & Pancakeswap Router & ZAP contracts where I need to the following:

read info from contracts (erc20 tokens, LP contracts, etc.) - Working
swap erc20 tokens - Working
zap tokens into LP pools - Not working
I went throught the ZAP router ABI and think I'm calling the right function but with the right parameters but not sure..

code snippet:
…
code defining vars, contracts, ABIs, pricing/helper functions, etc.
…
```
lpToken1.amountIn = await coinContracts[token1].balanceOf(account)
lpToken1.decimals = await coinContracts[token1].decimals()
lpToken1.address = tokens[token1].address
lpToken2.amountIn = await coinContracts[token2].balanceOf(account)
lpToken2.decimals = await coinContracts[token2].decimals()
lpToken2.address = tokens[token2].address
lpToken.address = tokens[lptoken].address
```

Console log Output to make sure token1 & 2 have the correct data
token1 price in USD: 269.76639309475314
token1 balance in USD: 79.72137050869273
token1 balance in LP: 0.295520022320538927

token2 price in USD: 122.43023186809978
token2 balance in USD: 0.2733755311081344
token2 balance in LP: 0.002232908710020704

Since we are zapping a single token (wbnb in this case) into an Liquidity Pool, rebalancing needs to take place (split wbnb amount into equals amount of wbnb/token2)…

//call function to estimate needed amounts
```
amounts = await PancakeswapZapContract.estimateZapInRebalancingSwap(
lpToken1.address,
lpToken2.address,
lpToken1.amountIn,
lpToken2.amountIn,
lpToken.address,
{
gasPrice: mygasPrice,
gasLimit: ‘310000’
}
);
```

// Log Output of estimate function to double check values/math.. Returned values seem close enough..
amounts[0] in Dec: 0.147439545491919011
amounts[0] in Dec w/ Slippage: 0.14449075458208063
amounts[0] in Ether w/ Slippage: 144490754582080630

amounts[1] in Dec: 0.326677820074572399
amounts[1] in Dec w/Slippage: 0.32014426367308096
amounts[1] in Ether w/Slippage: 320144263673080960
Are we selling wbnb? true

//Assign values to amountInMax & amountOutMin
```
amountInMax = amounts[0] // This is the estimated amount of bnb needed to buy the required amount of token2
amountOutMin = amounts[1] // This is the estimated amount of token2 needed
```

//Call the ZapIn rebalancing function with above values. The last parameter specifies token1 is the one being spent/sold for the zap.
```
txHash = await PancakeswapZapContract.callStatic.zapInBNBRebalancing(
lpToken1.address,
lpToken1.amountIn,
lpToken.address,
amountInMax,
amountOutMin,
isToken1Sold,
{
gasPrice: mygasPrice,
gasLimit: ‘310000’
}
);
```
The above function calls keeps failing with the following error:
reason: ‘SafeERC20: low-level call failed’,
code: ‘CALL_EXCEPTION’,
method: ‘zapInBNBRebalancing(address,uint256,address,uint256,uint256,bool)’,
errorArgs: [ ‘SafeERC20: low-level call failed’ ],
errorName: ‘Error’,
errorSignature: ‘Error(string)’,

Any ideas as to what could be wrong?
Thanks!!