/
uniswap-v3-algebra-abstract-provider.ts
288 lines (259 loc) · 12.5 KB
/
uniswap-v3-algebra-abstract-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import BigNumber from 'bignumber.js';
import { InsufficientLiquidityError, RubicSdkError } from 'src/common/errors';
import { PriceToken, PriceTokenAmount } from 'src/common/tokens';
import { combineOptions } from 'src/common/utils/options';
import { EvmBlockchainName } from 'src/core/blockchain/models/blockchain-name';
import { Web3Pure } from 'src/core/blockchain/web3-pure/web3-pure';
import { createTokenNativeAddressProxy } from 'src/features/common/utils/token-native-address-proxy';
import { OnChainCalculationOptions } from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-calculation-options';
import { OnChainProxyFeeInfo } from 'src/features/on-chain/calculation-manager/providers/common/models/on-chain-proxy-fee-info';
import { Exact } from 'src/features/on-chain/calculation-manager/providers/common/on-chain-trade/evm-on-chain-trade/models/exact';
import { getGasFeeInfo } from 'src/features/on-chain/calculation-manager/providers/common/utils/get-gas-fee-info';
import { CamelotArbitrumTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/arbitrum/camelot-arbitrum/camelot-arbitrum-trade';
import { BerachainTestnetAlgebraTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/berachain-testnet/berachain-testnet-algebra/berachain-testnet-algebra-trade';
import { evmProviderDefaultOptions } from 'src/features/on-chain/calculation-manager/providers/dexes/common/on-chain-provider/evm-on-chain-provider/constants/evm-provider-default-options';
import { EvmOnChainProvider } from 'src/features/on-chain/calculation-manager/providers/dexes/common/on-chain-provider/evm-on-chain-provider/evm-on-chain-provider';
import { GasPriceInfo } from 'src/features/on-chain/calculation-manager/providers/dexes/common/on-chain-provider/evm-on-chain-provider/models/gas-price-info';
import { UniswapV3TradeClass } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-abstract/models/uniswap-v3-trade-class';
import {
UniswapV3AlgebraCalculatedInfo,
UniswapV3AlgebraCalculatedInfoWithProfit
} from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-algebra-abstract/models/uniswap-v3-algebra-calculated-info';
import { UniswapV3AlgebraCalculationOptions } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-algebra-abstract/models/uniswap-v3-algebra-calculation-options';
import { UniswapV3AlgebraProviderConfiguration } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-algebra-abstract/models/uniswap-v3-algebra-provider-configuration';
import { UniswapV3AlgebraQuoterController } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-algebra-abstract/models/uniswap-v3-algebra-quoter-controller';
import { UniswapV3AlgebraRoute } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-algebra-abstract/models/uniswap-v3-algebra-route';
import { UniswapV3AlgebraTradeStructOmitPath } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-algebra-abstract/models/uniswap-v3-algebra-trade-struct';
import { UniswapV3AlgebraAbstractTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/common/uniswap-v3-algebra-abstract/uniswap-v3-algebra-abstract-trade';
import { getFromToTokensAmountsByExact } from 'src/features/on-chain/calculation-manager/providers/dexes/common/utils/get-from-to-tokens-amounts-by-exact';
import { ModeAlgebraTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/mode/algebra-mode/mode-algebra-trade';
import { AlgebraTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/polygon/algebra/algebra-trade';
import { QuickSwapV3Trade } from 'src/features/on-chain/calculation-manager/providers/dexes/polygon/quick-swap-v3/quick-swap-v3-trade';
import { QuickSwapV3PolygonZKEVMTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/polygon-zkevm/quick-swap-v3/quick-swap-v3-trade';
import { UniSwapV3ScrollSepoliaTrade } from 'src/features/on-chain/calculation-manager/providers/dexes/scroll-sepolia/uni-swap-v3-scroll-sepolia/uni-swap-v3-scroll-sepolia-trade';
import { AbiItem } from 'web3-utils';
import { AlgebraIntegralTrade } from '../../arthera-testnet/algebra-integral/algebra-integral-trade';
export abstract class UniswapV3AlgebraAbstractProvider<
T extends UniswapV3AlgebraAbstractTrade = UniswapV3AlgebraAbstractTrade
> extends EvmOnChainProvider {
protected abstract readonly contractAbi: AbiItem[];
protected abstract readonly contractAddress: string;
protected abstract readonly OnChainTradeClass:
| UniswapV3TradeClass<T>
| typeof AlgebraTrade
| typeof AlgebraIntegralTrade
| typeof QuickSwapV3Trade
| typeof QuickSwapV3PolygonZKEVMTrade
| typeof CamelotArbitrumTrade
| typeof UniSwapV3ScrollSepoliaTrade
| typeof BerachainTestnetAlgebraTrade
| typeof ModeAlgebraTrade;
protected abstract readonly quoterController: UniswapV3AlgebraQuoterController;
protected abstract readonly providerConfiguration: UniswapV3AlgebraProviderConfiguration;
protected readonly isRubicOptimisationEnabled: boolean = false;
protected readonly defaultOptions: UniswapV3AlgebraCalculationOptions = {
...evmProviderDefaultOptions,
deadlineMinutes: 20,
disableMultihops: false
};
protected abstract createTradeInstance(
tradeStruct: UniswapV3AlgebraTradeStructOmitPath,
route: UniswapV3AlgebraRoute,
providerAddress: string
): T;
public async calculate(
from: PriceTokenAmount<EvmBlockchainName>,
toToken: PriceToken<EvmBlockchainName>,
options?: OnChainCalculationOptions
): Promise<T> {
return this.calculateDifficultTrade(from, toToken, 'input', from.weiAmount, options);
}
/**
* Calculates trade, based on amount, user wants to get.
* @param fromToken Token to sell.
* @param to Token to get with output amount.
* @param options Additional options.
*/
public async calculateExactOutput(
fromToken: PriceToken<EvmBlockchainName>,
to: PriceTokenAmount<EvmBlockchainName>,
options?: OnChainCalculationOptions
): Promise<T> {
return this.calculateDifficultTrade(fromToken, to, 'output', to.weiAmount, options);
}
/**
* Calculates input amount, based on amount, user wants to get.
* @param fromToken Token to sell.
* @param to Token to get with output amount.
* @param options Additional options.
*/
public async calculateExactOutputAmount(
fromToken: PriceToken<EvmBlockchainName>,
to: PriceTokenAmount<EvmBlockchainName>,
options?: OnChainCalculationOptions
): Promise<BigNumber> {
return (await this.calculateExactOutput(fromToken, to, options)).from.tokenAmount;
}
protected async calculateDifficultTrade(
fromToken: PriceToken<EvmBlockchainName>,
toToken: PriceToken<EvmBlockchainName>,
exact: Exact,
weiAmount: BigNumber,
options?: OnChainCalculationOptions
): Promise<T> {
const fullOptions = combineOptions(options, this.defaultOptions);
let weiAmountWithoutFee = weiAmount;
let proxyFeeInfo: OnChainProxyFeeInfo | undefined;
if (fullOptions.useProxy) {
const proxyContractInfo = await this.handleProxyContract(
new PriceTokenAmount({
...fromToken.asStruct,
weiAmount
}),
fullOptions
);
weiAmountWithoutFee = proxyContractInfo.fromWithoutFee.weiAmount;
proxyFeeInfo = proxyContractInfo.proxyFeeInfo;
}
const fromClone = createTokenNativeAddressProxy(
fromToken,
this.providerConfiguration.wethAddress
);
const toClone = createTokenNativeAddressProxy(
toToken,
this.providerConfiguration.wethAddress
);
let gasPriceInfo: GasPriceInfo | undefined;
if (fullOptions.gasCalculation !== 'disabled') {
try {
gasPriceInfo = await this.getGasPriceInfo();
} catch {}
}
const { route, estimatedGas } = await this.getRoute(
fromClone,
toClone,
exact,
weiAmountWithoutFee,
fullOptions,
gasPriceInfo?.gasPriceInUsd
);
const { from, to, fromWithoutFee } = getFromToTokensAmountsByExact(
fromToken,
toToken,
exact,
weiAmount,
weiAmountWithoutFee,
route.outputAbsoluteAmount
);
const tradeStruct: UniswapV3AlgebraTradeStructOmitPath = {
from,
to,
gasFeeInfo: null,
exact,
slippageTolerance: fullOptions.slippageTolerance,
deadlineMinutes: fullOptions.deadlineMinutes,
useProxy: fullOptions.useProxy,
proxyFeeInfo,
fromWithoutFee,
withDeflation: fullOptions.withDeflation,
usedForCrossChain: fullOptions.usedForCrossChain
};
if (fullOptions.gasCalculation === 'disabled') {
return this.createTradeInstance(tradeStruct, route, fullOptions.providerAddress);
}
const gasFeeInfo = getGasFeeInfo(estimatedGas, gasPriceInfo!);
return this.createTradeInstance(
{
...tradeStruct,
gasFeeInfo
},
route,
fullOptions.providerAddress
);
}
protected async getRoute(
from: PriceToken<EvmBlockchainName>,
to: PriceToken<EvmBlockchainName>,
exact: Exact,
weiAmount: BigNumber,
options: UniswapV3AlgebraCalculationOptions,
gasPriceInUsd?: BigNumber
): Promise<UniswapV3AlgebraCalculatedInfo> {
const routes = (
await this.quoterController.getAllRoutes(
from,
to,
exact,
weiAmount.toFixed(0),
options.disableMultihops ? 0 : this.providerConfiguration.maxTransitTokens
)
).sort(
(a, b) =>
b.outputAbsoluteAmount.comparedTo(a.outputAbsoluteAmount) *
(exact === 'input' ? 1 : -1)
);
if (routes.length === 0) {
throw new InsufficientLiquidityError();
}
if (options.gasCalculation === 'disabled' && routes?.[0]) {
return {
route: routes[0]
};
}
if (
this.isRubicOptimisationEnabled &&
options.gasCalculation === 'rubicOptimisation' &&
to.price?.isFinite() &&
gasPriceInUsd
) {
const estimatedGasLimits = await this.OnChainTradeClass.estimateGasLimitForRoutes(
from,
to,
exact,
weiAmount,
options,
routes,
this.createTradeInstance
);
const calculatedProfits: UniswapV3AlgebraCalculatedInfoWithProfit[] = routes.map(
(route, index) => {
const estimatedGas = estimatedGasLimits[index];
if (!estimatedGas) {
throw new RubicSdkError('Estimated gas has have to be defined');
}
const gasFeeInUsd = gasPriceInUsd!.multipliedBy(estimatedGas);
const profit = Web3Pure.fromWei(route.outputAbsoluteAmount, to.decimals)
.multipliedBy(to.price)
.minus(gasFeeInUsd);
return {
route,
estimatedGas,
profit
};
}
);
const sortedRoutes = calculatedProfits.sort((a, b) => b.profit.comparedTo(a.profit))[0];
if (!sortedRoutes) {
throw new RubicSdkError('Sorted routes have to be defined');
}
return sortedRoutes;
}
const route = routes[0];
if (!route) {
throw new RubicSdkError('Route has to be defined');
}
const estimatedGas = await this.OnChainTradeClass.estimateGasLimitForRoute(
from,
to,
exact,
weiAmount,
options,
route,
this.createTradeInstance.bind(this)
);
return {
route,
estimatedGas
};
}
}