Advertisement
lscofield

amountout.js

Aug 11th, 2022 (edited)
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const {
  2.     Instance,
  3.     TokenDecimals } = require('./contract')
  4. const Decode = require('./decode')
  5. const { config } = require('./getConfig')
  6.  
  7. const getAmountOut = async (tokenIn, tokenOut, amount) => {
  8.     const ContractQuickSwap = (await Instance(
  9.         Types.ROUTER,
  10.         config.routerAddress, config)).methods
  11.  
  12.     const tokenInDecimals = await TokenDecimals(tokenIn)
  13.     const tokenOutDecimals = await TokenDecimals(tokenOut)
  14.     const amountIn = Decode.ToWei(amount, tokenInDecimals)
  15.  
  16.     const amounts = await ContractQuickSwap.getAmountsOut(amountIn, [tokenIn, tokenOut]).call()
  17.     var amountOutMin = Decode.FromWei(amounts[amounts.length - 1], tokenOutDecimals)
  18.     const expectedAmount = amountOutMin
  19.     amountOutMin -= ((amountOutMin * (config.slippage / 100)))
  20.  
  21.     return {
  22.         amountOutMin,
  23.         expectedAmount
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement