Skip to content

Commit

Permalink
encodeOrder better error handling and helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zavelevsky committed Jul 8, 2024
1 parent aa92762 commit f42164f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/utils/encoders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ export const encodeOrders = ([order0, order1]: [DecodedOrder, DecodedOrder]): [
return [encodeOrder(order0), encodeOrder(order1)];
};

export const isOrderEncodable = (order: DecodedOrder) => {
try {
encodeOrder(order);
return true;
} catch (e) {
return false;
}
};

/**
* Checks if the rates are equal after scaling them
* @param {string} x - the first rate
* @param {string} y - the second rate
* @returns {boolean} - true if the rates are equal after scaling, false otherwise
*/
export const scaleRatesAndCheckIsEqual = (x: string, y: string) => {
const xDec = new Decimal(x);
const yDec = new Decimal(y);
const xScaled = DecToBn(encodeRate(xDec));
const yScaled = DecToBn(encodeRate(yDec));
return xScaled.eq(yScaled);
};

export const encodeOrder = (
order: DecodedOrder,
z?: BigNumber
Expand All @@ -73,13 +96,16 @@ export const encodeOrder = (
const highestRate = new Decimal(order.highestRate);
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));

if (
!(
(highestRate.gte(marginalRate) && marginalRate.gt(lowestRate)) ||
(highestRate.eq(marginalRate) && marginalRate.eq(lowestRate)) ||
(highestRate.gt(marginalRate) &&
marginalRate.eq(lowestRate) &&
liquidity.isZero())
(H.gte(M) && M.gt(L)) ||
(H.eq(M) && M.eq(L)) ||
(H.gt(M) && M.eq(L) && y.isZero())
)
)
throw new Error(
Expand All @@ -90,11 +116,6 @@ export const encodeOrder = (
`(highestRate = ${highestRate}, marginalRate = ${marginalRate}, lowestRate = ${lowestRate}), liquidity = ${liquidity}`
);

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

return {
y,
z:
Expand Down
67 changes: 67 additions & 0 deletions tests/encoders.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from 'chai';
import {
encodeOrder,
isOrderEncodable,
scaleRatesAndCheckIsEqual,
decodeOrder,
calculateRequiredLiquidity,
calculateCorrelatedZ,
Expand Down Expand Up @@ -99,6 +101,27 @@ describe('encoders', () => {
);
});

it('should throw an exception when high price is higher than mid which is too close to min - so scaled rates are equal', () => {
const order = {
liquidity: '30424317585815',
lowestRate:
'9.999999999999947841981611412981873775987881042119111152377541884561651386320590972900390625',
highestRate:
'99.90009990009982561394106455162611276581802969426471250358190445695072412490844726562500000000000002',
marginalRate:
'9.999999999999973770354085873786350785392842669271401327708947225166629806745858567718077125618947319',
};
expect(() => {
encodeOrder(order);
}).to.throw(
'Either one of the following must hold:\n' +
'- highestRate >= marginalRate > lowestRate\n' +
'- highestRate == marginalRate == lowestRate\n' +
'- (highestRate > marginalRate == lowestRate) AND liquidity == 0\n' +
`(highestRate = ${order.highestRate}, marginalRate = ${order.marginalRate}, lowestRate = ${order.lowestRate}), liquidity = ${order.liquidity}`
);
});

it('should return the original order after computing and uncomputing, with tolerance', () => {
const originalOrder = {
liquidity: '100000000000000000000',
Expand Down Expand Up @@ -144,6 +167,50 @@ describe('encoders', () => {
});
});

describe('isOrderEncodable', () => {
it('should return false when high price is higher than mid which is too close to min - so scaled rates are equal', () => {
const order = {
liquidity: '30424317585815',
lowestRate:
'9.999999999999947841981611412981873775987881042119111152377541884561651386320590972900390625',
highestRate:
'99.90009990009982561394106455162611276581802969426471250358190445695072412490844726562500000000000002',
marginalRate:
'9.999999999999973770354085873786350785392842669271401327708947225166629806745858567718077125618947319',
};
expect(isOrderEncodable(order)).to.be.false;
});
it('should return true when the order is encodable', () => {
const order = {
liquidity: '100',
lowestRate: '0.5',
highestRate: '0.5',
marginalRate: '0.5',
};
expect(isOrderEncodable(order)).to.be.true;
});
});

describe('scaleRatesAndCheckIsEqual', () => {
it('should return true when the rates are equal', () => {
const x = '0.5';
const y = '0.5';
expect(scaleRatesAndCheckIsEqual(x, y)).to.be.true;
});
it('should return false when the rates are not equal', () => {
const x = '0.5';
const y = '0.6';
expect(scaleRatesAndCheckIsEqual(x, y)).to.be.false;
});
it('should return true when the rates are only equal afr scaling', () => {
const x =
'9.999999999999947841981611412981873775987881042119111152377541884561651386320590972900390625';
const y =
'9.999999999999973770354085873786350785392842669271401327708947225166629806745858567718077125618947319';
expect(scaleRatesAndCheckIsEqual(x, y)).to.be.true;
});
});

describe('encodeStrategy', () => {
it('should return the expected value', () => {
const strategy = {
Expand Down

0 comments on commit f42164f

Please sign in to comment.