@centurion_project/router-sdk 2.11.11 → 2.11.12

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.
Files changed (58) hide show
  1. package/dist/cjs/src/entities/mixedRoute/route.js +4 -19
  2. package/dist/cjs/src/entities/mixedRoute/route.js.map +1 -1
  3. package/dist/cjs/src/utils/index.js +19 -7
  4. package/dist/cjs/src/utils/index.js.map +1 -1
  5. package/dist/esm/src/entities/mixedRoute/route.js +5 -20
  6. package/dist/esm/src/entities/mixedRoute/route.js.map +1 -1
  7. package/dist/esm/src/utils/index.js +19 -7
  8. package/dist/esm/src/utils/index.js.map +1 -1
  9. package/dist/types/src/approveAndCall.d.ts +1 -0
  10. package/dist/types/src/approveAndCall.d.ts.map +1 -0
  11. package/dist/types/src/constants.d.ts +1 -0
  12. package/dist/types/src/constants.d.ts.map +1 -0
  13. package/dist/types/src/entities/mixedRoute/route.d.ts +1 -0
  14. package/dist/types/src/entities/mixedRoute/route.d.ts.map +1 -0
  15. package/dist/types/src/entities/mixedRoute/trade.d.ts +1 -0
  16. package/dist/types/src/entities/mixedRoute/trade.d.ts.map +1 -0
  17. package/dist/types/src/entities/protocol.d.ts +1 -0
  18. package/dist/types/src/entities/protocol.d.ts.map +1 -0
  19. package/dist/types/src/entities/route.d.ts +1 -0
  20. package/dist/types/src/entities/route.d.ts.map +1 -0
  21. package/dist/types/src/entities/trade.d.ts +1 -0
  22. package/dist/types/src/entities/trade.d.ts.map +1 -0
  23. package/dist/types/src/index.d.ts +1 -0
  24. package/dist/types/src/index.d.ts.map +1 -0
  25. package/dist/types/src/multicallExtended.d.ts +1 -0
  26. package/dist/types/src/multicallExtended.d.ts.map +1 -0
  27. package/dist/types/src/paymentsExtended.d.ts +1 -0
  28. package/dist/types/src/paymentsExtended.d.ts.map +1 -0
  29. package/dist/types/src/swapRouter.d.ts +1 -0
  30. package/dist/types/src/swapRouter.d.ts.map +1 -0
  31. package/dist/types/src/utils/TPool.d.ts +1 -0
  32. package/dist/types/src/utils/TPool.d.ts.map +1 -0
  33. package/dist/types/src/utils/encodeMixedRouteToPath.d.ts +1 -0
  34. package/dist/types/src/utils/encodeMixedRouteToPath.d.ts.map +1 -0
  35. package/dist/types/src/utils/index.d.ts +1 -0
  36. package/dist/types/src/utils/index.d.ts.map +1 -0
  37. package/dist/types/src/utils/pathCurrency.d.ts +1 -0
  38. package/dist/types/src/utils/pathCurrency.d.ts.map +1 -0
  39. package/package.json +12 -9
  40. package/src/abis/IApproveAndCall.json +213 -0
  41. package/src/abis/IMulticallExtended.json +78 -0
  42. package/src/abis/IPeripheryPaymentsWithFeeExtended.json +233 -0
  43. package/src/abis/ISwapRouter02.json +717 -0
  44. package/src/approveAndCall.ts +129 -0
  45. package/src/constants.ts +24 -0
  46. package/src/entities/mixedRoute/route.ts +121 -0
  47. package/src/entities/mixedRoute/trade.ts +530 -0
  48. package/src/entities/protocol.ts +6 -0
  49. package/src/entities/route.ts +94 -0
  50. package/src/entities/trade.ts +550 -0
  51. package/src/index.ts +14 -0
  52. package/src/multicallExtended.ts +48 -0
  53. package/src/paymentsExtended.ts +75 -0
  54. package/src/swapRouter.ts +700 -0
  55. package/src/utils/TPool.ts +5 -0
  56. package/src/utils/encodeMixedRouteToPath.ts +112 -0
  57. package/src/utils/index.ts +66 -0
  58. package/src/utils/pathCurrency.ts +36 -0
