<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 5bngr</title>
    <description>The latest articles on DEV Community by 5bngr (@5bngr).</description>
    <link>https://dev.to/5bngr</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F944732%2Fa708574a-2bd0-463d-a779-d0569388fe7f.png</url>
      <title>DEV Community: 5bngr</title>
      <link>https://dev.to/5bngr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/5bngr"/>
    <language>en</language>
    <item>
      <title>Pancakeswap ZAPInBNBRebalancing function Issue.</title>
      <dc:creator>5bngr</dc:creator>
      <pubDate>Sat, 15 Oct 2022 21:47:57 +0000</pubDate>
      <link>https://dev.to/5bngr/pancakeswap-zapinbnbrebalancing-function-issue-4fn7</link>
      <guid>https://dev.to/5bngr/pancakeswap-zapinbnbrebalancing-function-issue-4fn7</guid>
      <description>&lt;p&gt;Hello everyone!&lt;br&gt;
I’m fairly new to coding and the forum so I’m hoping I’m posting this in the right place.&lt;br&gt;
On to my question:&lt;br&gt;
As part of an automation project, I’m interacting with ERC20 &amp;amp; Pancakeswap Router &amp;amp; ZAP contracts where I need to the following:&lt;/p&gt;

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

&lt;p&gt;code snippet:&lt;br&gt;
…&lt;br&gt;
code defining vars, contracts, ABIs, pricing/helper functions, etc.&lt;br&gt;
…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Console log Output to make sure token1 &amp;amp; 2 have the correct data&lt;br&gt;
token1 price in USD: 269.76639309475314&lt;br&gt;
token1 balance in USD: 79.72137050869273&lt;br&gt;
token1 balance in LP: 0.295520022320538927&lt;/p&gt;

&lt;p&gt;token2 price in USD: 122.43023186809978&lt;br&gt;
token2 balance in USD: 0.2733755311081344&lt;br&gt;
token2 balance in LP: 0.002232908710020704&lt;/p&gt;

&lt;p&gt;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)…&lt;/p&gt;

&lt;p&gt;//call function to estimate needed amounts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;amounts = await PancakeswapZapContract.estimateZapInRebalancingSwap(
lpToken1.address,
lpToken2.address,
lpToken1.amountIn,
lpToken2.amountIn,
lpToken.address,
{
gasPrice: mygasPrice,
gasLimit: ‘310000’
}
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;amounts[1] in Dec: 0.326677820074572399&lt;br&gt;
amounts[1] in Dec w/Slippage: 0.32014426367308096&lt;br&gt;
amounts[1] in Ether w/Slippage: 320144263673080960&lt;br&gt;
Are we selling wbnb? true&lt;/p&gt;

&lt;p&gt;//Assign values to amountInMax &amp;amp; amountOutMin&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;//Call the ZapIn rebalancing function with above values. The last parameter specifies token1 is the one being spent/sold for the zap.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;txHash = await PancakeswapZapContract.callStatic.zapInBNBRebalancing(
lpToken1.address,
lpToken1.amountIn,
lpToken.address,
amountInMax,
amountOutMin,
isToken1Sold,
{
gasPrice: mygasPrice,
gasLimit: ‘310000’
}
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above function calls keeps failing with the following error:&lt;br&gt;
reason: ‘SafeERC20: low-level call failed’,&lt;br&gt;
code: ‘CALL_EXCEPTION’,&lt;br&gt;
method: ‘zapInBNBRebalancing(address,uint256,address,uint256,uint256,bool)’,&lt;br&gt;
errorArgs: [ ‘SafeERC20: low-level call failed’ ],&lt;br&gt;
errorName: ‘Error’,&lt;br&gt;
errorSignature: ‘Error(string)’,&lt;/p&gt;

&lt;p&gt;Any ideas as to what could be wrong?&lt;br&gt;
Thanks!!&lt;/p&gt;

</description>
      <category>pancakeswap</category>
      <category>zaprouter</category>
    </item>
  </channel>
</rss>
