@paraswap/dex-lib 4.0.3 → 4.0.4
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/prettier.xml +7 -0
- package/.idea/vcs.xml +6 -0
- package/build/abi/integral/factory.json +333 -0
- package/build/abi/integral/oracle.json +501 -0
- package/build/abi/{inception/inception-ineth-pool.json → integral/pool.json} +522 -543
- package/build/abi/{wombat/pool.json → integral/relayer.json} +720 -473
- package/build/dex/aave-v1/aave-v1.d.ts +1 -2
- package/build/dex/aave-v1/aave-v1.js +0 -23
- package/build/dex/aave-v1/aave-v1.js.map +1 -1
- package/build/dex/bebop/bebop.js +1 -1
- package/build/dex/bebop/bebop.js.map +1 -1
- package/build/dex/cables/cables.d.ts +4 -2
- package/build/dex/cables/cables.js +90 -155
- package/build/dex/cables/cables.js.map +1 -1
- package/build/dex/cables/constants.d.ts +0 -1
- package/build/dex/cables/constants.js +2 -3
- package/build/dex/cables/constants.js.map +1 -1
- package/build/dex/cables/rate-fetcher.js +26 -10
- package/build/dex/cables/rate-fetcher.js.map +1 -1
- package/build/dex/cables/types.d.ts +15 -28
- package/build/dex/cables/types.js +2 -13
- package/build/dex/cables/types.js.map +1 -1
- package/build/dex/dexalot/dexalot.js.map +1 -1
- package/build/dex/fluid-dex/fluid-dex-generate-pool.d.ts +49 -0
- package/build/dex/fluid-dex/fluid-dex-generate-pool.js +104 -0
- package/build/dex/fluid-dex/fluid-dex-generate-pool.js.map +1 -0
- package/build/dex/index.js +2 -0
- package/build/dex/index.js.map +1 -1
- package/build/dex/integral/config.d.ts +4 -0
- package/build/dex/integral/config.js +20 -0
- package/build/dex/integral/config.js.map +1 -0
- package/build/dex/integral/context.d.ts +40 -0
- package/build/dex/integral/context.js +158 -0
- package/build/dex/integral/context.js.map +1 -0
- package/build/dex/integral/integral-factory.d.ts +24 -0
- package/build/dex/integral/integral-factory.js +95 -0
- package/build/dex/integral/integral-factory.js.map +1 -0
- package/build/dex/integral/integral-pool.d.ts +50 -0
- package/build/dex/integral/integral-pool.js +149 -0
- package/build/dex/integral/integral-pool.js.map +1 -0
- package/build/dex/integral/integral-pricing.d.ts +18 -0
- package/build/dex/integral/integral-pricing.js +114 -0
- package/build/dex/integral/integral-pricing.js.map +1 -0
- package/build/dex/integral/integral-relayer.d.ts +42 -0
- package/build/dex/integral/integral-relayer.js +192 -0
- package/build/dex/integral/integral-relayer.js.map +1 -0
- package/build/dex/integral/integral-token.d.ts +27 -0
- package/build/dex/integral/integral-token.js +59 -0
- package/build/dex/integral/integral-token.js.map +1 -0
- package/build/dex/integral/integral.d.ts +41 -0
- package/build/dex/integral/integral.js +376 -0
- package/build/dex/integral/integral.js.map +1 -0
- package/build/dex/integral/types.d.ts +85 -0
- package/build/dex/integral/types.js +9 -0
- package/build/dex/integral/types.js.map +1 -0
- package/build/dex/integral/utils-e2e.d.ts +9 -0
- package/build/dex/integral/utils-e2e.js +131 -0
- package/build/dex/integral/utils-e2e.js.map +1 -0
- package/build/dex/integral/utils.d.ts +17 -0
- package/build/dex/integral/utils.js +94 -0
- package/build/dex/integral/utils.js.map +1 -0
- package/build/dex/jarvis.d.ts +1 -2
- package/build/dex/jarvis.js +0 -58
- package/build/dex/jarvis.js.map +1 -1
- package/build/dex/platypus/platypus.d.ts +1 -2
- package/build/dex/platypus/platypus.js +0 -45
- package/build/dex/platypus/platypus.js.map +1 -1
- package/build/dex/stable-pool.js +8 -1
- package/build/dex/stable-pool.js.map +1 -1
- package/build/dex/swaap-v1/swaap-v1.d.ts +1 -2
- package/build/dex/swaap-v1/swaap-v1.js +0 -14
- package/build/dex/swaap-v1/swaap-v1.js.map +1 -1
- package/build/dex/trader-joe-v2.1/optimizer.d.ts +2 -0
- package/build/dex/trader-joe-v2.1/optimizer.js +47 -0
- package/build/dex/trader-joe-v2.1/optimizer.js.map +1 -0
- package/build/dex/zerox/index.d.ts +26 -4
- package/build/dex/zerox/index.js +42 -50
- package/build/dex/zerox/index.js.map +1 -1
- package/build/dex/zerox/types.d.ts +0 -29
- package/build/dex/zerox/types.js +1 -8
- package/build/dex/zerox/types.js.map +1 -1
- package/build/executor/Executor01BytecodeBuilder.js +11 -6
- package/build/executor/Executor01BytecodeBuilder.js.map +1 -1
- package/build/executor/Executor02BytecodeBuilder.js +11 -6
- package/build/executor/Executor02BytecodeBuilder.js.map +1 -1
- package/build/executor/Executor03BytecodeBuilder.js +11 -6
- package/build/executor/Executor03BytecodeBuilder.js.map +1 -1
- package/build/types.d.ts +1 -0
- package/package.json +1 -1
- package/src/abi/cables/CablesMainnetRFQ.json +1083 -0
- package/src/dex/bebop/bebop.ts +1 -1
- package/src/dex/cables/cables-e2e.test.ts +207 -0
- package/src/dex/cables/cables-integration.test.ts +391 -0
- package/src/dex/cables/cables.ts +823 -0
- package/src/dex/cables/config.ts +13 -0
- package/src/dex/cables/constants.ts +33 -0
- package/src/dex/cables/rate-fetcher.ts +212 -0
- package/src/dex/cables/types.ts +113 -0
- package/src/dex/cables/validators.test.ts +151 -0
- package/src/dex/cables/validators.ts +61 -0
- package/src/dex/dexalot/dexalot.ts +1 -0
- package/src/dex/index.ts +2 -0
- package/src/executor/Executor01BytecodeBuilder.ts +15 -11
- package/src/executor/Executor02BytecodeBuilder.ts +13 -9
- package/src/executor/Executor03BytecodeBuilder.ts +16 -9
- package/src/types.ts +1 -0
- package/tests/constants-e2e.ts +2 -2
- package/.vscode/launch.json +0 -53
- package/.vscode/settings.json +0 -2
- package/.vscode/tasks.json +0 -15
- package/build/abi/inception/inception-ratio-feed.json +0 -329
- package/build/abi/inception/inception-vault.json +0 -991
- package/build/abi/uniswap-v3/AlienBaseV3Router.abi.json +0 -52
- package/build/abi/usual-m-smart-m/usualM.abi.json +0 -483
- package/build/abi/usual-m-usd0/usualCollateralDao.abi.json +0 -681
- package/build/abi/usual-m-wrapped-m/usualM.abi.json +0 -509
- package/build/dex/aave-v2/tokens-avalanche.json +0 -44
- package/build/dex/aave-v2/tokens-mainnet.json +0 -188
- package/build/dex/aave-v2/tokens-polygon.json +0 -44
- package/build/dex/balancer-v3/abi/balancerBatchRouter.d.ts +0 -729
- package/build/dex/balancer-v3/abi/balancerBatchRouter.js +0 -732
- package/build/dex/balancer-v3/abi/balancerBatchRouter.js.map +0 -1
- package/build/dex/balancer-v3/abi/balancerRouter.d.ts +0 -1406
- package/build/dex/balancer-v3/abi/balancerRouter.js +0 -1175
- package/build/dex/balancer-v3/abi/balancerRouter.js.map +0 -1
- package/build/dex/balancer-v3/abi/vaultExtension.V3.d.ts +0 -2081
- package/build/dex/balancer-v3/abi/vaultExtension.V3.js +0 -1851
- package/build/dex/balancer-v3/abi/vaultExtension.V3.js.map +0 -1
- package/build/dex/balancer-v3/balancer-v3-sepolia-pools.d.ts +0 -34
- package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js +0 -406
- package/build/dex/balancer-v3/balancer-v3-sepolia-pools.js.map +0 -1
- package/build/dex/inception/config.d.ts +0 -4
- package/build/dex/inception/config.js +0 -109
- package/build/dex/inception/config.js.map +0 -1
- package/build/dex/inception/inception-event-pool.d.ts +0 -16
- package/build/dex/inception/inception-event-pool.js +0 -49
- package/build/dex/inception/inception-event-pool.js.map +0 -1
- package/build/dex/inception/inception.d.ts +0 -43
- package/build/dex/inception/inception.js +0 -180
- package/build/dex/inception/inception.js.map +0 -1
- package/build/dex/inception/tokens.d.ts +0 -9
- package/build/dex/inception/tokens.js +0 -28
- package/build/dex/inception/tokens.js.map +0 -1
- package/build/dex/inception/types.d.ts +0 -22
- package/build/dex/inception/types.js +0 -3
- package/build/dex/inception/types.js.map +0 -1
- package/build/dex/inception/utils.d.ts +0 -7
- package/build/dex/inception/utils.js +0 -58
- package/build/dex/inception/utils.js.map +0 -1
- package/build/dex/maker-psm/scripts/gem.abi.json +0 -136
- package/build/dex/maker-psm/scripts/validate-state.d.ts +0 -0
- package/build/dex/maker-psm/scripts/validate-state.js +0 -185
- package/build/dex/maker-psm/scripts/validate-state.js.map +0 -1
- package/build/dex/maker-psm/scripts/vat.abi.json +0 -243
- package/build/dex/solidly-v3/scripts/check-event-pool-event.d.ts +0 -1
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js +0 -53
- package/build/dex/solidly-v3/scripts/check-event-pool-event.js.map +0 -1
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.d.ts +0 -17
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js +0 -64
- package/build/dex/uniswap-v3/forks/alien-base-v3/alien-base-v3.js.map +0 -1
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.d.ts +0 -17
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js +0 -79
- package/build/dex/uniswap-v3/forks/alien-v3/alien-v3.js.map +0 -1
- package/build/dex/uniswap-v3/uniswap-v3-pricing.d.ts +0 -4
- package/build/dex/uniswap-v3/uniswap-v3-pricing.js +0 -43
- package/build/dex/uniswap-v3/uniswap-v3-pricing.js.map +0 -1
- package/build/dex/uniswap-v3/uniswap-v3-worker.d.ts +0 -1
- package/build/dex/uniswap-v3/uniswap-v3-worker.js +0 -10
- package/build/dex/uniswap-v3/uniswap-v3-worker.js.map +0 -1
- package/build/dex/usual/types.d.ts +0 -13
- package/build/dex/usual/types.js +0 -3
- package/build/dex/usual/types.js.map +0 -1
- package/build/dex/usual/usual-bond.d.ts +0 -18
- package/build/dex/usual/usual-bond.js +0 -52
- package/build/dex/usual/usual-bond.js.map +0 -1
- package/build/dex/usual/usual-m-smart-m.d.ts +0 -17
- package/build/dex/usual/usual-m-smart-m.js +0 -50
- package/build/dex/usual/usual-m-smart-m.js.map +0 -1
- package/build/dex/usual/usual-m-usd0.d.ts +0 -17
- package/build/dex/usual/usual-m-usd0.js +0 -51
- package/build/dex/usual/usual-m-usd0.js.map +0 -1
- package/build/dex/usual/usual-m-wrapped-m.d.ts +0 -17
- package/build/dex/usual/usual-m-wrapped-m.js +0 -50
- package/build/dex/usual/usual-m-wrapped-m.js.map +0 -1
- package/build/dex/usual/usual.d.ts +0 -27
- package/build/dex/usual/usual.js +0 -125
- package/build/dex/usual/usual.js.map +0 -1
- package/build/dex/usual-m-smart-m/config.d.ts +0 -3
- package/build/dex/usual-m-smart-m/config.js +0 -13
- package/build/dex/usual-m-smart-m/config.js.map +0 -1
- package/build/dex/usual-m-smart-m/types.d.ts +0 -7
- package/build/dex/usual-m-smart-m/types.js +0 -3
- package/build/dex/usual-m-smart-m/types.js.map +0 -1
- package/build/dex/usual-m-smart-m/usual-m-smart-m.d.ts +0 -35
- package/build/dex/usual-m-smart-m/usual-m-smart-m.js +0 -161
- package/build/dex/usual-m-smart-m/usual-m-smart-m.js.map +0 -1
- package/build/dex/usual-m-usd0/config.d.ts +0 -3
- package/build/dex/usual-m-usd0/config.js +0 -14
- package/build/dex/usual-m-usd0/config.js.map +0 -1
- package/build/dex/usual-m-usd0/types.d.ts +0 -8
- package/build/dex/usual-m-usd0/types.js +0 -3
- package/build/dex/usual-m-usd0/types.js.map +0 -1
- package/build/dex/usual-m-usd0/usual-m-usd0.d.ts +0 -35
- package/build/dex/usual-m-usd0/usual-m-usd0.js +0 -159
- package/build/dex/usual-m-usd0/usual-m-usd0.js.map +0 -1
- package/build/lib/pub-sub.d.ts +0 -35
- package/build/lib/pub-sub.js +0 -106
- package/build/lib/pub-sub.js.map +0 -1
|
@@ -0,0 +1,823 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Address,
|
|
3
|
+
NumberAsString,
|
|
4
|
+
OptimalSwapExchange,
|
|
5
|
+
SwapSide,
|
|
6
|
+
} from '@paraswap/core';
|
|
7
|
+
import { assert, AsyncOrSync } from 'ts-essentials';
|
|
8
|
+
import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
|
|
9
|
+
import { ETHER_ADDRESS, Network, NULL_ADDRESS } from '../../constants';
|
|
10
|
+
import { IDexHelper } from '../../dex-helper';
|
|
11
|
+
import {
|
|
12
|
+
AdapterExchangeParam,
|
|
13
|
+
DexExchangeParam,
|
|
14
|
+
ExchangePrices,
|
|
15
|
+
ExchangeTxInfo,
|
|
16
|
+
Logger,
|
|
17
|
+
PoolLiquidity,
|
|
18
|
+
PoolPrices,
|
|
19
|
+
PreprocessTransactionOptions,
|
|
20
|
+
Token,
|
|
21
|
+
TransferFeeParams,
|
|
22
|
+
} from '../../types';
|
|
23
|
+
import { getDexKeysWithNetwork, Utils } from '../../utils';
|
|
24
|
+
import { IDex } from '../idex';
|
|
25
|
+
import { SimpleExchange } from '../simple-exchange';
|
|
26
|
+
import { CablesConfig } from './config';
|
|
27
|
+
import {
|
|
28
|
+
CABLES_API_BLACKLIST_POLLING_INTERVAL_MS,
|
|
29
|
+
CABLES_API_PAIRS_POLLING_INTERVAL_MS,
|
|
30
|
+
CABLES_API_PRICES_POLLING_INTERVAL_MS,
|
|
31
|
+
CABLES_API_TOKENS_POLLING_INTERVAL_MS,
|
|
32
|
+
CABLES_API_URL,
|
|
33
|
+
CABLES_BLACKLIST_CACHES_TTL_S,
|
|
34
|
+
CABLES_ERRORS_CACHE_KEY,
|
|
35
|
+
CABLES_FIRM_QUOTE_TIMEOUT_MS,
|
|
36
|
+
CABLES_GAS_COST,
|
|
37
|
+
CABLES_PAIRS_CACHES_TTL_S,
|
|
38
|
+
CABLES_PRICES_CACHES_TTL_S,
|
|
39
|
+
CABLES_RESTRICT_CHECK_INTERVAL_MS,
|
|
40
|
+
CABLES_RESTRICT_COUNT_THRESHOLD,
|
|
41
|
+
CABLES_RESTRICT_TTL_S,
|
|
42
|
+
CABLES_RESTRICTED_CACHE_KEY,
|
|
43
|
+
CABLES_TOKENS_CACHES_TTL_S,
|
|
44
|
+
} from './constants';
|
|
45
|
+
import { CablesRateFetcher } from './rate-fetcher';
|
|
46
|
+
import {
|
|
47
|
+
CablesData,
|
|
48
|
+
CablesRFQResponse,
|
|
49
|
+
RestrictData,
|
|
50
|
+
SlippageError,
|
|
51
|
+
} from './types';
|
|
52
|
+
import mainnetRFQAbi from '../../abi/cables/CablesMainnetRFQ.json';
|
|
53
|
+
import { Interface } from 'ethers/lib/utils';
|
|
54
|
+
import BigNumber from 'bignumber.js';
|
|
55
|
+
import { ethers } from 'ethers';
|
|
56
|
+
import { BI_MAX_UINT256 } from '../../bigint-constants';
|
|
57
|
+
import _ from 'lodash';
|
|
58
|
+
import { BebopData } from '../bebop/types';
|
|
59
|
+
|
|
60
|
+
export class Cables extends SimpleExchange implements IDex<any> {
|
|
61
|
+
public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
|
|
62
|
+
getDexKeysWithNetwork(CablesConfig);
|
|
63
|
+
|
|
64
|
+
readonly isStatePollingDex = true;
|
|
65
|
+
|
|
66
|
+
private rateFetcher: CablesRateFetcher;
|
|
67
|
+
|
|
68
|
+
logger: Logger;
|
|
69
|
+
private tokensMap: { [address: string]: Token } = {};
|
|
70
|
+
|
|
71
|
+
hasConstantPriceLargeAmounts: boolean = false;
|
|
72
|
+
|
|
73
|
+
constructor(
|
|
74
|
+
readonly network: Network,
|
|
75
|
+
readonly dexKey: string,
|
|
76
|
+
readonly dexHelper: IDexHelper,
|
|
77
|
+
readonly mainnetRFQAddress: string = CablesConfig['Cables'][network]
|
|
78
|
+
.mainnetRFQAddress,
|
|
79
|
+
protected rfqInterface = new Interface(mainnetRFQAbi),
|
|
80
|
+
) {
|
|
81
|
+
super(dexHelper, dexKey);
|
|
82
|
+
this.logger = dexHelper.getLogger(dexKey);
|
|
83
|
+
|
|
84
|
+
this.rateFetcher = new CablesRateFetcher(
|
|
85
|
+
this.dexHelper,
|
|
86
|
+
this.dexKey,
|
|
87
|
+
this.network,
|
|
88
|
+
this.logger,
|
|
89
|
+
{
|
|
90
|
+
rateConfig: {
|
|
91
|
+
pairsReqParams: {
|
|
92
|
+
url: CABLES_API_URL + '/pairs',
|
|
93
|
+
},
|
|
94
|
+
pricesReqParams: {
|
|
95
|
+
url: CABLES_API_URL + '/prices',
|
|
96
|
+
},
|
|
97
|
+
blacklistReqParams: {
|
|
98
|
+
url: CABLES_API_URL + '/blacklist',
|
|
99
|
+
},
|
|
100
|
+
tokensReqParams: {
|
|
101
|
+
url: CABLES_API_URL + '/tokens',
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
pricesIntervalMs: CABLES_API_PRICES_POLLING_INTERVAL_MS,
|
|
105
|
+
pricesCacheTTLSecs: CABLES_PRICES_CACHES_TTL_S,
|
|
106
|
+
pricesCacheKey: 'prices',
|
|
107
|
+
|
|
108
|
+
pairsIntervalMs: CABLES_API_PAIRS_POLLING_INTERVAL_MS,
|
|
109
|
+
pairsCacheTTLSecs: CABLES_PAIRS_CACHES_TTL_S,
|
|
110
|
+
pairsCacheKey: 'pairs',
|
|
111
|
+
|
|
112
|
+
tokensIntervalMs: CABLES_API_TOKENS_POLLING_INTERVAL_MS,
|
|
113
|
+
tokensCacheTTLSecs: CABLES_TOKENS_CACHES_TTL_S,
|
|
114
|
+
tokensCacheKey: 'tokens',
|
|
115
|
+
|
|
116
|
+
blacklistIntervalMs: CABLES_API_BLACKLIST_POLLING_INTERVAL_MS,
|
|
117
|
+
blacklistCacheTTLSecs: CABLES_BLACKLIST_CACHES_TTL_S,
|
|
118
|
+
blacklistCacheKey: 'blacklist',
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async preProcessTransaction?(
|
|
125
|
+
optimalSwapExchange: OptimalSwapExchange<CablesData>,
|
|
126
|
+
srcToken: Token,
|
|
127
|
+
destToken: Token,
|
|
128
|
+
side: SwapSide,
|
|
129
|
+
options: PreprocessTransactionOptions,
|
|
130
|
+
): Promise<[OptimalSwapExchange<CablesData>, ExchangeTxInfo]> {
|
|
131
|
+
if (await this.isBlacklisted(options.txOrigin)) {
|
|
132
|
+
this.logger.warn(
|
|
133
|
+
`${this.dexKey}-${this.network}: blacklisted TX Origin address '${options.txOrigin}' trying to build a transaction. Bailing...`,
|
|
134
|
+
);
|
|
135
|
+
throw new Error(
|
|
136
|
+
`${this.dexKey}-${
|
|
137
|
+
this.network
|
|
138
|
+
}: user=${options.txOrigin.toLowerCase()} is blacklisted`,
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (BigInt(optimalSwapExchange.srcAmount) === 0n) {
|
|
143
|
+
throw new Error('getFirmRate failed with srcAmount === 0');
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const normalizedSrcToken = this.normalizeToken(srcToken);
|
|
147
|
+
const normalizedDestToken = this.normalizeToken(destToken);
|
|
148
|
+
const swapIdentifier = `${this.dexKey}_${normalizedSrcToken.address}_${normalizedDestToken.address}_${side}`;
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
let makerToken = normalizedDestToken;
|
|
152
|
+
let takerToken = normalizedSrcToken;
|
|
153
|
+
|
|
154
|
+
const isSell = side === SwapSide.SELL;
|
|
155
|
+
const isBuy = side === SwapSide.BUY;
|
|
156
|
+
|
|
157
|
+
const rfqParams = {
|
|
158
|
+
makerAsset: ethers.utils.getAddress(makerToken.address),
|
|
159
|
+
takerAsset: ethers.utils.getAddress(takerToken.address),
|
|
160
|
+
...(isBuy && { makerAmount: optimalSwapExchange.destAmount }),
|
|
161
|
+
...(isSell && { takerAmount: optimalSwapExchange.srcAmount }),
|
|
162
|
+
userAddress: options.executionContractAddress,
|
|
163
|
+
chainId: String(this.network),
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
const rfq: CablesRFQResponse = await this.dexHelper.httpRequest.post(
|
|
167
|
+
`${CABLES_API_URL}/quote`,
|
|
168
|
+
rfqParams,
|
|
169
|
+
CABLES_FIRM_QUOTE_TIMEOUT_MS,
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
if (!rfq) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
'Failed to fetch RFQ' +
|
|
175
|
+
swapIdentifier +
|
|
176
|
+
JSON.stringify(rfq + 'params' + rfqParams),
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const { order } = rfq;
|
|
181
|
+
|
|
182
|
+
assert(
|
|
183
|
+
order.makerAsset.toLowerCase() === makerToken.address,
|
|
184
|
+
`QuoteData makerAsset=${order.makerAsset} is different from Paraswap makerAsset=${makerToken.address}`,
|
|
185
|
+
);
|
|
186
|
+
assert(
|
|
187
|
+
order.takerAsset.toLowerCase() === takerToken.address,
|
|
188
|
+
`QuoteData takerAsset=${order.takerAsset} is different from Paraswap takerAsset=${takerToken.address}`,
|
|
189
|
+
);
|
|
190
|
+
if (isSell) {
|
|
191
|
+
assert(
|
|
192
|
+
order.takerAmount === optimalSwapExchange.srcAmount,
|
|
193
|
+
`QuoteData takerAmount=${order.takerAmount} is different from Paraswap srcAmount=${optimalSwapExchange.srcAmount}`,
|
|
194
|
+
);
|
|
195
|
+
} else {
|
|
196
|
+
assert(
|
|
197
|
+
order.makerAmount === optimalSwapExchange.destAmount,
|
|
198
|
+
`QuoteData makerAmount=${order.makerAmount} is different from Paraswap destAmount=${optimalSwapExchange.destAmount}`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const expiryAsBigInt = BigInt(order.expiry);
|
|
203
|
+
const minDeadline = expiryAsBigInt > 0 ? expiryAsBigInt : BI_MAX_UINT256;
|
|
204
|
+
|
|
205
|
+
if (side === SwapSide.BUY) {
|
|
206
|
+
const requiredAmount = BigInt(optimalSwapExchange.srcAmount);
|
|
207
|
+
const quoteAmount = BigInt(order.takerAmount);
|
|
208
|
+
const requiredAmountWithSlippage = new BigNumber(
|
|
209
|
+
requiredAmount.toString(),
|
|
210
|
+
)
|
|
211
|
+
.multipliedBy(options.slippageFactor)
|
|
212
|
+
.toFixed(0);
|
|
213
|
+
if (quoteAmount > BigInt(requiredAmountWithSlippage)) {
|
|
214
|
+
throw new SlippageError(
|
|
215
|
+
`Slipped, factor: ${quoteAmount.toString()} > ${requiredAmountWithSlippage}`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
} else {
|
|
219
|
+
const requiredAmount = BigInt(optimalSwapExchange.destAmount);
|
|
220
|
+
const quoteAmount = BigInt(order.makerAmount);
|
|
221
|
+
const requiredAmountWithSlippage = new BigNumber(
|
|
222
|
+
requiredAmount.toString(),
|
|
223
|
+
)
|
|
224
|
+
.multipliedBy(options.slippageFactor)
|
|
225
|
+
.toFixed(0);
|
|
226
|
+
if (quoteAmount < BigInt(requiredAmountWithSlippage)) {
|
|
227
|
+
throw new SlippageError(
|
|
228
|
+
`Slipped, factor: ${
|
|
229
|
+
options.slippageFactor
|
|
230
|
+
} ${quoteAmount.toString()} < ${requiredAmountWithSlippage}`,
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return [
|
|
236
|
+
{
|
|
237
|
+
...optimalSwapExchange,
|
|
238
|
+
data: {
|
|
239
|
+
quoteData: order,
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
{ deadline: minDeadline },
|
|
243
|
+
];
|
|
244
|
+
} catch (e: any) {
|
|
245
|
+
const message = `${this.dexKey}-${this.network}: ${e}`;
|
|
246
|
+
this.logger.error(message);
|
|
247
|
+
if (!e?.isSlippageError) {
|
|
248
|
+
this.restrict();
|
|
249
|
+
}
|
|
250
|
+
throw new Error(message);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
getAdapterParam(
|
|
255
|
+
srcToken: string,
|
|
256
|
+
destToken: string,
|
|
257
|
+
srcAmount: string,
|
|
258
|
+
destAmount: string,
|
|
259
|
+
data: BebopData,
|
|
260
|
+
side: SwapSide,
|
|
261
|
+
): AdapterExchangeParam {
|
|
262
|
+
return {
|
|
263
|
+
targetExchange: this.mainnetRFQAddress,
|
|
264
|
+
payload: '0x',
|
|
265
|
+
networkFee: '0',
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
getDexParam(
|
|
270
|
+
srcToken: Address,
|
|
271
|
+
destToken: Address,
|
|
272
|
+
srcAmount: NumberAsString,
|
|
273
|
+
destAmount: NumberAsString,
|
|
274
|
+
recipient: Address,
|
|
275
|
+
data: CablesData,
|
|
276
|
+
side: SwapSide,
|
|
277
|
+
): DexExchangeParam {
|
|
278
|
+
const { quoteData } = data;
|
|
279
|
+
|
|
280
|
+
assert(
|
|
281
|
+
quoteData !== undefined,
|
|
282
|
+
`${this.dexKey}-${this.network}: quoteData undefined`,
|
|
283
|
+
);
|
|
284
|
+
|
|
285
|
+
const swapFunction = 'partialSwap';
|
|
286
|
+
const swapFunctionParams = [
|
|
287
|
+
[
|
|
288
|
+
quoteData.nonceAndMeta,
|
|
289
|
+
quoteData.expiry,
|
|
290
|
+
quoteData.makerAsset,
|
|
291
|
+
quoteData.takerAsset,
|
|
292
|
+
quoteData.maker,
|
|
293
|
+
quoteData.taker,
|
|
294
|
+
quoteData.makerAmount,
|
|
295
|
+
quoteData.takerAmount,
|
|
296
|
+
],
|
|
297
|
+
quoteData.signature,
|
|
298
|
+
// might be overwritten on Executors
|
|
299
|
+
quoteData.takerAmount,
|
|
300
|
+
];
|
|
301
|
+
|
|
302
|
+
const exchangeData = this.rfqInterface.encodeFunctionData(
|
|
303
|
+
swapFunction,
|
|
304
|
+
swapFunctionParams,
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const fromAmount = ethers.utils.defaultAbiCoder.encode(
|
|
308
|
+
['uint256'],
|
|
309
|
+
[quoteData.takerAmount],
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
const filledAmountIndex = exchangeData
|
|
313
|
+
.replace('0x', '')
|
|
314
|
+
.lastIndexOf(fromAmount.replace('0x', ''));
|
|
315
|
+
|
|
316
|
+
const filledAmountPos =
|
|
317
|
+
(filledAmountIndex !== -1 ? filledAmountIndex : exchangeData.length) / 2;
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
exchangeData,
|
|
321
|
+
needWrapNative: this.needWrapNative,
|
|
322
|
+
dexFuncHasRecipient: false,
|
|
323
|
+
targetExchange: this.mainnetRFQAddress,
|
|
324
|
+
returnAmountPos: undefined,
|
|
325
|
+
insertFromAmountPos: filledAmountPos,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
normalizeToken(token: Token): Token {
|
|
330
|
+
return {
|
|
331
|
+
...token,
|
|
332
|
+
address: this.normalizeTokenAddress(token.address),
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
normalizeTokenAddress(address: Address): Address {
|
|
337
|
+
return address.toLowerCase();
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
getTokenFromAddress(address: Address): Token {
|
|
341
|
+
return this.tokensMap[this.normalizeAddress(address)];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
getPoolIdentifier(srcAddress: Address, destAddress: Address) {
|
|
345
|
+
return `${this.dexKey}_${srcAddress}_${destAddress}`.toLowerCase();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
async getPoolIdentifiers(
|
|
349
|
+
srcToken: Token,
|
|
350
|
+
destToken: Token,
|
|
351
|
+
side: SwapSide,
|
|
352
|
+
blockNumber: number,
|
|
353
|
+
): Promise<string[]> {
|
|
354
|
+
if (!srcToken || !destToken) {
|
|
355
|
+
return [];
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (srcToken.address.toLowerCase() === destToken.address.toLowerCase()) {
|
|
359
|
+
return [];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const normalizedSrcToken = this.normalizeToken(srcToken);
|
|
363
|
+
const normalizedDestToken = this.normalizeToken(destToken);
|
|
364
|
+
|
|
365
|
+
const pairData = await this.getPairData(
|
|
366
|
+
normalizedSrcToken,
|
|
367
|
+
normalizedDestToken,
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
if (!pairData) {
|
|
371
|
+
return [];
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return [
|
|
375
|
+
this.getPoolIdentifier(
|
|
376
|
+
normalizedSrcToken.address,
|
|
377
|
+
normalizedDestToken.address,
|
|
378
|
+
),
|
|
379
|
+
];
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
calculateOrderPrice(
|
|
383
|
+
amounts: bigint[],
|
|
384
|
+
orderbook: string[][],
|
|
385
|
+
baseToken: Token,
|
|
386
|
+
quoteToken: Token,
|
|
387
|
+
isInputQuote: boolean,
|
|
388
|
+
) {
|
|
389
|
+
let result = [];
|
|
390
|
+
|
|
391
|
+
for (let i = 0; i < amounts.length; i++) {
|
|
392
|
+
let amt = amounts[i];
|
|
393
|
+
if (amt === 0n) {
|
|
394
|
+
result.push(amt);
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
let decimals = baseToken.decimals;
|
|
399
|
+
let out_decimals = quoteToken.decimals;
|
|
400
|
+
|
|
401
|
+
let price = this.calculatePriceSwap(
|
|
402
|
+
orderbook,
|
|
403
|
+
Number(amt) / 10 ** decimals,
|
|
404
|
+
isInputQuote,
|
|
405
|
+
);
|
|
406
|
+
result.push(BigInt(Math.round(price * 10 ** out_decimals)));
|
|
407
|
+
}
|
|
408
|
+
return result;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
calculatePriceSwap(
|
|
412
|
+
prices: string[][],
|
|
413
|
+
requiredQty: number,
|
|
414
|
+
qtyMode: Boolean,
|
|
415
|
+
) {
|
|
416
|
+
let sumBaseQty = 0;
|
|
417
|
+
let sumQuoteQty = 0;
|
|
418
|
+
const selectedRows: string[][] = [];
|
|
419
|
+
|
|
420
|
+
const isBase = qtyMode;
|
|
421
|
+
const isQuote = !qtyMode;
|
|
422
|
+
|
|
423
|
+
for (const [price, volume] of prices) {
|
|
424
|
+
if (isBase) {
|
|
425
|
+
if (sumBaseQty >= requiredQty) {
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (isQuote) {
|
|
431
|
+
if (sumQuoteQty >= requiredQty) {
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
let currentBaseQty = Number(volume);
|
|
437
|
+
let currentQuoteQty = Number(volume) * Number(price);
|
|
438
|
+
|
|
439
|
+
const overQty = isBase
|
|
440
|
+
? currentBaseQty + sumBaseQty > requiredQty
|
|
441
|
+
: currentQuoteQty + sumQuoteQty > requiredQty;
|
|
442
|
+
|
|
443
|
+
if (overQty) {
|
|
444
|
+
if (isBase) {
|
|
445
|
+
currentBaseQty = requiredQty - sumBaseQty;
|
|
446
|
+
currentQuoteQty = currentBaseQty * Number(price);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (isQuote) {
|
|
450
|
+
currentQuoteQty = requiredQty - sumQuoteQty;
|
|
451
|
+
currentBaseQty =
|
|
452
|
+
currentQuoteQty *
|
|
453
|
+
new BigNumber(1).dividedBy(new BigNumber(price)).toNumber();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
sumBaseQty += currentBaseQty;
|
|
458
|
+
sumQuoteQty += currentQuoteQty;
|
|
459
|
+
selectedRows.push([price, currentBaseQty.toString()]);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const vSumBase = selectedRows.reduce((sum: number, [price, volume]) => {
|
|
463
|
+
return sum + Number(price) * Number(volume);
|
|
464
|
+
}, 0);
|
|
465
|
+
|
|
466
|
+
const price = new BigNumber(vSumBase)
|
|
467
|
+
.dividedBy(new BigNumber(sumBaseQty))
|
|
468
|
+
.toNumber();
|
|
469
|
+
|
|
470
|
+
if (isBase) {
|
|
471
|
+
return requiredQty / price;
|
|
472
|
+
} else {
|
|
473
|
+
return requiredQty * price;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
async getPricesVolume(
|
|
478
|
+
srcToken: Token,
|
|
479
|
+
destToken: Token,
|
|
480
|
+
amounts: bigint[],
|
|
481
|
+
side: SwapSide,
|
|
482
|
+
blockNumber: number,
|
|
483
|
+
limitPools?: string[],
|
|
484
|
+
transferFees?: TransferFeeParams,
|
|
485
|
+
isFirstSwap?: boolean,
|
|
486
|
+
): Promise<ExchangePrices<CablesData> | null> {
|
|
487
|
+
try {
|
|
488
|
+
const normalizedSrcToken = this.normalizeToken(srcToken);
|
|
489
|
+
const normalizedDestToken = this.normalizeToken(destToken);
|
|
490
|
+
// If: same token, return null
|
|
491
|
+
if (
|
|
492
|
+
normalizedSrcToken.address.toLowerCase() ===
|
|
493
|
+
normalizedDestToken.address.toLowerCase()
|
|
494
|
+
) {
|
|
495
|
+
return null;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
const isRestricted = await this.isRestricted();
|
|
499
|
+
if (isRestricted) {
|
|
500
|
+
return null;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
await this.setTokensMap();
|
|
504
|
+
|
|
505
|
+
normalizedSrcToken.symbol =
|
|
506
|
+
this.tokensMap[normalizedSrcToken.address].symbol;
|
|
507
|
+
normalizedDestToken.symbol =
|
|
508
|
+
this.tokensMap[normalizedDestToken.address!].symbol;
|
|
509
|
+
|
|
510
|
+
// ---------- Pools ----------
|
|
511
|
+
let pools =
|
|
512
|
+
limitPools ||
|
|
513
|
+
(await this.getPoolIdentifiers(srcToken, destToken, side, blockNumber));
|
|
514
|
+
if (pools.length === 0) return null;
|
|
515
|
+
|
|
516
|
+
// ---------- Prices ----------
|
|
517
|
+
const priceMap = await this.getCachedPrices();
|
|
518
|
+
|
|
519
|
+
if (!priceMap) return null;
|
|
520
|
+
|
|
521
|
+
let isInputQuote = false;
|
|
522
|
+
let pairKey = `${normalizedSrcToken.symbol}/${normalizedDestToken.symbol}`;
|
|
523
|
+
const pairsKeys = Object.keys(priceMap);
|
|
524
|
+
|
|
525
|
+
if (!pairsKeys.includes(pairKey)) {
|
|
526
|
+
// Revert
|
|
527
|
+
isInputQuote = true;
|
|
528
|
+
pairKey = `${normalizedDestToken.symbol}/${normalizedSrcToken.symbol}`;
|
|
529
|
+
if (!pairsKeys.includes(pairKey)) {
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Orderbook
|
|
536
|
+
*/
|
|
537
|
+
const priceData = priceMap[pairKey];
|
|
538
|
+
|
|
539
|
+
let orderbook: any[] = [];
|
|
540
|
+
if (side === SwapSide.BUY) {
|
|
541
|
+
orderbook = priceData.asks;
|
|
542
|
+
} else {
|
|
543
|
+
orderbook = priceData.bids;
|
|
544
|
+
}
|
|
545
|
+
if (orderbook?.length === 0) {
|
|
546
|
+
throw new Error(`Empty orderbook for ${pairKey}`);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const prices = this.calculateOrderPrice(
|
|
550
|
+
amounts,
|
|
551
|
+
orderbook,
|
|
552
|
+
side === SwapSide.SELL ? srcToken : destToken,
|
|
553
|
+
side === SwapSide.SELL ? destToken : srcToken,
|
|
554
|
+
side === SwapSide.SELL ? isInputQuote : !isInputQuote,
|
|
555
|
+
);
|
|
556
|
+
|
|
557
|
+
const result = [
|
|
558
|
+
{
|
|
559
|
+
prices: prices,
|
|
560
|
+
unit: BigInt(normalizedDestToken.decimals),
|
|
561
|
+
exchange: this.dexKey,
|
|
562
|
+
gasCost: CABLES_GAS_COST,
|
|
563
|
+
poolAddresses: [this.mainnetRFQAddress],
|
|
564
|
+
data: {},
|
|
565
|
+
},
|
|
566
|
+
];
|
|
567
|
+
|
|
568
|
+
return result;
|
|
569
|
+
} catch (e: unknown) {
|
|
570
|
+
this.logger.error(
|
|
571
|
+
`Error in getPricesVolume`,
|
|
572
|
+
{
|
|
573
|
+
srcToken: srcToken.address || srcToken.symbol,
|
|
574
|
+
destToken: destToken.address || destToken.symbol,
|
|
575
|
+
side,
|
|
576
|
+
},
|
|
577
|
+
e,
|
|
578
|
+
);
|
|
579
|
+
return null;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
getCalldataGasCost(poolPrices: PoolPrices<CablesData>): number | number[] {
|
|
584
|
+
return (
|
|
585
|
+
CALLDATA_GAS_COST.DEX_OVERHEAD +
|
|
586
|
+
// addresses: makerAsset, takerAsset, maker, taker
|
|
587
|
+
CALLDATA_GAS_COST.ADDRESS * 4 +
|
|
588
|
+
// uint256: expiry
|
|
589
|
+
CALLDATA_GAS_COST.wordNonZeroBytes(16) +
|
|
590
|
+
// uint256: nonceAndMeta, makerAmount, takerAmount
|
|
591
|
+
CALLDATA_GAS_COST.AMOUNT * 3 +
|
|
592
|
+
// bytes: _signature (65 bytes)
|
|
593
|
+
CALLDATA_GAS_COST.FULL_WORD * 2 +
|
|
594
|
+
CALLDATA_GAS_COST.OFFSET_SMALL
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
async initializePricing(blockNumber: number): Promise<void> {
|
|
599
|
+
if (!this.dexHelper.config.isSlave) {
|
|
600
|
+
this.rateFetcher.start();
|
|
601
|
+
}
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
getAdapters(side: SwapSide): { name: string; index: number }[] | null {
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
releaseResources?(): AsyncOrSync<void> {
|
|
610
|
+
if (!this.dexHelper.config.isSlave && this.rateFetcher) {
|
|
611
|
+
this.rateFetcher.stop();
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
normalizeAddress(address: string): string {
|
|
616
|
+
return address.toLowerCase() === ETHER_ADDRESS
|
|
617
|
+
? NULL_ADDRESS
|
|
618
|
+
: address.toLowerCase();
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
async setTokensMap() {
|
|
622
|
+
const tokens = await this.getCachedTokens();
|
|
623
|
+
|
|
624
|
+
if (tokens) {
|
|
625
|
+
this.tokensMap = Object.keys(tokens).reduce((acc, key) => {
|
|
626
|
+
//@ts-ignore
|
|
627
|
+
acc[tokens[key].address.toLowerCase()] = tokens[key];
|
|
628
|
+
return acc;
|
|
629
|
+
}, {});
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
async getTopPoolsForToken(
|
|
634
|
+
tokenAddress: Address,
|
|
635
|
+
limit: number,
|
|
636
|
+
): Promise<PoolLiquidity[]> {
|
|
637
|
+
return [];
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* CACHED UTILS
|
|
642
|
+
*/
|
|
643
|
+
async getCachedTokens(): Promise<any> {
|
|
644
|
+
const cachedTokens = await this.dexHelper.cache.get(
|
|
645
|
+
this.dexKey,
|
|
646
|
+
this.network,
|
|
647
|
+
this.rateFetcher.tokensCacheKey,
|
|
648
|
+
);
|
|
649
|
+
|
|
650
|
+
return cachedTokens ? JSON.parse(cachedTokens) : {};
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
async getCachedPairs(): Promise<any> {
|
|
654
|
+
const cachedPairs = await this.dexHelper.cache.get(
|
|
655
|
+
this.dexKey,
|
|
656
|
+
this.network,
|
|
657
|
+
this.rateFetcher.pairsCacheKey,
|
|
658
|
+
);
|
|
659
|
+
|
|
660
|
+
return cachedPairs ? JSON.parse(cachedPairs) : {};
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
async getCachedPrices(): Promise<any> {
|
|
664
|
+
const cachedPrices = await this.dexHelper.cache.get(
|
|
665
|
+
this.dexKey,
|
|
666
|
+
this.network,
|
|
667
|
+
this.rateFetcher.pricesCacheKey,
|
|
668
|
+
);
|
|
669
|
+
|
|
670
|
+
return cachedPrices ? JSON.parse(cachedPrices) : {};
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
async getCachedTokensAddr(): Promise<any> {
|
|
674
|
+
const tokens = await this.getCachedTokens();
|
|
675
|
+
const tokensAddr: Record<string, Address> = {};
|
|
676
|
+
for (const key of Object.keys(tokens)) {
|
|
677
|
+
tokensAddr[tokens[key].symbol.toLowerCase()] = tokens[key].address;
|
|
678
|
+
}
|
|
679
|
+
return tokensAddr;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
getPairString(baseToken: Token, quoteToken: Token): string {
|
|
683
|
+
return `${baseToken.symbol}/${quoteToken.symbol}`.toLowerCase();
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// Function to find a key by address
|
|
687
|
+
private findKeyByAddress = (
|
|
688
|
+
jsonData: Record<string, { address: string }>,
|
|
689
|
+
targetAddress: string,
|
|
690
|
+
): string | undefined => {
|
|
691
|
+
const entries = Object.entries(jsonData);
|
|
692
|
+
const foundEntry = entries.find(
|
|
693
|
+
([_, value]) =>
|
|
694
|
+
value.address.toLowerCase() === targetAddress.toLowerCase(),
|
|
695
|
+
);
|
|
696
|
+
return foundEntry ? foundEntry[0] : undefined;
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
async getPairData(srcToken: Token, destToken: Token): Promise<any> {
|
|
700
|
+
if (srcToken.address === destToken.address) {
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const cachedTokens = await this.getCachedTokens();
|
|
705
|
+
|
|
706
|
+
srcToken.symbol = this.findKeyByAddress(cachedTokens, srcToken.address);
|
|
707
|
+
destToken.symbol = this.findKeyByAddress(cachedTokens, destToken.address);
|
|
708
|
+
|
|
709
|
+
const cachedPairs = await this.getCachedPairs();
|
|
710
|
+
|
|
711
|
+
const potentialPairs = [
|
|
712
|
+
{
|
|
713
|
+
base: srcToken.symbol,
|
|
714
|
+
quote: destToken.symbol,
|
|
715
|
+
identifier: this.getPairString(srcToken, destToken),
|
|
716
|
+
isSrcBase: true,
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
base: destToken.symbol,
|
|
720
|
+
quote: srcToken.symbol,
|
|
721
|
+
identifier: this.getPairString(destToken, srcToken),
|
|
722
|
+
isSrcBase: false,
|
|
723
|
+
},
|
|
724
|
+
];
|
|
725
|
+
|
|
726
|
+
for (const pair of potentialPairs) {
|
|
727
|
+
if (pair.identifier in cachedPairs) {
|
|
728
|
+
const pairData = cachedPairs[pair.identifier];
|
|
729
|
+
pairData.isSrcBase = pair.isSrcBase;
|
|
730
|
+
return pairData;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
return null;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
async isBlacklisted(txOrigin: Address): Promise<boolean> {
|
|
737
|
+
const cachedBlacklist = await this.dexHelper.cache.get(
|
|
738
|
+
this.dexKey,
|
|
739
|
+
this.network,
|
|
740
|
+
this.rateFetcher.blacklistCacheKey,
|
|
741
|
+
);
|
|
742
|
+
|
|
743
|
+
if (cachedBlacklist) {
|
|
744
|
+
const blacklist = JSON.parse(cachedBlacklist) as string[];
|
|
745
|
+
return blacklist.includes(txOrigin.toLowerCase());
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
async isRestricted(): Promise<boolean> {
|
|
752
|
+
const result = await this.dexHelper.cache.get(
|
|
753
|
+
this.dexKey,
|
|
754
|
+
this.network,
|
|
755
|
+
CABLES_RESTRICTED_CACHE_KEY,
|
|
756
|
+
);
|
|
757
|
+
|
|
758
|
+
return result === 'true';
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
async restrict() {
|
|
762
|
+
const errorsDataRaw = await this.dexHelper.cache.get(
|
|
763
|
+
this.dexKey,
|
|
764
|
+
this.network,
|
|
765
|
+
CABLES_ERRORS_CACHE_KEY,
|
|
766
|
+
);
|
|
767
|
+
|
|
768
|
+
const errorsData: RestrictData = Utils.Parse(errorsDataRaw);
|
|
769
|
+
const ERRORS_TTL_S = Math.floor(CABLES_RESTRICT_CHECK_INTERVAL_MS / 1000);
|
|
770
|
+
|
|
771
|
+
if (
|
|
772
|
+
!errorsData ||
|
|
773
|
+
errorsData?.addedDatetimeMs + CABLES_RESTRICT_CHECK_INTERVAL_MS <
|
|
774
|
+
Date.now()
|
|
775
|
+
) {
|
|
776
|
+
this.logger.warn(
|
|
777
|
+
`${this.dexKey}-${this.network}: First encounter of error OR error ocurred outside of threshold, setting up counter`,
|
|
778
|
+
);
|
|
779
|
+
const data: RestrictData = {
|
|
780
|
+
count: 1,
|
|
781
|
+
addedDatetimeMs: Date.now(),
|
|
782
|
+
};
|
|
783
|
+
await this.dexHelper.cache.setex(
|
|
784
|
+
this.dexKey,
|
|
785
|
+
this.network,
|
|
786
|
+
CABLES_ERRORS_CACHE_KEY,
|
|
787
|
+
ERRORS_TTL_S,
|
|
788
|
+
Utils.Serialize(data),
|
|
789
|
+
);
|
|
790
|
+
return;
|
|
791
|
+
} else {
|
|
792
|
+
if (errorsData.count + 1 >= CABLES_RESTRICT_COUNT_THRESHOLD) {
|
|
793
|
+
this.logger.warn(
|
|
794
|
+
`${this.dexKey}-${this.network}: Restricting due to error count=${
|
|
795
|
+
errorsData.count + 1
|
|
796
|
+
} within ${CABLES_RESTRICT_CHECK_INTERVAL_MS / 1000 / 60} minutes`,
|
|
797
|
+
);
|
|
798
|
+
await this.dexHelper.cache.setex(
|
|
799
|
+
this.dexKey,
|
|
800
|
+
this.network,
|
|
801
|
+
CABLES_RESTRICTED_CACHE_KEY,
|
|
802
|
+
CABLES_RESTRICT_TTL_S,
|
|
803
|
+
'true',
|
|
804
|
+
);
|
|
805
|
+
} else {
|
|
806
|
+
this.logger.warn(
|
|
807
|
+
`${this.dexKey}-${this.network}: Error count increased`,
|
|
808
|
+
);
|
|
809
|
+
const data: RestrictData = {
|
|
810
|
+
count: errorsData.count + 1,
|
|
811
|
+
addedDatetimeMs: errorsData.addedDatetimeMs,
|
|
812
|
+
};
|
|
813
|
+
await this.dexHelper.cache.setex(
|
|
814
|
+
this.dexKey,
|
|
815
|
+
this.network,
|
|
816
|
+
CABLES_RESTRICTED_CACHE_KEY,
|
|
817
|
+
ERRORS_TTL_S,
|
|
818
|
+
Utils.Serialize(data),
|
|
819
|
+
);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|