Skip to content

Commit

Permalink
Merge pull request #143 from bancorprotocol/simplify-function-encodeRate
Browse files Browse the repository at this point in the history
Simplify function `encodeRate`
  • Loading branch information
zavelevsky authored Aug 12, 2024
2 parents b8b241d + feffce5 commit 55e1394
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/utils/encoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function bitLength(value: BigNumber) {
export const encodeRate = (value: Decimal) => {
const data = DecToBn(value.sqrt().mul(ONE).floor());
const length = bitLength(data.div(ONE));
return BnToDec(data.shr(length).shl(length));
return data.shr(length).shl(length);
};

export const decodeRate = (value: Decimal) => {
Expand Down Expand Up @@ -82,8 +82,8 @@ export const isOrderEncodable = (order: DecodedOrder) => {
export const areScaledRatesEqual = (x: string, y: string): boolean => {
const xDec = new Decimal(x);
const yDec = new Decimal(y);
const xScaled = DecToBn(encodeRate(xDec));
const yScaled = DecToBn(encodeRate(yDec));
const xScaled = encodeRate(xDec);
const yScaled = encodeRate(yDec);
return xScaled.eq(yScaled);
};

Expand All @@ -97,9 +97,9 @@ export const encodeOrder = (
const marginalRate = new Decimal(order.marginalRate);

const y = DecToBn(liquidity);
const L = DecToBn(encodeRate(lowestRate));
const H = DecToBn(encodeRate(highestRate));
const M = DecToBn(encodeRate(marginalRate));
const L = encodeRate(lowestRate);
const H = encodeRate(highestRate);
const M = encodeRate(marginalRate);

if (
!(
Expand Down Expand Up @@ -155,11 +155,9 @@ export const calculateRequiredLiquidity = (
vagueOrder: DecodedOrder
): string => {
const z: BigNumber = calculateCorrelatedZ(knownOrder);
const L: BigNumber = DecToBn(encodeRate(new Decimal(vagueOrder.lowestRate)));
const H: BigNumber = DecToBn(encodeRate(new Decimal(vagueOrder.highestRate)));
const M: BigNumber = DecToBn(
encodeRate(new Decimal(vagueOrder.marginalRate))
);
const L: BigNumber = encodeRate(new Decimal(vagueOrder.lowestRate));
const H: BigNumber = encodeRate(new Decimal(vagueOrder.highestRate));
const M: BigNumber = encodeRate(new Decimal(vagueOrder.marginalRate));

return z.mul(M.sub(L)).div(H.sub(L)).toString();
};
Expand Down

0 comments on commit 55e1394

Please sign in to comment.