Skip to content

Commit

Permalink
Merge pull request #389 from balancer/develop
Browse files Browse the repository at this point in the history
Release 4.1.1-beta.10
  • Loading branch information
John Grant authored May 22, 2023
2 parents 23c8c90 + d55f17a commit 58866f5
Show file tree
Hide file tree
Showing 11 changed files with 2,349 additions and 2,561 deletions.
38 changes: 23 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{
"name": "@balancer-labs/sor",
"version": "4.1.1-beta.9",
"version": "4.1.1-beta.10",
"engines": {
"node": ">=14",
"npm": ">=8"
},
"license": "GPL-3.0-only",
"main": "dist/index.js",
"module": "dist/index.esm.js",
"module": "./dist/esm/index.js",
"main": "./dist/cjs/index.js",
"sideEffects": false,
"types": "dist/index.d.ts",
"scripts": {
"build": "rollup -c",
"build": "rimraf dist && rollup --config rollup.config.ts --configPlugin typescript",
"prepack": "yarn build",
"test": "TS_NODE_PROJECT='tsconfig.testing.json' nyc mocha -r ts-node/register test/*.spec.ts --timeout 20000",
"test:only": "TS_NODE_PROJECT='tsconfig.testing.json' npx mocha -r ts-node/register --timeout 20000",
Expand Down Expand Up @@ -38,17 +43,17 @@
"@ethersproject/wallet": "^5.4.0",
"@georgeroman/balancer-v2-pools": "0.0.7",
"@nomiclabs/hardhat-ethers": "^2.0.6",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-typescript": "^8.2.5",
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-terser": "^0.4.0",
"@rollup/plugin-typescript": "^11.0.0",
"@types/chai": "^4.2.10",
"@types/lodash.clonedeep": "^4.5.6",
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.20",
"@typescript-eslint/eslint-plugin": "^4.29.2",
"@typescript-eslint/parser": "^4.29.2",
"bignumber.js": "^9.0.1",
"@typescript-eslint/eslint-plugin": "^5.54.1",
"@typescript-eslint/parser": "^5.54.1",
"chai": "^4.2.0",
"dotenv": "^8.2.0",
"eslint": "^7.32.0",
Expand All @@ -63,10 +68,12 @@
"perf_hooks": "^0.0.1",
"prettier": "^2.3.2",
"pretty-quick": "^2.0.1",
"rollup": "^2.56.3",
"rollup-plugin-dts": "^4.0.0",
"rimraf": "^5.0.0",
"rollup": "^3.18.0",
"rollup-plugin-analyzer": "^4.0.0",
"rollup-plugin-dts": "^5.2.0",
"ts-node": "^10.0.0",
"typescript": "^4.3.5"
"typescript": "^4.9.5"
},
"dependencies": {
"isomorphic-fetch": "^2.2.1"
Expand All @@ -77,6 +84,7 @@
"@ethersproject/bignumber": "^5.4.2",
"@ethersproject/constants": "^5.4.0",
"@ethersproject/contracts": "^5.4.1",
"@ethersproject/providers": "^5.4.4"
"@ethersproject/providers": "^5.4.4",
"bignumber.js": "^9.0.1"
}
}
50 changes: 45 additions & 5 deletions rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,69 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { readFileSync } from 'node:fs';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import dts from 'rollup-plugin-dts';
import analyze from 'rollup-plugin-analyzer';
import terser from '@rollup/plugin-terser';

import pkg from './package.json';
const pkg = JSON.parse(
readFileSync(new URL('./package.json', import.meta.url)).toString()
);

const external = [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
];