@@ -0,0 +1,112 @@
1
+ import { pack } from '@ethersproject/solidity'
2
+ import { Currency } from '@centurion_project/sdk-core'
3
+ import { Pair } from '@centurion_project/v2-sdk'
4
+ import { Pool as V3Pool } from '@centurion_project/v3-sdk'
5
+ import { Pool as V4Pool } from '@centurion_project/v4-sdk'
6
+ import {
7
+ ADDRESS_ZERO,
8
+ MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER,
9
+ MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER,
10
+ MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER,
11
+ MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER,
12
+ } from '../constants'
13
+ import { MixedRouteSDK } from '../entities/mixedRoute/route'
14
+ import { TPool } from './TPool'
15
+
16
+ /**
17
+ * Converts a route to a hex encoded path
18
+ * @notice only supports exactIn route encodings
19
+ * @param route the mixed path to convert to an encoded path
20
+ * @param useMixedRouterQuoteV2 if true, uses the Mixed Quoter V2 encoding for v4 pools. By default, we do not set it. This is only used in SOR for explicit setting during onchain quoting.
21
+ * @returns the exactIn encoded path
22
+ */
23
+ export function encodeMixedRouteToPath(
24
+ route: MixedRouteSDK<Currency, Currency>,
25
+ useMixedRouterQuoteV2?: boolean
26
+ ): string {
27
+ const containsV4Pool = useMixedRouterQuoteV2 ?? route.pools.some((pool) => pool instanceof V4Pool)
28
+
29
+ let path: (string | number)[]
30
+ let types: string[]
31
+
32
+ if (containsV4Pool) {
33
+ path = [route.pathInput.isNative ? ADDRESS_ZERO : route.pathInput.address]
34
+ types = ['address']
35
+ let currencyIn = route.pathInput
36
+
37
+ for (const pool of route.pools) {
38
+ const currencyOut = currencyIn.equals(pool.token0) ? pool.token1 : pool.token0
39
+
40
+ if (pool instanceof V4Pool) {
41
+ // a tickSpacing of 0 indicates a "fake" v4 pool where the quote actually requires a wrap or unwrap
42
+ // the fake v4 pool will always have native as token0 and wrapped native as token1
43
+ if (pool.tickSpacing === 0) {
44
+ const wrapOrUnwrapEncoding = 0
45
+ path.push(wrapOrUnwrapEncoding, currencyOut.isNative ? ADDRESS_ZERO : currencyOut.wrapped.address)
46
+ types.push('uint8', 'address')
47
+ } else {
48
+ const v4Fee = pool.fee + MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER
49
+ path.push(
50
+ v4Fee,
51
+ pool.tickSpacing,
52
+ pool.hooks,
53
+ currencyOut.isNative ? ADDRESS_ZERO : currencyOut.wrapped.address
54
+ )
55
+ types.push('uint24', 'uint24', 'address', 'address')
56
+ }
57
+ } else if (pool instanceof V3Pool) {
58
+ const v3Fee = pool.fee + MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER
59
+ path.push(v3Fee, currencyOut.wrapped.address)
60
+ types.push('uint24', 'address')
61
+ } else if (pool instanceof Pair) {
62
+ const v2Fee = MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER
63
+ path.push(v2Fee, currencyOut.wrapped.address)
64
+ types.push('uint8', 'address')
65
+ } else {
66
+ throw new Error(`Unsupported pool type ${JSON.stringify(pool)}`)
67
+ }
68
+
69
+ currencyIn = currencyOut
70
+ }
71
+ } else {
72
+ // TODO: ROUTE-276 - delete this else block
73
+ // We introduced this else block as a safety measure to prevent non-v4 mixed routes from potentially regressing
74
+ // We'd like to gain more confidence in the new implementation before removing this block
75
+ const result = route.pools.reduce(
76
+ (
77
+ { inputToken, path, types }: { inputToken: Currency; path: (string | number)[]; types: string[] },
78
+ pool: TPool,
79
+ index
80
+ ): { inputToken: Currency; path: (string | number)[]; types: string[] } => {
81
+ const outputToken: Currency = pool.token0.equals(inputToken) ? pool.token1 : pool.token0
82
+ if (index === 0) {
83
+ return {
84
+ inputToken: outputToken,
85
+ types: ['address', 'uint24', 'address'],
86
+ path: [
87
+ inputToken.wrapped.address,
88
+ pool instanceof V3Pool ? pool.fee : MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER,
89
+ outputToken.wrapped.address,
90
+ ],
91
+ }
92
+ } else {
93
+ return {
94
+ inputToken: outputToken,
95
+ types: [...types, 'uint24', 'address'],
96
+ path: [
97
+ ...path,
98
+ pool instanceof V3Pool ? pool.fee : MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER,
99
+ outputToken.wrapped.address,
100
+ ],
101
+ }
102
+ }
103
+ },
104
+ { inputToken: route.input, path: [], types: [] }
105
+ )
106
+
107
+ path = result.path
108
+ types = result.types
109
+ }
110
+
111
+ return pack(types, path)
112
+ }
@@ -0,0 +1,66 @@
1
+ import { Currency } from '@centurion_project/sdk-core'
2
+ import { Pair } from '@centurion_project/v2-sdk'
3
+ import { Pool as V3Pool } from '@centurion_project/v3-sdk'
4
+ import { Pool as V4Pool } from '@centurion_project/v4-sdk'
5
+ import { MixedRouteSDK } from '../entities/mixedRoute/route'
6
+ import { TPool } from './TPool'
7
+
8
+ // `Pair.involvesToken` and `V3Pool.involvesToken` only accept a `Token`, so calling `involvesToken`
9
+ // on the `TPool` union with a bare `Currency` (e.g. native ETH from `route.path`) fails to
10
+ // typecheck. This checks the same token0/token1 membership but against any `Currency`.
11
+ const poolInvolvesCurrency = (pool: TPool, currency: Currency): boolean =>
12
+ pool.token0.equals(currency) || pool.token1.equals(currency)
13
+
14
+ /**
15
+ * Utility function to return each consecutive section of Pools or Pairs in a MixedRoute
16
+ * @param route
17
+ * @returns a nested array of Pools or Pairs in the order of the route
18
+ */
19
+ export const partitionMixedRouteByProtocol = (route: MixedRouteSDK<Currency, Currency>): TPool[][] => {
20
+ let acc = []
21
+
22
+ let left = 0
23
+ let right = 0
24
+ while (right < route.pools.length) {
25
+ if (
26
+ (route.pools[left] instanceof V4Pool && !(route.pools[right] instanceof V4Pool)) ||
27
+ (route.pools[left] instanceof V3Pool && !(route.pools[right] instanceof V3Pool)) ||
28
+ (route.pools[left] instanceof Pair && !(route.pools[right] instanceof Pair)) ||
29
+ // a native/wrapped boundary (e.g. a native-ETH v4 pool followed by a WETH v4 pool) needs a
30
+ // wrap/unwrap between sections, so it ends the section even within a single protocol
31
+ !poolInvolvesCurrency(route.pools[right], route.path[right])
32
+ ) {
33
+ acc.push(route.pools.slice(left, right))
34
+ left = right
35
+ }
36
+ // seek forward with right pointer
37
+ right++
38
+ if (right === route.pools.length) {
39
+ /// we reached the end, take the rest
40
+ acc.push(route.pools.slice(left, right))
41
+ }
42
+ }
43
+ return acc
44
+ }
45
+
46
+ /**
47
+ * Simple utility function to get the output of an array of Pools or Pairs
48
+ * @param pools
49
+ * @param firstInputToken
50
+ * @returns the output token of the last pool in the array
51
+ */
52
+ export const getOutputOfPools = (pools: TPool[], firstInputToken: Currency): Currency => {
53
+ const { inputToken: outputToken } = pools.reduce(
54
+ ({ inputToken }, pool: TPool): { inputToken: Currency } => {
55
+ // exact matches take priority so genuine ETH/WETH pools resolve to the correct side;
56
+ // the wrapped comparisons bridge native/wrapped boundaries the same way MixedRouteSDK does
57
+ if (pool.token0.equals(inputToken)) return { inputToken: pool.token1 }
58
+ if (pool.token1.equals(inputToken)) return { inputToken: pool.token0 }
59
+ if (pool.token0.wrapped.equals(inputToken.wrapped)) return { inputToken: pool.token1 }
60
+ if (pool.token1.wrapped.equals(inputToken.wrapped)) return { inputToken: pool.token0 }
61
+ throw new Error('PATH')
62
+ },
63
+ { inputToken: firstInputToken }
64
+ )
65
+ return outputToken
66
+ }
@@ -0,0 +1,36 @@
1
+ import { Currency, CurrencyAmount, Token } from '@centurion_project/sdk-core'
2
+ import { Pool as V4Pool } from '@centurion_project/v4-sdk'
3
+ import { TPool } from './TPool'
4
+
5
+ export function amountWithPathCurrency(amount: CurrencyAmount<Currency>, pool: TPool): CurrencyAmount<Currency> {
6
+ return CurrencyAmount.fromFractionalAmount(
7
+ getPathCurrency(amount.currency, pool),
8
+ amount.numerator,
9
+ amount.denominator
10
+ )
11
+ }
12
+
13
+ export function getPathCurrency(currency: Currency, pool: TPool): Currency {
14
+ // return currency if the currency matches a currency of the pool
15
+ if (pool.involvesToken(currency as Token)) {
16
+ return currency
17
+
18
+ // return if currency.wrapped if pool involves wrapped currency
19
+ } else if (pool.involvesToken(currency.wrapped as Token)) {
20
+ return currency.wrapped
21
+
22
+ // return native currency if pool involves native version of wrapped currency (only applies to V4)
23
+ } else if (pool instanceof V4Pool) {
24
+ if (pool.token0.wrapped.equals(currency)) {
25
+ return pool.token0
26
+ } else if (pool.token1.wrapped.equals(currency)) {
27
+ return pool.token1
28
+ }
29
+
30
+ // otherwise the token is invalid
31
+ } else {
32
+ throw new Error(`Expected currency ${currency.symbol} to be either ${pool.token0.symbol} or ${pool.token1.symbol}`)
33
+ }
34
+
35
+ return currency // this line needed for typescript to compile
36
+ }