Token Quantity Conversion

You are referring to the Legacy version of KyberSwap docs.

For the most updated information, please refer to:

Token Amount Conversion

Since getExpectedRate returns a rate, not the amount, the following code snippets show how to convert to both source and destination token amounts, taking their decimals into account.

calcSrcQty

ParameterDescription

dstQty

ERC20 destination token amount in its decimals

srcDecimals

ERC20 source token decimals

dstDecimals

ERC20 destination token decimals

rate

src -> dst conversion rate, independent of token decimals

Returns: ERC20 source token amount in its decimals.

Javascript

// DISCLAIMER: Code snippets in this guide are just examples and you
// should always do your own testing. If you have questions, visit our
// https://t.me/KyberDeveloper.

function calcSrcQty(dstQty, srcDecimals, dstDecimals, rate) {
  const PRECISION = 10 ** 18;
  //source quantity is rounded up. to avoid dest quantity being too low.
  if (srcDecimals >= dstDecimals) {
    numerator = PRECISION * dstQty * 10 ** (srcDecimals - dstDecimals);
    denominator = rate;
  } else {
    numerator = PRECISION * dstQty;
    denominator = rate * 10 ** (dstDecimals - srcDecimals);
  }
  return (numerator + denominator - 1) / denominator; //avoid rounding down errors
}

Solidity

Refer to the Utils contract.

calcDstQty

ParameterDescription

srcQty

ERC20 source token amount in its decimals

srcDecimals

ERC20 source token decimals

dstDecimals

ERC20 destination token decimals

rate

src -> dst conversion rate, independent of token decimals

Returns: ERC20 destination token amount in its decimals.

Javascript

// DISCLAIMER: Code snippets in this guide are just examples and you
// should always do your own testing. If you have questions, visit our
// https://t.me/KyberDeveloper.

function calcDstQty(srcQty, srcDecimals, dstDecimals, rate) {
  const PRECISION = 10 ** 18;
  if (dstDecimals >= srcDecimals) {
    return (srcQty * rate * 10 ** (dstDecimals - srcDecimals)) / PRECISION;
  } else {
    return (srcQty * rate) / (PRECISION * 10 ** (srcDecimals - dstDecimals));
  }
}

Solidity

Refer to the Utils contract.

calcRateFromQty

ParameterTypeDescription

srcAmount

Number

ERC20 source token amount in its decimals

destAmount

Number

ERC20 source token decimals

srcDecimals

Number

ERC20 destination token decimals

dstDecimals

Number

src -> dst conversion rate, independent of token decimals

Returns: Token conversion rate independent of token decimals

Javascript

// DISCLAIMER: Code snippets in this guide are just examples and you
// should always do your own testing. If you have questions, visit our
// https://t.me/KyberDeveloper.

function calcRateFromQty(srcAmount, destAmount, srcDecimals, dstDecimals) {
  const PRECISION = 10 ** 18;
  if (dstDecimals >= srcDecimals) {
    return (
      (destAmount * PRECISION) / (10 ** (dstDecimals - srcDecimals) * srcAmount)
    );
  } else {
    return (
      (destAmount * PRECISION * 10 ** (srcDecimals - dstDecimals)) / srcAmount
    );
  }
}

Solidity

Refer to the Utils2 contract.

Last updated

#477: Elastic fee tier and token launches

Change request updated