export default [
{
treeshake: { moduleSideEffects: false },
input: 'src/index.ts',
output: [
{ file: pkg.main, format: 'cjs', sourcemap: true },
{ file: pkg.module, format: 'es', sourcemap: true },
{
format: 'cjs',
sourcemap: true,
file: 'dist/cjs/index.js',
},
{
format: 'es',
sourcemap: true,
dir: 'dist/esm',
preserveModules: true,
// preserveModulesRoot is needed to be compatible with nodeResolve plugin:
// https://github.com/rollup/rollup/issues/3684
preserveModulesRoot: 'src',
},
],
plugins: [
json(),
nodeResolve(),
commonjs(),
typescript(),
terser({
compress: true,
}),
analyze({
hideDeps: true,
limit: 5,
summaryOnly: true,
onAnalysis,
}),
],
plugins: [nodeResolve(), json(), commonjs(), typescript()],
external,
},
{
treeshake: { moduleSideEffects: false },
input: 'src/index.ts',
output: [{ file: 'dist/index.d.ts', format: 'es' }],
plugins: [dts()],
},
];

const limitKB = 750;

function onAnalysis({ bundleSize }) {
if (bundleSize / 1000 < limitKB) return;
console.warn(`Bundle size exceeds ${limitKB} KB: ${bundleSize / 1000} KB`);
return process.exit(1);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export {
Vector2,
} from './pools/gyroEPool/gyroEMath/gyroEMathHelpers';
export * as LinearMaths from './pools/linearPool/linearMath';
export const ISOLATED_CONST = 'isolated';
4 changes: 2 additions & 2 deletions src/pools/composableStable/composableStablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class ComposableStablePool extends PhantomStablePool {
.mul(poolPairData.tokenInPriceRate)
.div(ONE);

let returnEvm: BigInt;
let returnEvm: bigint;

if (poolPairData.pairType === PairTypes.TokenToBpt) {
const amountsInBigInt = Array(
Expand Down Expand Up @@ -125,7 +125,7 @@ export class ComposableStablePool extends PhantomStablePool {
.mul(poolPairData.tokenOutPriceRate)
.div(ONE);

let returnEvm: BigInt;
let returnEvm: bigint;

if (poolPairData.pairType === PairTypes.TokenToBpt) {
returnEvm = _calcTokenInGivenExactBptOut(
Expand Down
4 changes: 2 additions & 2 deletions src/pools/phantomStablePool/phantomStablePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class PhantomStablePool implements PoolBase<PhantomStablePoolPairData> {
.mul(poolPairData.tokenInPriceRate)
.div(ONE);

let returnEvm: BigInt;
let returnEvm: bigint;

if (poolPairData.pairType === PairTypes.TokenToBpt) {
const amountsInBigInt = Array(
Expand Down Expand Up @@ -320,7 +320,7 @@ export class PhantomStablePool implements PoolBase<PhantomStablePoolPairData> {
.mul(poolPairData.tokenOutPriceRate)
.div(ONE);

let returnEvm: BigInt;
let returnEvm: bigint;

if (poolPairData.pairType === PairTypes.TokenToBpt) {
returnEvm = _calcTokenInGivenExactBptOut(
Expand Down
18 changes: 10 additions & 8 deletions test/gyro2Pool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import testPools from './testData/gyro2Pools/gyro2TestPool.json';
import { MockPoolDataService } from './lib/mockPoolDataService';
import { mockTokenPriceService } from './lib/mockTokenPriceService';

const maxDelta = bnum('0.00001');

describe('Gyro2Pool tests USDC > DAI', () => {
const testPool = cloneDeep(testPools).pools[0];
const pool = Gyro2Pool.fromPool(testPool);
Expand Down Expand Up @@ -75,20 +77,20 @@ describe('Gyro2Pool tests USDC > DAI', () => {
const normalizedLiquidity =
pool.getNormalizedLiquidity(poolPairData);

expect(Number(normalizedLiquidity.toString())).to.be.approximately(
1116333.916257166990921337,
0.00001
);
const delta = normalizedLiquidity
.minus(bnum('1116333.916257166990921337'))
.abs();
expect(delta.lt(maxDelta)).to.be.true;
});

it(`should correctly calculate normalized liquidity, DAI > USDC`, async () => {
const normalizedLiquidity =
pool.getNormalizedLiquidity(poolPairData2);

expect(Number(normalizedLiquidity.toString())).to.be.approximately(
1116217.358286598731855228,
0.00001
);
const delta = normalizedLiquidity
.minus(bnum('1116217.358286598731855228'))
.abs();
expect(delta.lt(maxDelta)).to.be.true;
});
});

Expand Down
14 changes: 6 additions & 8 deletions test/gyroEMath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const TEST_POOL_PAIR_DATA: GyroEPoolPairData = {
tokenInIsToken0: true,
};

const maxDelta = '10000000000000';

describe('gyroEMath tests', () => {
const poolPairData = TEST_POOL_PAIR_DATA;

Expand Down Expand Up @@ -98,10 +100,8 @@ describe('gyroEMath tests', () => {
DERIVED_GYRO_E_PARAMS,
invariant
);
expect(Number(formatFixed(a, 18))).to.be.approximately(
211.290746521816255142,
0.00001
);
const delta = a.sub('211290746521816255142').abs();
expect(delta.lt(maxDelta)).to.be.true;
});
});

Expand All @@ -128,10 +128,8 @@ describe('gyroEMath tests', () => {
DERIVED_GYRO_E_PARAMS,
invariant
);
expect(Number(formatFixed(b, 18))).to.be.approximately(
65.500131431538418723,
0.00001
);
const delta = b.sub('65500131431538418723').abs();
expect(delta.lt(maxDelta)).to.be.true;
});
});
});
44 changes: 20 additions & 24 deletions test/gyroEPool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const POOL = GyroEPool.fromPool({
dSq: '1.00000000000000002140811391783216360000',
});

const maxDelta = '0.00001';

describe('gyroEPool tests', () => {
const poolPairData = TEST_POOL_PAIR_DATA;

Expand All @@ -86,11 +88,10 @@ describe('gyroEPool tests', () => {
poolPairData,
SwapTypes.SwapExactIn
);

expect(Number(limitAmount)).to.be.approximately(
354.48480273457726733583,
0.00001
);
const delta = limitAmount
.minus(bnum('354.48480273457726733583'))
.abs();
expect(delta.lt(maxDelta)).to.be.true;
});

it(`should correctly calculate limit amount for swap exact out`, async () => {
Expand All @@ -109,11 +110,8 @@ describe('gyroEPool tests', () => {
poolPairData,
bnum('10')
);

expect(Number(swapAmount)).to.be.approximately(
2.821007799187925949,
0.00001
);
const delta = swapAmount.minus(bnum('2.821007799187925949')).abs();
expect(delta.lt(maxDelta)).to.be.true;
});

it(`should correctly calculate swap amount for swap exact out`, async () => {
Expand All @@ -130,10 +128,10 @@ describe('gyroEPool tests', () => {
18
);

expect(Number(reduced)).to.be.approximately(
32.257987339909373037,
0.00001
);
const delta = bnum(reduced)
.minus(bnum('32.257987339909373037'))
.abs();
expect(delta.lt(maxDelta)).to.be.true;
});
});

Expand All @@ -144,11 +142,10 @@ describe('gyroEPool tests', () => {
poolPairData,
bnum('10')
);

expect(Number(priceAfterSwap)).to.be.approximately(
3.544833007099248968,
0.00001
);
const delta = priceAfterSwap
.minus(bnum('3.544833007099248968'))
.abs();
expect(delta.lt(maxDelta)).to.be.true;
});

it(`should correctly calculate price after swap exact out`, async () => {
Expand All @@ -157,11 +154,10 @@ describe('gyroEPool tests', () => {
poolPairData,
bnum('10')
);

expect(Number(priceAfterSwap)).to.be.approximately(
3.544835504848199306,
0.00001
);
const delta = priceAfterSwap
.minus(bnum('3.544835504848199306'))
.abs();
expect(delta.lt(maxDelta)).to.be.true;
});
});

Expand Down
Loading

0 comments on commit 58866f5

Please sign in to comment.