@paraswap/dex-lib 2.19.0 → 2.19.1-swaap-v2
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/.idea/aws.xml +17 -0
- package/.idea/codeStyles/Project.xml +19 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/paraswap-dex-lib.iml +9 -0
- package/.idea/vcs.xml +6 -0
- package/build/abi/swaap-v2/vault.json +918 -0
- package/build/config.js +4 -2
- package/build/config.js.map +1 -1
- package/build/dex/aave-v3/tokens/arbitrum.json +50 -0
- package/build/dex/aave-v3/tokens/avalanche.json +74 -0
- package/build/dex/aave-v3/tokens/fantom.json +62 -0
- package/build/dex/aave-v3/tokens/optimism.json +50 -0
- package/build/dex/aave-v3/tokens/polygon.json +116 -0
- package/build/dex/index.js +2 -0
- package/build/dex/index.js.map +1 -1
- package/build/dex/pancakeswap-v3/types.d.ts +14 -0
- package/build/dex/pancakeswap-v3/types.js +3 -0
- package/build/dex/pancakeswap-v3/types.js.map +1 -0
- package/build/dex/swaap-v2/config.d.ts +4 -0
- package/build/dex/swaap-v2/config.js +19 -0
- package/build/dex/swaap-v2/config.js.map +1 -0
- package/build/dex/swaap-v2/constants.d.ts +11 -0
- package/build/dex/swaap-v2/constants.js +15 -0
- package/build/dex/swaap-v2/constants.js.map +1 -0
- package/build/dex/swaap-v2/rate-fetcher.d.ts +16 -0
- package/build/dex/swaap-v2/rate-fetcher.js +101 -0
- package/build/dex/swaap-v2/rate-fetcher.js.map +1 -0
- package/build/dex/swaap-v2/swaap-v2.d.ts +59 -0
- package/build/dex/swaap-v2/swaap-v2.js +525 -0
- package/build/dex/swaap-v2/swaap-v2.js.map +1 -0
- package/build/dex/swaap-v2/types.d.ts +78 -0
- package/build/dex/swaap-v2/types.js +10 -0
- package/build/dex/swaap-v2/types.js.map +1 -0
- package/build/dex/swaap-v2/utils.d.ts +7 -0
- package/build/dex/swaap-v2/utils.js +34 -0
- package/build/dex/swaap-v2/utils.js.map +1 -0
- package/build/dex/swaap-v2/validators.d.ts +6 -0
- package/build/dex/swaap-v2/validators.js +125 -0
- package/build/dex/swaap-v2/validators.js.map +1 -0
- package/build/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/abi/swaap-v2/vault.json +918 -0
- package/src/config.ts +5 -2
- package/src/dex/index.ts +2 -0
- package/src/dex/swaap-v2/config.ts +19 -0
- package/src/dex/swaap-v2/constants.ts +21 -0
- package/src/dex/swaap-v2/rate-fetcher.ts +165 -0
- package/src/dex/swaap-v2/swaap-v2-e2e.test.ts +199 -0
- package/src/dex/swaap-v2/swaap-v2-integration.test.ts +145 -0
- package/src/dex/swaap-v2/swaap-v2.ts +817 -0
- package/src/dex/swaap-v2/types.ts +90 -0
- package/src/dex/swaap-v2/utils.ts +44 -0
- package/src/dex/swaap-v2/validators.ts +130 -0
- package/src/types.ts +1 -0
- package/tests/constants-e2e.ts +1 -1
- package/tests/utils-e2e.ts +5 -0
- package/build/abi/DirectSwap.json +0 -1008
package/src/config.ts
CHANGED
|
@@ -29,6 +29,7 @@ type BaseConfig = {
|
|
|
29
29
|
rpcPollingBlocksBackToTriggerUpdate: number;
|
|
30
30
|
hashFlowAuthToken?: string;
|
|
31
31
|
hashFlowDisabledMMs: string[];
|
|
32
|
+
swaapV2AuthToken?: string;
|
|
32
33
|
};
|
|
33
34
|
|
|
34
35
|
const baseConfigs: { [network: number]: BaseConfig } = {
|
|
@@ -185,8 +186,8 @@ const baseConfigs: { [network: number]: BaseConfig } = {
|
|
|
185
186
|
process.env[`HASHFLOW_DISABLED_MMS_137`]?.split(',') || [],
|
|
186
187
|
adapterAddresses: {
|
|
187
188
|
PolygonAdapter01: '0xE44769f42E1e9592f86B82f206407a8f7C84b4ed',
|
|
188
|
-
PolygonAdapter02: '
|
|
189
|
-
PolygonBuyAdapter: '
|
|
189
|
+
PolygonAdapter02: '0xa05d8C3F278fC7b20b39Ea7A3035E3aD8D808c78',
|
|
190
|
+
PolygonBuyAdapter: '0xB11bCA7B01b425afD0743A4D77B4f593883f94C0',
|
|
190
191
|
},
|
|
191
192
|
uniswapV2ExchangeRouterAddress:
|
|
192
193
|
'0xf3938337F7294fEf84e9B2c6D548A93F956Cc281',
|
|
@@ -194,6 +195,7 @@ const baseConfigs: { [network: number]: BaseConfig } = {
|
|
|
194
195
|
rfqConfigs: {},
|
|
195
196
|
rpcPollingMaxAllowedStateDelayInBlocks: 2,
|
|
196
197
|
rpcPollingBlocksBackToTriggerUpdate: 1,
|
|
198
|
+
swaapV2AuthToken: process.env.API_KEY_SWAAP_V2_AUTH_TOKEN || '',
|
|
197
199
|
},
|
|
198
200
|
[Network.AVALANCHE]: {
|
|
199
201
|
network: Network.AVALANCHE,
|
|
@@ -345,6 +347,7 @@ export function generateConfig(network: number): Config {
|
|
|
345
347
|
rpcPollingBlocksBackToTriggerUpdate:
|
|
346
348
|
baseConfig.rpcPollingBlocksBackToTriggerUpdate,
|
|
347
349
|
hashFlowAuthToken: baseConfig.hashFlowAuthToken,
|
|
350
|
+
swaapV2AuthToken: baseConfig.swaapV2AuthToken,
|
|
348
351
|
hashFlowDisabledMMs: baseConfig.hashFlowDisabledMMs,
|
|
349
352
|
};
|
|
350
353
|
}
|
package/src/dex/index.ts
CHANGED
|
@@ -64,6 +64,7 @@ import { MaverickV1 } from './maverick-v1/maverick-v1';
|
|
|
64
64
|
import { QuickSwapV3 } from './quickswap/quickswap-v3';
|
|
65
65
|
import { ZyberSwapV3 } from './quickswap/zyberswap-v3';
|
|
66
66
|
import { TraderJoeV2 } from './trader-joe-v2';
|
|
67
|
+
import { SwaapV2 } from './swaap-v2/swaap-v2';
|
|
67
68
|
import { SpiritSwapV3 } from './quickswap/spiritswap-v3';
|
|
68
69
|
import { TraderJoeV21 } from './trader-joe-v2.1';
|
|
69
70
|
import { PancakeswapV3 } from './pancakeswap-v3/pancakeswap-v3';
|
|
@@ -131,6 +132,7 @@ const Dexes = [
|
|
|
131
132
|
Hashflow,
|
|
132
133
|
MaverickV1,
|
|
133
134
|
Camelot,
|
|
135
|
+
SwaapV2,
|
|
134
136
|
];
|
|
135
137
|
|
|
136
138
|
export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DexParams } from './types';
|
|
2
|
+
import { DexConfigMap, AdapterMappings } from '../../types';
|
|
3
|
+
import { Network, SwapSide } from '../../constants';
|
|
4
|
+
|
|
5
|
+
export const SwaapV2Config: DexConfigMap<DexParams> = {
|
|
6
|
+
SwaapV2: {
|
|
7
|
+
// Mainnet and Arbitrum will be supported later
|
|
8
|
+
// [Network.MAINNET]: {},
|
|
9
|
+
// [Network.ARBITRUM]: {},
|
|
10
|
+
[Network.POLYGON]: {},
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const Adapters: Record<number, AdapterMappings> = {
|
|
15
|
+
[Network.POLYGON]: {
|
|
16
|
+
[SwapSide.SELL]: [{ name: 'PolygonAdapter02', index: 8 }],
|
|
17
|
+
[SwapSide.BUY]: [{ name: 'PolygonBuyAdapter', index: 7 }],
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const SWAAP_RFQ_PRICES_CACHES_TTL_S = 3;
|
|
2
|
+
|
|
3
|
+
export const SWAAP_RFQ_QUOTE_TIMEOUT_MS = 2000;
|
|
4
|
+
|
|
5
|
+
export const SWAAP_RFQ_API_PRICES_POLLING_INTERVAL_MS = 1000;
|
|
6
|
+
|
|
7
|
+
export const SWAAP_RFQ_API_URL = 'https://api.swaap.finance/v1/rfq';
|
|
8
|
+
|
|
9
|
+
export const SWAAP_RFQ_PRICES_ENDPOINT = 'prices';
|
|
10
|
+
|
|
11
|
+
export const SWAAP_RFQ_QUOTE_ENDPOINT = 'quote';
|
|
12
|
+
|
|
13
|
+
export const SWAAP_RFQ_TOKENS_ENDPOINT = 'tokens';
|
|
14
|
+
|
|
15
|
+
export const SWAAP_BLACKLIST_TTL_S = 60 * 60 * 24; // 24 hours
|
|
16
|
+
|
|
17
|
+
export const GAS_COST_ESTIMATION = 142_000;
|
|
18
|
+
|
|
19
|
+
export const BATCH_SWAP_SELECTOR = '0x945bcec9';
|
|
20
|
+
|
|
21
|
+
export const CALLER_SLOT = 160;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { IDexHelper } from '../../dex-helper';
|
|
2
|
+
import { Fetcher } from '../../lib/fetcher/fetcher';
|
|
3
|
+
import { validateAndCast } from '../../lib/validators';
|
|
4
|
+
import { Logger, Token, Address } from '../../types';
|
|
5
|
+
import {
|
|
6
|
+
SwaapV2RateFetcherConfig,
|
|
7
|
+
SwaapV2PriceLevelsResponse,
|
|
8
|
+
SwaapV2QuoteResponse,
|
|
9
|
+
SwaapV2PriceLevels,
|
|
10
|
+
SwaapV2QuoteRequest,
|
|
11
|
+
SwaapV2OrderType,
|
|
12
|
+
} from './types';
|
|
13
|
+
import {
|
|
14
|
+
priceLevelsResponseValidator,
|
|
15
|
+
getQuoteResponseValidator,
|
|
16
|
+
} from './validators';
|
|
17
|
+
import { getPriceLevelsCacheKey, normalizeTokenAddress } from './utils';
|
|
18
|
+
import { SWAAP_RFQ_QUOTE_TIMEOUT_MS } from './constants';
|
|
19
|
+
import { RequestConfig } from '../../dex-helper/irequest-wrapper';
|
|
20
|
+
|
|
21
|
+
export class RateFetcher {
|
|
22
|
+
private rateFetcher: Fetcher<SwaapV2PriceLevelsResponse>;
|
|
23
|
+
private pricesCacheTTL: number;
|
|
24
|
+
|
|
25
|
+
constructor(
|
|
26
|
+
private dexHelper: IDexHelper,
|
|
27
|
+
private dexKey: string,
|
|
28
|
+
private logger: Logger,
|
|
29
|
+
config: SwaapV2RateFetcherConfig,
|
|
30
|
+
) {
|
|
31
|
+
this.pricesCacheTTL = config.rateConfig.pricesCacheTTLSecs;
|
|
32
|
+
|
|
33
|
+
this.rateFetcher = new Fetcher<SwaapV2PriceLevelsResponse>(
|
|
34
|
+
dexHelper.httpRequest,
|
|
35
|
+
{
|
|
36
|
+
info: {
|
|
37
|
+
requestOptions: config.rateConfig.pricesReqParams,
|
|
38
|
+
caster: (data: unknown) => {
|
|
39
|
+
return validateAndCast<SwaapV2PriceLevelsResponse>(
|
|
40
|
+
data,
|
|
41
|
+
priceLevelsResponseValidator,
|
|
42
|
+
);
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
handler: this.handleRatesResponse.bind(this),
|
|
46
|
+
},
|
|
47
|
+
config.rateConfig.pricesIntervalMs,
|
|
48
|
+
logger,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
start() {
|
|
53
|
+
this.rateFetcher.startPolling();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
stop() {
|
|
57
|
+
this.rateFetcher.stopPolling();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private handleRatesResponse(resp: SwaapV2PriceLevelsResponse): void {
|
|
61
|
+
if (!resp.success) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const levels = Object.keys(resp.levels)
|
|
66
|
+
.map(pairName => {
|
|
67
|
+
const pair = resp.levels[pairName];
|
|
68
|
+
if (!pair) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const levels = resp.levels[pairName];
|
|
72
|
+
|
|
73
|
+
if (!levels.asks || !levels.bids) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const pairSplit = pairName.split('/');
|
|
78
|
+
|
|
79
|
+
const baseAddress = pairSplit[0];
|
|
80
|
+
const quoteAddress = pairSplit[1];
|
|
81
|
+
pair.base = normalizeTokenAddress(baseAddress);
|
|
82
|
+
pair.quote = normalizeTokenAddress(quoteAddress);
|
|
83
|
+
return pair;
|
|
84
|
+
})
|
|
85
|
+
.filter((p: SwaapV2PriceLevels | undefined) => p != null);
|
|
86
|
+
|
|
87
|
+
this.dexHelper.cache.setex(
|
|
88
|
+
this.dexKey,
|
|
89
|
+
this.dexHelper.config.data.network,
|
|
90
|
+
`${getPriceLevelsCacheKey(this.dexKey)}`,
|
|
91
|
+
this.pricesCacheTTL,
|
|
92
|
+
JSON.stringify(levels),
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async getQuote(
|
|
97
|
+
networkId: number,
|
|
98
|
+
_srcToken: Token,
|
|
99
|
+
_destToken: Token,
|
|
100
|
+
srcAmount: string,
|
|
101
|
+
side: SwaapV2OrderType,
|
|
102
|
+
userAddress: Address,
|
|
103
|
+
aggregatorRecipient: Address,
|
|
104
|
+
tolerance: number,
|
|
105
|
+
requestParameters: RequestConfig,
|
|
106
|
+
): Promise<SwaapV2QuoteResponse> {
|
|
107
|
+
const srcToken = this.dexHelper.config.wrapETH(_srcToken);
|
|
108
|
+
const destToken = this.dexHelper.config.wrapETH(_destToken);
|
|
109
|
+
|
|
110
|
+
if (BigInt(srcAmount) === 0n) {
|
|
111
|
+
throw new Error('geQuote failed with srcAmount == 0');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const _payload: SwaapV2QuoteRequest = {
|
|
115
|
+
network_id: networkId,
|
|
116
|
+
origin: userAddress,
|
|
117
|
+
sender: aggregatorRecipient,
|
|
118
|
+
recipient: aggregatorRecipient,
|
|
119
|
+
timestamp: Math.round(Date.now() / 1000),
|
|
120
|
+
order_type: side,
|
|
121
|
+
token_in: srcToken.address,
|
|
122
|
+
token_out: destToken.address,
|
|
123
|
+
amount: srcAmount,
|
|
124
|
+
tolerance: tolerance,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
try {
|
|
128
|
+
let payload: RequestConfig = {
|
|
129
|
+
data: _payload,
|
|
130
|
+
...requestParameters,
|
|
131
|
+
timeout: SWAAP_RFQ_QUOTE_TIMEOUT_MS,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
this.logger.info(
|
|
135
|
+
'GetQuote Request:',
|
|
136
|
+
JSON.stringify(payload).replace(/(?:\r\n|\r|\n)/g, ' '),
|
|
137
|
+
);
|
|
138
|
+
const { data } = await this.dexHelper.httpRequest.request<unknown>(
|
|
139
|
+
payload,
|
|
140
|
+
);
|
|
141
|
+
this.logger.info(
|
|
142
|
+
'GetQuote Response: ',
|
|
143
|
+
JSON.stringify(data).replace(/(?:\r\n|\r|\n)/g, ' '),
|
|
144
|
+
);
|
|
145
|
+
const quoteResp = validateAndCast<SwaapV2QuoteResponse>(
|
|
146
|
+
data,
|
|
147
|
+
getQuoteResponseValidator,
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
return {
|
|
151
|
+
id: quoteResp.id,
|
|
152
|
+
calldata: quoteResp.calldata,
|
|
153
|
+
router: quoteResp.router,
|
|
154
|
+
expiration: quoteResp.expiration,
|
|
155
|
+
amount: quoteResp.amount,
|
|
156
|
+
guaranteed_price: quoteResp.guaranteed_price,
|
|
157
|
+
success: quoteResp.success,
|
|
158
|
+
recipient: quoteResp.recipient,
|
|
159
|
+
};
|
|
160
|
+
} catch (e) {
|
|
161
|
+
this.logger.error(e);
|
|
162
|
+
throw e;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
dotenv.config();
|
|
3
|
+
|
|
4
|
+
import { testE2E } from '../../../tests/utils-e2e';
|
|
5
|
+
import {
|
|
6
|
+
Tokens,
|
|
7
|
+
Holders,
|
|
8
|
+
NativeTokenSymbols,
|
|
9
|
+
} from '../../../tests/constants-e2e';
|
|
10
|
+
import { Network, ContractMethod, SwapSide } from '../../constants';
|
|
11
|
+
import { StaticJsonRpcProvider } from '@ethersproject/providers';
|
|
12
|
+
import { generateConfig } from '../../config';
|
|
13
|
+
|
|
14
|
+
const sleepMs: number = 5000;
|
|
15
|
+
|
|
16
|
+
function testForNetwork(
|
|
17
|
+
network: Network,
|
|
18
|
+
dexKey: string,
|
|
19
|
+
tokenASymbol: string,
|
|
20
|
+
tokenBSymbol: string,
|
|
21
|
+
tokenAAmount: string,
|
|
22
|
+
tokenBAmount: string,
|
|
23
|
+
nativeTokenAmount: string,
|
|
24
|
+
excludeNativeTokenTests: boolean = true,
|
|
25
|
+
) {
|
|
26
|
+
const provider = new StaticJsonRpcProvider(
|
|
27
|
+
generateConfig(network).privateHttpProvider,
|
|
28
|
+
network,
|
|
29
|
+
);
|
|
30
|
+
const tokens = Tokens[network];
|
|
31
|
+
const holders = Holders[network];
|
|
32
|
+
const nativeTokenSymbol = NativeTokenSymbols[network];
|
|
33
|
+
|
|
34
|
+
const sideToContractMethods = new Map([
|
|
35
|
+
[
|
|
36
|
+
SwapSide.SELL,
|
|
37
|
+
[
|
|
38
|
+
ContractMethod.simpleSwap,
|
|
39
|
+
ContractMethod.multiSwap,
|
|
40
|
+
ContractMethod.megaSwap,
|
|
41
|
+
],
|
|
42
|
+
],
|
|
43
|
+
[SwapSide.BUY, [ContractMethod.simpleBuy, ContractMethod.buy]],
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
describe(`${network}`, () => {
|
|
47
|
+
sideToContractMethods.forEach((contractMethods, side) =>
|
|
48
|
+
describe(`${side}`, () => {
|
|
49
|
+
contractMethods.forEach((contractMethod: ContractMethod) => {
|
|
50
|
+
describe(`${contractMethod}`, () => {
|
|
51
|
+
if (excludeNativeTokenTests) {
|
|
52
|
+
it(`${tokenASymbol} -> ${tokenBSymbol}`, async () => {
|
|
53
|
+
await testE2E(
|
|
54
|
+
tokens[tokenASymbol],
|
|
55
|
+
tokens[tokenBSymbol],
|
|
56
|
+
holders[tokenASymbol],
|
|
57
|
+
side === SwapSide.SELL ? tokenAAmount : tokenBAmount,
|
|
58
|
+
side,
|
|
59
|
+
dexKey,
|
|
60
|
+
contractMethod,
|
|
61
|
+
network,
|
|
62
|
+
provider,
|
|
63
|
+
undefined,
|
|
64
|
+
undefined,
|
|
65
|
+
undefined,
|
|
66
|
+
undefined,
|
|
67
|
+
sleepMs,
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
it(`${tokenBSymbol} -> ${tokenASymbol}`, async () => {
|
|
71
|
+
await testE2E(
|
|
72
|
+
tokens[tokenBSymbol],
|
|
73
|
+
tokens[tokenASymbol],
|
|
74
|
+
holders[tokenBSymbol],
|
|
75
|
+
side === SwapSide.SELL ? tokenBAmount : tokenAAmount,
|
|
76
|
+
side,
|
|
77
|
+
dexKey,
|
|
78
|
+
contractMethod,
|
|
79
|
+
network,
|
|
80
|
+
provider,
|
|
81
|
+
undefined,
|
|
82
|
+
undefined,
|
|
83
|
+
undefined,
|
|
84
|
+
undefined,
|
|
85
|
+
sleepMs,
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
it(`${nativeTokenSymbol} -> ${tokenASymbol}`, async () => {
|
|
90
|
+
await testE2E(
|
|
91
|
+
tokens[nativeTokenSymbol],
|
|
92
|
+
tokens[tokenASymbol],
|
|
93
|
+
holders[nativeTokenSymbol],
|
|
94
|
+
side === SwapSide.SELL ? nativeTokenAmount : tokenAAmount,
|
|
95
|
+
side,
|
|
96
|
+
dexKey,
|
|
97
|
+
contractMethod,
|
|
98
|
+
network,
|
|
99
|
+
provider,
|
|
100
|
+
undefined,
|
|
101
|
+
undefined,
|
|
102
|
+
undefined,
|
|
103
|
+
undefined,
|
|
104
|
+
sleepMs,
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
it(`${tokenASymbol} -> ${nativeTokenSymbol}`, async () => {
|
|
108
|
+
await testE2E(
|
|
109
|
+
tokens[tokenASymbol],
|
|
110
|
+
tokens[nativeTokenSymbol],
|
|
111
|
+
holders[tokenASymbol],
|
|
112
|
+
side === SwapSide.SELL ? tokenAAmount : nativeTokenAmount,
|
|
113
|
+
side,
|
|
114
|
+
dexKey,
|
|
115
|
+
contractMethod,
|
|
116
|
+
network,
|
|
117
|
+
provider,
|
|
118
|
+
undefined,
|
|
119
|
+
undefined,
|
|
120
|
+
undefined,
|
|
121
|
+
undefined,
|
|
122
|
+
sleepMs,
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
describe('SwaapV2 E2E', () => {
|
|
134
|
+
const dexKey = 'SwaapV2';
|
|
135
|
+
|
|
136
|
+
describe('Polygon', () => {
|
|
137
|
+
const network = Network.POLYGON;
|
|
138
|
+
|
|
139
|
+
const tokenASymbol: string = 'USDC';
|
|
140
|
+
const tokenBSymbol: string = 'WETH';
|
|
141
|
+
|
|
142
|
+
const tokenAAmount: string = '100000000';
|
|
143
|
+
const tokenBAmount: string = '100000000000000000';
|
|
144
|
+
const nativeTokenAmount = '1000000000000000000';
|
|
145
|
+
|
|
146
|
+
testForNetwork(
|
|
147
|
+
network,
|
|
148
|
+
dexKey,
|
|
149
|
+
tokenASymbol,
|
|
150
|
+
tokenBSymbol,
|
|
151
|
+
tokenAAmount,
|
|
152
|
+
tokenBAmount,
|
|
153
|
+
nativeTokenAmount,
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Arbitrum will be supported later
|
|
158
|
+
// describe('Arbitrum', () => {
|
|
159
|
+
// const network = Network.ARBITRUM;
|
|
160
|
+
//
|
|
161
|
+
// const tokenASymbol: string = 'ETH';
|
|
162
|
+
// const tokenBSymbol: string = 'DAI';
|
|
163
|
+
//
|
|
164
|
+
// const tokenAAmount: string = '100000000';
|
|
165
|
+
// const tokenBAmount: string = '1000000000000000000';
|
|
166
|
+
// const nativeTokenAmount = '1000000000000000000';
|
|
167
|
+
//
|
|
168
|
+
// testForNetwork(
|
|
169
|
+
// network,
|
|
170
|
+
// dexKey,
|
|
171
|
+
// tokenASymbol,
|
|
172
|
+
// tokenBSymbol,
|
|
173
|
+
// tokenAAmount,
|
|
174
|
+
// tokenBAmount,
|
|
175
|
+
// nativeTokenAmount,
|
|
176
|
+
// );
|
|
177
|
+
// });
|
|
178
|
+
|
|
179
|
+
// Mainnet will be supported later
|
|
180
|
+
// describe('Mainnet', () => {
|
|
181
|
+
// const network = Network.MAINNET;
|
|
182
|
+
// const tokenASymbol: string = 'USDC';
|
|
183
|
+
// const tokenBSymbol: string = 'USDT';
|
|
184
|
+
//
|
|
185
|
+
// const tokenAAmount: string = '100000000';
|
|
186
|
+
// const tokenBAmount: string = '1000000000000000000';
|
|
187
|
+
// const nativeTokenAmount = '1000000000000000000';
|
|
188
|
+
//
|
|
189
|
+
// testForNetwork(
|
|
190
|
+
// network,
|
|
191
|
+
// dexKey,
|
|
192
|
+
// tokenASymbol,
|
|
193
|
+
// tokenBSymbol,
|
|
194
|
+
// tokenAAmount,
|
|
195
|
+
// tokenBAmount,
|
|
196
|
+
// nativeTokenAmount,
|
|
197
|
+
// );
|
|
198
|
+
// });
|
|
199
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
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 { SwaapV2 } from './swaap-v2';
|
|
9
|
+
import {
|
|
10
|
+
checkPoolPrices,
|
|
11
|
+
checkPoolsLiquidity,
|
|
12
|
+
checkConstantPoolPrices,
|
|
13
|
+
sleep,
|
|
14
|
+
} from '../../../tests/utils';
|
|
15
|
+
import { Tokens } from '../../../tests/constants-e2e';
|
|
16
|
+
|
|
17
|
+
async function testPricingOnNetwork(
|
|
18
|
+
swaapV2: SwaapV2,
|
|
19
|
+
network: Network,
|
|
20
|
+
dexKey: string,
|
|
21
|
+
blockNumber: number,
|
|
22
|
+
srcTokenSymbol: string,
|
|
23
|
+
destTokenSymbol: string,
|
|
24
|
+
side: SwapSide,
|
|
25
|
+
amounts: bigint[],
|
|
26
|
+
) {
|
|
27
|
+
const networkTokens = Tokens[network];
|
|
28
|
+
|
|
29
|
+
const pools = await swaapV2.getPoolIdentifiers(
|
|
30
|
+
networkTokens[srcTokenSymbol],
|
|
31
|
+
networkTokens[destTokenSymbol],
|
|
32
|
+
side,
|
|
33
|
+
blockNumber,
|
|
34
|
+
);
|
|
35
|
+
console.log(
|
|
36
|
+
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Identifiers: `,
|
|
37
|
+
pools,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(pools.length).toBeGreaterThan(0);
|
|
41
|
+
|
|
42
|
+
const poolPrices = await swaapV2.getPricesVolume(
|
|
43
|
+
networkTokens[srcTokenSymbol],
|
|
44
|
+
networkTokens[destTokenSymbol],
|
|
45
|
+
amounts,
|
|
46
|
+
side,
|
|
47
|
+
blockNumber,
|
|
48
|
+
pools,
|
|
49
|
+
);
|
|
50
|
+
console.log(
|
|
51
|
+
`${srcTokenSymbol} <> ${destTokenSymbol} Pool Prices: `,
|
|
52
|
+
poolPrices,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
expect(poolPrices).not.toBeNull();
|
|
56
|
+
if (swaapV2.hasConstantPriceLargeAmounts) {
|
|
57
|
+
checkConstantPoolPrices(poolPrices!, amounts, dexKey);
|
|
58
|
+
} else {
|
|
59
|
+
checkPoolPrices(poolPrices!, amounts, side, dexKey);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
describe('Swaap V2', function () {
|
|
64
|
+
const dexKey = 'SwaapV2';
|
|
65
|
+
let blockNumber: number;
|
|
66
|
+
let swaapV2: SwaapV2;
|
|
67
|
+
|
|
68
|
+
describe('Polygon', () => {
|
|
69
|
+
const network = Network.POLYGON;
|
|
70
|
+
const dexHelper = new DummyDexHelper(network);
|
|
71
|
+
|
|
72
|
+
const tokens = Tokens[network];
|
|
73
|
+
|
|
74
|
+
const srcTokenSymbol = 'WETH';
|
|
75
|
+
const destTokenSymbol = 'USDC';
|
|
76
|
+
|
|
77
|
+
const amountsForSell = [
|
|
78
|
+
0n,
|
|
79
|
+
1n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
80
|
+
2n * BI_POWS[tokens[srcTokenSymbol].decimals],
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
const amountsForBuy = [
|
|
84
|
+
0n,
|
|
85
|
+
10n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
86
|
+
20n * BI_POWS[tokens[destTokenSymbol].decimals],
|
|
87
|
+
];
|
|
88
|
+
|
|
89
|
+
beforeAll(async () => {
|
|
90
|
+
swaapV2 = new SwaapV2(network, dexKey, dexHelper);
|
|
91
|
+
if (swaapV2.initializePricing) {
|
|
92
|
+
await swaapV2.initializePricing(0);
|
|
93
|
+
await sleep(5000);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('getPoolIdentifiers and getPricesVolume SELL', async function () {
|
|
98
|
+
await testPricingOnNetwork(
|
|
99
|
+
swaapV2,
|
|
100
|
+
network,
|
|
101
|
+
dexKey,
|
|
102
|
+
blockNumber,
|
|
103
|
+
srcTokenSymbol,
|
|
104
|
+
destTokenSymbol,
|
|
105
|
+
SwapSide.SELL,
|
|
106
|
+
amountsForSell,
|
|
107
|
+
);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('getPoolIdentifiers and getPricesVolume BUY', async function () {
|
|
111
|
+
await testPricingOnNetwork(
|
|
112
|
+
swaapV2,
|
|
113
|
+
network,
|
|
114
|
+
dexKey,
|
|
115
|
+
blockNumber,
|
|
116
|
+
srcTokenSymbol,
|
|
117
|
+
destTokenSymbol,
|
|
118
|
+
SwapSide.BUY,
|
|
119
|
+
amountsForBuy,
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('getTopPoolsForToken', async function () {
|
|
124
|
+
// We have to check without calling initializePricing, because
|
|
125
|
+
// pool-tracker is not calling that function
|
|
126
|
+
const blocknumber = await dexHelper.web3Provider.eth.getBlockNumber();
|
|
127
|
+
|
|
128
|
+
const swaapV2 = new SwaapV2(network, dexKey, dexHelper);
|
|
129
|
+
await swaapV2.initializePricing(blocknumber);
|
|
130
|
+
const poolLiquidity = await swaapV2.getTopPoolsForToken(
|
|
131
|
+
tokens[srcTokenSymbol].address,
|
|
132
|
+
10,
|
|
133
|
+
);
|
|
134
|
+
console.log(`${srcTokenSymbol} Top Pools:`, poolLiquidity);
|
|
135
|
+
|
|
136
|
+
if (!swaapV2.hasConstantPriceLargeAmounts) {
|
|
137
|
+
checkPoolsLiquidity(
|
|
138
|
+
poolLiquidity,
|
|
139
|
+
Tokens[network][srcTokenSymbol].address,
|
|
140
|
+
dexKey,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|