Skip to content

Commit

Permalink
Code reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
barak manos committed Aug 15, 2024
1 parent a17e548 commit 0744fcb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 42 deletions.
21 changes: 1 addition & 20 deletions src/trade-matcher/trade.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MAX_UINT128, MAX_UINT256, uint128, add, sub, mul, mulDivF, mulDivC, minFactor } from './utils';
import { ONE_48, Decimal, BigNumber, BigNumberMax } from '../utils/numerics';
import { EncodedOrder, DecodedOrder } from '../common/types';
import { decodeFloatInitialRate } from '../utils/encoders';
Expand All @@ -7,26 +8,6 @@ const isLegacyTradeBySourceRange = getRuntimeConfig().legacyTradeBySourceRange;

const C = BigNumber.from(ONE_48);

const MAX_UINT128 = BigNumber.from(2).pow(128).sub(1);
const MAX_UINT256 = BigNumber.from(2).pow(256).sub(1);

function check(val: BigNumber, max: BigNumber) {
if (val.gte(0) && val.lte(max)) {
return val;
}
throw null;
}

const uint128 = (n: BigNumber) => check(n, MAX_UINT128);
const add = (a: BigNumber, b: BigNumber) => check(a.add(b), MAX_UINT256);
const sub = (a: BigNumber, b: BigNumber) => check(a.sub(b), MAX_UINT256);
const mul = (a: BigNumber, b: BigNumber) => check(a.mul(b), MAX_UINT256);
const mulDivF = (a: BigNumber, b: BigNumber, c: BigNumber) =>
check(a.mul(b).div(c), MAX_UINT256);
const mulDivC = (a: BigNumber, b: BigNumber, c: BigNumber) =>
check(a.mul(b).add(c).sub(1).div(c), MAX_UINT256);
const minFactor = (a: BigNumber, b: BigNumber) => mulDivC(a, b, MAX_UINT256);

//
// x * (A * y + B * z) ^ 2
// ---------------------------------
Expand Down
26 changes: 4 additions & 22 deletions src/trade-matcher/trade_gradient.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
import { BigNumber } from '../utils/numerics';

const MAX_UINT128 = BigNumber.from(2).pow(128).sub(1);
const MAX_UINT256 = BigNumber.from(2).pow(256).sub(1);

function check(val: BigNumber, max: BigNumber) {
if (val.gte(0) && val.lte(max)) {
return val;
}
throw null;
}

const uint128 = (n: BigNumber) => check(n, MAX_UINT128);
const add = (a: BigNumber, b: BigNumber) => check(a.add(b), MAX_UINT256);
const mul = (a: BigNumber, b: BigNumber) => check(a.mul(b), MAX_UINT256);
const mulDivF = (a: BigNumber, b: BigNumber, c: BigNumber) =>
check(a.mul(b).div(c), MAX_UINT256);
const mulDivC = (a: BigNumber, b: BigNumber, c: BigNumber) =>
check(a.mul(b).add(c).sub(1).div(c), MAX_UINT256);
const minFactor = (a: BigNumber, b: BigNumber) => mulDivC(a, b, MAX_UINT256);
import { MAX_UINT128, uint128, add, mul, mulDivF, mulDivC, minFactor } from './utils';
import { ONE_48, ONE_24, BigNumber } from '../utils/numerics';

const EXP_ONE = BigNumber.from("0x0080000000000000000000000000000000"); // 1
const EXP_MID = BigNumber.from("0x0400000000000000000000000000000000"); // 8
const EXP_MAX = BigNumber.from("0x2cb53f09f05cc627c85ddebfccfeb72758"); // ceil(ln2) * 129
const EXP_LN2 = BigNumber.from("0x0058b90bfbe8e7bcd5e4f1d9cc01f97b58"); // ceil(ln2)

const R_ONE = BigNumber.from(1).shl(48); // = 2 ^ 48
const M_ONE = BigNumber.from(1).shl(24); // = 2 ^ 24
const R_ONE = BigNumber.from(ONE_48); // = 2 ^ 48
const M_ONE = BigNumber.from(ONE_24); // = 2 ^ 24

const RR = R_ONE.mul(R_ONE); // = 2 ^ 96
const MM = M_ONE.mul(M_ONE); // = 2 ^ 48
Expand Down
19 changes: 19 additions & 0 deletions src/trade-matcher/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { Rate } from '../common/types';
import { BigNumber } from '../utils/numerics';

export const MAX_UINT128 = BigNumber.from(2).pow(128).sub(1);
export const MAX_UINT256 = BigNumber.from(2).pow(256).sub(1);

export const uint128 = (n: BigNumber) => check(n, MAX_UINT128);
export const add = (a: BigNumber, b: BigNumber) => check(a.add(b), MAX_UINT256);
export const sub = (a: BigNumber, b: BigNumber) => check(a.sub(b), MAX_UINT256);
export const mul = (a: BigNumber, b: BigNumber) => check(a.mul(b), MAX_UINT256);
export const mulDivF = (a: BigNumber, b: BigNumber, c: BigNumber) => check(a.mul(b).div(c), MAX_UINT256);
export const mulDivC = (a: BigNumber, b: BigNumber, c: BigNumber) => check(a.mul(b).add(c).sub(1).div(c), MAX_UINT256);
export const minFactor = (a: BigNumber, b: BigNumber) => mulDivC(a, b, MAX_UINT256);

export const sortByMinRate = (x: Rate, y: Rate): number => {
const lhs = x.output.mul(y.input);
Expand All @@ -12,3 +24,10 @@ export const sortByMinRate = (x: Rate, y: Rate): number => {
};

export const sortByMaxRate = (x: Rate, y: Rate): number => sortByMinRate(y, x);

function check(val: BigNumber, max: BigNumber) {
if (val.gte(0) && val.lte(max)) {
return val;
}
throw null;
}

0 comments on commit 0744fcb

Please sign in to comment.