@paraswap/dex-lib 3.8.23 → 3.8.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/abi/infusion/InfusionFactory.json +147 -0
- package/build/abi/infusion/InfusionPair.json +658 -0
- package/build/abi/infusion/InfusionRouter.json +442 -0
- package/build/dex/index.js +4 -0
- package/build/dex/index.js.map +1 -1
- package/build/dex/infusion/config.d.ts +3 -0
- package/build/dex/infusion/config.js +20 -0
- package/build/dex/infusion/config.js.map +1 -0
- package/build/dex/infusion/infusion-stable-pool.d.ts +4 -0
- package/build/dex/infusion/infusion-stable-pool.js +74 -0
- package/build/dex/infusion/infusion-stable-pool.js.map +1 -0
- package/build/dex/infusion/infusion.d.ts +51 -0
- package/build/dex/infusion/infusion.js +457 -0
- package/build/dex/infusion/infusion.js.map +1 -0
- package/build/dex/infusion/types.d.ts +45 -0
- package/build/dex/infusion/types.js +3 -0
- package/build/dex/infusion/types.js.map +1 -0
- package/build/dex/infusion/utils/isStablePair.d.ts +2 -0
- package/build/dex/infusion/utils/isStablePair.js +18 -0
- package/build/dex/infusion/utils/isStablePair.js.map +1 -0
- package/build/dex/lite-psm/lite-psm.js +2 -5
- package/build/dex/lite-psm/lite-psm.js.map +1 -1
- package/build/dex/maker-psm/maker-psm.js +2 -6
- package/build/dex/maker-psm/maker-psm.js.map +1 -1
- package/build/dex/oswap/oswap-pool.js +8 -8
- package/build/dex/oswap/oswap-pool.js.map +1 -1
- package/build/dex/oswap/oswap.d.ts +5 -5
- package/build/dex/oswap/oswap.js +62 -46
- package/build/dex/oswap/oswap.js.map +1 -1
- package/build/dex/oswap/types.d.ts +4 -4
- package/package.json +1 -1
- package/src/abi/infusion/InfusionFactory.json +147 -0
- package/src/abi/infusion/InfusionPair.json +658 -0
- package/src/abi/infusion/InfusionRouter.json +442 -0
- package/src/abi/oswap/oswap.abi.json +452 -0
- package/src/dex/index.ts +4 -0
- package/src/dex/infusion/config.ts +21 -0
- package/src/dex/infusion/infusion-e2e.test.ts +110 -0
- package/src/dex/infusion/infusion-integration.test.ts +232 -0
- package/src/dex/infusion/infusion-stable-pool.ts +91 -0
- package/src/dex/infusion/infusion.ts +652 -0
- package/src/dex/infusion/types.ts +60 -0
- package/src/dex/infusion/utils/isStablePair.ts +18 -0
- package/src/dex/lite-psm/lite-psm.ts +2 -5
- package/src/dex/maker-psm/maker-psm.ts +2 -6
- package/src/dex/oswap/config.ts +32 -0
- package/src/dex/oswap/oswap-e2e.test.ts +91 -0
- package/src/dex/oswap/oswap-events.test.ts +88 -0
- package/src/dex/oswap/oswap-integration.test.ts +291 -0
- package/src/dex/oswap/oswap-pool.ts +190 -0
- package/src/dex/oswap/oswap.ts +414 -0
- package/src/dex/oswap/types.ts +29 -0
- package/tests/constants-e2e.ts +1 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Network } from '../../../constants';
|
|
2
|
+
|
|
3
|
+
export const stablecoins: Record<number, string[]> = {
|
|
4
|
+
[Network.BASE]: [
|
|
5
|
+
'0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'.toLowerCase(), // usdc
|
|
6
|
+
'0xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA'.toLowerCase(), // usdbc
|
|
7
|
+
'0xEB466342C4d449BC9f53A865D5Cb90586f405215'.toLowerCase(), // axelar usdc
|
|
8
|
+
],
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function isStablePair(chainId: number, tokenA: string, tokenB: string) {
|
|
12
|
+
const stables = stablecoins[chainId];
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
stables.includes(tokenA.toLocaleLowerCase()) &&
|
|
16
|
+
stables.includes(tokenB.toLocaleLowerCase())
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -242,7 +242,7 @@ export class LitePsm
|
|
|
242
242
|
},
|
|
243
243
|
poolAddresses: [eventPool.poolConfig.psmAddress],
|
|
244
244
|
exchange: this.dexKey,
|
|
245
|
-
gasCost:
|
|
245
|
+
gasCost: 50000,
|
|
246
246
|
poolIdentifier,
|
|
247
247
|
},
|
|
248
248
|
];
|
|
@@ -450,10 +450,7 @@ export class LitePsm
|
|
|
450
450
|
// not used on the contract, but used for analytics
|
|
451
451
|
destToken,
|
|
452
452
|
fromAmount,
|
|
453
|
-
|
|
454
|
-
side === SwapSide.BUY && srcToken.toLowerCase() === this.dai.address
|
|
455
|
-
? toAmount
|
|
456
|
-
: quotedAmount,
|
|
453
|
+
toAmount,
|
|
457
454
|
data.toll,
|
|
458
455
|
to18ConversionFactor.toString(),
|
|
459
456
|
data.psmAddress,
|
|
@@ -397,7 +397,7 @@ export class MakerPsm
|
|
|
397
397
|
},
|
|
398
398
|
poolAddresses: [eventPool.poolConfig.psmAddress],
|
|
399
399
|
exchange: this.dexKey,
|
|
400
|
-
gasCost:
|
|
400
|
+
gasCost: 200 * 1000,
|
|
401
401
|
poolIdentifier,
|
|
402
402
|
},
|
|
403
403
|
];
|
|
@@ -653,11 +653,7 @@ export class MakerPsm
|
|
|
653
653
|
// not used on the contract, but used for analytics
|
|
654
654
|
destToken,
|
|
655
655
|
fromAmount,
|
|
656
|
-
|
|
657
|
-
// TODO: Investigate `toAmount` vs `quotedAmount`
|
|
658
|
-
side === SwapSide.BUY && srcToken.toLowerCase() === this.dai.address
|
|
659
|
-
? toAmount
|
|
660
|
-
: quotedAmount,
|
|
656
|
+
toAmount,
|
|
661
657
|
data.toll,
|
|
662
658
|
to18ConversionFactor.toString(),
|
|
663
659
|
data.psmAddress,
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { DexParams } from './types';
|
|
2
|
+
import { DexConfigMap, AdapterMappings } from '../../types';
|
|
3
|
+
import { Network, SwapSide } from '../../constants';
|
|
4
|
+
|
|
5
|
+
export const OSWAP_GAS_COST = 80_000;
|
|
6
|
+
|
|
7
|
+
// Important:
|
|
8
|
+
// - All addresses should be lower case.
|
|
9
|
+
// - Only tokens with 18 decimals are supported.
|
|
10
|
+
export const OSwapConfig: DexConfigMap<DexParams> = {
|
|
11
|
+
OSwap: {
|
|
12
|
+
[Network.MAINNET]: {
|
|
13
|
+
pools: [
|
|
14
|
+
{
|
|
15
|
+
id: 'OSwap_0x85b78aca6deae198fbf201c82daf6ca21942acc6', // Pool identifier: `{dex_key}_{pool_address}`
|
|
16
|
+
address: '0x85b78aca6deae198fbf201c82daf6ca21942acc6', // Address of the pool
|
|
17
|
+
token0: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', // WETH
|
|
18
|
+
token1: '0xae7ab96520de3a18e5e111b5eaab095312d7fe84', // STETH
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export const Adapters: Record<number, AdapterMappings> = {
|
|
26
|
+
[Network.MAINNET]: {
|
|
27
|
+
// Note: We re-use the SmarDex adapters since it implements
|
|
28
|
+
// an Uniswap V2 router compatible interface, which OSwap supports.
|
|
29
|
+
[SwapSide.SELL]: [{ name: 'Adapter04', index: 6 }],
|
|
30
|
+
[SwapSide.BUY]: [{ name: 'BuyAdapter02', index: 2 }],
|
|
31
|
+
},
|
|
32
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
dotenv.config();
|
|
4
|
+
|
|
5
|
+
import { testE2E } from '../../../tests/utils-e2e';
|
|
6
|
+
import { Tokens, Holders } from '../../../tests/constants-e2e';
|
|
7
|
+
import { Network, ContractMethod, SwapSide } from '../../constants';
|
|
8
|
+
import { StaticJsonRpcProvider } from '@ethersproject/providers';
|
|
9
|
+
import { generateConfig } from '../../config';
|
|
10
|
+
|
|
11
|
+
function testForNetwork(
|
|
12
|
+
network: Network,
|
|
13
|
+
dexKey: string,
|
|
14
|
+
tokenASymbol: string,
|
|
15
|
+
tokenBSymbol: string,
|
|
16
|
+
tokenAAmount: string,
|
|
17
|
+
tokenBAmount: string,
|
|
18
|
+
) {
|
|
19
|
+
const provider = new StaticJsonRpcProvider(
|
|
20
|
+
generateConfig(network).privateHttpProvider,
|
|
21
|
+
network,
|
|
22
|
+
);
|
|
23
|
+
const tokens = Tokens[network];
|
|
24
|
+
const holders = Holders[network];
|
|
25
|
+
|
|
26
|
+
const sideToContractMethods = new Map([
|
|
27
|
+
[SwapSide.SELL, [ContractMethod.swapExactAmountIn]],
|
|
28
|
+
[SwapSide.BUY, [ContractMethod.swapExactAmountOut]],
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
describe(`${network}`, () => {
|
|
32
|
+
sideToContractMethods.forEach((contractMethods, side) =>
|
|
33
|
+
describe(`${side}`, () => {
|
|
34
|
+
contractMethods.forEach((contractMethod: ContractMethod) => {
|
|
35
|
+
describe(`${contractMethod}`, () => {
|
|
36
|
+
it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
|
|
37
|
+
await testE2E(
|
|
38
|
+
tokens[tokenASymbol],
|
|
39
|
+
tokens[tokenBSymbol],
|
|
40
|
+
holders[tokenASymbol],
|
|
41
|
+
side === SwapSide.SELL ? tokenAAmount : tokenBAmount,
|
|
42
|
+
side,
|
|
43
|
+
dexKey,
|
|
44
|
+
contractMethod,
|
|
45
|
+
network,
|
|
46
|
+
provider,
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it(`${tokenBSymbol} -> ${tokenASymbol}`, async () => {
|
|
51
|
+
await testE2E(
|
|
52
|
+
tokens[tokenBSymbol],
|
|
53
|
+
tokens[tokenASymbol],
|
|
54
|
+
holders[tokenBSymbol],
|
|
55
|
+
side === SwapSide.SELL ? tokenBAmount : tokenAAmount,
|
|
56
|
+
side,
|
|
57
|
+
dexKey,
|
|
58
|
+
contractMethod,
|
|
59
|
+
network,
|
|
60
|
+
provider,
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
describe('Oswap E2E', () => {
|
|
71
|
+
const dexKey = 'OSwap';
|
|
72
|
+
|
|
73
|
+
describe('Mainnet', () => {
|
|
74
|
+
const network = Network.MAINNET;
|
|
75
|
+
|
|
76
|
+
const tokenASymbol: string = 'WETH';
|
|
77
|
+
const tokenBSymbol: string = 'STETH';
|
|
78
|
+
|
|
79
|
+
const tokenAAmount: string = '10000000000000';
|
|
80
|
+
const tokenBAmount: string = '10000000000000';
|
|
81
|
+
|
|
82
|
+
testForNetwork(
|
|
83
|
+
network,
|
|
84
|
+
dexKey,
|
|
85
|
+
tokenASymbol,
|
|
86
|
+
tokenBSymbol,
|
|
87
|
+
tokenAAmount,
|
|
88
|
+
tokenBAmount,
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
dotenv.config();
|
|
4
|
+
|
|
5
|
+
import { OSwapConfig } from './config';
|
|
6
|
+
import { OSwapEventPool } from './oswap-pool';
|
|
7
|
+
import { Network } from '../../constants';
|
|
8
|
+
import { Address } from '../../types';
|
|
9
|
+
import { DummyDexHelper } from '../../dex-helper/index';
|
|
10
|
+
import { testEventSubscriber } from '../../../tests/utils-events';
|
|
11
|
+
import { OSwapPool, OSwapPoolState } from './types';
|
|
12
|
+
|
|
13
|
+
jest.setTimeout(50 * 1000);
|
|
14
|
+
|
|
15
|
+
// eventName -> blockNumbers
|
|
16
|
+
type EventMappings = Record<string, number[]>;
|
|
17
|
+
|
|
18
|
+
// Helper function. Returns the absolute value of the difference between 2 bigints.
|
|
19
|
+
function absdelta(a: bigint, b: bigint) {
|
|
20
|
+
const delta = a - b;
|
|
21
|
+
return delta < 0 ? -delta : delta;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function stateCompare(state1: OSwapPoolState, state2: OSwapPoolState) {
|
|
25
|
+
const ROUNDING_ERROR = 2;
|
|
26
|
+
|
|
27
|
+
expect(state1.traderate0).toEqual(state2.traderate0);
|
|
28
|
+
expect(state1.traderate1).toEqual(state2.traderate1);
|
|
29
|
+
// Some ERC20 tokens have rounding error which can lead to the balance computed from Transfer event valuee
|
|
30
|
+
// to deviate by a couple gwei vs the on-chain balance.
|
|
31
|
+
// For example stETH: https://docs.lido.fi/guides/lido-tokens-integration-guide/#1-2-wei-corner-case
|
|
32
|
+
expect(
|
|
33
|
+
absdelta(BigInt(state1.balance0), BigInt(state2.balance0)),
|
|
34
|
+
).toBeLessThanOrEqual(ROUNDING_ERROR);
|
|
35
|
+
expect(
|
|
36
|
+
absdelta(BigInt(state1.balance1), BigInt(state2.balance1)),
|
|
37
|
+
).toBeLessThanOrEqual(ROUNDING_ERROR);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('Oswap EventPool Mainnet', function () {
|
|
41
|
+
const dexKey = 'OSwap';
|
|
42
|
+
const network = Network.MAINNET;
|
|
43
|
+
const dexHelper = new DummyDexHelper(network);
|
|
44
|
+
const logger = dexHelper.getLogger(dexKey);
|
|
45
|
+
const pool: OSwapPool = OSwapConfig[dexKey][network].pools[0];
|
|
46
|
+
|
|
47
|
+
let eventPool: OSwapEventPool;
|
|
48
|
+
|
|
49
|
+
// poolAddress -> EventMappings
|
|
50
|
+
const eventsToTest: Record<Address, EventMappings> = {
|
|
51
|
+
[pool.address]: {
|
|
52
|
+
TraderateChanged: [18917344],
|
|
53
|
+
Transfer: [18922097, 18924756],
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
beforeEach(async () => {
|
|
58
|
+
eventPool = new OSwapEventPool(dexKey, pool, network, dexHelper, logger);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
Object.entries(eventsToTest).forEach(
|
|
62
|
+
([poolAddress, events]: [string, EventMappings]) => {
|
|
63
|
+
describe(`Events for ${poolAddress}`, () => {
|
|
64
|
+
Object.entries(events).forEach(
|
|
65
|
+
([eventName, blockNumbers]: [string, number[]]) => {
|
|
66
|
+
describe(`${eventName}`, () => {
|
|
67
|
+
blockNumbers.forEach((blockNumber: number) => {
|
|
68
|
+
it(`State after ${blockNumber}`, async function () {
|
|
69
|
+
await testEventSubscriber(
|
|
70
|
+
eventPool,
|
|
71
|
+
eventPool.addressesSubscribed,
|
|
72
|
+
(_blockNumber: number) =>
|
|
73
|
+
eventPool.generateState(_blockNumber),
|
|
74
|
+
blockNumber,
|
|
75
|
+
`${dexKey}_${poolAddress}`,
|
|
76
|
+
dexHelper.provider,
|
|
77
|
+
(state1: OSwapPoolState, state2: OSwapPoolState) =>
|
|
78
|
+
stateCompare(state1, state2),
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
);
|
|
88
|
+
});
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
dotenv.config();
|
|
4
|
+
|
|
5
|
+
import { DummyDexHelper } from '../../dex-helper/index';
|
|
6
|
+
import { Network, SwapSide } from '../../constants';
|
|
7
|
+
import { BI_POWS } from '../../bigint-constants';
|
|
8
|
+
import { MultiCallParams } from '../../lib/multi-wrapper';
|
|
9
|
+
import { uint256ToBigInt } from '../../lib/decoders';
|
|
10
|
+
import { getBigIntPow } from '../../utils';
|
|
11
|
+
import {
|
|
12
|
+
checkPoolPrices,
|
|
13
|
+
checkPoolsLiquidity,
|
|
14
|
+
checkConstantPoolPrices,
|
|
15
|
+
} from '../../../tests/utils';
|
|
16
|
+
import { Tokens } from '../../../tests/constants-e2e';
|
|
17
|
+
import { Address } from '../../types';
|
|
18
|
+
import { OSwap } from './oswap';
|
|
19
|
+
import { OSwapPool } from './types';
|
|
20
|
+
|
|
21
|
+
async function getOnchainTraderates(
|
|
22
|
+
oswap: OSwap,
|
|
23
|
+
pool: OSwapPool,
|
|
24
|
+
blockNumber: number,
|
|
25
|
+
): Promise<{ traderate0: bigint; traderate1: bigint }> {
|
|
26
|
+
const callData: MultiCallParams<bigint>[] = [
|
|
27
|
+
{
|
|
28
|
+
target: pool.address,
|
|
29
|
+
callData: oswap.iOSwap.encodeFunctionData('traderate0', []),
|
|
30
|
+
decodeFunction: uint256ToBigInt,
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
target: pool.address,
|
|
34
|
+
callData: oswap.iOSwap.encodeFunctionData('traderate1', []),
|
|
35
|
+
decodeFunction: uint256ToBigInt,
|
|
36
|
+
},
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const results = await oswap.dexHelper.multiWrapper.aggregate<bigint>(
|
|
40
|
+
callData,
|
|
41
|
+
blockNumber,
|
|
42
|
+
oswap.dexHelper.multiWrapper.defaultBatchSize,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
return { traderate0: results[0], traderate1: results[1] };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Check prices passed as arguments against prices calculated from on-chain data.
|
|
49
|
+
async function checkOnChainPricing(
|
|
50
|
+
oswap: OSwap,
|
|
51
|
+
pool: OSwapPool,
|
|
52
|
+
blockNumber: number,
|
|
53
|
+
prices: bigint[],
|
|
54
|
+
side: SwapSide,
|
|
55
|
+
srcAddress: Address,
|
|
56
|
+
destAddress: Address,
|
|
57
|
+
amounts: bigint[],
|
|
58
|
+
) {
|
|
59
|
+
// Get the onchain trade rates from the pool and calculate the prices.
|
|
60
|
+
const data = await getOnchainTraderates(oswap, pool, blockNumber);
|
|
61
|
+
let expectedPrices: bigint[] = [];
|
|
62
|
+
for (const amount of amounts) {
|
|
63
|
+
const rate =
|
|
64
|
+
srcAddress.toLowerCase() === pool.token0
|
|
65
|
+
? data.traderate0
|
|
66
|
+
: data.traderate1;
|
|
67
|
+
if (side === SwapSide.SELL) {
|
|
68
|
+
expectedPrices.push((amount * rate) / getBigIntPow(36));
|
|
69
|
+
} else {
|
|
70
|
+
// SwapSide.BUY
|
|
71
|
+
expectedPrices.push((amount * getBigIntPow(36)) / rate);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
expect(prices).toEqual(expectedPrices);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function testPricingOnNetwork(
|
|
78
|
+
oswap: OSwap,
|
|
79
|
+
network: Network,
|
|
80
|
+
dexKey: string,
|
|
81
|
+
blockNumber: number,
|
|
82
|
+
srcTokenSymbol: string,
|
|
83
|
+
destTokenSymbol: string,
|
|
84
|
+
side: SwapSide,
|
|
85
|
+
amounts: bigint[],
|
|
86
|
+
) {
|
|
87
|
+
const networkTokens = Tokens[network];
|
|
88
|
+
const srcToken = networkTokens[srcTokenSymbol];
|
|
89
|
+
const destToken = networkTokens[destTokenSymbol];
|
|
90
|
+
|
|
91
|
+
const poolIds = await oswap.getPoolIdentifiers(
|
|
92
|
+
srcToken,
|
|
93
|
+
destToken,
|
|
94
|
+
side,
|
|
95
|
+
blockNumber,
|
|
96
|
+
);
|
|
97
|
+
console.log(
|
|
98
|
+
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Identifiers: ${poolIds}`,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
expect(poolIds.length).toBeGreaterThan(0);
|
|
102
|
+
|
|
103
|
+
// Get calculated prices based on the stored state.
|
|
104
|
+
const poolPrices = await oswap.getPricesVolume(
|
|
105
|
+
srcToken,
|
|
106
|
+
destToken,
|
|
107
|
+
amounts,
|
|
108
|
+
side,
|
|
109
|
+
blockNumber,
|
|
110
|
+
poolIds,
|
|
111
|
+
);
|
|
112
|
+
console.log(
|
|
113
|
+
`${side} ${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
|
|
114
|
+
poolPrices,
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(poolPrices).not.toBeNull();
|
|
118
|
+
if (oswap.hasConstantPriceLargeAmounts) {
|
|
119
|
+
checkConstantPoolPrices(poolPrices!, amounts, dexKey);
|
|
120
|
+
} else {
|
|
121
|
+
checkPoolPrices(poolPrices!, amounts, side, dexKey);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Check that the prices calculated from onchain data match with the ones calculated from the stored state.
|
|
125
|
+
const pool = oswap.getPoolById(poolIds[0]);
|
|
126
|
+
expect(pool).not.toBeNull();
|
|
127
|
+
await checkOnChainPricing(
|
|
128
|
+
oswap,
|
|
129
|
+
pool as OSwapPool,
|
|
130
|
+
blockNumber,
|
|
131
|
+
poolPrices![0].prices,
|
|
132
|
+
side,
|
|
133
|
+
srcToken.address,
|
|
134
|
+
destToken.address,
|
|
135
|
+
amounts,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
describe('OSwap', function () {
|
|
140
|
+
const dexKey = 'OSwap';
|
|
141
|
+
let blockNumber: number;
|
|
142
|
+
let oswap: OSwap;
|
|
143
|
+
|
|
144
|
+
describe('Mainnet', () => {
|
|
145
|
+
const network = Network.MAINNET;
|
|
146
|
+
const dexHelper = new DummyDexHelper(network);
|
|
147
|
+
|
|
148
|
+
const tokens = Tokens[network];
|
|
149
|
+
|
|
150
|
+
const srcTokenSymbol = 'WETH';
|
|
151
|
+
const destTokenSymbol = 'STETH';
|
|
152
|
+
|
|
153
|
+
const amountsForSell = [
|
|
154
|
+
0n,
|
|
155
|
+
1n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
156
|
+
2n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
157
|
+
3n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
158
|
+
4n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
159
|
+
5n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
160
|
+
6n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
161
|
+
7n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
162
|
+
8n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
163
|
+
9n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
164
|
+
10n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
const amountsForBuy = [
|
|
168
|
+
0n,
|
|
169
|
+
1n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
170
|
+
2n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
171
|
+
3n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
172
|
+
4n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
173
|
+
5n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
174
|
+
6n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
175
|
+
7n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
176
|
+
8n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
177
|
+
9n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
178
|
+
10n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
// Return a blockNumber to use for the tests.
|
|
182
|
+
// Check that the pool has enough liquidity to run the tests at the current blockNumber.
|
|
183
|
+
// If not, fallback to a known blockNumber in the past with
|
|
184
|
+
// high enough liquidity - the on-chain queries will just be a bit slower to execute.
|
|
185
|
+
async function getBlockNumberForTesting(oswap: OSwap): Promise<number> {
|
|
186
|
+
const DEFAULT_BLOCK_NUMBER = 18888241;
|
|
187
|
+
|
|
188
|
+
blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
|
|
189
|
+
const srcToken = Tokens[network][srcTokenSymbol];
|
|
190
|
+
const destToken = Tokens[network][destTokenSymbol];
|
|
191
|
+
|
|
192
|
+
// Get the pool and its state for the given test pair.
|
|
193
|
+
const pool = oswap.getPoolByTokenPair(srcToken, destToken);
|
|
194
|
+
if (!pool)
|
|
195
|
+
throw new Error(
|
|
196
|
+
`No pool found for pair ${srcTokenSymbol}-${destTokenSymbol}`,
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
const eventPool = oswap.eventPools[pool.id];
|
|
200
|
+
const state = await eventPool.getStateOrGenerate(blockNumber, true);
|
|
201
|
+
|
|
202
|
+
const minBalance =
|
|
203
|
+
state.balance0 < state.balance1 ? state.balance0 : state.balance1;
|
|
204
|
+
const maxAmount = [...amountsForSell, ...amountsForBuy].reduce(
|
|
205
|
+
(max, amount) => (amount > max ? amount : max),
|
|
206
|
+
);
|
|
207
|
+
const hasEnoughLiquidity = BigInt(minBalance) > maxAmount;
|
|
208
|
+
|
|
209
|
+
if (!hasEnoughLiquidity) {
|
|
210
|
+
return DEFAULT_BLOCK_NUMBER;
|
|
211
|
+
}
|
|
212
|
+
return blockNumber;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
beforeAll(async () => {
|
|
216
|
+
oswap = new OSwap(network, dexKey, dexHelper);
|
|
217
|
+
|
|
218
|
+
blockNumber = await getBlockNumberForTesting(oswap);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('getPoolIdentifiers and getPricesVolume SELL', async function () {
|
|
222
|
+
await testPricingOnNetwork(
|
|
223
|
+
oswap,
|
|
224
|
+
network,
|
|
225
|
+
dexKey,
|
|
226
|
+
blockNumber,
|
|
227
|
+
srcTokenSymbol,
|
|
228
|
+
destTokenSymbol,
|
|
229
|
+
SwapSide.SELL,
|
|
230
|
+
amountsForSell,
|
|
231
|
+
);
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('getPoolIdentifiers and getPricesVolume BUY', async function () {
|
|
235
|
+
await testPricingOnNetwork(
|
|
236
|
+
oswap,
|
|
237
|
+
network,
|
|
238
|
+
dexKey,
|
|
239
|
+
blockNumber,
|
|
240
|
+
srcTokenSymbol,
|
|
241
|
+
destTokenSymbol,
|
|
242
|
+
SwapSide.BUY,
|
|
243
|
+
amountsForBuy,
|
|
244
|
+
);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it(`getTopPoolsForToken ${srcTokenSymbol}`, async function () {
|
|
248
|
+
// We have to check without calling initializePricing, because
|
|
249
|
+
// pool-tracker is not calling that function
|
|
250
|
+
const newOSwap = new OSwap(network, dexKey, dexHelper);
|
|
251
|
+
await newOSwap.updatePoolState?.();
|
|
252
|
+
|
|
253
|
+
const poolLiquidity = await newOSwap.getTopPoolsForToken(
|
|
254
|
+
tokens[srcTokenSymbol].address,
|
|
255
|
+
10,
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
console.log(`${srcTokenSymbol} top pools:`, poolLiquidity);
|
|
259
|
+
|
|
260
|
+
if (!newOSwap.hasConstantPriceLargeAmounts) {
|
|
261
|
+
checkPoolsLiquidity(
|
|
262
|
+
poolLiquidity,
|
|
263
|
+
Tokens[network][srcTokenSymbol].address,
|
|
264
|
+
dexKey,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it(`getTopPoolsForToken ${destTokenSymbol}`, async function () {
|
|
270
|
+
// We have to check without calling initializePricing, because
|
|
271
|
+
// pool-tracker is not calling that function
|
|
272
|
+
const newOSwap = new OSwap(network, dexKey, dexHelper);
|
|
273
|
+
await newOSwap.updatePoolState?.();
|
|
274
|
+
|
|
275
|
+
const poolLiquidity = await newOSwap.getTopPoolsForToken(
|
|
276
|
+
tokens[destTokenSymbol].address,
|
|
277
|
+
10,
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
console.log(`${destTokenSymbol} top pools:`, poolLiquidity);
|
|
281
|
+
|
|
282
|
+
if (!newOSwap.hasConstantPriceLargeAmounts) {
|
|
283
|
+
checkPoolsLiquidity(
|
|
284
|
+
poolLiquidity,
|
|
285
|
+
Tokens[network][destTokenSymbol].address,
|
|
286
|
+
dexKey,
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
});
|