I'm struggling with "getAmountsOut" function, which works kinda fine for BNB/CAKE or CAKE/BNB. But it in some cases it just returns straight wrong values.


public async Task<decimal> GetAmountsOut(string token0, string token1, decimal wbnbAmountToSpend)
    string formattedValue = wbnbAmountToSpend.ToString("0.####");
    decimal result = decimal.Parse(formattedValue);
    var amountsOut = await _pancakeSwapV2Router.GetFunction("getAmountsOut").CallAsync<List<BigInteger>>(
        Web3.Convert.ToWei(result), new[] { token0, token1 });
    return Web3.Convert.FromWei(amountsOut[1]);

Method itself returns 0.00000000119623015M amount of NVTN for 0.003 BNB while on exchange I get normal value: Same goes for vice-versa:

enter image description here

While in code for NVTN/BNB with same amount of 0.003 it returns: 739.338138502378758758M

Can you see the mistake? Maybe this is decimals/conversion problems?

UPD: Yea, this seems to be a problem with decimals, but I don't understand why it happens in first place if both tokens are 9 decimals. Also, why this doesn't happen with BNB/CAKE for example?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.