Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing the code with less precision and uncommenting the code with … #434

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/router/sorClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,8 @@ function iterateSwapAmountsApproximation(
// 1.042818e-12 while using normal js math operations it was
// 1.0428184989387553e-12. This loss of precision caused an important bug

// let weighted_average_SPaS = sumSPaSDividedByDerivativeSPaSs.div(
// sumInverseDerivativeSPaSs
// );
const weighted_average_SPaS = bnum(
sumSPaSDividedByDerivativeSPaSs.toNumber() /
sumInverseDerivativeSPaSs.toNumber()
const weighted_average_SPaS = sumSPaSDividedByDerivativeSPaSs.div(
sumInverseDerivativeSPaSs
);

swapAmounts.forEach((swapAmount, i) => {
Expand Down
14 changes: 7 additions & 7 deletions test/fullSwaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('Tests full swaps against known values', () => {
// These test should highlight any changes in maths that may unexpectedly change result
assert.equal(
swapInfo.returnAmount.toString(),
'2932409883691685',
'2932409883691175',
'V2 sanity check.'
);
}).timeout(10000);
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('Tests full swaps against known values', () => {
swapInfo.tokenAddresses[swapInfo.swaps[0].assetOutIndex],
USDC.address
);
assert.equal(swapInfo.swaps[0].amount, '89883768143240164');
assert.equal(swapInfo.swaps[0].amount, '89883768143225903');
assert.equal(
swapInfo.swaps[1].poolId,
'0x57755f7dec33320bca83159c26e93751bfd30fbe'
Expand All @@ -197,7 +197,7 @@ describe('Tests full swaps against known values', () => {
swapInfo.tokenAddresses[swapInfo.swaps[1].assetOutIndex],
USDC.address
);
assert.equal(swapInfo.swaps[1].amount, '10116231856759836');
assert.equal(swapInfo.swaps[1].amount, '10116231856774097');
}).timeout(10000);

it('should full swap weighted swapExactOut', async () => {
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('Tests full swaps against known values', () => {
});

// The expected test results are from previous version
assert.equal(swapInfo.returnAmount.toString(), '99251606996075153');
assert.equal(swapInfo.returnAmount.toString(), '99251606996078174');
assert.equal(swapInfo.swaps.length, 2);
assert.equal(
swapInfo.swaps[0].poolId,
Expand Down Expand Up @@ -426,7 +426,7 @@ describe('Tests full swaps against known values', () => {
swapInfo.tokenAddresses[swapInfo.swaps[0].assetOutIndex],
USDC.address
);
assert.equal(swapInfo.swaps[0].amount, '692256505473389013');
assert.equal(swapInfo.swaps[0].amount, '692256505473351912');
assert.equal(
swapInfo.swaps[1].poolId,
'0x57755f7dec33320bca83159c26e93751bfd30fbe'
Expand All @@ -439,7 +439,7 @@ describe('Tests full swaps against known values', () => {
swapInfo.tokenAddresses[swapInfo.swaps[1].assetOutIndex],
USDC.address
);
assert.equal(swapInfo.swaps[1].amount, '77743494526610987');
assert.equal(swapInfo.swaps[1].amount, '77743494526648088');
}).timeout(10000);

it('should full swap stable & weighted swapExactOut', async () => {
Expand Down Expand Up @@ -480,7 +480,7 @@ describe('Tests full swaps against known values', () => {
},
});

assert.equal(swapInfo.returnAmount.toString(), '100600365514359821527');
assert.equal(swapInfo.returnAmount.toString(), '100600365514359819807');
assert.equal(swapInfo.swaps.length, 3);
assert.equal(
swapInfo.swaps[0].poolId,
Expand Down
54 changes: 54 additions & 0 deletions test/precisionTest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// TS_NODE_PROJECT='tsconfig.testing.json' npx mocha -r ts-node/register test/fullSwaps.spec.ts
import { BigNumber, parseFixed } from '@ethersproject/bignumber';
import { Zero } from '@ethersproject/constants';
import { JsonRpcProvider } from '@ethersproject/providers';
import cloneDeep from 'lodash.clonedeep';
import { assert } from 'chai';
import { SubgraphPoolBase, SwapTypes } from '../src/types';
import { getFullSwap } from './lib/testHelpers';
import precisionTestPools from './testData/precisionTestPools.json';
import _ from 'lodash';

const B80BAL20WETH = '0x5c6ee304399dbdb9c8ef030ab642b10820db8f56';
const USDC = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';

const provider = new JsonRpcProvider(``);

describe('Tests full swaps against known values', () => {
const gasPrice = parseFixed('30', 9);

it('Should have a reasonable return amount value', async () => {
const tokenIn = B80BAL20WETH;
const tokenOut = USDC;
const swapType = SwapTypes.SwapExactIn;
const returnAmountDecimals = 18;
const maxPools = 4;
const swapAmount = parseFixed('14', 18);
const swapGas = BigNumber.from('100000');
const costOutputToken = Zero;

const swapInfo = await getFullSwap(
cloneDeep(
precisionTestPools.map((obj) =>
_.omitBy(obj, _.isNull)
) as SubgraphPoolBase[]
),
tokenIn,
tokenOut,
returnAmountDecimals,
maxPools,
swapType,
swapAmount,
costOutputToken,
gasPrice,
provider,
swapGas
);

assert.equal(
swapInfo.returnAmount.toString(),
'243265259',
'Should have a reasonable return amount value'
);
}).timeout(10000);
});
Loading
Loading