@luxfi/dex 1.2.1 → 1.3.0
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/client/clob.d.ts +52 -0
- package/dist/client/clob.d.ts.map +1 -0
- package/dist/client/clob.js +196 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +6 -0
- package/dist/client/types.d.ts +126 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +5 -0
- package/dist/hooks/index.d.ts +7 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +6 -0
- package/dist/hooks/use-quote.d.ts +18 -0
- package/dist/hooks/use-quote.d.ts.map +1 -0
- package/dist/hooks/use-quote.js +65 -0
- package/dist/hooks/use-swap.d.ts +17 -0
- package/dist/hooks/use-swap.d.ts.map +1 -0
- package/dist/hooks/use-swap.js +75 -0
- package/dist/index.d.ts +33 -115
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +41 -225
- package/dist/precompile/abis.d.ts +400 -0
- package/dist/precompile/abis.d.ts.map +1 -0
- package/dist/precompile/abis.js +287 -0
- package/dist/precompile/addresses.d.ts +65 -0
- package/dist/precompile/addresses.d.ts.map +1 -0
- package/dist/precompile/addresses.js +52 -0
- package/dist/precompile/index.d.ts +8 -0
- package/dist/precompile/index.d.ts.map +1 -0
- package/dist/precompile/index.js +7 -0
- package/dist/precompile/types.d.ts +76 -0
- package/dist/precompile/types.d.ts.map +1 -0
- package/dist/precompile/types.js +17 -0
- package/dist/router/index.d.ts +7 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +6 -0
- package/dist/router/router.d.ts +58 -0
- package/dist/router/router.d.ts.map +1 -0
- package/dist/router/router.js +272 -0
- package/dist/router/types.d.ts +76 -0
- package/dist/router/types.d.ts.map +1 -0
- package/dist/router/types.js +1 -0
- package/package.json +55 -29
- package/src/client/clob.ts +256 -0
- package/src/client/index.ts +6 -0
- package/src/client/types.ts +148 -0
- package/src/hooks/index.ts +6 -0
- package/src/hooks/use-quote.ts +92 -0
- package/src/hooks/use-swap.ts +103 -0
- package/src/index.ts +76 -309
- package/src/precompile/abis.ts +291 -0
- package/src/precompile/addresses.ts +72 -0
- package/src/precompile/index.ts +7 -0
- package/src/precompile/types.ts +96 -0
- package/src/router/index.ts +6 -0
- package/src/router/router.ts +338 -0
- package/src/router/types.ts +87 -0
- package/dist/marketData.d.ts +0 -152
- package/dist/marketData.d.ts.map +0 -1
- package/dist/marketData.js +0 -253
- package/src/marketData.ts +0 -351
- package/tsconfig.json +0 -19
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DEX Precompile Addresses
|
|
3
|
+
* Native Go implementation at EVM level
|
|
4
|
+
*
|
|
5
|
+
* Lux Precompile Address Standard:
|
|
6
|
+
* - Prefix format: 0xNNNN000000000000000000000000000000000000
|
|
7
|
+
* - Range 0x0400-0x04FF is reserved for DEX precompiles
|
|
8
|
+
*
|
|
9
|
+
* @see ~/work/lux/precompile/dex/module.go for implementation
|
|
10
|
+
*/
|
|
11
|
+
import type { Address } from 'viem'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* DEX Precompile contract addresses
|
|
15
|
+
* These are native precompiles, not deployed contracts
|
|
16
|
+
*/
|
|
17
|
+
export const DEX_PRECOMPILES = {
|
|
18
|
+
/**
|
|
19
|
+
* PoolManager (0x0400)
|
|
20
|
+
* Singleton managing all liquidity pools
|
|
21
|
+
* - Initialize pools
|
|
22
|
+
* - Execute swaps
|
|
23
|
+
* - Modify liquidity
|
|
24
|
+
* - Flash accounting settlement
|
|
25
|
+
*/
|
|
26
|
+
POOL_MANAGER: '0x0400000000000000000000000000000000000000' as Address,
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* SwapRouter (0x0401)
|
|
30
|
+
* Optimized swap routing
|
|
31
|
+
* - exactInputSingle / exactOutputSingle
|
|
32
|
+
* - Multi-hop swaps
|
|
33
|
+
* - Native LUX support
|
|
34
|
+
*/
|
|
35
|
+
SWAP_ROUTER: '0x0401000000000000000000000000000000000000' as Address,
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* HooksRegistry (0x0402)
|
|
39
|
+
* Hook contract registry
|
|
40
|
+
* - Register hook contracts
|
|
41
|
+
* - Query hook permissions
|
|
42
|
+
*/
|
|
43
|
+
HOOKS_REGISTRY: '0x0402000000000000000000000000000000000000' as Address,
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* FlashLoan (0x0403)
|
|
47
|
+
* Flash loan facility
|
|
48
|
+
* - Borrow any token
|
|
49
|
+
* - Repay in same transaction
|
|
50
|
+
*/
|
|
51
|
+
FLASH_LOAN: '0x0403000000000000000000000000000000000000' as Address,
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Lending (0x0410)
|
|
55
|
+
* Lending protocol
|
|
56
|
+
*/
|
|
57
|
+
LENDING: '0x0410000000000000000000000000000000000000' as Address,
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Liquid (0x0430)
|
|
61
|
+
* Liquid staking vaults
|
|
62
|
+
*/
|
|
63
|
+
LIQUID: '0x0430000000000000000000000000000000000000' as Address,
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Teleport (0x0440)
|
|
67
|
+
* Cross-chain bridge
|
|
68
|
+
*/
|
|
69
|
+
TELEPORT: '0x0440000000000000000000000000000000000000' as Address,
|
|
70
|
+
} as const
|
|
71
|
+
|
|
72
|
+
export type DexPrecompile = keyof typeof DEX_PRECOMPILES
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DEX Precompile Types
|
|
3
|
+
* Native Uniswap v4-style AMM implementation
|
|
4
|
+
*/
|
|
5
|
+
import type { Address } from 'viem'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Currency type - address(0) = native LUX
|
|
9
|
+
*/
|
|
10
|
+
export interface Currency {
|
|
11
|
+
address: Address
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Pool key uniquely identifies a pool
|
|
16
|
+
*/
|
|
17
|
+
export interface PoolKey {
|
|
18
|
+
currency0: Address // Lower address token (sorted)
|
|
19
|
+
currency1: Address // Higher address token (sorted)
|
|
20
|
+
fee: number // Fee in basis points (3000 = 0.30%)
|
|
21
|
+
tickSpacing: number // Tick spacing for concentrated liquidity
|
|
22
|
+
hooks: Address // Hook contract (address(0) = no hooks)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Balance delta from swap/liquidity operations
|
|
27
|
+
* Positive = user owes pool, Negative = pool owes user
|
|
28
|
+
*/
|
|
29
|
+
export interface BalanceDelta {
|
|
30
|
+
amount0: bigint
|
|
31
|
+
amount1: bigint
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Swap parameters
|
|
36
|
+
*/
|
|
37
|
+
export interface SwapParams {
|
|
38
|
+
zeroForOne: boolean // true = swap token0 for token1
|
|
39
|
+
amountSpecified: bigint // Positive = exact input, Negative = exact output
|
|
40
|
+
sqrtPriceLimitX96: bigint // Price limit (0 = no limit)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Modify liquidity parameters
|
|
45
|
+
*/
|
|
46
|
+
export interface ModifyLiquidityParams {
|
|
47
|
+
tickLower: number
|
|
48
|
+
tickUpper: number
|
|
49
|
+
liquidityDelta: bigint
|
|
50
|
+
salt: `0x${string}`
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Pool state
|
|
55
|
+
*/
|
|
56
|
+
export interface PoolState {
|
|
57
|
+
sqrtPriceX96: bigint
|
|
58
|
+
tick: number
|
|
59
|
+
protocolFee: number
|
|
60
|
+
lpFee: number
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Position info
|
|
65
|
+
*/
|
|
66
|
+
export interface Position {
|
|
67
|
+
liquidity: bigint
|
|
68
|
+
feeGrowthInside0LastX128: bigint
|
|
69
|
+
feeGrowthInside1LastX128: bigint
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Native LUX currency constant
|
|
74
|
+
*/
|
|
75
|
+
export const NATIVE_LUX: Address = '0x0000000000000000000000000000000000000000'
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Sort currencies for pool key creation
|
|
79
|
+
*/
|
|
80
|
+
export function sortCurrencies(a: Address, b: Address): [Address, Address] {
|
|
81
|
+
return a.toLowerCase() < b.toLowerCase() ? [a, b] : [b, a]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Create a pool key from two currencies
|
|
86
|
+
*/
|
|
87
|
+
export function createPoolKey(
|
|
88
|
+
tokenA: Address,
|
|
89
|
+
tokenB: Address,
|
|
90
|
+
fee: number = 3000,
|
|
91
|
+
tickSpacing: number = 60,
|
|
92
|
+
hooks: Address = '0x0000000000000000000000000000000000000000'
|
|
93
|
+
): PoolKey {
|
|
94
|
+
const [currency0, currency1] = sortCurrencies(tokenA, tokenB)
|
|
95
|
+
return { currency0, currency1, fee, tickSpacing, hooks }
|
|
96
|
+
}
|
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Omnichain Router
|
|
3
|
+
* Routes orders between CLOB and AMM for best execution
|
|
4
|
+
*/
|
|
5
|
+
import type { Address, PublicClient } from 'viem'
|
|
6
|
+
import { DEX_PRECOMPILES } from '../precompile/addresses'
|
|
7
|
+
import { POOL_MANAGER_ABI, SWAP_ROUTER_ABI } from '../precompile/abis'
|
|
8
|
+
import { createPoolKey, type PoolKey } from '../precompile/types'
|
|
9
|
+
import type { ICLOBClient } from '../client/types'
|
|
10
|
+
import type {
|
|
11
|
+
RouterConfig,
|
|
12
|
+
QuoteRequest,
|
|
13
|
+
Quote,
|
|
14
|
+
RouteStep,
|
|
15
|
+
SwapRequest,
|
|
16
|
+
SwapResult,
|
|
17
|
+
} from './types'
|
|
18
|
+
|
|
19
|
+
const DEFAULT_CONFIG: RouterConfig = {
|
|
20
|
+
clobEnabled: true,
|
|
21
|
+
ammEnabled: true,
|
|
22
|
+
maxHops: 3,
|
|
23
|
+
preferCLOB: false,
|
|
24
|
+
hybridEnabled: true,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Omnichain Router
|
|
29
|
+
* Best execution routing between CLOB and AMM
|
|
30
|
+
*/
|
|
31
|
+
export class OmnichainRouter {
|
|
32
|
+
private config: RouterConfig
|
|
33
|
+
private publicClient: PublicClient | null = null
|
|
34
|
+
private clobClient: ICLOBClient | null = null
|
|
35
|
+
|
|
36
|
+
constructor(config: Partial<RouterConfig> = {}) {
|
|
37
|
+
this.config = { ...DEFAULT_CONFIG, ...config }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Set the public client for AMM interactions
|
|
42
|
+
*/
|
|
43
|
+
setPublicClient(client: PublicClient) {
|
|
44
|
+
this.publicClient = client
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Set the CLOB client
|
|
49
|
+
*/
|
|
50
|
+
setCLOBClient(client: ICLOBClient) {
|
|
51
|
+
this.clobClient = client
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get a quote for a swap
|
|
56
|
+
*/
|
|
57
|
+
async getQuote(request: QuoteRequest): Promise<Quote> {
|
|
58
|
+
const {
|
|
59
|
+
tokenIn,
|
|
60
|
+
tokenOut,
|
|
61
|
+
amountIn,
|
|
62
|
+
slippageTolerance = 50, // 0.5%
|
|
63
|
+
preferredSource,
|
|
64
|
+
} = request
|
|
65
|
+
|
|
66
|
+
const quotes: Quote[] = []
|
|
67
|
+
|
|
68
|
+
// Get AMM quote if enabled
|
|
69
|
+
if (this.config.ammEnabled && (!preferredSource || preferredSource !== 'clob')) {
|
|
70
|
+
try {
|
|
71
|
+
const ammQuote = await this.getAMMQuote(tokenIn, tokenOut, amountIn)
|
|
72
|
+
if (ammQuote) quotes.push(ammQuote)
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.warn('AMM quote failed:', error)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Get CLOB quote if enabled
|
|
79
|
+
if (this.config.clobEnabled && this.clobClient && (!preferredSource || preferredSource !== 'amm')) {
|
|
80
|
+
try {
|
|
81
|
+
const clobQuote = await this.getCLOBQuote(tokenIn, tokenOut, amountIn)
|
|
82
|
+
if (clobQuote) quotes.push(clobQuote)
|
|
83
|
+
} catch (error) {
|
|
84
|
+
console.warn('CLOB quote failed:', error)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (quotes.length === 0) {
|
|
89
|
+
throw new Error('No quotes available')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Select best quote (highest output)
|
|
93
|
+
const bestQuote = quotes.reduce((best, quote) =>
|
|
94
|
+
quote.amountOut > best.amountOut ? quote : best
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
// Apply slippage tolerance
|
|
98
|
+
const minimumAmountOut = bestQuote.amountOut - (bestQuote.amountOut * BigInt(slippageTolerance)) / 10000n
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
...bestQuote,
|
|
102
|
+
minimumAmountOut,
|
|
103
|
+
validUntil: Date.now() + 30000, // 30 seconds
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get AMM quote using precompiles
|
|
109
|
+
*/
|
|
110
|
+
private async getAMMQuote(
|
|
111
|
+
tokenIn: Address,
|
|
112
|
+
tokenOut: Address,
|
|
113
|
+
amountIn: bigint
|
|
114
|
+
): Promise<Quote | null> {
|
|
115
|
+
if (!this.publicClient) return null
|
|
116
|
+
|
|
117
|
+
try {
|
|
118
|
+
// Create pool key
|
|
119
|
+
const poolKey = createPoolKey(tokenIn, tokenOut)
|
|
120
|
+
const zeroForOne = tokenIn.toLowerCase() < tokenOut.toLowerCase()
|
|
121
|
+
|
|
122
|
+
// Get pool state
|
|
123
|
+
const slot0 = await this.publicClient.readContract({
|
|
124
|
+
address: DEX_PRECOMPILES.POOL_MANAGER,
|
|
125
|
+
abi: POOL_MANAGER_ABI,
|
|
126
|
+
functionName: 'getSlot0',
|
|
127
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
128
|
+
args: [poolKey] as any,
|
|
129
|
+
}) as [bigint, number, number, number]
|
|
130
|
+
|
|
131
|
+
if (slot0[0] === 0n) {
|
|
132
|
+
return null // Pool doesn't exist
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Calculate expected output (simplified - real implementation would use tick math)
|
|
136
|
+
// For now, use a simple estimate based on sqrt price
|
|
137
|
+
const sqrtPriceX96 = slot0[0]
|
|
138
|
+
const price = (sqrtPriceX96 * sqrtPriceX96) / (2n ** 192n)
|
|
139
|
+
const amountOut = zeroForOne
|
|
140
|
+
? (amountIn * price) / (10n ** 18n)
|
|
141
|
+
: (amountIn * (10n ** 18n)) / price
|
|
142
|
+
|
|
143
|
+
// Estimate price impact (simplified)
|
|
144
|
+
const priceImpact = Number((amountIn * 10n) / (amountIn + amountOut))
|
|
145
|
+
|
|
146
|
+
const step: RouteStep = {
|
|
147
|
+
source: 'amm',
|
|
148
|
+
tokenIn,
|
|
149
|
+
tokenOut,
|
|
150
|
+
amountIn,
|
|
151
|
+
amountOut,
|
|
152
|
+
pool: DEX_PRECOMPILES.POOL_MANAGER,
|
|
153
|
+
fee: (amountIn * BigInt(poolKey.fee)) / 1_000_000n,
|
|
154
|
+
priceImpact,
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
tokenIn,
|
|
159
|
+
tokenOut,
|
|
160
|
+
amountIn,
|
|
161
|
+
amountOut,
|
|
162
|
+
minimumAmountOut: amountOut,
|
|
163
|
+
route: [step],
|
|
164
|
+
priceImpact,
|
|
165
|
+
estimatedGas: 150_000n,
|
|
166
|
+
validUntil: Date.now() + 30000,
|
|
167
|
+
}
|
|
168
|
+
} catch (error) {
|
|
169
|
+
console.error('AMM quote error:', error)
|
|
170
|
+
return null
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Get CLOB quote
|
|
176
|
+
*/
|
|
177
|
+
private async getCLOBQuote(
|
|
178
|
+
tokenIn: Address,
|
|
179
|
+
tokenOut: Address,
|
|
180
|
+
amountIn: bigint
|
|
181
|
+
): Promise<Quote | null> {
|
|
182
|
+
if (!this.clobClient || !this.clobClient.isConnected()) return null
|
|
183
|
+
|
|
184
|
+
try {
|
|
185
|
+
// Convert addresses to symbol (e.g., "LUX-USDT")
|
|
186
|
+
const symbol = this.getSymbol(tokenIn, tokenOut)
|
|
187
|
+
if (!symbol) return null
|
|
188
|
+
|
|
189
|
+
const orderBook = await this.clobClient.getOrderBook(symbol, 50)
|
|
190
|
+
|
|
191
|
+
// Calculate output by walking the order book
|
|
192
|
+
let remainingIn = amountIn
|
|
193
|
+
let totalOut = 0n
|
|
194
|
+
const side = tokenIn.toLowerCase() < tokenOut.toLowerCase() ? 'asks' : 'bids'
|
|
195
|
+
const levels = side === 'asks' ? orderBook.asks : orderBook.bids
|
|
196
|
+
|
|
197
|
+
for (const level of levels) {
|
|
198
|
+
if (remainingIn <= 0n) break
|
|
199
|
+
|
|
200
|
+
const levelSize = BigInt(Math.floor(level.size * 1e18))
|
|
201
|
+
const levelPrice = BigInt(Math.floor(level.price * 1e18))
|
|
202
|
+
|
|
203
|
+
const fillSize = remainingIn < levelSize ? remainingIn : levelSize
|
|
204
|
+
const fillOut = side === 'asks'
|
|
205
|
+
? (fillSize * (10n ** 18n)) / levelPrice
|
|
206
|
+
: (fillSize * levelPrice) / (10n ** 18n)
|
|
207
|
+
|
|
208
|
+
totalOut += fillOut
|
|
209
|
+
remainingIn -= fillSize
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (totalOut === 0n) return null
|
|
213
|
+
|
|
214
|
+
// Calculate price impact
|
|
215
|
+
const avgPrice = (amountIn * (10n ** 18n)) / totalOut
|
|
216
|
+
const bestPrice = BigInt(Math.floor(levels[0]?.price * 1e18 || 0))
|
|
217
|
+
const priceImpact = bestPrice > 0n
|
|
218
|
+
? Number(((avgPrice - bestPrice) * 10000n) / bestPrice)
|
|
219
|
+
: 0
|
|
220
|
+
|
|
221
|
+
const step: RouteStep = {
|
|
222
|
+
source: 'clob',
|
|
223
|
+
tokenIn,
|
|
224
|
+
tokenOut,
|
|
225
|
+
amountIn,
|
|
226
|
+
amountOut: totalOut,
|
|
227
|
+
symbol,
|
|
228
|
+
fee: (amountIn * 30n) / 10000n, // 0.3% maker fee estimate
|
|
229
|
+
priceImpact,
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
tokenIn,
|
|
234
|
+
tokenOut,
|
|
235
|
+
amountIn,
|
|
236
|
+
amountOut: totalOut,
|
|
237
|
+
minimumAmountOut: totalOut,
|
|
238
|
+
route: [step],
|
|
239
|
+
priceImpact,
|
|
240
|
+
estimatedGas: 50_000n, // CLOB is off-chain
|
|
241
|
+
validUntil: Date.now() + 10000, // 10 seconds (faster expiry for CLOB)
|
|
242
|
+
}
|
|
243
|
+
} catch (error) {
|
|
244
|
+
console.error('CLOB quote error:', error)
|
|
245
|
+
return null
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Convert token addresses to trading symbol
|
|
251
|
+
*/
|
|
252
|
+
private getSymbol(tokenIn: Address, tokenOut: Address): string | null {
|
|
253
|
+
// TODO: Implement proper token-to-symbol mapping
|
|
254
|
+
// This should look up the token registry
|
|
255
|
+
const tokenMap: Record<string, string> = {
|
|
256
|
+
'0x0000000000000000000000000000000000000000': 'LUX',
|
|
257
|
+
// Add more tokens as needed
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const symbolIn = tokenMap[tokenIn.toLowerCase()]
|
|
261
|
+
const symbolOut = tokenMap[tokenOut.toLowerCase()]
|
|
262
|
+
|
|
263
|
+
if (!symbolIn || !symbolOut) return null
|
|
264
|
+
|
|
265
|
+
return `${symbolIn}-${symbolOut}`
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Execute a swap
|
|
270
|
+
*/
|
|
271
|
+
async executeSwap(request: SwapRequest): Promise<SwapResult> {
|
|
272
|
+
const { quote, recipient, deadline = Math.floor(Date.now() / 1000) + 1200 } = request
|
|
273
|
+
|
|
274
|
+
if (Date.now() > quote.validUntil) {
|
|
275
|
+
throw new Error('Quote expired')
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Determine execution path
|
|
279
|
+
const source = quote.route[0]?.source
|
|
280
|
+
|
|
281
|
+
if (source === 'amm') {
|
|
282
|
+
return this.executeAMMSwap(quote, recipient, deadline)
|
|
283
|
+
} else if (source === 'clob') {
|
|
284
|
+
return this.executeCLOBSwap(quote, recipient)
|
|
285
|
+
} else {
|
|
286
|
+
throw new Error(`Unknown route source: ${source}`)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Execute AMM swap via precompile
|
|
292
|
+
*/
|
|
293
|
+
private async executeAMMSwap(
|
|
294
|
+
quote: Quote,
|
|
295
|
+
recipient: Address,
|
|
296
|
+
deadline: number
|
|
297
|
+
): Promise<SwapResult> {
|
|
298
|
+
// This would be called by the wallet/wagmi on the frontend
|
|
299
|
+
// Just return the transaction parameters
|
|
300
|
+
throw new Error('AMM swap execution should be handled by wagmi writeContract')
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Execute CLOB swap
|
|
305
|
+
*/
|
|
306
|
+
private async executeCLOBSwap(quote: Quote, recipient: Address): Promise<SwapResult> {
|
|
307
|
+
if (!this.clobClient) {
|
|
308
|
+
throw new Error('CLOB client not configured')
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const step = quote.route[0]
|
|
312
|
+
if (!step?.symbol) {
|
|
313
|
+
throw new Error('Invalid CLOB route')
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// Place market order on CLOB
|
|
317
|
+
const order = await this.clobClient.placeOrder({
|
|
318
|
+
symbol: step.symbol,
|
|
319
|
+
side: step.tokenIn.toLowerCase() < step.tokenOut.toLowerCase() ? 'buy' : 'sell',
|
|
320
|
+
type: 'market',
|
|
321
|
+
size: Number(quote.amountIn) / 1e18,
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
txHash: `0x${order.orderId}` as `0x${string}`,
|
|
326
|
+
amountIn: quote.amountIn,
|
|
327
|
+
amountOut: quote.amountOut,
|
|
328
|
+
route: quote.route,
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Create an omnichain router instance
|
|
335
|
+
*/
|
|
336
|
+
export function createRouter(config?: Partial<RouterConfig>): OmnichainRouter {
|
|
337
|
+
return new OmnichainRouter(config)
|
|
338
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Router Types
|
|
3
|
+
* Types for hybrid CLOB + AMM routing
|
|
4
|
+
*/
|
|
5
|
+
import type { Address } from 'viem'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Route source - where liquidity comes from
|
|
9
|
+
*/
|
|
10
|
+
export type RouteSource = 'clob' | 'amm' | 'hybrid'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Quote request
|
|
14
|
+
*/
|
|
15
|
+
export interface QuoteRequest {
|
|
16
|
+
tokenIn: Address
|
|
17
|
+
tokenOut: Address
|
|
18
|
+
amountIn: bigint
|
|
19
|
+
slippageTolerance?: number // basis points (default 50 = 0.5%)
|
|
20
|
+
preferredSource?: RouteSource
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Route step
|
|
25
|
+
*/
|
|
26
|
+
export interface RouteStep {
|
|
27
|
+
source: RouteSource
|
|
28
|
+
tokenIn: Address
|
|
29
|
+
tokenOut: Address
|
|
30
|
+
amountIn: bigint
|
|
31
|
+
amountOut: bigint
|
|
32
|
+
pool?: Address // For AMM
|
|
33
|
+
symbol?: string // For CLOB
|
|
34
|
+
fee: bigint
|
|
35
|
+
priceImpact: number // basis points
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Quote response
|
|
40
|
+
*/
|
|
41
|
+
export interface Quote {
|
|
42
|
+
tokenIn: Address
|
|
43
|
+
tokenOut: Address
|
|
44
|
+
amountIn: bigint
|
|
45
|
+
amountOut: bigint
|
|
46
|
+
minimumAmountOut: bigint
|
|
47
|
+
route: RouteStep[]
|
|
48
|
+
priceImpact: number // total price impact in basis points
|
|
49
|
+
estimatedGas: bigint
|
|
50
|
+
validUntil: number // timestamp
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Swap request
|
|
55
|
+
*/
|
|
56
|
+
export interface SwapRequest {
|
|
57
|
+
quote: Quote
|
|
58
|
+
recipient: Address
|
|
59
|
+
deadline?: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Swap result
|
|
64
|
+
*/
|
|
65
|
+
export interface SwapResult {
|
|
66
|
+
txHash: `0x${string}`
|
|
67
|
+
amountIn: bigint
|
|
68
|
+
amountOut: bigint
|
|
69
|
+
route: RouteStep[]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Router configuration
|
|
74
|
+
*/
|
|
75
|
+
export interface RouterConfig {
|
|
76
|
+
// CLOB connection
|
|
77
|
+
clobUrl?: string
|
|
78
|
+
clobEnabled?: boolean
|
|
79
|
+
|
|
80
|
+
// AMM configuration
|
|
81
|
+
ammEnabled?: boolean
|
|
82
|
+
maxHops?: number // max number of AMM hops
|
|
83
|
+
|
|
84
|
+
// Routing preferences
|
|
85
|
+
preferCLOB?: boolean // prefer CLOB for limit orders
|
|
86
|
+
hybridEnabled?: boolean // allow splitting between CLOB and AMM
|
|
87
|
+
}
|