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