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