|
| 1 | +import 'server-only'; |
| 2 | +import config from '@/config/env-config'; |
| 3 | + |
| 4 | +interface ERC20Resource { |
| 5 | + kind: 'erc20'; |
| 6 | + token: string; |
| 7 | + chainId: number; |
| 8 | +} |
| 9 | + |
| 10 | +interface NativeResource { |
| 11 | + kind: 'native'; |
| 12 | + chainId: number; |
| 13 | +} |
| 14 | + |
| 15 | +type Resource = ERC20Resource | NativeResource; |
| 16 | + |
| 17 | +interface FlowInput { |
| 18 | + name: string; |
| 19 | + resource: Resource; |
| 20 | +} |
| 21 | + |
| 22 | +interface SwapNode { |
| 23 | + id: string; |
| 24 | + op: 'lifi.swap'; |
| 25 | + bind: { |
| 26 | + amountIn: { $ref: string }; |
| 27 | + }; |
| 28 | + config: { |
| 29 | + resourceOut: Resource; |
| 30 | + slippage: number; |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +interface Flow { |
| 35 | + version: number; |
| 36 | + id: string; |
| 37 | + chainId: number; |
| 38 | + inputs: FlowInput[]; |
| 39 | + nodes: SwapNode[]; |
| 40 | +} |
| 41 | + |
| 42 | +interface DirectDepositInput { |
| 43 | + kind: 'directDeposit'; |
| 44 | + amount: string; |
| 45 | +} |
| 46 | + |
| 47 | +interface Run { |
| 48 | + inputs: Record<string, DirectDepositInput>; |
| 49 | + signer: string; |
| 50 | + sweepTo: string; |
| 51 | + simulationPolicy?: string; |
| 52 | + checkOnChainAllowances?: boolean; |
| 53 | + maxPriceImpactBps?: number; |
| 54 | +} |
| 55 | + |
| 56 | +interface ComposeRequestBody { |
| 57 | + flow: Flow; |
| 58 | + run: Run; |
| 59 | +} |
| 60 | + |
| 61 | +interface TransactionRequest { |
| 62 | + to: string; |
| 63 | + data: string; |
| 64 | + value: string; |
| 65 | +} |
| 66 | + |
| 67 | +interface ApprovalItem { |
| 68 | + token: string; |
| 69 | + spender: string; |
| 70 | + amount: string; |
| 71 | + transactionRequest: TransactionRequest; |
| 72 | +} |
| 73 | + |
| 74 | +export interface ProducedResourceSimulated { |
| 75 | + amountOut: string; |
| 76 | + amountOutMin: string; |
| 77 | +} |
| 78 | + |
| 79 | +export interface ProducedResource { |
| 80 | + kind: 'native' | 'erc20'; |
| 81 | + chainId: number; |
| 82 | + availability: string; |
| 83 | + owner: string; |
| 84 | + simulated: ProducedResourceSimulated; |
| 85 | +} |
| 86 | + |
| 87 | +export interface PriceImpact { |
| 88 | + inputValueUsd: number; |
| 89 | + outputValueUsd: number; |
| 90 | + impactBps: number; |
| 91 | + unpricedInputs: string[]; |
| 92 | + unpricedOutputs: string[]; |
| 93 | +} |
| 94 | + |
| 95 | +export interface ComposeResponseData { |
| 96 | + producedResources: Record<string, ProducedResource>; |
| 97 | + transactionRequest: TransactionRequest; |
| 98 | + userProxy: string; |
| 99 | + approvals: ApprovalItem[]; |
| 100 | + priceImpact: PriceImpact; |
| 101 | +} |
| 102 | + |
| 103 | +interface ComposeResponse { |
| 104 | + data: ComposeResponseData; |
| 105 | + success: boolean; |
| 106 | + error?: { |
| 107 | + message: string; |
| 108 | + kind: string; |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +class LifiComposerClient { |
| 113 | + private baseUrl: string; |
| 114 | + private apiKey: string; |
| 115 | + |
| 116 | + constructor(baseUrl: string, apiKey: string) { |
| 117 | + this.baseUrl = baseUrl; |
| 118 | + this.apiKey = apiKey; |
| 119 | + } |
| 120 | + |
| 121 | + async compose(body: ComposeRequestBody): Promise<ComposeResponse> { |
| 122 | + const response = await fetch(`${this.baseUrl}/compose`, { |
| 123 | + method: 'POST', |
| 124 | + headers: { |
| 125 | + 'Content-Type': 'application/json', |
| 126 | + 'x-lifi-api-key': this.apiKey, |
| 127 | + }, |
| 128 | + body: JSON.stringify(body), |
| 129 | + }); |
| 130 | + |
| 131 | + if (!response.ok) { |
| 132 | + throw new Error( |
| 133 | + `LiFi Composer failed: ${response.status} ${response.statusText}`, |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return response.json(); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +export const makeLifiComposerClient = (): LifiComposerClient => { |
| 142 | + return new LifiComposerClient( |
| 143 | + config.NEXT_PUBLIC_LIFI_COMPOSER_BACKEND_URL, |
| 144 | + config.LIFI_COMPOSER_API_KEY, |
| 145 | + ); |
| 146 | +}; |
0 commit comments