@compass-labs/widgets 0.1.42 → 0.1.43

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.
@@ -0,0 +1,1837 @@
1
+ import { Injectable, Inject, Catch, Get, Query, HttpCode, Post, Body, Controller, UseFilters, Module, HttpException } from '@nestjs/common';
2
+ import { CompassApiSDK } from '@compass-labs/api-sdk';
3
+ import { createWalletClient, http, createPublicClient } from 'viem';
4
+ import { privateKeyToAccount } from 'viem/accounts';
5
+ import { arbitrum, base, mainnet } from 'viem/chains';
6
+
7
+ var __defProp = Object.defineProperty;
8
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
+ var __decorateClass = (decorators, target, key, kind) => {
10
+ var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
11
+ for (var i = decorators.length - 1, decorator; i >= 0; i--)
12
+ if (decorator = decorators[i])
13
+ result = (kind ? decorator(target, key, result) : decorator(result)) || result;
14
+ if (kind && result) __defProp(target, key, result);
15
+ return result;
16
+ };
17
+ var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
18
+ var CHAIN_MAP = {
19
+ ethereum: mainnet,
20
+ base,
21
+ arbitrum
22
+ };
23
+ var CREDIT_TOKENS = {
24
+ base: ["USDC", "WETH", "USDT", "DAI", "WBTC"],
25
+ ethereum: ["USDC", "WETH", "USDT", "DAI", "WBTC"],
26
+ arbitrum: ["USDC", "WETH", "USDT", "DAI", "WBTC"]
27
+ };
28
+
29
+ // src/server/core/compass-service.ts
30
+ var CompassServiceError = class extends Error {
31
+ constructor(message, statusCode) {
32
+ super(message);
33
+ this.statusCode = statusCode;
34
+ this.name = "CompassServiceError";
35
+ }
36
+ };
37
+ var CompassCoreService = class {
38
+ constructor(config) {
39
+ this.config = config;
40
+ const { apiKey, serverUrl = "https://api.compasslabs.ai" } = config;
41
+ this.client = new CompassApiSDK({
42
+ apiKeyAuth: apiKey,
43
+ serverURL: serverUrl
44
+ });
45
+ }
46
+ // --- Earn ---
47
+ async earnAccountCheck(params) {
48
+ const { owner, chain = "base" } = params;
49
+ if (!owner) {
50
+ throw new CompassServiceError("Missing owner parameter", 400);
51
+ }
52
+ const response = await this.client.earn.earnCreateAccount({
53
+ chain,
54
+ owner,
55
+ sender: owner,
56
+ estimateGas: false
57
+ });
58
+ const earnAccountAddress = response.earnAccountAddress;
59
+ const hasTransaction = !!response.transaction;
60
+ return {
61
+ earnAccountAddress,
62
+ isDeployed: !hasTransaction,
63
+ needsCreation: hasTransaction
64
+ };
65
+ }
66
+ async earnAccountBalances(params) {
67
+ const { owner, chain = "base" } = params;
68
+ if (!owner) {
69
+ throw new CompassServiceError("Missing owner parameter", 400);
70
+ }
71
+ const response = await this.client.earn.earnBalances({
72
+ chain,
73
+ owner
74
+ });
75
+ const data = response;
76
+ const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
77
+ const balances = {};
78
+ for (const [symbol, tokenData] of Object.entries(data.balances)) {
79
+ const td = tokenData;
80
+ const hasRealTransfers = td.transfers.some((t) => {
81
+ const fromAddr = (t.from_address || t.fromAddress || "").toLowerCase();
82
+ const toAddr = (t.to_address || t.toAddress || "").toLowerCase();
83
+ return fromAddr !== ZERO_ADDRESS && toAddr !== ZERO_ADDRESS;
84
+ });
85
+ const balanceFormatted = td.balance_formatted || td.balanceFormatted || "0";
86
+ const balanceNum = parseFloat(balanceFormatted);
87
+ if (balanceNum === 0 && !hasRealTransfers) {
88
+ continue;
89
+ }
90
+ if (!hasRealTransfers && td.transfers.length > 0) {
91
+ continue;
92
+ }
93
+ const usdValue = td.usd_value || td.usdValue || "0";
94
+ const usdValueNum = parseFloat(usdValue);
95
+ if (usdValueNum === 0 || isNaN(usdValueNum)) {
96
+ continue;
97
+ }
98
+ balances[symbol] = {
99
+ balance: balanceFormatted,
100
+ usdValue
101
+ };
102
+ }
103
+ const earnAccountAddr = data.earn_account_address || data.earnAccountAddress || "";
104
+ const totalUsd = data.total_usd_value || data.totalUsdValue || "0";
105
+ return {
106
+ earnAccountAddress: earnAccountAddr,
107
+ balances,
108
+ totalUsdValue: totalUsd
109
+ };
110
+ }
111
+ async createAccount(body) {
112
+ const { owner, chain = "base" } = body;
113
+ const { gasSponsorPrivateKey, rpcUrls } = this.config;
114
+ if (!owner) {
115
+ throw new CompassServiceError("Missing owner parameter", 400);
116
+ }
117
+ if (!gasSponsorPrivateKey) {
118
+ throw new CompassServiceError(
119
+ "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config.",
120
+ 500
121
+ );
122
+ }
123
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
124
+ if (!viemChain) {
125
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 500);
126
+ }
127
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
128
+ if (!rpcUrl) {
129
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
130
+ }
131
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
132
+ const walletClient = createWalletClient({
133
+ account: sponsorAccount,
134
+ chain: viemChain,
135
+ transport: http(rpcUrl)
136
+ });
137
+ const publicClient = createPublicClient({
138
+ chain: viemChain,
139
+ transport: http(rpcUrl)
140
+ });
141
+ const response = await this.client.earn.earnCreateAccount({
142
+ chain,
143
+ owner,
144
+ sender: sponsorAccount.address,
145
+ estimateGas: false
146
+ });
147
+ const earnAccountAddress = response.earnAccountAddress;
148
+ if (!response.transaction) {
149
+ return {
150
+ earnAccountAddress,
151
+ success: true,
152
+ alreadyExists: true
153
+ };
154
+ }
155
+ const transaction = response.transaction;
156
+ const txHash = await walletClient.sendTransaction({
157
+ to: transaction.to,
158
+ data: transaction.data,
159
+ value: transaction.value ? BigInt(transaction.value) : 0n,
160
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
161
+ });
162
+ const receipt = await publicClient.waitForTransactionReceipt({
163
+ hash: txHash
164
+ });
165
+ if (receipt.status === "reverted") {
166
+ throw new CompassServiceError("Account creation transaction reverted", 500);
167
+ }
168
+ return {
169
+ earnAccountAddress,
170
+ txHash,
171
+ success: true
172
+ };
173
+ }
174
+ async managePrepare(body, action) {
175
+ const { amount, token, owner, chain, venueType, vaultAddress, marketAddress, maxSlippagePercent } = body;
176
+ let venue;
177
+ if (venueType === "VAULT" && vaultAddress) {
178
+ venue = {
179
+ type: "VAULT",
180
+ vaultAddress
181
+ };
182
+ } else if (venueType === "AAVE") {
183
+ venue = {
184
+ type: "AAVE",
185
+ token
186
+ };
187
+ } else if (venueType === "PENDLE_PT" && marketAddress) {
188
+ venue = {
189
+ type: "PENDLE_PT",
190
+ marketAddress,
191
+ token: action === "DEPOSIT" ? token : void 0,
192
+ maxSlippagePercent: maxSlippagePercent ?? 1
193
+ };
194
+ } else {
195
+ throw new CompassServiceError("Invalid venue type or missing address", 400);
196
+ }
197
+ const response = await this.client.earn.earnManage({
198
+ owner,
199
+ chain,
200
+ venue,
201
+ action,
202
+ amount,
203
+ gasSponsorship: true
204
+ });
205
+ const eip712 = response.eip712;
206
+ if (!eip712) {
207
+ throw new CompassServiceError("No EIP-712 data returned from API", 500);
208
+ }
209
+ const types = eip712.types;
210
+ const normalizedTypes = {
211
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
212
+ SafeTx: types.safeTx || types.SafeTx
213
+ };
214
+ return {
215
+ eip712,
216
+ normalizedTypes,
217
+ domain: eip712.domain,
218
+ message: eip712.message
219
+ };
220
+ }
221
+ async execute(body) {
222
+ const { owner, eip712, signature, chain } = body;
223
+ const { gasSponsorPrivateKey, rpcUrls } = this.config;
224
+ if (!gasSponsorPrivateKey) {
225
+ throw new CompassServiceError(
226
+ "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config.",
227
+ 500
228
+ );
229
+ }
230
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
231
+ if (!viemChain) {
232
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 500);
233
+ }
234
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
235
+ if (!rpcUrl) {
236
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
237
+ }
238
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
239
+ const walletClient = createWalletClient({
240
+ account: sponsorAccount,
241
+ chain: viemChain,
242
+ transport: http(rpcUrl)
243
+ });
244
+ const response = await this.client.gasSponsorship.gasSponsorshipPrepare({
245
+ chain,
246
+ owner,
247
+ sender: sponsorAccount.address,
248
+ eip712,
249
+ signature
250
+ });
251
+ const transaction = response.transaction;
252
+ if (!transaction) {
253
+ throw new CompassServiceError(
254
+ "No transaction returned from gas sponsorship prepare",
255
+ 500
256
+ );
257
+ }
258
+ const txHash = await walletClient.sendTransaction({
259
+ to: transaction.to,
260
+ data: transaction.data,
261
+ value: transaction.value ? BigInt(transaction.value) : 0n,
262
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
263
+ });
264
+ return { txHash, success: true };
265
+ }
266
+ // --- Transfer ---
267
+ async transferApprove(body) {
268
+ const { owner, chain = "base", token } = body;
269
+ if (!owner || !token) {
270
+ throw new CompassServiceError("Missing owner or token parameter", 400);
271
+ }
272
+ try {
273
+ const response = await this.client.gasSponsorship.gasSponsorshipApproveTransfer({
274
+ owner,
275
+ chain,
276
+ token,
277
+ gasSponsorship: true
278
+ });
279
+ const eip712 = response.eip712 || response.eip_712;
280
+ const transaction = response.transaction;
281
+ if (!eip712 && !transaction) {
282
+ return {
283
+ approved: true,
284
+ message: "Token already approved for Permit2"
285
+ };
286
+ }
287
+ if (eip712) {
288
+ const types = eip712.types;
289
+ const normalizedTypes = {
290
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
291
+ Permit: types.permit || types.Permit
292
+ };
293
+ return {
294
+ approved: false,
295
+ eip712,
296
+ normalizedTypes,
297
+ domain: eip712.domain,
298
+ message: eip712.message
299
+ };
300
+ }
301
+ return {
302
+ approved: false,
303
+ transaction,
304
+ requiresTransaction: true
305
+ };
306
+ } catch (error) {
307
+ const errorMessage = error instanceof Error ? error.message : String(error);
308
+ if (errorMessage.includes("already set") || errorMessage.includes("already been set")) {
309
+ return {
310
+ approved: true,
311
+ message: "Token allowance already set"
312
+ };
313
+ }
314
+ throw error;
315
+ }
316
+ }
317
+ async transferPrepare(body) {
318
+ const { owner, chain = "base", token, amount, action, product } = body;
319
+ const { gasSponsorPrivateKey } = this.config;
320
+ if (!owner || !token || !amount || !action) {
321
+ throw new CompassServiceError("Missing required parameters", 400);
322
+ }
323
+ let spender;
324
+ if (action === "DEPOSIT" && gasSponsorPrivateKey) {
325
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
326
+ spender = sponsorAccount.address;
327
+ }
328
+ let response;
329
+ if (product === "credit") {
330
+ response = await this.client.credit.creditTransfer({
331
+ owner,
332
+ chain,
333
+ token,
334
+ amount,
335
+ action,
336
+ gasSponsorship: true,
337
+ ...spender && { spender }
338
+ });
339
+ } else {
340
+ response = await this.client.earn.earnTransfer({
341
+ owner,
342
+ chain,
343
+ token,
344
+ amount,
345
+ action,
346
+ gasSponsorship: true,
347
+ ...spender && { spender }
348
+ });
349
+ }
350
+ const eip712 = response.eip712;
351
+ if (!eip712) {
352
+ throw new CompassServiceError("No EIP-712 data returned from API", 500);
353
+ }
354
+ const types = eip712.types;
355
+ let normalizedTypes;
356
+ if (action === "DEPOSIT") {
357
+ normalizedTypes = {
358
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
359
+ PermitTransferFrom: types.permitTransferFrom || types.PermitTransferFrom,
360
+ TokenPermissions: types.tokenPermissions || types.TokenPermissions
361
+ };
362
+ } else {
363
+ normalizedTypes = {
364
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
365
+ SafeTx: types.safeTx || types.SafeTx
366
+ };
367
+ }
368
+ return {
369
+ eip712,
370
+ normalizedTypes,
371
+ domain: eip712.domain,
372
+ message: eip712.message,
373
+ primaryType: eip712.primaryType
374
+ };
375
+ }
376
+ async transferExecute(body) {
377
+ const { owner, chain = "base", eip712, signature, product } = body;
378
+ const { gasSponsorPrivateKey, rpcUrls } = this.config;
379
+ if (!owner || !eip712 || !signature) {
380
+ throw new CompassServiceError("Missing required parameters", 400);
381
+ }
382
+ if (!gasSponsorPrivateKey) {
383
+ throw new CompassServiceError(
384
+ "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config.",
385
+ 500
386
+ );
387
+ }
388
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
389
+ if (!viemChain) {
390
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 500);
391
+ }
392
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
393
+ if (!rpcUrl) {
394
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
395
+ }
396
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
397
+ const walletClient = createWalletClient({
398
+ account: sponsorAccount,
399
+ chain: viemChain,
400
+ transport: http(rpcUrl)
401
+ });
402
+ const response = await this.client.gasSponsorship.gasSponsorshipPrepare({
403
+ chain,
404
+ owner,
405
+ sender: sponsorAccount.address,
406
+ eip712,
407
+ signature,
408
+ ...product === "credit" && { product: "credit" }
409
+ });
410
+ const transaction = response.transaction;
411
+ if (!transaction) {
412
+ throw new CompassServiceError(
413
+ "No transaction returned from gas sponsorship prepare",
414
+ 500
415
+ );
416
+ }
417
+ const txHash = await walletClient.sendTransaction({
418
+ to: transaction.to,
419
+ data: transaction.data,
420
+ value: transaction.value ? BigInt(transaction.value) : 0n,
421
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
422
+ });
423
+ return { txHash, success: true };
424
+ }
425
+ async approvalExecute(body) {
426
+ const { owner, chain = "base", transaction } = body;
427
+ const { gasSponsorPrivateKey, rpcUrls } = this.config;
428
+ if (!owner || !transaction) {
429
+ throw new CompassServiceError("Missing required parameters (owner, transaction)", 400);
430
+ }
431
+ if (!gasSponsorPrivateKey) {
432
+ throw new CompassServiceError(
433
+ "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config.",
434
+ 500
435
+ );
436
+ }
437
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
438
+ if (!viemChain) {
439
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 500);
440
+ }
441
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
442
+ if (!rpcUrl) {
443
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
444
+ }
445
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
446
+ const walletClient = createWalletClient({
447
+ account: sponsorAccount,
448
+ chain: viemChain,
449
+ transport: http(rpcUrl)
450
+ });
451
+ const publicClient = createPublicClient({
452
+ chain: viemChain,
453
+ transport: http(rpcUrl)
454
+ });
455
+ const txHash = await walletClient.sendTransaction({
456
+ to: transaction.to,
457
+ data: transaction.data,
458
+ value: transaction.value ? BigInt(transaction.value) : 0n,
459
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
460
+ });
461
+ const receipt = await publicClient.waitForTransactionReceipt({
462
+ hash: txHash,
463
+ timeout: 6e4
464
+ });
465
+ if (receipt.status === "reverted") {
466
+ throw new CompassServiceError("Approval transaction reverted", 500);
467
+ }
468
+ return { txHash, status: "success" };
469
+ }
470
+ // --- Swap ---
471
+ async swapQuote(params) {
472
+ const { owner, chain = "base", tokenIn, tokenOut, amountIn } = params;
473
+ if (!owner || !tokenIn || !tokenOut || !amountIn) {
474
+ throw new CompassServiceError("Missing required parameters: owner, tokenIn, tokenOut, amountIn", 400);
475
+ }
476
+ try {
477
+ const response = await this.client.earn.earnSwap({
478
+ owner,
479
+ chain,
480
+ tokenIn,
481
+ tokenOut,
482
+ amountIn,
483
+ slippage: 1,
484
+ gasSponsorship: true
485
+ });
486
+ const estimatedAmountOut = response.estimatedAmountOut || "0";
487
+ return {
488
+ tokenIn,
489
+ tokenOut,
490
+ amountIn,
491
+ estimatedAmountOut: estimatedAmountOut?.toString() || "0"
492
+ };
493
+ } catch (error) {
494
+ let errorMessage = "Failed to get swap quote";
495
+ try {
496
+ const bodyMessage = error?.body?.message || error?.message || "";
497
+ if (bodyMessage.includes("{")) {
498
+ const jsonMatch = bodyMessage.match(/\{.*\}/s);
499
+ if (jsonMatch) {
500
+ const parsed = JSON.parse(jsonMatch[0]);
501
+ errorMessage = parsed.description || parsed.error || parsed.message || errorMessage;
502
+ }
503
+ } else if (bodyMessage) {
504
+ const balanceMatch = bodyMessage.match(/Insufficient \w+ balance[^.]+/i);
505
+ if (balanceMatch) {
506
+ errorMessage = balanceMatch[0];
507
+ } else {
508
+ errorMessage = bodyMessage;
509
+ }
510
+ }
511
+ } catch {
512
+ errorMessage = error?.body?.error || error?.message || "Failed to get swap quote";
513
+ }
514
+ throw new CompassServiceError(errorMessage, 400);
515
+ }
516
+ }
517
+ async swapPrepare(body) {
518
+ const { owner, chain = "base", tokenIn, tokenOut, amountIn, slippage = 1 } = body;
519
+ if (!owner || !tokenIn || !tokenOut || !amountIn) {
520
+ throw new CompassServiceError("Missing required parameters: owner, tokenIn, tokenOut, amountIn", 400);
521
+ }
522
+ const response = await this.client.earn.earnSwap({
523
+ owner,
524
+ chain,
525
+ tokenIn,
526
+ tokenOut,
527
+ amountIn,
528
+ slippage,
529
+ gasSponsorship: true
530
+ });
531
+ const eip712 = response.eip712;
532
+ if (!eip712) {
533
+ throw new CompassServiceError("No EIP-712 data returned from API", 500);
534
+ }
535
+ const types = eip712.types;
536
+ const normalizedTypes = {
537
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
538
+ SafeTx: types.safeTx || types.SafeTx
539
+ };
540
+ return {
541
+ eip712,
542
+ normalizedTypes,
543
+ domain: eip712.domain,
544
+ message: eip712.message,
545
+ estimatedAmountOut: response.estimatedAmountOut?.toString() || "0"
546
+ };
547
+ }
548
+ async swapExecute(body) {
549
+ const { owner, chain = "base", eip712, signature } = body;
550
+ if (!owner || !eip712 || !signature) {
551
+ throw new CompassServiceError("Missing required parameters: owner, eip712, signature", 400);
552
+ }
553
+ if (!this.config.gasSponsorPrivateKey) {
554
+ throw new CompassServiceError("Gas sponsor not configured", 500);
555
+ }
556
+ const chainLower = chain.toLowerCase();
557
+ const rpcUrl = this.config.rpcUrls?.[chainLower];
558
+ if (!rpcUrl) {
559
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
560
+ }
561
+ const viemChain = CHAIN_MAP[chainLower];
562
+ if (!viemChain) {
563
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 400);
564
+ }
565
+ const sponsorAccount = privateKeyToAccount(this.config.gasSponsorPrivateKey);
566
+ const walletClient = createWalletClient({
567
+ account: sponsorAccount,
568
+ chain: viemChain,
569
+ transport: http(rpcUrl)
570
+ });
571
+ const publicClient = createPublicClient({
572
+ chain: viemChain,
573
+ transport: http(rpcUrl)
574
+ });
575
+ const response = await this.client.gasSponsorship.gasSponsorshipPrepare({
576
+ chain,
577
+ owner,
578
+ sender: sponsorAccount.address,
579
+ eip712,
580
+ signature
581
+ });
582
+ const transaction = response.transaction;
583
+ if (!transaction) {
584
+ throw new CompassServiceError("No transaction returned from gas sponsorship prepare", 500);
585
+ }
586
+ const txHash = await walletClient.sendTransaction({
587
+ to: transaction.to,
588
+ data: transaction.data,
589
+ value: transaction.value ? BigInt(transaction.value) : 0n,
590
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
591
+ });
592
+ const receipt = await publicClient.waitForTransactionReceipt({
593
+ hash: txHash
594
+ });
595
+ if (receipt.status === "reverted") {
596
+ throw new CompassServiceError("Transaction reverted", 500);
597
+ }
598
+ return { txHash, success: true };
599
+ }
600
+ // --- Token ---
601
+ async tokenBalance(params) {
602
+ const { chain = "base", token, address } = params;
603
+ if (!token || !address) {
604
+ throw new CompassServiceError("Missing token or address parameter", 400);
605
+ }
606
+ try {
607
+ const response = await this.client.token.tokenBalance({
608
+ chain,
609
+ token,
610
+ user: address
611
+ });
612
+ return {
613
+ token,
614
+ address,
615
+ balance: response.amount || "0",
616
+ balanceRaw: response.balanceRaw || "0"
617
+ };
618
+ } catch {
619
+ return {
620
+ token,
621
+ address,
622
+ balance: "0",
623
+ balanceRaw: "0"
624
+ };
625
+ }
626
+ }
627
+ async tokenPrices(params) {
628
+ const { chain = "base", tokens } = params;
629
+ if (!tokens) {
630
+ throw new CompassServiceError("Missing tokens parameter", 400);
631
+ }
632
+ const tokenList = tokens.split(",").map((t) => t.trim().toUpperCase());
633
+ const prices = {};
634
+ const results = await Promise.allSettled(
635
+ tokenList.map(async (symbol) => {
636
+ const resp = await this.client.token.tokenPrice({ chain, token: symbol });
637
+ return { symbol, price: parseFloat(resp.price || "0") };
638
+ })
639
+ );
640
+ for (const result of results) {
641
+ if (result.status === "fulfilled" && result.value.price > 0) {
642
+ prices[result.value.symbol] = result.value.price;
643
+ }
644
+ }
645
+ return { prices };
646
+ }
647
+ // --- Bundle ---
648
+ async bundlePrepare(body) {
649
+ const { owner, chain = "base", actions } = body;
650
+ if (!owner || !actions || actions.length === 0) {
651
+ throw new CompassServiceError("Missing owner or actions", 400);
652
+ }
653
+ const response = await this.client.earn.earnBundle({
654
+ owner,
655
+ chain,
656
+ gasSponsorship: true,
657
+ actions
658
+ });
659
+ const eip712 = response.eip712;
660
+ if (!eip712) {
661
+ throw new CompassServiceError("No EIP-712 data returned from API", 500);
662
+ }
663
+ const types = eip712.types;
664
+ const normalizedTypes = {
665
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
666
+ SafeTx: types.safeTx || types.SafeTx
667
+ };
668
+ return {
669
+ eip712,
670
+ normalizedTypes,
671
+ domain: eip712.domain,
672
+ message: eip712.message,
673
+ actionsCount: response.actionsCount || actions.length
674
+ };
675
+ }
676
+ async bundleExecute(body) {
677
+ return this.transferExecute(body);
678
+ }
679
+ // --- Data ---
680
+ async vaults(params) {
681
+ const { chain = "base", orderBy = "apy_7d", direction = "desc", limit = "100", assetSymbol, minTvlUsd } = params;
682
+ try {
683
+ const response = await this.client.earn.earnVaults({
684
+ chain,
685
+ orderBy,
686
+ direction,
687
+ limit: parseInt(limit, 10),
688
+ ...assetSymbol && { assetSymbol },
689
+ ...minTvlUsd && { minTvlUsd: parseFloat(minTvlUsd) }
690
+ });
691
+ return response;
692
+ } catch {
693
+ throw new CompassServiceError("Failed to fetch vaults", 500);
694
+ }
695
+ }
696
+ async aaveMarkets(params) {
697
+ const { chain = "base" } = params;
698
+ try {
699
+ const response = await this.client.earn.earnAaveMarkets({
700
+ chain
701
+ });
702
+ return response;
703
+ } catch {
704
+ throw new CompassServiceError("Failed to fetch Aave markets", 500);
705
+ }
706
+ }
707
+ async pendleMarkets(params) {
708
+ const { chain = "base", orderBy = "implied_apy", direction = "desc", limit = "100", underlyingSymbol, minTvlUsd } = params;
709
+ try {
710
+ const response = await this.client.earn.earnPendleMarkets({
711
+ chain,
712
+ orderBy,
713
+ direction,
714
+ limit: parseInt(limit, 10),
715
+ ...underlyingSymbol && { underlyingSymbol },
716
+ ...minTvlUsd && { minTvlUsd: parseFloat(minTvlUsd) }
717
+ });
718
+ return response;
719
+ } catch {
720
+ throw new CompassServiceError("Failed to fetch Pendle markets", 500);
721
+ }
722
+ }
723
+ async positions(params) {
724
+ const { chain = "base", owner } = params;
725
+ if (!owner) {
726
+ throw new CompassServiceError("Missing owner parameter", 400);
727
+ }
728
+ try {
729
+ const positionsResponse = await this.client.earn.earnPositions({
730
+ chain,
731
+ owner
732
+ });
733
+ const raw = JSON.parse(JSON.stringify(positionsResponse));
734
+ const positions = [];
735
+ const aavePositions = raw.aave || [];
736
+ for (const a of aavePositions) {
737
+ const balance = a.balance || "0";
738
+ const symbol = (a.reserveSymbol || a.reserve_symbol || "UNKNOWN").toUpperCase();
739
+ const pnl = a.pnl;
740
+ positions.push({
741
+ protocol: "aave",
742
+ symbol,
743
+ name: `${symbol} on Aave`,
744
+ balance,
745
+ balanceUsd: a.usdValue || a.usd_value || balance,
746
+ apy: parseFloat(a.apy || "0"),
747
+ pnl: pnl ? {
748
+ unrealizedPnl: pnl.unrealizedPnl ?? pnl.unrealized_pnl ?? "0",
749
+ realizedPnl: pnl.realizedPnl ?? pnl.realized_pnl ?? "0",
750
+ totalPnl: pnl.totalPnl ?? pnl.total_pnl ?? "0",
751
+ totalDeposited: pnl.totalDeposited ?? pnl.total_deposited ?? "0"
752
+ } : null,
753
+ deposits: (a.deposits || []).map((d) => ({
754
+ amount: d.inputAmount || d.input_amount || d.amount || "0",
755
+ blockNumber: d.blockNumber || d.block_number || 0,
756
+ timestamp: d.blockTimestamp || d.block_timestamp || void 0,
757
+ txHash: d.transactionHash || d.transaction_hash || d.txHash || ""
758
+ })),
759
+ withdrawals: (a.withdrawals || []).map((w) => ({
760
+ amount: w.outputAmount || w.output_amount || w.amount || "0",
761
+ blockNumber: w.blockNumber || w.block_number || 0,
762
+ timestamp: w.blockTimestamp || w.block_timestamp || void 0,
763
+ txHash: w.transactionHash || w.transaction_hash || w.txHash || ""
764
+ }))
765
+ });
766
+ }
767
+ const vaultPositions = raw.vaults || [];
768
+ for (const v of vaultPositions) {
769
+ const balance = v.balance || "0";
770
+ const symbol = (v.underlyingSymbol || v.underlying_symbol || "TOKEN").toUpperCase();
771
+ const vaultName = v.vaultName || v.vault_name || `${symbol} Vault`;
772
+ const pnl = v.pnl;
773
+ positions.push({
774
+ protocol: "vaults",
775
+ symbol,
776
+ name: vaultName,
777
+ balance,
778
+ balanceUsd: v.usdValue || v.usd_value || balance,
779
+ apy: parseFloat(v.apy7d || v.apy_7d || "0"),
780
+ vaultAddress: v.vaultAddress || v.vault_address || void 0,
781
+ pnl: pnl ? {
782
+ unrealizedPnl: pnl.unrealizedPnl ?? pnl.unrealized_pnl ?? "0",
783
+ realizedPnl: pnl.realizedPnl ?? pnl.realized_pnl ?? "0",
784
+ totalPnl: pnl.totalPnl ?? pnl.total_pnl ?? "0",
785
+ totalDeposited: pnl.totalDeposited ?? pnl.total_deposited ?? "0"
786
+ } : null,
787
+ deposits: (v.deposits || []).map((d) => ({
788
+ amount: d.inputAmount || d.input_amount || d.amount || "0",
789
+ blockNumber: d.blockNumber || d.block_number || 0,
790
+ timestamp: d.blockTimestamp || d.block_timestamp || void 0,
791
+ txHash: d.transactionHash || d.transaction_hash || d.txHash || ""
792
+ })),
793
+ withdrawals: (v.withdrawals || []).map((w) => ({
794
+ amount: w.outputAmount || w.output_amount || w.amount || "0",
795
+ blockNumber: w.blockNumber || w.block_number || 0,
796
+ timestamp: w.blockTimestamp || w.block_timestamp || void 0,
797
+ txHash: w.transactionHash || w.transaction_hash || w.txHash || ""
798
+ }))
799
+ });
800
+ }
801
+ const pendlePositions = raw.pendlePt || raw.pendle_pt || [];
802
+ for (const p of pendlePositions) {
803
+ const balance = p.ptBalance || p.pt_balance || p.balance || "0";
804
+ const symbol = (p.underlyingSymbol || p.underlying_symbol || "PT").toUpperCase();
805
+ const pnl = p.pnl;
806
+ positions.push({
807
+ protocol: "pendle",
808
+ symbol,
809
+ name: `PT-${symbol}`,
810
+ balance,
811
+ balanceUsd: p.usdValue || p.usd_value || balance,
812
+ apy: parseFloat(p.impliedApy || p.implied_apy || "0"),
813
+ marketAddress: p.marketAddress || p.market_address || void 0,
814
+ pnl: pnl ? {
815
+ unrealizedPnl: pnl.unrealizedPnl ?? pnl.unrealized_pnl ?? "0",
816
+ realizedPnl: pnl.realizedPnl ?? pnl.realized_pnl ?? "0",
817
+ totalPnl: pnl.totalPnl ?? pnl.total_pnl ?? "0",
818
+ totalDeposited: pnl.totalDeposited ?? pnl.total_deposited ?? "0"
819
+ } : null,
820
+ deposits: (p.deposits || []).map((d) => ({
821
+ amount: d.inputAmount || d.input_amount || d.amount || "0",
822
+ blockNumber: d.blockNumber || d.block_number || 0,
823
+ timestamp: d.blockTimestamp || d.block_timestamp || void 0,
824
+ txHash: d.transactionHash || d.transaction_hash || d.txHash || ""
825
+ })),
826
+ withdrawals: (p.withdrawals || []).map((w) => ({
827
+ amount: w.outputAmount || w.output_amount || w.amount || "0",
828
+ blockNumber: w.blockNumber || w.block_number || 0,
829
+ timestamp: w.blockTimestamp || w.block_timestamp || void 0,
830
+ txHash: w.transactionHash || w.transaction_hash || w.txHash || ""
831
+ }))
832
+ });
833
+ }
834
+ return { positions };
835
+ } catch {
836
+ throw new CompassServiceError("Failed to fetch positions", 500);
837
+ }
838
+ }
839
+ async txReceipt(params) {
840
+ const { hash, chain } = params;
841
+ if (!hash || !chain) {
842
+ throw new CompassServiceError("Missing hash or chain parameter", 400);
843
+ }
844
+ const rpcUrl = this.config.rpcUrls?.[chain.toLowerCase()];
845
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
846
+ if (!viemChain) {
847
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 400);
848
+ }
849
+ if (!rpcUrl) {
850
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
851
+ }
852
+ const publicClient = createPublicClient({
853
+ chain: viemChain,
854
+ transport: http(rpcUrl)
855
+ });
856
+ try {
857
+ const receipt = await publicClient.getTransactionReceipt({
858
+ hash
859
+ });
860
+ return {
861
+ status: receipt.status,
862
+ blockNumber: receipt.blockNumber.toString()
863
+ };
864
+ } catch {
865
+ return { status: "pending" };
866
+ }
867
+ }
868
+ // --- Rebalance ---
869
+ async rebalancePreview(body) {
870
+ const { owner, chain = "base", targets, slippage = 0.5 } = body;
871
+ if (!owner) {
872
+ throw new CompassServiceError("Missing owner parameter", 400);
873
+ }
874
+ if (!targets || targets.length === 0) {
875
+ throw new CompassServiceError("Missing targets", 400);
876
+ }
877
+ for (const t of targets) {
878
+ if (t.targetPercent < 0 || t.targetPercent > 100) {
879
+ throw new CompassServiceError(`Invalid target percentage: ${t.targetPercent}%`, 400);
880
+ }
881
+ }
882
+ try {
883
+ const positionsResponse = await this.client.earn.earnPositions({
884
+ chain,
885
+ owner
886
+ });
887
+ const positionsRaw = JSON.parse(JSON.stringify(positionsResponse));
888
+ const balancesResponse = await this.client.earn.earnBalances({
889
+ chain,
890
+ owner
891
+ });
892
+ const balancesRaw = JSON.parse(JSON.stringify(balancesResponse));
893
+ const currentPositions = [];
894
+ for (const a of positionsRaw.aave || []) {
895
+ const balance = a.balance || "0";
896
+ if (parseFloat(balance) <= 0) continue;
897
+ const symbol = (a.reserveSymbol || a.reserve_symbol || "UNKNOWN").toUpperCase();
898
+ currentPositions.push({
899
+ venueType: "AAVE",
900
+ venueAddress: symbol,
901
+ token: symbol,
902
+ usdValue: parseFloat(a.usdValue || a.usd_value || balance),
903
+ balance
904
+ });
905
+ }
906
+ for (const v of positionsRaw.vaults || []) {
907
+ const balance = v.balance || "0";
908
+ if (parseFloat(balance) <= 0) continue;
909
+ const symbol = (v.underlyingSymbol || v.underlying_symbol || "TOKEN").toUpperCase();
910
+ currentPositions.push({
911
+ venueType: "VAULT",
912
+ venueAddress: v.vaultAddress || v.vault_address || "",
913
+ token: symbol,
914
+ usdValue: parseFloat(v.usdValue || v.usd_value || balance),
915
+ balance
916
+ });
917
+ }
918
+ for (const p of positionsRaw.pendlePt || positionsRaw.pendle_pt || []) {
919
+ const balance = p.ptBalance || p.pt_balance || p.balance || "0";
920
+ if (parseFloat(balance) <= 0) continue;
921
+ const symbol = (p.underlyingSymbol || p.underlying_symbol || "PT").toUpperCase();
922
+ currentPositions.push({
923
+ venueType: "PENDLE_PT",
924
+ venueAddress: p.marketAddress || p.market_address || "",
925
+ token: symbol,
926
+ usdValue: parseFloat(p.usdValue || p.usd_value || balance),
927
+ balance
928
+ });
929
+ }
930
+ let totalIdleUsd = 0;
931
+ for (const [, tokenData] of Object.entries(balancesRaw.balances || {})) {
932
+ const td = tokenData;
933
+ const usdVal = parseFloat(td.usd_value || td.usdValue || "0");
934
+ totalIdleUsd += usdVal;
935
+ }
936
+ const totalPositionUsd = currentPositions.reduce((sum, p) => sum + p.usdValue, 0);
937
+ const totalUsd = totalPositionUsd + totalIdleUsd;
938
+ if (totalUsd <= 0) {
939
+ throw new CompassServiceError("No portfolio value found to rebalance", 400);
940
+ }
941
+ const allTokenSymbols = /* @__PURE__ */ new Set();
942
+ for (const pos of currentPositions) allTokenSymbols.add(pos.token.toUpperCase());
943
+ for (const t of targets) if (t.token) allTokenSymbols.add(t.token.toUpperCase());
944
+ for (const sym of Object.keys(balancesRaw.balances || {})) allTokenSymbols.add(sym.toUpperCase());
945
+ const tokenPrices = {};
946
+ const priceResults = await Promise.allSettled(
947
+ [...allTokenSymbols].map(async (symbol) => {
948
+ const resp = await this.client.token.tokenPrice({ chain, token: symbol });
949
+ return { symbol, price: parseFloat(resp.price || "0") };
950
+ })
951
+ );
952
+ for (const result of priceResults) {
953
+ if (result.status === "fulfilled" && result.value.price > 0) {
954
+ tokenPrices[result.value.symbol] = result.value.price;
955
+ }
956
+ }
957
+ const bundleActions = [];
958
+ const actionsSummary = [];
959
+ const warnings = [];
960
+ const MIN_THRESHOLD_USD = 0.01;
961
+ const CHANGE_THRESHOLD_PCT = 0.1;
962
+ const pendingDeposits = [];
963
+ for (const target of targets) {
964
+ const originalPct = target.originalPercent ?? target.targetPercent;
965
+ if (Math.abs(target.targetPercent - originalPct) <= CHANGE_THRESHOLD_PCT) continue;
966
+ const targetUsd = totalUsd * (target.targetPercent / 100);
967
+ const current = currentPositions.find(
968
+ (p) => p.venueType === target.venueType && p.venueAddress.toLowerCase() === target.venueAddress.toLowerCase()
969
+ );
970
+ const currentUsd = current?.usdValue || 0;
971
+ const deltaUsd = targetUsd - currentUsd;
972
+ if (Math.abs(deltaUsd) < MIN_THRESHOLD_USD) continue;
973
+ if (deltaUsd < 0 && current) {
974
+ const withdrawFraction = Math.abs(deltaUsd) / currentUsd;
975
+ const withdrawAmount = (parseFloat(current.balance) * withdrawFraction).toString();
976
+ let venue;
977
+ if (target.venueType === "VAULT") {
978
+ venue = { type: "VAULT", vaultAddress: target.venueAddress };
979
+ } else if (target.venueType === "AAVE") {
980
+ venue = { type: "AAVE", token: current.token };
981
+ } else if (target.venueType === "PENDLE_PT") {
982
+ venue = { type: "PENDLE_PT", marketAddress: target.venueAddress, maxSlippagePercent: slippage };
983
+ warnings.push(`Withdrawing from Pendle PT - check maturity implications`);
984
+ }
985
+ bundleActions.push({
986
+ body: {
987
+ actionType: "V2_MANAGE",
988
+ venue,
989
+ action: "WITHDRAW",
990
+ amount: withdrawAmount
991
+ }
992
+ });
993
+ actionsSummary.push({
994
+ type: "withdraw",
995
+ venue: target.venueAddress,
996
+ token: current.token,
997
+ amount: withdrawAmount,
998
+ usdValue: Math.abs(deltaUsd)
999
+ });
1000
+ } else if (deltaUsd > 0) {
1001
+ let venue;
1002
+ const token = target.token || current?.token || "";
1003
+ if (target.venueType === "VAULT") {
1004
+ venue = { type: "VAULT", vaultAddress: target.venueAddress };
1005
+ } else if (target.venueType === "AAVE") {
1006
+ venue = { type: "AAVE", token };
1007
+ } else if (target.venueType === "PENDLE_PT") {
1008
+ venue = { type: "PENDLE_PT", marketAddress: target.venueAddress, token, maxSlippagePercent: slippage };
1009
+ }
1010
+ pendingDeposits.push({ venue, venueAddress: target.venueAddress, token, deltaUsd });
1011
+ }
1012
+ }
1013
+ for (const current of currentPositions) {
1014
+ const hasTarget = targets.some(
1015
+ (t) => t.venueType === current.venueType && t.venueAddress.toLowerCase() === current.venueAddress.toLowerCase()
1016
+ );
1017
+ if (!hasTarget && current.usdValue >= MIN_THRESHOLD_USD) {
1018
+ let venue;
1019
+ if (current.venueType === "VAULT") {
1020
+ venue = { type: "VAULT", vaultAddress: current.venueAddress };
1021
+ } else if (current.venueType === "AAVE") {
1022
+ venue = { type: "AAVE", token: current.token };
1023
+ } else if (current.venueType === "PENDLE_PT") {
1024
+ venue = { type: "PENDLE_PT", marketAddress: current.venueAddress, maxSlippagePercent: slippage };
1025
+ }
1026
+ bundleActions.unshift({
1027
+ body: {
1028
+ actionType: "V2_MANAGE",
1029
+ venue,
1030
+ action: "WITHDRAW",
1031
+ amount: current.balance
1032
+ }
1033
+ });
1034
+ actionsSummary.unshift({
1035
+ type: "withdraw",
1036
+ venue: current.venueAddress,
1037
+ token: current.token,
1038
+ amount: current.balance,
1039
+ usdValue: current.usdValue
1040
+ });
1041
+ }
1042
+ }
1043
+ const availableByToken = {};
1044
+ for (const action of actionsSummary) {
1045
+ if (action.type === "withdraw") {
1046
+ const key = action.token.toUpperCase();
1047
+ if (!availableByToken[key]) availableByToken[key] = { usd: 0, tokenAmount: 0 };
1048
+ availableByToken[key].usd += action.usdValue;
1049
+ availableByToken[key].tokenAmount += parseFloat(action.amount);
1050
+ }
1051
+ }
1052
+ for (const [tokenSymbol, tokenData] of Object.entries(balancesRaw.balances || {})) {
1053
+ const td = tokenData;
1054
+ const usdVal = parseFloat(td.usd_value || td.usdValue || "0");
1055
+ const bal = parseFloat(td.balance_formatted || td.balanceFormatted || "0");
1056
+ if (usdVal > MIN_THRESHOLD_USD) {
1057
+ const key = tokenSymbol.toUpperCase();
1058
+ if (!availableByToken[key]) availableByToken[key] = { usd: 0, tokenAmount: 0 };
1059
+ availableByToken[key].usd += usdVal;
1060
+ availableByToken[key].tokenAmount += bal;
1061
+ }
1062
+ }
1063
+ const depositNeedsByToken = {};
1064
+ for (const dep of pendingDeposits) {
1065
+ const key = dep.token.toUpperCase();
1066
+ depositNeedsByToken[key] = (depositNeedsByToken[key] || 0) + dep.deltaUsd;
1067
+ }
1068
+ for (const [depositToken, neededUsd] of Object.entries(depositNeedsByToken)) {
1069
+ const availableUsd = availableByToken[depositToken]?.usd || 0;
1070
+ let shortfallUsd = neededUsd - availableUsd;
1071
+ if (shortfallUsd <= MIN_THRESHOLD_USD) continue;
1072
+ for (const [sourceToken, sourceData] of Object.entries(availableByToken)) {
1073
+ if (sourceToken === depositToken) continue;
1074
+ const sourceNeeded = depositNeedsByToken[sourceToken] || 0;
1075
+ const sourceExcess = sourceData.usd - sourceNeeded;
1076
+ if (sourceExcess <= MIN_THRESHOLD_USD) continue;
1077
+ const swapUsd = Math.min(shortfallUsd, sourceExcess);
1078
+ if (swapUsd < MIN_THRESHOLD_USD) continue;
1079
+ const tokenAmountIn = sourceData.usd > 0 ? swapUsd / sourceData.usd * sourceData.tokenAmount : tokenPrices[sourceToken] ? swapUsd / tokenPrices[sourceToken] : swapUsd;
1080
+ bundleActions.push({
1081
+ body: {
1082
+ actionType: "V2_SWAP",
1083
+ tokenIn: sourceToken,
1084
+ tokenOut: depositToken,
1085
+ amountIn: tokenAmountIn.toString(),
1086
+ slippage
1087
+ }
1088
+ });
1089
+ actionsSummary.push({
1090
+ type: "swap",
1091
+ token: sourceToken,
1092
+ tokenOut: depositToken,
1093
+ amount: tokenAmountIn,
1094
+ usdValue: swapUsd
1095
+ });
1096
+ sourceData.usd -= swapUsd;
1097
+ sourceData.tokenAmount -= tokenAmountIn;
1098
+ const slippageFactor = 1 - slippage / 100;
1099
+ if (!availableByToken[depositToken]) availableByToken[depositToken] = { usd: 0, tokenAmount: 0 };
1100
+ const receivedUsd = swapUsd * slippageFactor;
1101
+ const existingData = availableByToken[depositToken];
1102
+ const impliedPrice = existingData.tokenAmount > 0 && existingData.usd > 0 ? existingData.usd / existingData.tokenAmount : tokenPrices[depositToken] || 1;
1103
+ availableByToken[depositToken].usd += receivedUsd;
1104
+ availableByToken[depositToken].tokenAmount += receivedUsd / impliedPrice;
1105
+ shortfallUsd -= swapUsd;
1106
+ warnings.push(`Swap ${sourceToken} to ${depositToken} involves slippage risk`);
1107
+ if (shortfallUsd <= MIN_THRESHOLD_USD) break;
1108
+ }
1109
+ }
1110
+ for (const dep of pendingDeposits) {
1111
+ const key = dep.token.toUpperCase();
1112
+ const available = availableByToken[key];
1113
+ const tokenPrice = available && available.tokenAmount > 0 && available.usd > 0 ? available.usd / available.tokenAmount : tokenPrices[key] || 1;
1114
+ const desiredTokens = dep.deltaUsd / tokenPrice;
1115
+ const maxAvailableTokens = available ? available.tokenAmount * 0.95 : 0;
1116
+ const maxAvailableUsd = maxAvailableTokens * tokenPrice;
1117
+ if (maxAvailableUsd <= MIN_THRESHOLD_USD) {
1118
+ warnings.push(`Skipping deposit to ${dep.token} - insufficient available balance`);
1119
+ continue;
1120
+ }
1121
+ const depositTokenAmount = Math.min(desiredTokens, maxAvailableTokens);
1122
+ bundleActions.push({
1123
+ body: {
1124
+ actionType: "V2_MANAGE",
1125
+ venue: dep.venue,
1126
+ action: "DEPOSIT",
1127
+ amount: depositTokenAmount.toString()
1128
+ }
1129
+ });
1130
+ const depositUsd = depositTokenAmount * tokenPrice;
1131
+ actionsSummary.push({
1132
+ type: "deposit",
1133
+ venue: dep.venueAddress,
1134
+ token: dep.token,
1135
+ amount: depositTokenAmount.toString(),
1136
+ usdValue: depositUsd
1137
+ });
1138
+ if (available) {
1139
+ available.usd -= depositUsd;
1140
+ available.tokenAmount -= depositTokenAmount;
1141
+ }
1142
+ }
1143
+ if (bundleActions.length === 0 && pendingDeposits.length === 0) {
1144
+ return {
1145
+ actions: [],
1146
+ actionsCount: 0,
1147
+ warnings: ["Portfolio is already at target allocation"]
1148
+ };
1149
+ }
1150
+ bundleActions.sort((a, b) => {
1151
+ const getOrder = (action) => {
1152
+ if (action.body.action === "WITHDRAW") return 0;
1153
+ if (action.body.actionType === "V2_SWAP") return 1;
1154
+ if (action.body.action === "DEPOSIT") return 2;
1155
+ return 3;
1156
+ };
1157
+ return getOrder(a) - getOrder(b);
1158
+ });
1159
+ actionsSummary.sort((a, b) => {
1160
+ const order = { withdraw: 0, swap: 1, deposit: 2 };
1161
+ return (order[a.type] || 0) - (order[b.type] || 0);
1162
+ });
1163
+ if (actionsSummary.some((a) => a.type === "swap")) {
1164
+ warnings.push("Swap amounts are estimates - actual amounts may vary due to slippage");
1165
+ }
1166
+ const bundleResponse = await this.client.earn.earnBundle({
1167
+ owner,
1168
+ chain,
1169
+ gasSponsorship: true,
1170
+ actions: bundleActions
1171
+ });
1172
+ const eip712 = bundleResponse.eip712;
1173
+ if (!eip712) {
1174
+ throw new CompassServiceError("No EIP-712 data returned from bundle API", 500);
1175
+ }
1176
+ const types = eip712.types;
1177
+ const normalizedTypes = {
1178
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
1179
+ SafeTx: types.safeTx || types.SafeTx
1180
+ };
1181
+ return {
1182
+ eip712,
1183
+ normalizedTypes,
1184
+ domain: eip712.domain,
1185
+ message: eip712.message,
1186
+ actions: actionsSummary,
1187
+ actionsCount: bundleActions.length,
1188
+ warnings
1189
+ };
1190
+ } catch (error) {
1191
+ if (error instanceof CompassServiceError) throw error;
1192
+ const message = error instanceof Error ? error.message : "Failed to compute rebalance preview";
1193
+ throw new CompassServiceError(message, 502);
1194
+ }
1195
+ }
1196
+ // --- Credit ---
1197
+ async creditAccountCheck(params) {
1198
+ const { owner, chain = "base" } = params;
1199
+ if (!owner) {
1200
+ throw new CompassServiceError("Missing owner parameter", 400);
1201
+ }
1202
+ const response = await this.client.credit.creditCreateAccount({
1203
+ chain,
1204
+ owner,
1205
+ sender: owner,
1206
+ estimateGas: false
1207
+ });
1208
+ const creditAccountAddress = response.creditAccountAddress;
1209
+ const hasTransaction = !!response.transaction;
1210
+ return {
1211
+ creditAccountAddress,
1212
+ isDeployed: !hasTransaction,
1213
+ needsCreation: hasTransaction
1214
+ };
1215
+ }
1216
+ async creditCreateAccount(body) {
1217
+ const { owner, chain = "base" } = body;
1218
+ const { gasSponsorPrivateKey, rpcUrls } = this.config;
1219
+ if (!owner) {
1220
+ throw new CompassServiceError("Missing owner parameter", 400);
1221
+ }
1222
+ if (!gasSponsorPrivateKey) {
1223
+ throw new CompassServiceError(
1224
+ "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config.",
1225
+ 500
1226
+ );
1227
+ }
1228
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
1229
+ if (!viemChain) {
1230
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 500);
1231
+ }
1232
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
1233
+ if (!rpcUrl) {
1234
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
1235
+ }
1236
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
1237
+ const walletClient = createWalletClient({
1238
+ account: sponsorAccount,
1239
+ chain: viemChain,
1240
+ transport: http(rpcUrl)
1241
+ });
1242
+ const publicClient = createPublicClient({
1243
+ chain: viemChain,
1244
+ transport: http(rpcUrl)
1245
+ });
1246
+ const response = await this.client.credit.creditCreateAccount({
1247
+ chain,
1248
+ owner,
1249
+ sender: sponsorAccount.address,
1250
+ estimateGas: false
1251
+ });
1252
+ const creditAccountAddress = response.creditAccountAddress;
1253
+ if (!response.transaction) {
1254
+ return {
1255
+ creditAccountAddress,
1256
+ success: true,
1257
+ alreadyExists: true
1258
+ };
1259
+ }
1260
+ const transaction = response.transaction;
1261
+ const txHash = await walletClient.sendTransaction({
1262
+ to: transaction.to,
1263
+ data: transaction.data,
1264
+ value: transaction.value ? BigInt(transaction.value) : 0n,
1265
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
1266
+ });
1267
+ const receipt = await publicClient.waitForTransactionReceipt({
1268
+ hash: txHash
1269
+ });
1270
+ if (receipt.status === "reverted") {
1271
+ throw new CompassServiceError("Account creation transaction reverted", 500);
1272
+ }
1273
+ return {
1274
+ creditAccountAddress,
1275
+ txHash,
1276
+ success: true
1277
+ };
1278
+ }
1279
+ async creditPositions(params) {
1280
+ const { owner, chain = "base" } = params;
1281
+ if (!owner) {
1282
+ throw new CompassServiceError("Missing owner parameter", 400);
1283
+ }
1284
+ const response = await this.client.credit.creditPositions({
1285
+ chain,
1286
+ owner
1287
+ });
1288
+ return response;
1289
+ }
1290
+ async creditBalances(params) {
1291
+ const { owner, chain = "base" } = params;
1292
+ if (!owner) {
1293
+ throw new CompassServiceError("Missing owner parameter", 400);
1294
+ }
1295
+ const tokens = CREDIT_TOKENS[chain.toLowerCase()] || CREDIT_TOKENS["base"];
1296
+ const balances = await Promise.allSettled(
1297
+ tokens.map(async (token) => {
1298
+ const response = await this.client.token.tokenBalance({
1299
+ chain,
1300
+ token,
1301
+ user: owner
1302
+ });
1303
+ return {
1304
+ tokenSymbol: token,
1305
+ amount: response.amount || "0",
1306
+ decimals: response.decimals || 18,
1307
+ tokenAddress: response.tokenAddress || ""
1308
+ };
1309
+ })
1310
+ );
1311
+ const result = balances.filter((b) => b.status === "fulfilled").map((b) => b.value);
1312
+ return result;
1313
+ }
1314
+ async creditBundlePrepare(body) {
1315
+ const { owner, chain = "base", actions } = body;
1316
+ if (!owner || !actions || actions.length === 0) {
1317
+ throw new CompassServiceError("Missing owner or actions", 400);
1318
+ }
1319
+ const wrappedActions = actions.map((action) => ({ body: action }));
1320
+ const response = await this.client.credit.creditBundle({
1321
+ owner,
1322
+ chain,
1323
+ gasSponsorship: true,
1324
+ actions: wrappedActions
1325
+ });
1326
+ const eip712 = response.eip712;
1327
+ if (!eip712) {
1328
+ throw new CompassServiceError("No EIP-712 data returned from API", 500);
1329
+ }
1330
+ const types = eip712.types;
1331
+ const normalizedTypes = {
1332
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
1333
+ SafeTx: types.safeTx || types.SafeTx
1334
+ };
1335
+ return {
1336
+ eip712,
1337
+ normalizedTypes,
1338
+ domain: eip712.domain,
1339
+ message: eip712.message,
1340
+ actionsCount: response.actionsCount || actions.length
1341
+ };
1342
+ }
1343
+ async creditTransfer(body) {
1344
+ const { owner, chain = "base", token, amount } = body;
1345
+ if (!owner || !token || !amount) {
1346
+ throw new CompassServiceError("Missing required parameters", 400);
1347
+ }
1348
+ const response = await this.client.credit.creditTransfer({
1349
+ owner,
1350
+ chain,
1351
+ token,
1352
+ amount,
1353
+ action: "DEPOSIT",
1354
+ gasSponsorship: true
1355
+ });
1356
+ const eip712 = response.eip712;
1357
+ if (!eip712) {
1358
+ throw new CompassServiceError("No EIP-712 data returned from API", 500);
1359
+ }
1360
+ const types = eip712.types;
1361
+ const normalizedTypes = {
1362
+ EIP712Domain: types.eip712Domain || types.EIP712Domain
1363
+ };
1364
+ if (types.permitTransferFrom || types.PermitTransferFrom) {
1365
+ normalizedTypes.PermitTransferFrom = types.permitTransferFrom || types.PermitTransferFrom;
1366
+ }
1367
+ if (types.tokenPermissions || types.TokenPermissions) {
1368
+ normalizedTypes.TokenPermissions = types.tokenPermissions || types.TokenPermissions;
1369
+ }
1370
+ if (types.safeTx || types.SafeTx) {
1371
+ normalizedTypes.SafeTx = types.safeTx || types.SafeTx;
1372
+ }
1373
+ return {
1374
+ eip712,
1375
+ normalizedTypes,
1376
+ domain: eip712.domain,
1377
+ message: eip712.message,
1378
+ primaryType: eip712.primaryType
1379
+ };
1380
+ }
1381
+ async creditExecute(body) {
1382
+ const { owner, eip712, signature, chain = "base" } = body;
1383
+ const { gasSponsorPrivateKey, rpcUrls } = this.config;
1384
+ if (!owner || !eip712 || !signature) {
1385
+ throw new CompassServiceError("Missing required parameters (owner, eip712, signature)", 400);
1386
+ }
1387
+ if (!gasSponsorPrivateKey) {
1388
+ throw new CompassServiceError(
1389
+ "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config.",
1390
+ 500
1391
+ );
1392
+ }
1393
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
1394
+ if (!viemChain) {
1395
+ throw new CompassServiceError(`Unsupported chain: ${chain}`, 500);
1396
+ }
1397
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
1398
+ if (!rpcUrl) {
1399
+ throw new CompassServiceError(`No RPC URL configured for chain: ${chain}`, 500);
1400
+ }
1401
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
1402
+ const walletClient = createWalletClient({
1403
+ account: sponsorAccount,
1404
+ chain: viemChain,
1405
+ transport: http(rpcUrl)
1406
+ });
1407
+ const publicClient = createPublicClient({
1408
+ chain: viemChain,
1409
+ transport: http(rpcUrl)
1410
+ });
1411
+ const response = await this.client.gasSponsorship.gasSponsorshipPrepare({
1412
+ chain,
1413
+ owner,
1414
+ sender: sponsorAccount.address,
1415
+ eip712,
1416
+ signature,
1417
+ product: "credit"
1418
+ });
1419
+ const transaction = response.transaction;
1420
+ if (!transaction) {
1421
+ throw new CompassServiceError(
1422
+ "No transaction returned from gas sponsorship prepare",
1423
+ 500
1424
+ );
1425
+ }
1426
+ const txHash = await walletClient.sendTransaction({
1427
+ to: transaction.to,
1428
+ data: transaction.data,
1429
+ value: transaction.value ? BigInt(transaction.value) : 0n,
1430
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
1431
+ });
1432
+ const receipt = await publicClient.waitForTransactionReceipt({
1433
+ hash: txHash
1434
+ });
1435
+ if (receipt.status === "reverted") {
1436
+ throw new CompassServiceError("Transaction reverted", 500);
1437
+ }
1438
+ return { txHash, success: true };
1439
+ }
1440
+ };
1441
+
1442
+ // src/server/nestjs/compass.service.ts
1443
+ var COMPASS_CONFIG = /* @__PURE__ */ Symbol("COMPASS_CONFIG");
1444
+ var CompassService = class extends CompassCoreService {
1445
+ constructor(config) {
1446
+ super(config);
1447
+ }
1448
+ };
1449
+ CompassService = __decorateClass([
1450
+ Injectable(),
1451
+ __decorateParam(0, Inject(COMPASS_CONFIG))
1452
+ ], CompassService);
1453
+
1454
+ // src/server/core/utils.ts
1455
+ function extractErrorMessage(error) {
1456
+ if (!(error instanceof Error)) {
1457
+ return { message: "Something went wrong. Please try again.", status: 500 };
1458
+ }
1459
+ const raw = error.message || "";
1460
+ const jsonMatch = raw.match(/Body:\s*(\{[\s\S]*\})/);
1461
+ if (jsonMatch) {
1462
+ try {
1463
+ const body = JSON.parse(jsonMatch[1]);
1464
+ if (Array.isArray(body.detail)) {
1465
+ const msgs = body.detail.map((d) => d.msg || d.message).filter(Boolean);
1466
+ if (msgs.length > 0) return { message: msgs.join(". "), status: 422 };
1467
+ }
1468
+ if (typeof body.detail === "string") return { message: body.detail, status: 422 };
1469
+ if (typeof body.error === "string") return { message: body.error, status: 500 };
1470
+ if (typeof body.description === "string") return { message: body.description, status: 500 };
1471
+ } catch {
1472
+ }
1473
+ }
1474
+ if (error.name === "SDKValidationError" || raw.startsWith("Input validation failed")) {
1475
+ return { message: "Invalid request data. Please check your inputs and try again.", status: 400 };
1476
+ }
1477
+ const knownPatterns = ["Insufficient", "not deployed", "reverted", "not configured", "Unsupported chain"];
1478
+ const lines = raw.split("\n");
1479
+ for (const pattern of knownPatterns) {
1480
+ const matchingLine = lines.find((line) => line.includes(pattern));
1481
+ if (matchingLine) {
1482
+ return { message: matchingLine.trim(), status: 500 };
1483
+ }
1484
+ }
1485
+ return { message: "Something went wrong. Please try again.", status: 500 };
1486
+ }
1487
+
1488
+ // src/server/nestjs/compass-exception.filter.ts
1489
+ var CompassExceptionFilter = class {
1490
+ catch(exception, host) {
1491
+ const ctx = host.switchToHttp();
1492
+ const response = ctx.getResponse();
1493
+ if (exception instanceof CompassServiceError) {
1494
+ response.status(exception.statusCode).json({ error: exception.message });
1495
+ return;
1496
+ }
1497
+ if (exception instanceof HttpException) {
1498
+ const status2 = exception.getStatus();
1499
+ const body = exception.getResponse();
1500
+ response.status(status2).json(typeof body === "string" ? { error: body } : body);
1501
+ return;
1502
+ }
1503
+ const { message, status } = extractErrorMessage(exception);
1504
+ response.status(status).json({ error: message });
1505
+ }
1506
+ };
1507
+ CompassExceptionFilter = __decorateClass([
1508
+ Catch()
1509
+ ], CompassExceptionFilter);
1510
+
1511
+ // src/server/nestjs/earn.controller.ts
1512
+ var EarnController = class {
1513
+ constructor(compass) {
1514
+ this.compass = compass;
1515
+ }
1516
+ earnAccountCheck(params) {
1517
+ return this.compass.earnAccountCheck(params);
1518
+ }
1519
+ earnAccountBalances(params) {
1520
+ return this.compass.earnAccountBalances(params);
1521
+ }
1522
+ createAccount(body) {
1523
+ return this.compass.createAccount(body);
1524
+ }
1525
+ depositPrepare(body) {
1526
+ return this.compass.managePrepare(body, "DEPOSIT");
1527
+ }
1528
+ depositExecute(body) {
1529
+ return this.compass.execute(body);
1530
+ }
1531
+ withdrawPrepare(body) {
1532
+ return this.compass.managePrepare(body, "WITHDRAW");
1533
+ }
1534
+ withdrawExecute(body) {
1535
+ return this.compass.execute(body);
1536
+ }
1537
+ positions(params) {
1538
+ return this.compass.positions(params);
1539
+ }
1540
+ vaults(params) {
1541
+ return this.compass.vaults(params);
1542
+ }
1543
+ aaveMarkets(params) {
1544
+ return this.compass.aaveMarkets(params);
1545
+ }
1546
+ pendleMarkets(params) {
1547
+ return this.compass.pendleMarkets(params);
1548
+ }
1549
+ };
1550
+ __decorateClass([
1551
+ Get("earn-account/check"),
1552
+ __decorateParam(0, Query())
1553
+ ], EarnController.prototype, "earnAccountCheck", 1);
1554
+ __decorateClass([
1555
+ Get("earn-account/balances"),
1556
+ __decorateParam(0, Query())
1557
+ ], EarnController.prototype, "earnAccountBalances", 1);
1558
+ __decorateClass([
1559
+ HttpCode(200),
1560
+ Post("create-account"),
1561
+ __decorateParam(0, Body())
1562
+ ], EarnController.prototype, "createAccount", 1);
1563
+ __decorateClass([
1564
+ HttpCode(200),
1565
+ Post("deposit/prepare"),
1566
+ __decorateParam(0, Body())
1567
+ ], EarnController.prototype, "depositPrepare", 1);
1568
+ __decorateClass([
1569
+ HttpCode(200),
1570
+ Post("deposit/execute"),
1571
+ __decorateParam(0, Body())
1572
+ ], EarnController.prototype, "depositExecute", 1);
1573
+ __decorateClass([
1574
+ HttpCode(200),
1575
+ Post("withdraw/prepare"),
1576
+ __decorateParam(0, Body())
1577
+ ], EarnController.prototype, "withdrawPrepare", 1);
1578
+ __decorateClass([
1579
+ HttpCode(200),
1580
+ Post("withdraw/execute"),
1581
+ __decorateParam(0, Body())
1582
+ ], EarnController.prototype, "withdrawExecute", 1);
1583
+ __decorateClass([
1584
+ Get("positions"),
1585
+ __decorateParam(0, Query())
1586
+ ], EarnController.prototype, "positions", 1);
1587
+ __decorateClass([
1588
+ Get("vaults"),
1589
+ __decorateParam(0, Query())
1590
+ ], EarnController.prototype, "vaults", 1);
1591
+ __decorateClass([
1592
+ Get("aave/markets"),
1593
+ __decorateParam(0, Query())
1594
+ ], EarnController.prototype, "aaveMarkets", 1);
1595
+ __decorateClass([
1596
+ Get("pendle/markets"),
1597
+ __decorateParam(0, Query())
1598
+ ], EarnController.prototype, "pendleMarkets", 1);
1599
+ EarnController = __decorateClass([
1600
+ Controller("compass"),
1601
+ UseFilters(CompassExceptionFilter),
1602
+ __decorateParam(0, Inject(CompassService))
1603
+ ], EarnController);
1604
+ var CreditController = class {
1605
+ constructor(compass) {
1606
+ this.compass = compass;
1607
+ }
1608
+ creditAccountCheck(params) {
1609
+ return this.compass.creditAccountCheck(params);
1610
+ }
1611
+ creditCreateAccount(body) {
1612
+ return this.compass.creditCreateAccount(body);
1613
+ }
1614
+ creditPositions(params) {
1615
+ return this.compass.creditPositions(params);
1616
+ }
1617
+ creditBalances(params) {
1618
+ return this.compass.creditBalances(params);
1619
+ }
1620
+ creditBundlePrepare(body) {
1621
+ return this.compass.creditBundlePrepare(body);
1622
+ }
1623
+ creditExecute(body) {
1624
+ return this.compass.creditExecute(body);
1625
+ }
1626
+ creditTransfer(body) {
1627
+ return this.compass.creditTransfer(body);
1628
+ }
1629
+ };
1630
+ __decorateClass([
1631
+ Get("credit-account/check"),
1632
+ __decorateParam(0, Query())
1633
+ ], CreditController.prototype, "creditAccountCheck", 1);
1634
+ __decorateClass([
1635
+ HttpCode(200),
1636
+ Post("credit-account/create"),
1637
+ __decorateParam(0, Body())
1638
+ ], CreditController.prototype, "creditCreateAccount", 1);
1639
+ __decorateClass([
1640
+ Get("credit/positions"),
1641
+ __decorateParam(0, Query())
1642
+ ], CreditController.prototype, "creditPositions", 1);
1643
+ __decorateClass([
1644
+ Get("credit/balances"),
1645
+ __decorateParam(0, Query())
1646
+ ], CreditController.prototype, "creditBalances", 1);
1647
+ __decorateClass([
1648
+ HttpCode(200),
1649
+ Post("credit/bundle/prepare"),
1650
+ __decorateParam(0, Body())
1651
+ ], CreditController.prototype, "creditBundlePrepare", 1);
1652
+ __decorateClass([
1653
+ HttpCode(200),
1654
+ Post("credit/bundle/execute"),
1655
+ __decorateParam(0, Body())
1656
+ ], CreditController.prototype, "creditExecute", 1);
1657
+ __decorateClass([
1658
+ HttpCode(200),
1659
+ Post("credit/transfer"),
1660
+ __decorateParam(0, Body())
1661
+ ], CreditController.prototype, "creditTransfer", 1);
1662
+ CreditController = __decorateClass([
1663
+ Controller("compass"),
1664
+ UseFilters(CompassExceptionFilter),
1665
+ __decorateParam(0, Inject(CompassService))
1666
+ ], CreditController);
1667
+ var TokenController = class {
1668
+ constructor(compass) {
1669
+ this.compass = compass;
1670
+ }
1671
+ tokenBalance(params) {
1672
+ return this.compass.tokenBalance(params);
1673
+ }
1674
+ tokenPrices(params) {
1675
+ return this.compass.tokenPrices(params);
1676
+ }
1677
+ swapQuote(params) {
1678
+ return this.compass.swapQuote(params);
1679
+ }
1680
+ swapPrepare(body) {
1681
+ return this.compass.swapPrepare(body);
1682
+ }
1683
+ swapExecute(body) {
1684
+ return this.compass.swapExecute(body);
1685
+ }
1686
+ txReceipt(params) {
1687
+ return this.compass.txReceipt(params);
1688
+ }
1689
+ };
1690
+ __decorateClass([
1691
+ Get("token/balance"),
1692
+ __decorateParam(0, Query())
1693
+ ], TokenController.prototype, "tokenBalance", 1);
1694
+ __decorateClass([
1695
+ Get("token/prices"),
1696
+ __decorateParam(0, Query())
1697
+ ], TokenController.prototype, "tokenPrices", 1);
1698
+ __decorateClass([
1699
+ Get("swap/quote"),
1700
+ __decorateParam(0, Query())
1701
+ ], TokenController.prototype, "swapQuote", 1);
1702
+ __decorateClass([
1703
+ HttpCode(200),
1704
+ Post("swap/prepare"),
1705
+ __decorateParam(0, Body())
1706
+ ], TokenController.prototype, "swapPrepare", 1);
1707
+ __decorateClass([
1708
+ HttpCode(200),
1709
+ Post("swap/execute"),
1710
+ __decorateParam(0, Body())
1711
+ ], TokenController.prototype, "swapExecute", 1);
1712
+ __decorateClass([
1713
+ Get("tx/receipt"),
1714
+ __decorateParam(0, Query())
1715
+ ], TokenController.prototype, "txReceipt", 1);
1716
+ TokenController = __decorateClass([
1717
+ Controller("compass"),
1718
+ UseFilters(CompassExceptionFilter),
1719
+ __decorateParam(0, Inject(CompassService))
1720
+ ], TokenController);
1721
+ var BundleController = class {
1722
+ constructor(compass) {
1723
+ this.compass = compass;
1724
+ }
1725
+ bundlePrepare(body) {
1726
+ return this.compass.bundlePrepare(body);
1727
+ }
1728
+ bundleExecute(body) {
1729
+ return this.compass.bundleExecute(body);
1730
+ }
1731
+ rebalancePreview(body) {
1732
+ return this.compass.rebalancePreview(body);
1733
+ }
1734
+ transferApprove(body) {
1735
+ return this.compass.transferApprove(body);
1736
+ }
1737
+ transferPrepare(body) {
1738
+ return this.compass.transferPrepare(body);
1739
+ }
1740
+ transferExecute(body) {
1741
+ return this.compass.transferExecute(body);
1742
+ }
1743
+ approvalExecute(body) {
1744
+ return this.compass.approvalExecute(body);
1745
+ }
1746
+ };
1747
+ __decorateClass([
1748
+ HttpCode(200),
1749
+ Post("bundle/prepare"),
1750
+ __decorateParam(0, Body())
1751
+ ], BundleController.prototype, "bundlePrepare", 1);
1752
+ __decorateClass([
1753
+ HttpCode(200),
1754
+ Post("bundle/execute"),
1755
+ __decorateParam(0, Body())
1756
+ ], BundleController.prototype, "bundleExecute", 1);
1757
+ __decorateClass([
1758
+ HttpCode(200),
1759
+ Post("rebalance/preview"),
1760
+ __decorateParam(0, Body())
1761
+ ], BundleController.prototype, "rebalancePreview", 1);
1762
+ __decorateClass([
1763
+ HttpCode(200),
1764
+ Post("transfer/approve"),
1765
+ __decorateParam(0, Body())
1766
+ ], BundleController.prototype, "transferApprove", 1);
1767
+ __decorateClass([
1768
+ HttpCode(200),
1769
+ Post("transfer/prepare"),
1770
+ __decorateParam(0, Body())
1771
+ ], BundleController.prototype, "transferPrepare", 1);
1772
+ __decorateClass([
1773
+ HttpCode(200),
1774
+ Post("transfer/execute"),
1775
+ __decorateParam(0, Body())
1776
+ ], BundleController.prototype, "transferExecute", 1);
1777
+ __decorateClass([
1778
+ HttpCode(200),
1779
+ Post("approval/execute"),
1780
+ __decorateParam(0, Body())
1781
+ ], BundleController.prototype, "approvalExecute", 1);
1782
+ BundleController = __decorateClass([
1783
+ Controller("compass"),
1784
+ UseFilters(CompassExceptionFilter),
1785
+ __decorateParam(0, Inject(CompassService))
1786
+ ], BundleController);
1787
+
1788
+ // src/server/nestjs/compass.module.ts
1789
+ var CompassModule = class {
1790
+ static register(config) {
1791
+ return {
1792
+ module: CompassModule,
1793
+ providers: [
1794
+ {
1795
+ provide: COMPASS_CONFIG,
1796
+ useValue: config
1797
+ },
1798
+ CompassService
1799
+ ],
1800
+ controllers: [
1801
+ EarnController,
1802
+ CreditController,
1803
+ TokenController,
1804
+ BundleController
1805
+ ],
1806
+ exports: [CompassService]
1807
+ };
1808
+ }
1809
+ static registerAsync(options) {
1810
+ return {
1811
+ module: CompassModule,
1812
+ imports: options.imports || [],
1813
+ providers: [
1814
+ {
1815
+ provide: COMPASS_CONFIG,
1816
+ useFactory: options.useFactory,
1817
+ inject: options.inject || []
1818
+ },
1819
+ CompassService
1820
+ ],
1821
+ controllers: [
1822
+ EarnController,
1823
+ CreditController,
1824
+ TokenController,
1825
+ BundleController
1826
+ ],
1827
+ exports: [CompassService]
1828
+ };
1829
+ }
1830
+ };
1831
+ CompassModule = __decorateClass([
1832
+ Module({})
1833
+ ], CompassModule);
1834
+
1835
+ export { BundleController, COMPASS_CONFIG, CompassExceptionFilter, CompassModule, CompassService, CreditController, EarnController, TokenController };
1836
+ //# sourceMappingURL=index.mjs.map
1837
+ //# sourceMappingURL=index.mjs.map