0

i have a question about interacting with the Uniswap quoter contract.

I am able to get a quote using quoteExactInputSingle. The method has 1 parameter for the input amount, which works fine when it is an integer like 100. The contract parameter is a uint (therefore we must use something equivalent in javascript that can be converted using jsbn/jsbi).

Does anyone know how to get quotes using fractional input amounts instead? E.g. wanting to swap 0.00014 UNI in javascript:

const amountIn = 0.00014;
await quoterContract.callStatic.quoteExactInputSingle(
  token0Address,
  token1Address,
  poolFee,
  amountIn,  // The parameter for input amount
  0
);

produces:

Error: invalid BigNumber string (argument="value", value="0.00014", code=INVALID_ARGUMENT, version=bignumber/5.7.0)
  reason: 'invalid BigNumber string',
  code: 'INVALID_ARGUMENT',
  argument: 'value',
  value: '0.00014'
}


I have tried finding the answer in the uniswap source code:

I did notice that the uniswap product multiplies decimal amounts by 20 decimal places (before sending the amount in a http request to their API), which is then converted "back" to a decimal in a smart contract (after they identify the number of decimal places for the input token). This is obviously because the EVM doesn't support fractions, however i can't find an explanation for why the multiplication is done to exactly 20 decimal places.

I do also know about the Token sdk class, e.g.:

const token = new Token(5, token0Address, 18, 'UNI', 'Uniswap');

However i haven't found a way to use the Token instance with the quoter contract.

1 Answer 1

0

i just needed to convert the token balance to token amount in the contract call, silly me :). Theres no such thing as fractional tokens.

const amountIn = 0.00014;  // before
const amountIn = new Decimal('0.00014e18').toString();  // after. This specific token in this example has 18 decimal places

await quoterContract.callStatic.quoteExactInputSingle(
  token0Address,
  token1Address,
  poolFee,
  amountIn,
  0
);

I also figured out that the uniswap front-end caches quotes to an extent, hence i was getting thrown off with seeing token amount being passed in as 0.00014e20 instead of 0.00014e18 in the http call history.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.