Skip to content

Commit

Permalink
Merge pull request paraswap#91 from paraswap/feature/back-517
Browse files Browse the repository at this point in the history
feature/back-517 - replace all BigInt() and BigNumber() unnecessary intializations
  • Loading branch information
shresthagrawal authored Apr 21, 2022
2 parents 847ff9a + af9bcd3 commit f2a7501
Show file tree
Hide file tree
Showing 42 changed files with 411 additions and 432 deletions.
7 changes: 2 additions & 5 deletions dex-template/__DexName__-integration.test.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dotenv.config();

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

const amounts = [
BigInt('0'),
BigInt('1000000000000000000'),
BigInt('2000000000000000000'),
];
const amounts = [0n, BI_POWS[18], 2000000000000000000n];

const dexKey = '__DexName__';

Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ module.exports = {
'/src/dex/.*\\.(test|spec)\\.(ts)$',
],
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
testTImeout: 30 * 1000,
};
9 changes: 3 additions & 6 deletions scripts/run-dex-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +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_POWS } from '../src/bigint-constants';

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

const amounts = [
BigInt('0'),
BigInt('1000000000000000000'),
BigInt('2000000000000000000'),
];
const amounts = [0n, BI_POWS[18], 2000000000000000000n];

async function main() {
const dexHelper = new DummyDexHelper(Network.MAINNET);
Expand All @@ -67,7 +64,7 @@ async function main() {
SwapSide.SELL,
blocknumber,
);
console.log('WETH <> DAI Pool Ideintifiers: ', pools);
console.log('WETH <> DAI Pool Identifiers: ', pools);

const prices = await balancerV2.getPricesVolume(
from,
Expand Down
10 changes: 10 additions & 0 deletions src/bigint-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MAX_INT } from './constants';

// Indexes represent 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 = 2n ** 256n - 1n;
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const MIN_INT =
'-57896044618658097711785492504343953926634992332820282019728792003956564819967';

export const MAX_BLOCKS_HISTORY = 7;
export const MAX_UINT_BIGINT = BigInt(2) ** BigInt(256) - BigInt(1);

export const SETUP_RETRY_TIMEOUT = 20 * 1000; // 20s

Expand Down
11 changes: 4 additions & 7 deletions src/dex/aave-v1/aave-v1-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { aaveV1GetToken } from './tokens';
import { BI_POWS } from '../../bigint-constants';

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

const amounts = [
BigInt('0'),
BigInt('1000000000000000000'),
BigInt('2000000000000000000'),
];
const amounts = [0n, BI_POWS[18], 2000000000000000000n];

const dexKey = 'AaveV1';
it('getPoolIdentifiers and getPricesVolume SELL', async function () {
Expand All @@ -42,7 +39,7 @@ describe('AaveV1', function () {
SwapSide.SELL,
blocknumber,
);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Ideintifiers: `, pools);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Identifiers: `, pools);

expect(pools.length).toBeGreaterThan(0);

Expand Down Expand Up @@ -72,7 +69,7 @@ describe('AaveV1', function () {
SwapSide.BUY,
blocknumber,
);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Ideintifiers: `, pools);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Identifiers: `, pools);

expect(pools.length).toBeGreaterThan(0);

Expand Down
8 changes: 4 additions & 4 deletions src/dex/aave-v1/aave-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Logger,
} from '../../types';
import { SwapSide, Network } from '../../constants';
import { getDexKeysWithNetwork } from '../../utils';
import { getBigIntPow, getDexKeysWithNetwork } from '../../utils';
import { IDex } from '../../dex/idex';
import { IDexHelper } from '../../dex-helper/idex-helper';
import { AaveV1Data, AaveV1Param } from './types';
Expand Down Expand Up @@ -63,7 +63,7 @@ export class AaveV1
}

// Returns list of pool identifiers that can be used
// for a given swap. poolIdentifers must be unique
// for a given swap. poolIdentifiers must be unique
// across DEXes. It is recommended to use
// ${dexKey}_${poolAddress} as a poolIdentifier
async getPoolIdentifiers(
Expand Down Expand Up @@ -99,8 +99,8 @@ export class AaveV1
return [
{
prices: amounts,
unit: BigInt(
10 ** (side === SwapSide.SELL ? destToken : srcToken).decimals,
unit: getBigIntPow(
(side === SwapSide.SELL ? destToken : srcToken).decimals,
),
gasCost: AaveGasCost,
exchange: this.dexKey,
Expand Down
11 changes: 4 additions & 7 deletions src/dex/aave-v2/aave-v2-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { aaveV2GetToken } from './tokens';
import { BI_POWS } from '../../bigint-constants';

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

const amounts = [
BigInt('0'),
BigInt('1000000000000000000'),
BigInt('2000000000000000000'),
];
const amounts = [0n, BI_POWS[18], 2000000000000000000n];

const dexKey = 'AaveV2';
if (!aUSDT) {
Expand All @@ -43,7 +40,7 @@ describe('AaveV2', function () {
SwapSide.SELL,
blocknumber,
);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Ideintifiers: `, pools);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Identifiers: `, pools);

expect(pools.length).toBeGreaterThan(0);

Expand Down Expand Up @@ -72,7 +69,7 @@ describe('AaveV2', function () {
SwapSide.BUY,
blocknumber,
);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Ideintifiers: `, pools);
console.log(`${USDTSymbol} <> ${aUSDTSymbol} Pool Identifiers: `, pools);

expect(pools.length).toBeGreaterThan(0);

Expand Down
13 changes: 9 additions & 4 deletions src/dex/aave-v2/aave-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
Logger,
} from '../../types';
import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
import { isETHAddress, getDexKeysWithNetwork, wrapETH } from '../../utils';
import {
isETHAddress,
getDexKeysWithNetwork,
wrapETH,
getBigIntPow,
} from '../../utils';
import { AaveV2Data, AaveV2Param, AaveV2PoolAndWethFunctions } from './types';

import WETH_GATEWAY_ABI_MAINNET from '../../abi/aave-weth-gateway.json';
Expand Down Expand Up @@ -83,7 +88,7 @@ export class AaveV2
}

// Returns list of pool identifiers that can be used
// for a given swap. poolIdentifers must be unique
// for a given swap. poolIdentifiers must be unique
// across DEXes. It is recommended to use
// ${dexKey}_${poolAddress} as a poolIdentifier
async getPoolIdentifiers(
Expand Down Expand Up @@ -131,8 +136,8 @@ export class AaveV2
return [
{
prices: amounts,
unit: BigInt(
10 ** (side === SwapSide.SELL ? destToken : srcToken).decimals,
unit: getBigIntPow(
(side === SwapSide.SELL ? destToken : srcToken).decimals,
),
gasCost: isETHAddress(srcToken.address)
? Aave2ETHGasCost
Expand Down
13 changes: 7 additions & 6 deletions src/dex/aave-v3/aave-v3-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import {
} from '../../../tests/utils';
import { Tokens } from '../../../tests/constants-e2e';
import { getTokenFromASymbol } from './tokens';
import { BI_POWS } from '../../bigint-constants';

/*
README
======
This test script adds tests for AaveV3 general integration
with the DEX interface. The test cases below are example tests.
This test script adds tests for AaveV3 general integration
with the DEX interface. The test cases below are example tests.
It is recommended to add tests which cover AaveV3 specific
logic.
logic.
You can run this individual test script by running:
`npx jest src/dex/<dex-name>/<dex-name>-integration.test.ts`
Expand All @@ -33,7 +34,7 @@ const TokenA = Tokens[network][TokenASymbol];
const TokenBSymbol = 'aUSDT';
const TokenB = getTokenFromASymbol(network, TokenBSymbol);

const amounts = [BigInt('0'), BigInt('1000000'), BigInt('2000000')];
const amounts = [0n, BI_POWS[6], 2000000n];

const dexKey = 'AaveV3';

Expand All @@ -52,7 +53,7 @@ describe('AaveV3', function () {
blocknumber,
);
console.log(
`${TokenASymbol} <> ${TokenBSymbol} Pool Ideintifiers: `,
`${TokenASymbol} <> ${TokenBSymbol} Pool Identifiers: `,
pools,
);

Expand Down Expand Up @@ -87,7 +88,7 @@ describe('AaveV3', function () {
blocknumber,
);
console.log(
`${TokenASymbol} <> ${TokenBSymbol} Pool Ideintifiers: `,
`${TokenASymbol} <> ${TokenBSymbol} Pool Identifiers: `,
pools,
);

Expand Down
11 changes: 8 additions & 3 deletions src/dex/aave-v3/aave-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
Logger,
} from '../../types';
import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
import { wrapETH, getDexKeysWithNetwork, isETHAddress } from '../../utils';
import {
wrapETH,
getDexKeysWithNetwork,
isETHAddress,
getBigIntPow,
} from '../../utils';
import { IDex } from '../../dex/idex';
import { IDexHelper } from '../../dex-helper/idex-helper';
import { Data, Param, PoolAndWethFunctions } from './types';
Expand Down Expand Up @@ -95,8 +100,8 @@ export class AaveV3 extends SimpleExchange implements IDex<Data, Param> {
return [
{
prices: amounts,
unit: BigInt(
10 ** (side === SwapSide.SELL ? destToken : srcToken).decimals,
unit: getBigIntPow(
(side === SwapSide.SELL ? destToken : srcToken).decimals,
),
gasCost: isETHAddress(srcToken.address)
? this.config.ethGasCost
Expand Down
8 changes: 4 additions & 4 deletions src/dex/balancer-v2/LinearMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function _calcBptOutPerMainIn(
): bigint {
// Amount out, so we round down overall.

if (bptSupply == BigInt(0)) {
if (bptSupply == 0n) {
return _toNominal(mainIn, params);
}

Expand Down Expand Up @@ -97,7 +97,7 @@ export function _calcMainInPerBptOut(
params: Params,
): bigint {
// Amount in, so we round up overall.
if (bptSupply == BigInt(0)) {
if (bptSupply == 0n) {
return _fromNominal(bptOut, params);
}
const previousNominalMain = _toNominal(mainBalance, params);
Expand Down Expand Up @@ -162,7 +162,7 @@ export function _calcBptOutPerWrappedIn(
params: Params,
): bigint {
// Amount out, so we round down overall.
if (bptSupply == BigInt(0)) {
if (bptSupply == 0n) {
// Return nominal DAI
return wrappedIn;
}
Expand All @@ -186,7 +186,7 @@ export function _calcWrappedInPerBptOut(
params: Params,
): bigint {
// Amount in, so we round up overall.
if (bptSupply == BigInt(0)) {
if (bptSupply == 0n) {
// Return nominal DAI
return bptOut;
}
Expand Down
Loading

0 comments on commit f2a7501

Please sign in to comment.