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