Skip to content

Commit

Permalink
feat: use BI_POWS array instead of individual ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Verisana committed Apr 19, 2022
1 parent 224e57e commit 80109d8
Show file tree
Hide file tree
Showing 22 changed files with 69 additions and 86 deletions.
4 changes: 2 additions & 2 deletions dex-template/__DexName__-integration.test.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dotenv.config();

import { DummyDexHelper } from '../../dex-helper/index';
import { Network, SwapSide } from '../../constants';
import { BI_0, BI_POW_18 } from '../../bigint-constants';
import { BI_0, BI_POWS } from '../../bigint-constants';
import { __DexName__ } from './__DexNameParam__';
import {
checkPoolPrices,
Expand Down Expand Up @@ -34,7 +34,7 @@ const TokenA = Tokens[network][TokenASymbol];
const TokenBSymbol = 'TokenBSymbol';
const TokenB = Tokens[network][TokenBSymbol];

const amounts = [BI_0, BI_POW_18, BigInt('2000000000000000000')];
const amounts = [BI_0, BI_POWS[18], BigInt('2000000000000000000')];

const dexKey = '__DexName__';

Expand Down
4 changes: 2 additions & 2 deletions scripts/run-dex-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dotenv.config();
import { Network, SwapSide } from '../src/constants';
import { BalancerV2 } from '../src/dex/balancer-v2/balancer-v2';
import { DummyDexHelper } from '../src/dex-helper/index';
import { BI_0, BI_POW_18 } from '../src/bigint-constants';
import { BI_0, BI_POWS } from '../src/bigint-constants';

const WETH = {
address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
Expand Down Expand Up @@ -45,7 +45,7 @@ const bbadai = {
decimals: 18,
};

const amounts = [BI_0, BI_POW_18, BigInt('2000000000000000000')];
const amounts = [BI_0, BI_POWS[18], BigInt('2000000000000000000')];

async function main() {
const dexHelper = new DummyDexHelper(Network.MAINNET);
Expand Down
28 changes: 5 additions & 23 deletions src/bigint-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,11 @@ export const BI_248 = BigInt(248);
// Used only on API side
export const BI_200_000 = BigInt(200 * 1000);

export const BI_POW_3 = BigInt(10 ** 3);
export const BI_POW_4 = BigInt(10 ** 4);
export const BI_POW_5 = BigInt(10 ** 5);
export const BI_POW_6 = BigInt(10 ** 6);
export const BI_POW_7 = BigInt(10 ** 7);
export const BI_POW_8 = BigInt(10 ** 8);
export const BI_POW_9 = BigInt(10 ** 9);
export const BI_POW_10 = BigInt(10 ** 10);
export const BI_POW_11 = BigInt(10 ** 11);
export const BI_POW_12 = BigInt(10 ** 12);
export const BI_POW_13 = BigInt(10 ** 13);
export const BI_POW_14 = BigInt(10 ** 14);
export const BI_POW_15 = BigInt(10 ** 15);
export const BI_POW_16 = BigInt(10 ** 16);
export const BI_POW_17 = BigInt(10 ** 17);
export const BI_POW_18 = BigInt(10 ** 18);
export const BI_POW_19 = BigInt(10 ** 19);
export const BI_POW_20 = BigInt(10 ** 20);

// Not safe to use number for initialization
export const BI_POW_36 = BigInt(
'1' + '0000000000' + '0000000000' + '0000000000' + '000000',
);
// Index represents the number of zeros after 1
// We use as much as 36 zeros
export const BI_POWS = new Array(37)
.fill(undefined)
.map((_0, index) => BigInt(`1${'0'.repeat(index)}`));

export const BI_MAX_INT = BigInt(MAX_INT);
export const BI_MAX_UINT = BI_2 ** BigInt(256) - BI_1;
4 changes: 2 additions & 2 deletions src/dex/aave-v1/aave-v1-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { aaveV1GetToken } from './tokens';
import { BI_0, BI_POW_18 } from '../../bigint-constants';
import { BI_0, BI_POWS } from '../../bigint-constants';

describe('AaveV1', function () {
describe('AaveV1 MAINNET', () => {
Expand All @@ -25,7 +25,7 @@ describe('AaveV1', function () {
return;
}

const amounts = [BI_0, BI_POW_18, BigInt('2000000000000000000')];
const amounts = [BI_0, BI_POWS[18], BigInt('2000000000000000000')];

const dexKey = 'AaveV1';
it('getPoolIdentifiers and getPricesVolume SELL', async function () {
Expand Down
4 changes: 2 additions & 2 deletions src/dex/aave-v2/aave-v2-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { aaveV2GetToken } from './tokens';
import { BI_0, BI_POW_18 } from '../../bigint-constants';
import { BI_0, BI_POWS } from '../../bigint-constants';

describe('AaveV2', function () {
describe('AaveV2 MAINNET', () => {
Expand All @@ -21,7 +21,7 @@ describe('AaveV2', function () {
const aUSDTSymbol = 'aUSDT';
const aUSDT = aaveV2GetToken(network, aUSDTSymbol);

const amounts = [BI_0, BI_POW_18, BigInt('2000000000000000000')];
const amounts = [BI_0, BI_POWS[18], BigInt('2000000000000000000')];

const dexKey = 'AaveV2';
if (!aUSDT) {
Expand Down
4 changes: 2 additions & 2 deletions src/dex/aave-v3/aave-v3-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { getTokenFromASymbol } from './tokens';
import { BI_0, BI_POW_6 } from '../../bigint-constants';
import { BI_0, BI_POWS } from '../../bigint-constants';

/*
README
Expand All @@ -34,7 +34,7 @@ const TokenA = Tokens[network][TokenASymbol];
const TokenBSymbol = 'aUSDT';
const TokenB = getTokenFromASymbol(network, TokenBSymbol);

const amounts = [BI_0, BI_POW_6, BigInt('2000000')];
const amounts = [BI_0, BI_POWS[6], BigInt('2000000')];

const dexKey = 'AaveV3';

Expand Down
4 changes: 2 additions & 2 deletions src/dex/balancer-v2/StableMath.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BI_0, BI_1, BI_2, BI_POW_3 } from '../../bigint-constants';
import { BI_0, BI_1, BI_2, BI_POWS } from '../../bigint-constants';
import { MathSol } from './balancer-v2-math';

const AMP_PRECISION = BI_POW_3;
const AMP_PRECISION = BI_POWS[3];

export function _calculateInvariant(
amp: bigint,
Expand Down
4 changes: 2 additions & 2 deletions src/dex/balancer-v2/balancer-v2-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DummyDexHelper } from '../../dex-helper/index';
import { Network, SwapSide } from '../../constants';
import { BalancerV2 } from './balancer-v2';
import { checkPoolPrices, checkPoolsLiquidity } from '../../../tests/utils';
import { BI_0, BI_POW_18 } from '../../bigint-constants';
import { BI_0, BI_POWS } from '../../bigint-constants';

const WETH = {
address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
Expand All @@ -27,7 +27,7 @@ const BBAUSD = {
decimals: 18,
};

const amounts = [BI_0, BI_POW_18, BigInt('2000000000000000000')];
const amounts = [BI_0, BI_POWS[18], BigInt('2000000000000000000')];

const dexKey = 'BalancerV2';

Expand Down
20 changes: 8 additions & 12 deletions src/dex/balancer-v2/balancer-v2-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import {
BI_9,
BI_MAX_INT,
BI_MINUS_ONE,
BI_POW_17,
BI_POW_18,
BI_POW_20,
BI_POW_36,
BI_POW_4,
BI_POWS,
} from '../../bigint-constants';

const _require = (b: boolean, message: string) => {
Expand Down Expand Up @@ -105,8 +101,8 @@ export class MathSol {
}

// Modification: Taken from the fixed point class
static ONE = BI_POW_18; // 18 decimal places
static MAX_POW_RELATIVE_ERROR = BI_POW_4; // 10 000
static ONE = BI_POWS[18]; // 18 decimal places
static MAX_POW_RELATIVE_ERROR = BI_POWS[4]; // 10 000

static mulUpFixed(a: bigint, b: bigint): bigint {
const product = a * b;
Expand Down Expand Up @@ -187,12 +183,12 @@ class LogExpMath {
// two numbers, and multiply by ONE when dividing them.

// All arguments and return values are 18 decimal fixed point numbers.
static ONE_18: bigint = BI_POW_18;
static ONE_18: bigint = BI_POWS[18];

// Internally, intermediate values are computed with higher precision as 20 decimal fixed point numbers, and in the
// case of ln36, 36 decimals.
static ONE_20: bigint = BI_POW_20;
static ONE_36: bigint = BI_POW_36;
static ONE_20: bigint = BI_POWS[20];
static ONE_36: bigint = BI_POWS[36];

// The domain of natural exponentiation is bound by the word size and number of decimals used.
//
Expand All @@ -206,8 +202,8 @@ class LogExpMath {

// Bounds for ln_36's argument. Both ln(0.9) and ln(1.1) can be represented with 36 decimal places in a fixed point
// 256 bit integer.
static LN_36_LOWER_BOUND: bigint = LogExpMath.ONE_18 - BI_POW_17;
static LN_36_UPPER_BOUND: bigint = LogExpMath.ONE_18 + BI_POW_17;
static LN_36_LOWER_BOUND: bigint = LogExpMath.ONE_18 - BI_POWS[17];
static LN_36_UPPER_BOUND: bigint = LogExpMath.ONE_18 + BI_POWS[17];

static MILD_EXPONENT_BOUND: bigint =
BigInt(2) ** BigInt(254) / LogExpMath.ONE_20;
Expand Down
4 changes: 2 additions & 2 deletions src/dex/balancer-v2/balancer-v2-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
BI_2,
BI_3,
BI_99,
BI_POW_3,
BI_POWS,
} from '../../bigint-constants';

const _require = (b: boolean, message: string) => {
Expand Down Expand Up @@ -184,7 +184,7 @@ abstract class BaseMinimalSwapInfoPool extends BasePool {
}

class StableMath {
static _AMP_PRECISION = BI_POW_3;
static _AMP_PRECISION = BI_POWS[3];

static _calculateInvariant(
amplificationParameter: bigint,
Expand Down
4 changes: 2 additions & 2 deletions src/dex/balancer-v2/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getAddress } from '@ethersproject/address';
import { Interface, Result } from '@ethersproject/abi';
import { getBigIntPow } from '../../utils';
import { BI_POW_18 } from '../../bigint-constants';
import { BI_POWS } from '../../bigint-constants';

export const isSameAddress = (address1: string, address2: string): boolean =>
getAddress(address1) === getAddress(address2);

export function getTokenScalingFactor(tokenDecimals: number): bigint {
return BI_POW_18 * getBigIntPow(18 - tokenDecimals);
return BI_POWS[18] * getBigIntPow(18 - tokenDecimals);
}

export function decodeThrowError(
Expand Down
24 changes: 13 additions & 11 deletions src/dex/kyberdmm/fee-formula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,23 @@ import {
BI_3,
BI_5,
BI_9,
BI_POW_18,
BI_POW_3,
BI_POW_4,
BI_POWS,
} from '../../bigint-constants';
import { mulInPrecision, unsafePowInPrecision } from './math-ext';

export const PRECISION = BI_POW_18;
export const PRECISION = BI_POWS[18];
const R0 = BigInt('1477405064814996100'); // 1.4774050648149961

const C0 = (BigInt(60) * PRECISION) / BI_POW_4;
const C0 = (BigInt(60) * PRECISION) / BI_POWS[4];

const A = (PRECISION * BigInt(20000)) / BigInt(27);
const B = (PRECISION * BigInt(250)) / BI_9;
const C1 = (PRECISION * BigInt(985)) / BigInt(27);
const U = (BigInt(120) * PRECISION) / BI_100;

const G = (BigInt(836) * PRECISION) / BI_POW_3;
const G = (BigInt(836) * PRECISION) / BI_POWS[3];
const F = BI_5 * PRECISION;
const L = (BI_2 * PRECISION) / BI_POW_4;
const L = (BI_2 * PRECISION) / BI_POWS[4];
// C2 = 25 * PRECISION - (F * (PRECISION - G)**2) / ((PRECISION - G)**2 + L * PRECISION)
const C2 = BigInt('20036905816356657810');

Expand All @@ -39,11 +37,15 @@ export const getFee = (rFactorInPrecision: bigint): bigint => {
if (rFactorInPrecision > U) {
const tmp = rFactorInPrecision - U;
const tmp3 = unsafePowInPrecision(tmp, BI_3);
return (C1 + mulInPrecision(A, tmp3) + mulInPrecision(B, tmp)) / BI_POW_4;
return (
(C1 + mulInPrecision(A, tmp3) + mulInPrecision(B, tmp)) / BI_POWS[4]
);
} else {
const tmp = U - rFactorInPrecision;
const tmp3 = unsafePowInPrecision(tmp, BI_3);
return (C1 - mulInPrecision(A, tmp3) - mulInPrecision(B, tmp)) / BI_POW_4;
return (
(C1 - mulInPrecision(A, tmp3) - mulInPrecision(B, tmp)) / BI_POWS[4]
);
}
} else {
// [ C2 + sign(r - G) * F * (r-G) ^2 / (L + (r-G) ^2) ] / 10000
Expand All @@ -52,9 +54,9 @@ export const getFee = (rFactorInPrecision: bigint): bigint => {
tmp = unsafePowInPrecision(tmp, BI_2);
const tmp2 = (F * tmp) / (tmp + L);
if (rFactorInPrecision > G) {
return (C2 + tmp2) / BI_POW_4;
return (C2 + tmp2) / BI_POWS[4];
} else {
return (C2 - tmp2) / BI_POW_4;
return (C2 - tmp2) / BI_POWS[4];
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/dex/kyberdmm/kyberdmm-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DummyDexHelper } from '../../dex-helper/index';
import { testEventSubscriber } from '../../../tests/utils-events';
import { Tokens } from './../../../tests/constants-e2e';
import { DeepReadonly } from 'ts-essentials';
import { BI_POW_4 } from '../../bigint-constants';
import { BI_POWS } from '../../bigint-constants';

jest.setTimeout(50 * 1000);

Expand Down Expand Up @@ -39,7 +39,7 @@ const poolsParams: PoolParam[] = [
},
{
address: '0xA97642500517C728cE1339A466DE0F10C19034CD',
ampBps: BI_POW_4,
ampBps: BI_POWS[4],
token0Symbol: 'REQ',
token1Symbol: 'WETH',
},
Expand Down
4 changes: 2 additions & 2 deletions src/dex/kyberdmm/kyberdmm-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Network, SwapSide } from '../../constants';
import { KyberDmm } from './kyberdmm';
import { checkPoolPrices, checkPoolsLiquidity } from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { BI_0, BI_POW_6 } from '../../bigint-constants';
import { BI_0, BI_POWS } from '../../bigint-constants';

const network = Network.MAINNET;
const TokenASymbol = 'USDT';
Expand All @@ -15,7 +15,7 @@ const TokenA = Tokens[network][TokenASymbol];
const TokenBSymbol = 'WBTC';
const TokenB = Tokens[network][TokenBSymbol];

const amounts = [BI_0, BI_POW_6, BigInt('2000000')];
const amounts = [BI_0, BI_POWS[6], BigInt('2000000')];

const dexKey = 'KyberDmm';

Expand Down
4 changes: 2 additions & 2 deletions src/dex/kyberdmm/math-ext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BI_0, BI_2, BI_POW_18 } from '../../bigint-constants';
import { BI_0, BI_2, BI_POWS } from '../../bigint-constants';

const PRECISION = BI_POW_18;
const PRECISION = BI_POWS[18];

/// @dev Returns x*y in precision
export const mulInPrecision = (x: bigint, y: bigint): bigint => {
Expand Down
4 changes: 2 additions & 2 deletions src/dex/kyberdmm/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { KyberDmmAbiEvents, TradeInfo } from './types';
import { StatefulEventSubscriber } from '../../stateful-event-subscriber';
import { Address, BlockHeader, Log, Logger, Token } from '../../types';
import { IDexHelper } from '../../dex-helper/idex-helper';
import { BI_10, BI_20, BI_30, BI_4, BI_POW_4 } from '../../bigint-constants';
import { BI_10, BI_20, BI_30, BI_4, BI_POWS } from '../../bigint-constants';

export type KyberDmmPools = { [poolAddress: string]: KyberDmmPool };

Expand Down Expand Up @@ -183,7 +183,7 @@ export class KyberDmmPool extends StatefulEventSubscriber<KyberDmmPoolState> {
}
}

const BPS = BI_POW_4;
const BPS = BI_POWS[4];

const getFinalFee = (feeInPrecision: bigint, _ampBps: bigint): bigint => {
if (_ampBps <= 20000) {
Expand Down
6 changes: 3 additions & 3 deletions src/dex/maker-psm/maker-psm-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Network, SwapSide } from '../../constants';
import { MakerPsm } from './maker-psm';
import { checkPoolPrices, checkPoolsLiquidity } from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { BI_0, BI_POW_18, BI_POW_8 } from '../../bigint-constants';
import { BI_0, BI_POWS } from '../../bigint-constants';

const network = Network.MAINNET;
const TokenASymbol = 'USDC';
Expand All @@ -15,9 +15,9 @@ const TokenA = Tokens[network][TokenASymbol];
const TokenBSymbol = 'DAI';
const TokenB = Tokens[network][TokenBSymbol];

const tokenAAmounts = [BI_0, BI_POW_8, BigInt('200000000')];
const tokenAAmounts = [BI_0, BI_POWS[8], BigInt('200000000')];

const tokenBAmounts = [BI_0, BI_POW_18, BigInt('2000000000000000000')];
const tokenBAmounts = [BI_0, BI_POWS[18], BigInt('2000000000000000000')];

const dexKey = 'MakerPsm';

Expand Down
Loading

0 comments on commit 80109d8

Please sign in to comment.