@compass-labs/widgets 0.1.34 → 0.1.36

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.
@@ -32,6 +32,8 @@ function createCompassHandler(config) {
32
32
  return await handleSwapQuote(client, searchParams);
33
33
  case "token/balance":
34
34
  return await handleTokenBalance(client, searchParams);
35
+ case "token/prices":
36
+ return await handleTokenPrices(client, searchParams);
35
37
  case "vaults":
36
38
  return await handleVaults(client, searchParams);
37
39
  case "aave/markets":
@@ -40,6 +42,14 @@ function createCompassHandler(config) {
40
42
  return await handlePendleMarkets(client, searchParams);
41
43
  case "positions":
42
44
  return await handlePositions(client, searchParams);
45
+ case "credit-account/check":
46
+ return await handleCreditAccountCheck(client, searchParams);
47
+ case "credit/positions":
48
+ return await handleCreditPositions(client, searchParams);
49
+ case "credit/balances":
50
+ return await handleCreditBalances(client, searchParams);
51
+ case "tx/receipt":
52
+ return await handleTxReceipt(searchParams, config);
43
53
  default:
44
54
  return jsonResponse({ error: `Unknown GET route: ${route}` }, 404);
45
55
  }
@@ -73,6 +83,14 @@ function createCompassHandler(config) {
73
83
  return await handleSwapExecute(client, body, config);
74
84
  case "rebalance/preview":
75
85
  return await handleRebalancePreview(client, body, config);
86
+ case "credit-account/create":
87
+ return await handleCreditCreateAccount(client, body, config);
88
+ case "credit/bundle/prepare":
89
+ return await handleCreditBundlePrepare(client, body);
90
+ case "credit/bundle/execute":
91
+ return await handleCreditExecute(client, body, config);
92
+ case "credit/transfer":
93
+ return await handleCreditTransfer(client, body);
76
94
  default:
77
95
  return jsonResponse({ error: `Unknown POST route: ${route}` }, 404);
78
96
  }
@@ -646,6 +664,26 @@ async function handleTokenBalance(client, params) {
646
664
  });
647
665
  }
648
666
  }
