Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…nto simplify-function-encodeRate
  • Loading branch information
barak manos committed Aug 12, 2024
2 parents a375d2e + b8b241d commit feffce5
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 60 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@typechain/ethers-v5": "^11.1.1",
"@types/chai": "^4.3.10",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.0",
"@types/node": "^18.18.8",
"@types/node": "^18.19.17",
"@types/sinon": "^10.0.15",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.12.0",
"chai": "^4.3.10",
"eslint": "^8.46.0",
Expand All @@ -108,8 +108,8 @@
"sinon": "^16.0.0",
"ts-node": "^10.9.1",
"tslib": "^2.6.2",
"typechain": "^8.3.1",
"typescript": "^5.2.2"
"typechain": "^8.3.2",
"typescript": "^5.5.4"
},
"dependencies": {
"@ethersproject/abi": "^5.7.0",
Expand Down
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 areScaledRatesEqual = (x: string, y: string): boolean => {
const xDec = new Decimal(x);
const yDec = new Decimal(y);
const xScaled = encodeRate(xDec);
const yScaled = 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 = encodeRate(lowestRate);
const H = encodeRate(highestRate);
const M = 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 = encodeRate(lowestRate);
const H = encodeRate(highestRate);
const M = 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,
areScaledRatesEqual,
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('areScaledRatesEqual', () => {
it('should return true when the rates are equal', () => {
const x = '0.5';
const y = '0.5';
expect(areScaledRatesEqual(x, y)).to.be.true;
});
it('should return false when the rates are not equal', () => {
const x = '0.5';
const y = '0.6';
expect(areScaledRatesEqual(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(areScaledRatesEqual(x, y)).to.be.true;
});
});

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

0 comments on commit feffce5

Please sign in to comment.