@centurion_project/router-sdk 2.11.10 → 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,129 @@
1
+ import { Interface } from '@ethersproject/abi'
2
+ import invariant from 'tiny-invariant'
3
+ import IApproveAndCall from './abis/IApproveAndCall.json'
4
+ import { Currency, Percent, Token } from '@centurion_project/sdk-core'
5
+ import {
6
+ MintSpecificOptions,
7
+ IncreaseSpecificOptions,
8
+ NonfungiblePositionManager,
9
+ Position,
10
+ toHex,
11
+ } from '@centurion_project/v3-sdk'
12
+ import JSBI from 'jsbi'
13
+
14
+ // condensed version of v3-sdk AddLiquidityOptions containing only necessary swap + add attributes
15
+ export type CondensedAddLiquidityOptions = Omit<MintSpecificOptions, 'createPool'> | IncreaseSpecificOptions
16
+
17
+ export enum ApprovalTypes {
18
+ NOT_REQUIRED = 0,
19
+ MAX = 1,
20
+ MAX_MINUS_ONE = 2,
21
+ ZERO_THEN_MAX = 3,
22
+ ZERO_THEN_MAX_MINUS_ONE = 4,
23
+ }
24
+
25
+ // type guard
26
+ export function isMint(options: CondensedAddLiquidityOptions): options is Omit<MintSpecificOptions, 'createPool'> {
27
+ return Object.keys(options).some((k) => k === 'recipient')
28
+ }
29
+
30
+ export abstract class ApproveAndCall {
31
+ public static INTERFACE: Interface = new Interface(IApproveAndCall.abi)
32
+
33
+ /**
34
+ * Cannot be constructed.
35
+ */
36
+ private constructor() {}
37
+
38
+ public static encodeApproveMax(token: Token): string {
39
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMax', [token.address])
40
+ }
41
+
42
+ public static encodeApproveMaxMinusOne(token: Token): string {
43
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveMaxMinusOne', [token.address])
44
+ }
45
+
46
+ public static encodeApproveZeroThenMax(token: Token): string {
47
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMax', [token.address])
48
+ }
49
+
50
+ public static encodeApproveZeroThenMaxMinusOne(token: Token): string {
51
+ return ApproveAndCall.INTERFACE.encodeFunctionData('approveZeroThenMaxMinusOne', [token.address])
52
+ }
53
+
54
+ public static encodeCallPositionManager(calldatas: string[]): string {
55
+ invariant(calldatas.length > 0, 'NULL_CALLDATA')
56
+
57
+ if (calldatas.length === 1) {
58
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', calldatas)
59
+ } else {
60
+ const encodedMulticall = NonfungiblePositionManager.INTERFACE.encodeFunctionData('multicall', [calldatas])
61
+ return ApproveAndCall.INTERFACE.encodeFunctionData('callPositionManager', [encodedMulticall])
62
+ }
63
+ }
64
+ /**
65
+ * Encode adding liquidity to a position in the nft manager contract
66
+ * @param position Forcasted position with expected amount out from swap
67
+ * @param minimalPosition Forcasted position with custom minimal token amounts
68
+ * @param addLiquidityOptions Options for adding liquidity
69
+ * @param slippageTolerance Defines maximum slippage
70
+ */
71
+ public static encodeAddLiquidity(
72
+ position: Position,
73
+ minimalPosition: Position,
74
+ addLiquidityOptions: CondensedAddLiquidityOptions,
75
+ slippageTolerance: Percent
76
+ ): string {
77
+ let { amount0: amount0Min, amount1: amount1Min } = position.mintAmountsWithSlippage(slippageTolerance)
78
+
79
+ // position.mintAmountsWithSlippage() can create amounts not dependenable in scenarios
80
+ // such as range orders. Allow the option to provide a position with custom minimum amounts
81
+ // for these scenarios
82
+ if (JSBI.lessThan(minimalPosition.amount0.quotient, amount0Min)) {
83
+ amount0Min = minimalPosition.amount0.quotient
84
+ }
85
+ if (JSBI.lessThan(minimalPosition.amount1.quotient, amount1Min)) {
86
+ amount1Min = minimalPosition.amount1.quotient
87
+ }
88
+
89
+ if (isMint(addLiquidityOptions)) {
90
+ return ApproveAndCall.INTERFACE.encodeFunctionData('mint', [
91
+ {
92
+ token0: position.pool.token0.address,
93
+ token1: position.pool.token1.address,
94
+ fee: position.pool.fee,
95
+ tickLower: position.tickLower,
96
+ tickUpper: position.tickUpper,
97
+ amount0Min: toHex(amount0Min),
98
+ amount1Min: toHex(amount1Min),
99
+ recipient: addLiquidityOptions.recipient,
100
+ },
101
+ ])
102
+ } else {
103
+ return ApproveAndCall.INTERFACE.encodeFunctionData('increaseLiquidity', [
104
+ {
105
+ token0: position.pool.token0.address,
106
+ token1: position.pool.token1.address,
107
+ amount0Min: toHex(amount0Min),
108
+ amount1Min: toHex(amount1Min),
109
+ tokenId: toHex(addLiquidityOptions.tokenId),
110
+ },
111
+ ])
112
+ }
113
+ }
114
+
115
+ public static encodeApprove(token: Currency, approvalType: ApprovalTypes): string {
116
+ switch (approvalType) {
117
+ case ApprovalTypes.MAX:
118
+ return ApproveAndCall.encodeApproveMax(token.wrapped)
119
+ case ApprovalTypes.MAX_MINUS_ONE:
120
+ return ApproveAndCall.encodeApproveMaxMinusOne(token.wrapped)
121
+ case ApprovalTypes.ZERO_THEN_MAX:
122
+ return ApproveAndCall.encodeApproveZeroThenMax(token.wrapped)
123
+ case ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:
124
+ return ApproveAndCall.encodeApproveZeroThenMaxMinusOne(token.wrapped)
125
+ default:
126
+ throw new Error('Error: invalid ApprovalType')
127
+ }
128
+ }
129
+ }
@@ -0,0 +1,24 @@
1
+ import { Percent } from '@centurion_project/sdk-core'
2
+ import JSBI from 'jsbi'
3
+
4
+ export const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
5
+ export const MSG_SENDER = '0x0000000000000000000000000000000000000001'
6
+ export const ADDRESS_THIS = '0x0000000000000000000000000000000000000002'
7
+
8
+ export const ZERO = JSBI.BigInt(0)
9
+ export const ONE = JSBI.BigInt(1)
10
+
11
+ // = 1 << 23 or 0b0100000000000000000000000
12
+ export const MIXED_QUOTER_V1_V2_FEE_PATH_PLACEHOLDER = 1 << 23
13
+
14
+ // = 10 << 4 or 0b00100000
15
+ export const MIXED_QUOTER_V2_V2_FEE_PATH_PLACEHOLDER = 2 << 4
16
+
17
+ // = 11 << 20 or 0b001100000000000000000000
18
+ export const MIXED_QUOTER_V2_V3_FEE_PATH_PLACEHOLDER = 3 << 20
19
+
20
+ // = 100 << 20 or 0b010000000000000000000000
21
+ export const MIXED_QUOTER_V2_V4_FEE_PATH_PLACEHOLDER = 4 << 20
22
+
23
+ export const ZERO_PERCENT = new Percent(ZERO)
24
+ export const ONE_HUNDRED_PERCENT = new Percent(100, 100)
@@ -0,0 +1,121 @@
1
+ import invariant from 'tiny-invariant'
2
+ import { Currency, Fraction, Price, Token } from '@centurion_project/sdk-core'
3
+ import { Pool as V4Pool } from '@centurion_project/v4-sdk'
4
+ import { getPathCurrency } from '../../utils/pathCurrency'
5
+ import { TPool } from '../../utils/TPool'
6
+
7
+ /**
8
+ * Represents a list of pools or pairs through which a swap can occur
9
+ * @template TInput The input token
10
+ * @template TOutput The output token
11
+ */
12
+ export class MixedRouteSDK<TInput extends Currency, TOutput extends Currency> {
13
+ public readonly pools: TPool[]
14
+ public readonly path: Currency[]
15
+ public readonly input: TInput
16
+ public readonly output: TOutput
17
+ public readonly pathInput: Currency // routes may need to wrap/unwrap a currency to begin trading path
18
+ public readonly pathOutput: Currency // routes may need to wrap/unwrap a currency at the end of trading path
19
+
20
+ private _midPrice: Price<TInput, TOutput> | null = null
21
+
22
+ /**
23
+ * Creates an instance of route.
24
+ * @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
25
+ * @param input The input token
26
+ * @param output The output token
27
+ * @param retainsFakePool Set to true to filter out a pool that has a fake eth-weth pool
28
+ */
29
+ public constructor(pools: TPool[], input: TInput, output: TOutput, retainFakePools = false) {
30
+ pools = retainFakePools ? pools : pools.filter((pool) => !(pool instanceof V4Pool && pool.tickSpacing === 0))
31
+ invariant(pools.length > 0, 'POOLS')
32
+ // there is a pool mismatched to the path if we do not retain the fake eth-weth pools
33
+
34
+ const chainId = pools[0].chainId
35
+ const allOnSameChain = pools.every((pool) => pool.chainId === chainId)
36
+ invariant(allOnSameChain, 'CHAIN_IDS')
37
+
38
+ this.pathInput = getPathCurrency(input, pools[0])
39
+ this.pathOutput = getPathCurrency(output, pools[pools.length - 1])
40
+
41
+ if (!(pools[0] instanceof V4Pool)) {
42
+ invariant(pools[0].involvesToken(this.pathInput as Token), 'INPUT')
43
+ } else {
44
+ invariant((pools[0] as V4Pool).v4InvolvesToken(this.pathInput), 'INPUT')
45
+ }
46
+ const lastPool = pools[pools.length - 1]
47
+ if (lastPool instanceof V4Pool) {
48
+ invariant(lastPool.v4InvolvesToken(output) || lastPool.v4InvolvesToken(output.wrapped), 'OUTPUT')
49
+ } else {
50
+ invariant(lastPool.involvesToken(output.wrapped as Token), 'OUTPUT')
51
+ }
52
+
53
+ /**
54
+ * Normalizes token0-token1 order and selects the next token/fee step to add to the path
55
+ * */
56
+ const tokenPath: Currency[] = [this.pathInput]
57
+ pools[0].token0.equals(this.pathInput) ? tokenPath.push(pools[0].token1) : tokenPath.push(pools[0].token0)
58
+
59
+ for (let i = 1; i < pools.length; i++) {
60
+ const pool = pools[i]
61
+ const inputToken = tokenPath[i]
62
+
63
+ let outputToken
64
+ if (
65
+ // we hit an edge case if it's a v4 pool and neither of the tokens are in the pool OR it is not a v4 pool but the input currency is eth
66
+ (pool instanceof V4Pool && !pool.involvesToken(inputToken)) ||
67
+ (!(pool instanceof V4Pool) && inputToken.isNative)
68
+ ) {
69
+ // We handle the case where the inputToken =/= pool.token0 or pool.token1. There are 2 specific cases.
70
+ if (inputToken.equals(pool.token0.wrapped)) {
71
+ // 1) the inputToken is WETH and the current pool has ETH
72
+ // for example, pools: USDC-WETH, ETH-PEPE, path: USDC, WETH, PEPE
73
+ // second pool is a v4 pool, the first could be any version
74
+ outputToken = pool.token1
75
+ } else if (inputToken.wrapped.equals(pool.token0) || inputToken.wrapped.equals(pool.token1)) {
76
+ // 2) the inputToken is ETH and the current pool has WETH
77
+ // for example, pools: USDC-ETH, WETH-PEPE, path: USDC, ETH, PEPE
78
+ // first pool is a v4 pool, the second could be any version
79
+ outputToken = inputToken.wrapped.equals(pool.token0) ? pool.token1 : pool.token0
80
+ } else {
81
+ throw new Error(`POOL_MISMATCH pool: ${JSON.stringify(pool)} inputToken: ${JSON.stringify(inputToken)}`)
82
+ }
83
+ } else {
84
+ // then the input token must equal either token0 or token1
85
+ invariant(
86
+ inputToken.equals(pool.token0) || inputToken.equals(pool.token1),
87
+ `PATH pool ${JSON.stringify(pool)} inputToken ${JSON.stringify(inputToken)}`
88
+ )
89
+ outputToken = inputToken.equals(pool.token0) ? pool.token1 : pool.token0
90
+ }
91
+ tokenPath.push(outputToken)
92
+ }
93
+
94
+ this.pools = pools
95
+ this.path = tokenPath
96
+ this.input = input
97
+ this.output = output ?? tokenPath[tokenPath.length - 1]
98
+ }
99
+
100
+ public get chainId(): number {
101
+ return this.pools[0].chainId
102
+ }
103
+
104
+ /**
105
+ * Returns the mid price of the route
106
+ */
107
+ public get midPrice(): Price<TInput, TOutput> {
108
+ if (this._midPrice !== null) return this._midPrice
109
+
110
+ // this.path[i + 1] is always pool i's own currency object (the constructor pushes the
111
+ // pool's currency even when bridging a native/wrapped boundary), so exact equality is
112
+ // enough to orient each pool's price along the already-resolved path
113
+ const price = this.pools.reduce(
114
+ (price, pool, i) =>
115
+ price.multiply((this.path[i + 1].equals(pool.token1) ? pool.token0Price : pool.token1Price).asFraction),
116
+ new Fraction(1)
117
+ )
118
+
119
+ return (this._midPrice = new Price(this.input, this.output, price.denominator, price.numerator))
120
+ }
121
+ }