667
+ async function handleTokenPrices(client, params) {
668
+ const { chain = "base", tokens } = params;
669
+ if (!tokens) {
670
+ return jsonResponse({ error: "Missing tokens parameter" }, 400);
671
+ }
672
+ const tokenList = tokens.split(",").map((t) => t.trim().toUpperCase());
673
+ const prices = {};
674
+ const results = await Promise.allSettled(
675
+ tokenList.map(async (symbol) => {
676
+ const resp = await client.token.tokenPrice({ chain, token: symbol });
677
+ return { symbol, price: parseFloat(resp.price || "0") };
678
+ })
679
+ );
680
+ for (const result of results) {
681
+ if (result.status === "fulfilled" && result.value.price > 0) {
682
+ prices[result.value.symbol] = result.value.price;
683
+ }
684
+ }
685
+ return jsonResponse({ prices });
686
+ }
649
687
  async function handleBundlePrepare(client, body) {
650
688
  const { owner, chain = "base", actions } = body;
651
689
  if (!owner || !actions || actions.length === 0) {
@@ -1161,6 +1199,279 @@ async function handleRebalancePreview(client, body, config) {
1161
1199
  return jsonResponse({ error: message }, 502);
1162
1200
  }
1163
1201
  }
1202
+ var CREDIT_TOKENS = {
1203
+ base: ["USDC", "WETH", "USDT", "DAI", "WBTC"],
1204
+ ethereum: ["USDC", "WETH", "USDT", "DAI", "WBTC"],
1205
+ arbitrum: ["USDC", "WETH", "USDT", "DAI", "WBTC"]
1206
+ };
1207
+ async function handleCreditAccountCheck(client, params) {
1208
+ const { owner, chain = "base" } = params;
1209
+ if (!owner) {
1210
+ return jsonResponse({ error: "Missing owner parameter" }, 400);
1211
+ }
1212
+ const response = await client.credit.creditCreateAccount({
1213
+ chain,
1214
+ owner,
1215
+ sender: owner,
1216
+ estimateGas: false
1217
+ });
1218
+ const creditAccountAddress = response.creditAccountAddress;
1219
+ const hasTransaction = !!response.transaction;
1220
+ return jsonResponse({
1221
+ creditAccountAddress,
1222
+ isDeployed: !hasTransaction,
1223
+ needsCreation: hasTransaction
1224
+ });
1225
+ }
1226
+ async function handleCreditCreateAccount(client, body, config) {
1227
+ const { owner, chain = "base" } = body;
1228
+ const { gasSponsorPrivateKey, rpcUrls } = config;
1229
+ if (!owner) {
1230
+ return jsonResponse({ error: "Missing owner parameter" }, 400);
1231
+ }
1232
+ if (!gasSponsorPrivateKey) {
1233
+ return jsonResponse(
1234
+ { error: "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config." },
1235
+ 500
1236
+ );
1237
+ }
1238
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
1239
+ if (!viemChain) {
1240
+ return jsonResponse({ error: `Unsupported chain: ${chain}` }, 500);
1241
+ }
1242
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
1243
+ if (!rpcUrl) {
1244
+ return jsonResponse({ error: `No RPC URL configured for chain: ${chain}` }, 500);
1245
+ }
1246
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
1247
+ const walletClient = createWalletClient({
1248
+ account: sponsorAccount,
1249
+ chain: viemChain,
1250
+ transport: http(rpcUrl)
1251
+ });
1252
+ const publicClient = createPublicClient({
1253
+ chain: viemChain,
1254
+ transport: http(rpcUrl)
1255
+ });
1256
+ const response = await client.credit.creditCreateAccount({
1257
+ chain,
1258
+ owner,
1259
+ sender: sponsorAccount.address,
1260
+ estimateGas: false
1261
+ });
1262
+ const creditAccountAddress = response.creditAccountAddress;
1263
+ if (!response.transaction) {
1264
+ return jsonResponse({
1265
+ creditAccountAddress,
1266
+ success: true,
1267
+ alreadyExists: true
1268
+ });
1269
+ }
1270
+ const transaction = response.transaction;
1271
+ const txHash = await walletClient.sendTransaction({
1272
+ to: transaction.to,
1273
+ data: transaction.data,
1274
+ value: transaction.value ? BigInt(transaction.value) : 0n,
1275
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
1276
+ });
1277
+ const receipt = await publicClient.waitForTransactionReceipt({
1278
+ hash: txHash
1279
+ });
1280
+ if (receipt.status === "reverted") {
1281
+ return jsonResponse({ error: "Account creation transaction reverted" }, 500);
1282
+ }
1283
+ return jsonResponse({
1284
+ creditAccountAddress,
1285
+ txHash,
1286
+ success: true
1287
+ });
1288
+ }
1289
+ async function handleCreditPositions(client, params) {
1290
+ const { owner, chain = "base" } = params;
1291
+ if (!owner) {
1292
+ return jsonResponse({ error: "Missing owner parameter" }, 400);
1293
+ }
1294
+ const response = await client.credit.creditPositions({
1295
+ chain,
1296
+ owner
1297
+ });
1298
+ return jsonResponse(response);
1299
+ }
1300
+ async function handleCreditBalances(client, params) {
1301
+ const { owner, chain = "base" } = params;
1302
+ if (!owner) {
1303
+ return jsonResponse({ error: "Missing owner parameter" }, 400);
1304
+ }
1305
+ const tokens = CREDIT_TOKENS[chain.toLowerCase()] || CREDIT_TOKENS["base"];
1306
+ const balances = await Promise.allSettled(
1307
+ tokens.map(async (token) => {
1308
+ const response = await client.token.tokenBalance({
1309
+ chain,
1310
+ token,
1311
+ user: owner
1312
+ });
1313
+ return {
1314
+ tokenSymbol: token,
1315
+ amount: response.amount || "0",
1316
+ decimals: response.decimals || 18,
1317
+ tokenAddress: response.tokenAddress || ""
1318
+ };
1319
+ })
1320
+ );
1321
+ const result = balances.filter((b) => b.status === "fulfilled").map((b) => b.value);
1322
+ return jsonResponse(result);
1323
+ }
1324
+ async function handleCreditBundlePrepare(client, body) {
1325
+ const { owner, chain = "base", actions } = body;
1326
+ if (!owner || !actions || actions.length === 0) {
1327
+ return jsonResponse({ error: "Missing owner or actions" }, 400);
1328
+ }
1329
+ const wrappedActions = actions.map((action) => ({ body: action }));
1330
+ const response = await client.credit.creditBundle({
1331
+ owner,
1332
+ chain,
1333
+ gasSponsorship: true,
1334
+ actions: wrappedActions
1335
+ });
1336
+ const eip712 = response.eip712;
1337
+ if (!eip712) {
1338
+ return jsonResponse({ error: "No EIP-712 data returned from API" }, 500);
1339
+ }
1340
+ const types = eip712.types;
1341
+ const normalizedTypes = {
1342
+ EIP712Domain: types.eip712Domain || types.EIP712Domain,
1343
+ SafeTx: types.safeTx || types.SafeTx
1344
+ };
1345
+ return jsonResponse({
1346
+ eip712,
1347
+ normalizedTypes,
1348
+ domain: eip712.domain,
1349
+ message: eip712.message,
1350
+ actionsCount: response.actionsCount || actions.length
1351
+ });
1352
+ }
1353
+ async function handleCreditTransfer(client, body) {
1354
+ const { owner, chain = "base", token, amount } = body;
1355
+ if (!owner || !token || !amount) {
1356
+ return jsonResponse({ error: "Missing required parameters" }, 400);
1357
+ }
1358
+ const response = await client.credit.creditTransfer({
1359
+ owner,
1360
+ chain,
1361
+ token,
1362
+ amount,
1363
+ gasSponsorship: true
1364
+ });
1365
+ const eip712 = response.eip712;
1366
+ if (!eip712) {
1367
+ return jsonResponse({ error: "No EIP-712 data returned from API" }, 500);
1368
+ }
1369
+ const types = eip712.types;
1370
+ const normalizedTypes = {
1371
+ EIP712Domain: types.eip712Domain || types.EIP712Domain
1372
+ };
1373
+ if (types.permitTransferFrom || types.PermitTransferFrom) {
1374
+ normalizedTypes.PermitTransferFrom = types.permitTransferFrom || types.PermitTransferFrom;
1375
+ }
1376
+ if (types.tokenPermissions || types.TokenPermissions) {
1377
+ normalizedTypes.TokenPermissions = types.tokenPermissions || types.TokenPermissions;
1378
+ }
1379
+ if (types.safeTx || types.SafeTx) {
1380
+ normalizedTypes.SafeTx = types.safeTx || types.SafeTx;
1381
+ }
1382
+ return jsonResponse({
1383
+ eip712,
1384
+ normalizedTypes,
1385
+ domain: eip712.domain,
1386
+ message: eip712.message,
1387
+ primaryType: eip712.primaryType
1388
+ });
1389
+ }
1390
+ async function handleCreditExecute(client, body, config) {
1391
+ const { owner, eip712, signature, chain = "base" } = body;
1392
+ const { gasSponsorPrivateKey, rpcUrls } = config;
1393
+ if (!owner || !eip712 || !signature) {
1394
+ return jsonResponse({ error: "Missing required parameters (owner, eip712, signature)" }, 400);
1395
+ }
1396
+ if (!gasSponsorPrivateKey) {
1397
+ return jsonResponse(
1398
+ { error: "Gas sponsor not configured. Set gasSponsorPrivateKey in handler config." },
1399
+ 500
1400
+ );
1401
+ }
1402
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
1403
+ if (!viemChain) {
1404
+ return jsonResponse({ error: `Unsupported chain: ${chain}` }, 500);
1405
+ }
1406
+ const rpcUrl = rpcUrls?.[chain.toLowerCase()];
1407
+ if (!rpcUrl) {
1408
+ return jsonResponse({ error: `No RPC URL configured for chain: ${chain}` }, 500);
1409
+ }
1410
+ const sponsorAccount = privateKeyToAccount(gasSponsorPrivateKey);
1411
+ const walletClient = createWalletClient({
1412
+ account: sponsorAccount,
1413
+ chain: viemChain,
1414
+ transport: http(rpcUrl)
1415
+ });
1416
+ const publicClient = createPublicClient({
1417
+ chain: viemChain,
1418
+ transport: http(rpcUrl)
1419
+ });
1420
+ const response = await client.gasSponsorship.gasSponsorshipPrepare({
1421
+ chain,
1422
+ owner,
1423
+ sender: sponsorAccount.address,
1424
+ eip712,
1425
+ signature,
1426
+ product: "credit"
1427
+ });
1428
+ const transaction = response.transaction;
1429
+ if (!transaction) {
1430
+ return jsonResponse(
1431
+ { error: "No transaction returned from gas sponsorship prepare" },
1432
+ 500
1433
+ );
1434
+ }
1435
+ const txHash = await walletClient.sendTransaction({
1436
+ to: transaction.to,
1437
+ data: transaction.data,
1438
+ value: transaction.value ? BigInt(transaction.value) : 0n,
1439
+ gas: transaction.gas ? BigInt(transaction.gas) : void 0
1440
+ });
1441
+ const receipt = await publicClient.waitForTransactionReceipt({
1442
+ hash: txHash
1443
+ });
1444
+ if (receipt.status === "reverted") {
1445
+ return jsonResponse({ error: "Transaction reverted" }, 500);
1446
+ }
1447
+ return jsonResponse({ txHash, success: true });
1448
+ }
1449
+ async function handleTxReceipt(params, config) {
1450
+ const { hash, chain } = params;
1451
+ if (!hash || !chain) {
1452
+ return jsonResponse({ error: "Missing hash or chain parameter" }, 400);
1453
+ }
1454
+ const rpcUrl = config.rpcUrls?.[chain.toLowerCase()];
1455
+ const viemChain = CHAIN_MAP[chain.toLowerCase()];
1456
+ if (!viemChain) {
1457
+ return jsonResponse({ error: `Unsupported chain: ${chain}` }, 400);
1458
+ }
1459
+ const publicClient = createPublicClient({
1460
+ chain: viemChain,
1461
+ transport: http(rpcUrl)
1462
+ });
1463
+ try {
1464
+ const receipt = await publicClient.getTransactionReceipt({
1465
+ hash
1466
+ });
1467
+ return jsonResponse({
1468
+ status: receipt.status,
1469
+ blockNumber: receipt.blockNumber.toString()
1470
+ });
1471
+ } catch {
1472
+ return jsonResponse({ status: "pending" });
1473
+ }
1474
+ }
1164
1475
 
1165
1476
  export { createCompassHandler };
1166
1477
  //# sourceMappingURL=index.mjs.map