@centurion_project/router-sdk 2.11.11 → 2.11.13
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/dist/cjs/src/entities/mixedRoute/route.js +4 -19
- package/dist/cjs/src/entities/mixedRoute/route.js.map +1 -1
- package/dist/cjs/src/utils/index.js +19 -7
- package/dist/cjs/src/utils/index.js.map +1 -1
- package/dist/esm/src/entities/mixedRoute/route.js +5 -20
- package/dist/esm/src/entities/mixedRoute/route.js.map +1 -1
- package/dist/esm/src/utils/index.js +19 -7
- package/dist/esm/src/utils/index.js.map +1 -1
- package/dist/types/src/approveAndCall.d.ts +1 -0
- package/dist/types/src/approveAndCall.d.ts.map +1 -0
- package/dist/types/src/constants.d.ts +1 -0
- package/dist/types/src/constants.d.ts.map +1 -0
- package/dist/types/src/entities/mixedRoute/route.d.ts +1 -0
- package/dist/types/src/entities/mixedRoute/route.d.ts.map +1 -0
- package/dist/types/src/entities/mixedRoute/trade.d.ts +1 -0
- package/dist/types/src/entities/mixedRoute/trade.d.ts.map +1 -0
- package/dist/types/src/entities/protocol.d.ts +1 -0
- package/dist/types/src/entities/protocol.d.ts.map +1 -0
- package/dist/types/src/entities/route.d.ts +1 -0
- package/dist/types/src/entities/route.d.ts.map +1 -0
- package/dist/types/src/entities/trade.d.ts +1 -0
- package/dist/types/src/entities/trade.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/multicallExtended.d.ts +1 -0
- package/dist/types/src/multicallExtended.d.ts.map +1 -0
- package/dist/types/src/paymentsExtended.d.ts +1 -0
- package/dist/types/src/paymentsExtended.d.ts.map +1 -0
- package/dist/types/src/swapRouter.d.ts +1 -0
- package/dist/types/src/swapRouter.d.ts.map +1 -0
- package/dist/types/src/utils/TPool.d.ts +1 -0
- package/dist/types/src/utils/TPool.d.ts.map +1 -0
- package/dist/types/src/utils/encodeMixedRouteToPath.d.ts +1 -0
- package/dist/types/src/utils/encodeMixedRouteToPath.d.ts.map +1 -0
- package/dist/types/src/utils/index.d.ts +1 -0
- package/dist/types/src/utils/index.d.ts.map +1 -0
- package/dist/types/src/utils/pathCurrency.d.ts +1 -0
- package/dist/types/src/utils/pathCurrency.d.ts.map +1 -0
- package/package.json +12 -9
- package/src/abis/IApproveAndCall.json +213 -0
- package/src/abis/IMulticallExtended.json +78 -0
- package/src/abis/IPeripheryPaymentsWithFeeExtended.json +233 -0
- package/src/abis/ISwapRouter02.json +717 -0
- package/src/approveAndCall.ts +129 -0
- package/src/constants.ts +24 -0
- package/src/entities/mixedRoute/route.ts +121 -0
- package/src/entities/mixedRoute/trade.ts +530 -0
- package/src/entities/protocol.ts +6 -0
- package/src/entities/route.ts +94 -0
- package/src/entities/trade.ts +550 -0
- package/src/index.ts +14 -0
- package/src/multicallExtended.ts +48 -0
- package/src/paymentsExtended.ts +75 -0
- package/src/swapRouter.ts +700 -0
- package/src/utils/TPool.ts +5 -0
- package/src/utils/encodeMixedRouteToPath.ts +112 -0
- package/src/utils/index.ts +66 -0
- package/src/utils/pathCurrency.ts +36 -0
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
import { Currency, CurrencyAmount, Fraction, Percent, Price, TradeType, Ether } from '@centurion_project/sdk-core'
|
|
2
|
+
import { Pair, Route as V2RouteSDK, Trade as V2TradeSDK } from '@centurion_project/v2-sdk'
|
|
3
|
+
import { Pool as V3Pool, Route as V3RouteSDK, Trade as V3TradeSDK } from '@centurion_project/v3-sdk'
|
|
4
|
+
import { Pool as V4Pool, Route as V4RouteSDK, Trade as V4TradeSDK } from '@centurion_project/v4-sdk'
|
|
5
|
+
import JSBI from 'jsbi'
|
|
6
|
+
import invariant from 'tiny-invariant'
|
|
7
|
+
import { ONE, ONE_HUNDRED_PERCENT, ZERO, ZERO_PERCENT } from '../constants'
|
|
8
|
+
import { MixedRouteSDK } from './mixedRoute/route'
|
|
9
|
+
import { MixedRouteTrade as MixedRouteTradeSDK } from './mixedRoute/trade'
|
|
10
|
+
import { IRoute, MixedRoute, RouteV2, RouteV3, RouteV4 } from './route'
|
|
11
|
+
|
|
12
|
+
export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType> {
|
|
13
|
+
public readonly routes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[]
|
|
14
|
+
public readonly tradeType: TTradeType
|
|
15
|
+
private _outputAmount: CurrencyAmount<TOutput> | undefined
|
|
16
|
+
private _inputAmount: CurrencyAmount<TInput> | undefined
|
|
17
|
+
private _nativeInputRoutes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] | undefined
|
|
18
|
+
private _wethInputRoutes: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] | undefined
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The swaps of the trade, i.e. which routes and how much is swapped in each that
|
|
22
|
+
* make up the trade. May consist of swaps in v2 or v3.
|
|
23
|
+
*/
|
|
24
|
+
public readonly swaps: {
|
|
25
|
+
route: IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>
|
|
26
|
+
inputAmount: CurrencyAmount<TInput>
|
|
27
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
28
|
+
minHopPriceX36?: bigint[]
|
|
29
|
+
}[]
|
|
30
|
+
|
|
31
|
+
// construct a trade across v2 and v3 routes from pre-computed amounts
|
|
32
|
+
public constructor({
|
|
33
|
+
v2Routes = [],
|
|
34
|
+
v3Routes = [],
|
|
35
|
+
v4Routes = [],
|
|
36
|
+
mixedRoutes = [],
|
|
37
|
+
tradeType,
|
|
38
|
+
}: {
|
|
39
|
+
v2Routes?: {
|
|
40
|
+
routev2: V2RouteSDK<TInput, TOutput>
|
|
41
|
+
inputAmount: CurrencyAmount<TInput>
|
|
42
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
43
|
+
minHopPriceX36?: bigint[]
|
|
44
|
+
}[]
|
|
45
|
+
v3Routes?: {
|
|
46
|
+
routev3: V3RouteSDK<TInput, TOutput>
|
|
47
|
+
inputAmount: CurrencyAmount<TInput>
|
|
48
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
49
|
+
minHopPriceX36?: bigint[]
|
|
50
|
+
}[]
|
|
51
|
+
v4Routes?: {
|
|
52
|
+
routev4: V4RouteSDK<TInput, TOutput>
|
|
53
|
+
inputAmount: CurrencyAmount<TInput>
|
|
54
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
55
|
+
minHopPriceX36?: bigint[]
|
|
56
|
+
}[]
|
|
57
|
+
mixedRoutes?: {
|
|
58
|
+
mixedRoute: MixedRouteSDK<TInput, TOutput>
|
|
59
|
+
inputAmount: CurrencyAmount<TInput>
|
|
60
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
61
|
+
minHopPriceX36?: bigint[]
|
|
62
|
+
}[]
|
|
63
|
+
tradeType: TTradeType
|
|
64
|
+
}) {
|
|
65
|
+
this.swaps = []
|
|
66
|
+
this.routes = []
|
|
67
|
+
// wrap v2 routes
|
|
68
|
+
for (const { routev2, inputAmount, outputAmount, minHopPriceX36 } of v2Routes) {
|
|
69
|
+
const route = new RouteV2(routev2)
|
|
70
|
+
this.routes.push(route)
|
|
71
|
+
this.swaps.push({
|
|
72
|
+
route,
|
|
73
|
+
inputAmount,
|
|
74
|
+
outputAmount,
|
|
75
|
+
minHopPriceX36,
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
// wrap v3 routes
|
|
79
|
+
for (const { routev3, inputAmount, outputAmount, minHopPriceX36 } of v3Routes) {
|
|
80
|
+
const route = new RouteV3(routev3)
|
|
81
|
+
this.routes.push(route)
|
|
82
|
+
this.swaps.push({
|
|
83
|
+
route,
|
|
84
|
+
inputAmount,
|
|
85
|
+
outputAmount,
|
|
86
|
+
minHopPriceX36,
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
// wrap v4 routes
|
|
90
|
+
for (const { routev4, inputAmount, outputAmount, minHopPriceX36 } of v4Routes) {
|
|
91
|
+
const route = new RouteV4(routev4)
|
|
92
|
+
this.routes.push(route)
|
|
93
|
+
this.swaps.push({
|
|
94
|
+
route,
|
|
95
|
+
inputAmount,
|
|
96
|
+
outputAmount,
|
|
97
|
+
minHopPriceX36,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
for (const { mixedRoute, inputAmount, outputAmount, minHopPriceX36 } of mixedRoutes) {
|
|
101
|
+
const route = new MixedRoute(mixedRoute)
|
|
102
|
+
this.routes.push(route)
|
|
103
|
+
this.swaps.push({
|
|
104
|
+
route,
|
|
105
|
+
inputAmount,
|
|
106
|
+
outputAmount,
|
|
107
|
+
minHopPriceX36,
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (this.swaps.length === 0) {
|
|
112
|
+
throw new Error('No routes provided when calling Trade constructor')
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
this.tradeType = tradeType
|
|
116
|
+
|
|
117
|
+
// each route must have the same input and output currency
|
|
118
|
+
const inputCurrency = this.swaps[0].inputAmount.currency
|
|
119
|
+
const outputCurrency = this.swaps[0].outputAmount.currency
|
|
120
|
+
invariant(
|
|
121
|
+
this.swaps.every(({ route }) => inputCurrency.wrapped.equals(route.input.wrapped)),
|
|
122
|
+
'INPUT_CURRENCY_MATCH'
|
|
123
|
+
)
|
|
124
|
+
invariant(
|
|
125
|
+
this.swaps.every(({ route }) => outputCurrency.wrapped.equals(route.output.wrapped)),
|
|
126
|
+
'OUTPUT_CURRENCY_MATCH'
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
// pools must be unique inter protocols
|
|
130
|
+
const numPools = this.swaps.map(({ route }) => route.pools.length).reduce((total, cur) => total + cur, 0)
|
|
131
|
+
const poolIdentifierSet = new Set<string>()
|
|
132
|
+
for (const { route } of this.swaps) {
|
|
133
|
+
for (const pool of route.pools) {
|
|
134
|
+
if (pool instanceof V4Pool) {
|
|
135
|
+
poolIdentifierSet.add(pool.poolId)
|
|
136
|
+
} else if (pool instanceof V3Pool) {
|
|
137
|
+
poolIdentifierSet.add(V3Pool.getAddress(pool.token0, pool.token1, pool.fee))
|
|
138
|
+
} else if (pool instanceof Pair) {
|
|
139
|
+
const pair = pool
|
|
140
|
+
poolIdentifierSet.add(Pair.getAddress(pair.token0, pair.token1))
|
|
141
|
+
} else {
|
|
142
|
+
throw new Error('Unexpected pool type in route when constructing trade object')
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
invariant(numPools === poolIdentifierSet.size, 'POOLS_DUPLICATED')
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public get inputAmount(): CurrencyAmount<TInput> {
|
|
150
|
+
if (this._inputAmount) {
|
|
151
|
+
return this._inputAmount
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const inputAmountCurrency = this.swaps[0].inputAmount.currency
|
|
155
|
+
const totalInputFromRoutes = this.swaps
|
|
156
|
+
.map(({ inputAmount: routeInputAmount }) => routeInputAmount)
|
|
157
|
+
.reduce((total, cur) => total.add(cur), CurrencyAmount.fromRawAmount(inputAmountCurrency, 0))
|
|
158
|
+
|
|
159
|
+
this._inputAmount = totalInputFromRoutes
|
|
160
|
+
return this._inputAmount
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
public get outputAmount(): CurrencyAmount<TOutput> {
|
|
164
|
+
if (this._outputAmount) {
|
|
165
|
+
return this._outputAmount
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const outputCurrency = this.swaps[0].outputAmount.currency
|
|
169
|
+
const totalOutputFromRoutes = this.swaps
|
|
170
|
+
.map(({ outputAmount: routeOutputAmount }) => routeOutputAmount)
|
|
171
|
+
.reduce((total, cur) => total.add(cur), CurrencyAmount.fromRawAmount(outputCurrency, 0))
|
|
172
|
+
|
|
173
|
+
this._outputAmount = totalOutputFromRoutes
|
|
174
|
+
return this._outputAmount
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Returns the sum of all swaps within the trade
|
|
179
|
+
* @returns
|
|
180
|
+
* inputAmount: total input amount
|
|
181
|
+
* inputAmountNative: total amount of native currency required for ETH input paths
|
|
182
|
+
* - 0 if inputAmount is native but no native input paths
|
|
183
|
+
* - undefined if inputAmount is not native
|
|
184
|
+
* outputAmount: total output amount
|
|
185
|
+
* outputAmountNative: total amount of native currency returned from ETH output paths
|
|
186
|
+
* - 0 if outputAmount is native but no native output paths
|
|
187
|
+
* - undefined if outputAmount is not native
|
|
188
|
+
*/
|
|
189
|
+
public get amounts(): {
|
|
190
|
+
inputAmount: CurrencyAmount<TInput>
|
|
191
|
+
inputAmountNative: CurrencyAmount<TInput> | undefined
|
|
192
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
193
|
+
outputAmountNative: CurrencyAmount<TOutput> | undefined
|
|
194
|
+
} {
|
|
195
|
+
// Find native currencies for reduce below
|
|
196
|
+
const inputNativeCurrency = this.swaps.find(({ inputAmount }) => inputAmount.currency.isNative)?.inputAmount
|
|
197
|
+
.currency
|
|
198
|
+
const outputNativeCurrency = this.swaps.find(({ outputAmount }) => outputAmount.currency.isNative)?.outputAmount
|
|
199
|
+
.currency
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
inputAmount: this.inputAmount,
|
|
203
|
+
inputAmountNative: inputNativeCurrency
|
|
204
|
+
? this.swaps.reduce((total, swap) => {
|
|
205
|
+
return swap.route.pathInput.isNative ? total.add(swap.inputAmount) : total
|
|
206
|
+
}, CurrencyAmount.fromRawAmount(inputNativeCurrency, 0))
|
|
207
|
+
: undefined,
|
|
208
|
+
outputAmount: this.outputAmount,
|
|
209
|
+
outputAmountNative: outputNativeCurrency
|
|
210
|
+
? this.swaps.reduce((total, swap) => {
|
|
211
|
+
return swap.route.pathOutput.isNative ? total.add(swap.outputAmount) : total
|
|
212
|
+
}, CurrencyAmount.fromRawAmount(outputNativeCurrency, 0))
|
|
213
|
+
: undefined,
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
public get numberOfInputWraps(): number {
|
|
218
|
+
// if the trade's input is eth it may require a wrap
|
|
219
|
+
if (this.inputAmount.currency.isNative) {
|
|
220
|
+
return this.wethInputRoutes.length
|
|
221
|
+
} else return 0
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
public get numberOfInputUnwraps(): number {
|
|
225
|
+
// if the trade's input is weth, it may require an unwrap
|
|
226
|
+
if (this.isWrappedNative(this.inputAmount.currency)) {
|
|
227
|
+
return this.nativeInputRoutes.length
|
|
228
|
+
} else return 0
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
public get nativeInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] {
|
|
232
|
+
if (this._nativeInputRoutes) {
|
|
233
|
+
return this._nativeInputRoutes
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
this._nativeInputRoutes = this.routes.filter((route) => route.pathInput.isNative)
|
|
237
|
+
return this._nativeInputRoutes
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
public get wethInputRoutes(): IRoute<TInput, TOutput, Pair | V3Pool | V4Pool>[] {
|
|
241
|
+
if (this._wethInputRoutes) {
|
|
242
|
+
return this._wethInputRoutes
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
this._wethInputRoutes = this.routes.filter((route) => this.isWrappedNative(route.pathInput))
|
|
246
|
+
return this._wethInputRoutes
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private _executionPrice: Price<TInput, TOutput> | undefined
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The price expressed in terms of output amount/input amount.
|
|
253
|
+
*/
|
|
254
|
+
public get executionPrice(): Price<TInput, TOutput> {
|
|
255
|
+
return (
|
|
256
|
+
this._executionPrice ??
|
|
257
|
+
(this._executionPrice = new Price(
|
|
258
|
+
this.inputAmount.currency,
|
|
259
|
+
this.outputAmount.currency,
|
|
260
|
+
this.inputAmount.quotient,
|
|
261
|
+
this.outputAmount.quotient
|
|
262
|
+
))
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Returns the sell tax of the input token
|
|
268
|
+
*/
|
|
269
|
+
public get inputTax(): Percent {
|
|
270
|
+
const inputCurrency = this.inputAmount.currency
|
|
271
|
+
if (inputCurrency.isNative || !inputCurrency.wrapped.sellFeeBps) return ZERO_PERCENT
|
|
272
|
+
|
|
273
|
+
return new Percent(inputCurrency.wrapped.sellFeeBps.toNumber(), 10000)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Returns the buy tax of the output token
|
|
278
|
+
*/
|
|
279
|
+
public get outputTax(): Percent {
|
|
280
|
+
const outputCurrency = this.outputAmount.currency
|
|
281
|
+
if (outputCurrency.isNative || !outputCurrency.wrapped.buyFeeBps) return ZERO_PERCENT
|
|
282
|
+
|
|
283
|
+
return new Percent(outputCurrency.wrapped.buyFeeBps.toNumber(), 10000)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private isWrappedNative(currency: Currency): boolean {
|
|
287
|
+
const chainId = currency.chainId
|
|
288
|
+
return currency.equals(Ether.onChain(chainId).wrapped)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The cached result of the price impact computation
|
|
293
|
+
* @private
|
|
294
|
+
*/
|
|
295
|
+
private _priceImpact: Percent | undefined
|
|
296
|
+
/**
|
|
297
|
+
* Returns the percent difference between the route's mid price and the expected execution price
|
|
298
|
+
* In order to exclude token taxes from the price impact calculation, the spot price is calculated
|
|
299
|
+
* using a ratio of values that go into the pools, which are the post-tax input amount and pre-tax output amount.
|
|
300
|
+
*/
|
|
301
|
+
public get priceImpact(): Percent {
|
|
302
|
+
if (this._priceImpact) {
|
|
303
|
+
return this._priceImpact
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// returns 0% price impact even though this may be inaccurate as a swap may have occured.
|
|
307
|
+
// because we're unable to derive the pre-buy-tax amount, use 0% as a placeholder.
|
|
308
|
+
if (this.outputTax.equalTo(ONE_HUNDRED_PERCENT)) return ZERO_PERCENT
|
|
309
|
+
|
|
310
|
+
let spotOutputAmount = CurrencyAmount.fromRawAmount(this.outputAmount.currency, 0)
|
|
311
|
+
for (const { route, inputAmount } of this.swaps) {
|
|
312
|
+
const midPrice = route.midPrice
|
|
313
|
+
const postTaxInputAmount = inputAmount.multiply(new Fraction(ONE).subtract(this.inputTax))
|
|
314
|
+
spotOutputAmount = spotOutputAmount.add(midPrice.quote(postTaxInputAmount))
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// if the total output of this trade is 0, then most likely the post-tax input was also 0, and therefore this swap
|
|
318
|
+
// does not move the pools' market price
|
|
319
|
+
if (spotOutputAmount.equalTo(ZERO)) return ZERO_PERCENT
|
|
320
|
+
|
|
321
|
+
const preTaxOutputAmount = this.outputAmount.divide(new Fraction(ONE).subtract(this.outputTax))
|
|
322
|
+
const priceImpact = spotOutputAmount.subtract(preTaxOutputAmount).divide(spotOutputAmount)
|
|
323
|
+
this._priceImpact = new Percent(priceImpact.numerator, priceImpact.denominator)
|
|
324
|
+
|
|
325
|
+
return this._priceImpact
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
330
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
331
|
+
* @returns The amount out
|
|
332
|
+
*/
|
|
333
|
+
public minimumAmountOut(slippageTolerance: Percent, amountOut = this.outputAmount): CurrencyAmount<TOutput> {
|
|
334
|
+
invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE')
|
|
335
|
+
if (this.tradeType === TradeType.EXACT_OUTPUT) {
|
|
336
|
+
return amountOut
|
|
337
|
+
} else {
|
|
338
|
+
const slippageAdjustedAmountOut = new Fraction(ONE)
|
|
339
|
+
.subtract(slippageTolerance)
|
|
340
|
+
.multiply(amountOut.quotient).quotient
|
|
341
|
+
const clampedAmount = JSBI.greaterThan(slippageAdjustedAmountOut, JSBI.BigInt(0))
|
|
342
|
+
? slippageAdjustedAmountOut
|
|
343
|
+
: JSBI.BigInt(0)
|
|
344
|
+
return CurrencyAmount.fromRawAmount(amountOut.currency, clampedAmount)
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
350
|
+
* @param slippageTolerance The tolerance of unfavorable slippage from the execution price of this trade
|
|
351
|
+
* @returns The amount in
|
|
352
|
+
*/
|
|
353
|
+
public maximumAmountIn(slippageTolerance: Percent, amountIn = this.inputAmount): CurrencyAmount<TInput> {
|
|
354
|
+
invariant(!slippageTolerance.lessThan(ZERO), 'SLIPPAGE_TOLERANCE')
|
|
355
|
+
if (this.tradeType === TradeType.EXACT_INPUT) {
|
|
356
|
+
return amountIn
|
|
357
|
+
} else {
|
|
358
|
+
const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(amountIn.quotient).quotient
|
|
359
|
+
return CurrencyAmount.fromRawAmount(amountIn.currency, slippageAdjustedAmountIn)
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Return the execution price after accounting for slippage tolerance
|
|
365
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
366
|
+
* @returns The execution price
|
|
367
|
+
*/
|
|
368
|
+
public worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput> {
|
|
369
|
+
return new Price(
|
|
370
|
+
this.inputAmount.currency,
|
|
371
|
+
this.outputAmount.currency,
|
|
372
|
+
this.maximumAmountIn(slippageTolerance).quotient,
|
|
373
|
+
this.minimumAmountOut(slippageTolerance).quotient
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
public static async fromRoutes<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(
|
|
378
|
+
v2Routes: {
|
|
379
|
+
routev2: V2RouteSDK<TInput, TOutput>
|
|
380
|
+
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>
|
|
381
|
+
minHopPriceX36?: bigint[]
|
|
382
|
+
}[],
|
|
383
|
+
v3Routes: {
|
|
384
|
+
routev3: V3RouteSDK<TInput, TOutput>
|
|
385
|
+
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>
|
|
386
|
+
minHopPriceX36?: bigint[]
|
|
387
|
+
}[],
|
|
388
|
+
tradeType: TTradeType,
|
|
389
|
+
mixedRoutes?: {
|
|
390
|
+
mixedRoute: MixedRouteSDK<TInput, TOutput>
|
|
391
|
+
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>
|
|
392
|
+
minHopPriceX36?: bigint[]
|
|
393
|
+
}[],
|
|
394
|
+
v4Routes?: {
|
|
395
|
+
routev4: V4RouteSDK<TInput, TOutput>
|
|
396
|
+
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>
|
|
397
|
+
minHopPriceX36?: bigint[]
|
|
398
|
+
}[]
|
|
399
|
+
): Promise<Trade<TInput, TOutput, TTradeType>> {
|
|
400
|
+
const populatedV2Routes: {
|
|
401
|
+
routev2: V2RouteSDK<TInput, TOutput>
|
|
402
|
+
inputAmount: CurrencyAmount<TInput>
|
|
403
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
404
|
+
minHopPriceX36?: bigint[]
|
|
405
|
+
}[] = []
|
|
406
|
+
|
|
407
|
+
const populatedV3Routes: {
|
|
408
|
+
routev3: V3RouteSDK<TInput, TOutput>
|
|
409
|
+
inputAmount: CurrencyAmount<TInput>
|
|
410
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
411
|
+
minHopPriceX36?: bigint[]
|
|
412
|
+
}[] = []
|
|
413
|
+
|
|
414
|
+
const populatedV4Routes: {
|
|
415
|
+
routev4: V4RouteSDK<TInput, TOutput>
|
|
416
|
+
inputAmount: CurrencyAmount<TInput>
|
|
417
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
418
|
+
minHopPriceX36?: bigint[]
|
|
419
|
+
}[] = []
|
|
420
|
+
|
|
421
|
+
const populatedMixedRoutes: {
|
|
422
|
+
mixedRoute: MixedRouteSDK<TInput, TOutput>
|
|
423
|
+
inputAmount: CurrencyAmount<TInput>
|
|
424
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
425
|
+
minHopPriceX36?: bigint[]
|
|
426
|
+
}[] = []
|
|
427
|
+
|
|
428
|
+
for (const { routev2, amount, minHopPriceX36 } of v2Routes) {
|
|
429
|
+
const v2Trade = new V2TradeSDK(routev2, amount, tradeType)
|
|
430
|
+
const { inputAmount, outputAmount } = v2Trade
|
|
431
|
+
|
|
432
|
+
populatedV2Routes.push({
|
|
433
|
+
routev2,
|
|
434
|
+
inputAmount,
|
|
435
|
+
outputAmount,
|
|
436
|
+
minHopPriceX36,
|
|
437
|
+
})
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
for (const { routev3, amount, minHopPriceX36 } of v3Routes) {
|
|
441
|
+
const v3Trade = await V3TradeSDK.fromRoute(routev3, amount, tradeType)
|
|
442
|
+
const { inputAmount, outputAmount } = v3Trade
|
|
443
|
+
|
|
444
|
+
populatedV3Routes.push({
|
|
445
|
+
routev3,
|
|
446
|
+
inputAmount,
|
|
447
|
+
outputAmount,
|
|
448
|
+
minHopPriceX36,
|
|
449
|
+
})
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (v4Routes) {
|
|
453
|
+
for (const { routev4, amount, minHopPriceX36 } of v4Routes) {
|
|
454
|
+
const v4Trade = await V4TradeSDK.fromRoute(routev4, amount, tradeType)
|
|
455
|
+
const { inputAmount, outputAmount } = v4Trade
|
|
456
|
+
|
|
457
|
+
populatedV4Routes.push({
|
|
458
|
+
routev4,
|
|
459
|
+
inputAmount,
|
|
460
|
+
outputAmount,
|
|
461
|
+
minHopPriceX36,
|
|
462
|
+
})
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (mixedRoutes) {
|
|
467
|
+
for (const { mixedRoute, amount, minHopPriceX36 } of mixedRoutes) {
|
|
468
|
+
const mixedRouteTrade = await MixedRouteTradeSDK.fromRoute(mixedRoute, amount, tradeType)
|
|
469
|
+
const { inputAmount, outputAmount } = mixedRouteTrade
|
|
470
|
+
|
|
471
|
+
populatedMixedRoutes.push({
|
|
472
|
+
mixedRoute,
|
|
473
|
+
inputAmount,
|
|
474
|
+
outputAmount,
|
|
475
|
+
minHopPriceX36,
|
|
476
|
+
})
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
return new Trade({
|
|
481
|
+
v2Routes: populatedV2Routes,
|
|
482
|
+
v3Routes: populatedV3Routes,
|
|
483
|
+
v4Routes: populatedV4Routes,
|
|
484
|
+
mixedRoutes: populatedMixedRoutes,
|
|
485
|
+
tradeType,
|
|
486
|
+
})
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
public static async fromRoute<TInput extends Currency, TOutput extends Currency, TTradeType extends TradeType>(
|
|
490
|
+
route:
|
|
491
|
+
| V2RouteSDK<TInput, TOutput>
|
|
492
|
+
| V3RouteSDK<TInput, TOutput>
|
|
493
|
+
| V4RouteSDK<TInput, TOutput>
|
|
494
|
+
| MixedRouteSDK<TInput, TOutput>,
|
|
495
|
+
amount: TTradeType extends TradeType.EXACT_INPUT ? CurrencyAmount<TInput> : CurrencyAmount<TOutput>,
|
|
496
|
+
tradeType: TTradeType
|
|
497
|
+
): Promise<Trade<TInput, TOutput, TTradeType>> {
|
|
498
|
+
let v2Routes: {
|
|
499
|
+
routev2: V2RouteSDK<TInput, TOutput>
|
|
500
|
+
inputAmount: CurrencyAmount<TInput>
|
|
501
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
502
|
+
}[] = []
|
|
503
|
+
|
|
504
|
+
let v3Routes: {
|
|
505
|
+
routev3: V3RouteSDK<TInput, TOutput>
|
|
506
|
+
inputAmount: CurrencyAmount<TInput>
|
|
507
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
508
|
+
}[] = []
|
|
509
|
+
|
|
510
|
+
let v4Routes: {
|
|
511
|
+
routev4: V4RouteSDK<TInput, TOutput>
|
|
512
|
+
inputAmount: CurrencyAmount<TInput>
|
|
513
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
514
|
+
}[] = []
|
|
515
|
+
|
|
516
|
+
let mixedRoutes: {
|
|
517
|
+
mixedRoute: MixedRouteSDK<TInput, TOutput>
|
|
518
|
+
inputAmount: CurrencyAmount<TInput>
|
|
519
|
+
outputAmount: CurrencyAmount<TOutput>
|
|
520
|
+
}[] = []
|
|
521
|
+
|
|
522
|
+
if (route instanceof V2RouteSDK) {
|
|
523
|
+
const v2Trade = new V2TradeSDK(route, amount, tradeType)
|
|
524
|
+
const { inputAmount, outputAmount } = v2Trade
|
|
525
|
+
v2Routes = [{ routev2: route, inputAmount, outputAmount }]
|
|
526
|
+
} else if (route instanceof V3RouteSDK) {
|
|
527
|
+
const v3Trade = await V3TradeSDK.fromRoute(route, amount, tradeType)
|
|
528
|
+
const { inputAmount, outputAmount } = v3Trade
|
|
529
|
+
v3Routes = [{ routev3: route, inputAmount, outputAmount }]
|
|
530
|
+
} else if (route instanceof V4RouteSDK) {
|
|
531
|
+
const v4Trade = await V4TradeSDK.fromRoute(route, amount, tradeType)
|
|
532
|
+
const { inputAmount, outputAmount } = v4Trade
|
|
533
|
+
v4Routes = [{ routev4: route, inputAmount, outputAmount }]
|
|
534
|
+
} else if (route instanceof MixedRouteSDK) {
|
|
535
|
+
const mixedRouteTrade = await MixedRouteTradeSDK.fromRoute(route, amount, tradeType)
|
|
536
|
+
const { inputAmount, outputAmount } = mixedRouteTrade
|
|
537
|
+
mixedRoutes = [{ mixedRoute: route, inputAmount, outputAmount }]
|
|
538
|
+
} else {
|
|
539
|
+
throw new Error('Invalid route type')
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
return new Trade({
|
|
543
|
+
v2Routes,
|
|
544
|
+
v3Routes,
|
|
545
|
+
v4Routes,
|
|
546
|
+
mixedRoutes,
|
|
547
|
+
tradeType,
|
|
548
|
+
})
|
|
549
|
+
}
|
|
550
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from './constants'
|
|
2
|
+
export * from './approveAndCall'
|
|
3
|
+
export * from './multicallExtended'
|
|
4
|
+
export * from './paymentsExtended'
|
|
5
|
+
export * from './swapRouter'
|
|
6
|
+
export * from './entities/trade'
|
|
7
|
+
export * from './entities/protocol'
|
|
8
|
+
export * from './entities/route'
|
|
9
|
+
export * from './entities/mixedRoute/route'
|
|
10
|
+
export * from './entities/mixedRoute/trade'
|
|
11
|
+
export * from './utils/encodeMixedRouteToPath'
|
|
12
|
+
export * from './utils/TPool'
|
|
13
|
+
export * from './utils/pathCurrency'
|
|
14
|
+
export * from './utils'
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi'
|
|
2
|
+
import { BigintIsh } from '@centurion_project/sdk-core'
|
|
3
|
+
import IMulticallExtended from './abis/IMulticallExtended.json'
|
|
4
|
+
import { Multicall, toHex } from '@centurion_project/v3-sdk'
|
|
5
|
+
|
|
6
|
+
// deadline or previousBlockhash
|
|
7
|
+
export type Validation = BigintIsh | string
|
|
8
|
+
|
|
9
|
+
function validateAndParseBytes32(bytes32: string): string {
|
|
10
|
+
if (!bytes32.match(/^0x[0-9a-fA-F]{64}$/)) {
|
|
11
|
+
throw new Error(`${bytes32} is not valid bytes32.`)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return bytes32.toLowerCase()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export abstract class MulticallExtended {
|
|
18
|
+
public static INTERFACE: Interface = new Interface(IMulticallExtended.abi)
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Cannot be constructed.
|
|
22
|
+
*/
|
|
23
|
+
private constructor() {}
|
|
24
|
+
|
|
25
|
+
public static encodeMulticall(calldatas: string | string[], validation?: Validation): string {
|
|
26
|
+
// if there's no validation, we can just fall back to regular multicall
|
|
27
|
+
if (typeof validation === 'undefined') {
|
|
28
|
+
return Multicall.encodeMulticall(calldatas)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// if there is validation, we have to normalize calldatas
|
|
32
|
+
if (!Array.isArray(calldatas)) {
|
|
33
|
+
calldatas = [calldatas]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// this means the validation value should be a previousBlockhash
|
|
37
|
+
if (typeof validation === 'string' && validation.startsWith('0x')) {
|
|
38
|
+
const previousBlockhash = validateAndParseBytes32(validation)
|
|
39
|
+
return MulticallExtended.INTERFACE.encodeFunctionData('multicall(bytes32,bytes[])', [
|
|
40
|
+
previousBlockhash,
|
|
41
|
+
calldatas,
|
|
42
|
+
])
|
|
43
|
+
} else {
|
|
44
|
+
const deadline = toHex(validation)
|
|
45
|
+
return MulticallExtended.INTERFACE.encodeFunctionData('multicall(uint256,bytes[])', [deadline, calldatas])
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Interface } from '@ethersproject/abi'
|
|
2
|
+
import { Percent, Token, validateAndParseAddress } from '@centurion_project/sdk-core'
|
|
3
|
+
import IPeripheryPaymentsWithFeeExtended from './abis/IPeripheryPaymentsWithFeeExtended.json'
|
|
4
|
+
import { FeeOptions, Payments, toHex } from '@centurion_project/v3-sdk'
|
|
5
|
+
import JSBI from 'jsbi'
|
|
6
|
+
|
|
7
|
+
function encodeFeeBips(fee: Percent): string {
|
|
8
|
+
return toHex(fee.multiply(10_000).quotient)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export abstract class PaymentsExtended {
|
|
12
|
+
public static INTERFACE: Interface = new Interface(IPeripheryPaymentsWithFeeExtended.abi)
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Cannot be constructed.
|
|
16
|
+
*/
|
|
17
|
+
private constructor() {}
|
|
18
|
+
|
|
19
|
+
public static encodeUnwrapWETH9(amountMinimum: JSBI, recipient?: string, feeOptions?: FeeOptions): string {
|
|
20
|
+
// if there's a recipient, just pass it along
|
|
21
|
+
if (typeof recipient === 'string') {
|
|
22
|
+
return Payments.encodeUnwrapWETH9(amountMinimum, recipient, feeOptions)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!!feeOptions) {
|
|
26
|
+
const feeBips = encodeFeeBips(feeOptions.fee)
|
|
27
|
+
const feeRecipient = validateAndParseAddress(feeOptions.recipient)
|
|
28
|
+
|
|
29
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('unwrapWETH9WithFee(uint256,uint256,address)', [
|
|
30
|
+
toHex(amountMinimum),
|
|
31
|
+
feeBips,
|
|
32
|
+
feeRecipient,
|
|
33
|
+
])
|
|
34
|
+
} else {
|
|
35
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('unwrapWETH9(uint256)', [toHex(amountMinimum)])
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public static encodeSweepToken(
|
|
40
|
+
token: Token,
|
|
41
|
+
amountMinimum: JSBI,
|
|
42
|
+
recipient?: string,
|
|
43
|
+
feeOptions?: FeeOptions
|
|
44
|
+
): string {
|
|
45
|
+
// if there's a recipient, just pass it along
|
|
46
|
+
if (typeof recipient === 'string') {
|
|
47
|
+
return Payments.encodeSweepToken(token, amountMinimum, recipient, feeOptions)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!!feeOptions) {
|
|
51
|
+
const feeBips = encodeFeeBips(feeOptions.fee)
|
|
52
|
+
const feeRecipient = validateAndParseAddress(feeOptions.recipient)
|
|
53
|
+
|
|
54
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('sweepTokenWithFee(address,uint256,uint256,address)', [
|
|
55
|
+
token.address,
|
|
56
|
+
toHex(amountMinimum),
|
|
57
|
+
feeBips,
|
|
58
|
+
feeRecipient,
|
|
59
|
+
])
|
|
60
|
+
} else {
|
|
61
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('sweepToken(address,uint256)', [
|
|
62
|
+
token.address,
|
|
63
|
+
toHex(amountMinimum),
|
|
64
|
+
])
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public static encodePull(token: Token, amount: JSBI): string {
|
|
69
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('pull', [token.address, toHex(amount)])
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public static encodeWrapETH(amount: JSBI): string {
|
|
73
|
+
return PaymentsExtended.INTERFACE.encodeFunctionData('wrapETH', [toHex(amount)])
|
|
74
|
+
}
|
|
75
|
+
}
|