sdk-triggerx 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.
package/dist/api/jobs.js CHANGED
@@ -18,6 +18,7 @@ const config_1 = require("../config");
18
18
  const validation_1 = require("../utils/validation");
19
19
  const errors_1 = require("../utils/errors");
20
20
  const SafeWallet_1 = require("../contracts/safe/SafeWallet");
21
+ const traceId_1 = require("../utils/traceId");
21
22
  const JOB_ID = '300949528249665590178224313442040528409305273634097553067152835846309150732';
22
23
  const DYNAMIC_ARGS_URL = 'https://teal-random-koala-993.mypinata.cloud/ipfs/bafkreif426p7t7takzhw3g6we2h6wsvf27p5jxj3gaiynqf22p3jvhx4la';
23
24
  // Helper function to encode multisend batch transactions
@@ -435,6 +436,8 @@ async function createJob(client, params) {
435
436
  const args = jobInput.arguments ? JSON.stringify(jobInput.arguments) : '';
436
437
  let job_cost_prediction = 0n;
437
438
  try {
439
+ // Generate trace ID for /api/fees request
440
+ const feesTraceId = (0, traceId_1.generateTraceId)('get', '/api/fees', userAddress);
438
441
  const feeRes = await client.get('/api/fees', {
439
442
  params: {
440
443
  ipfs_url,
@@ -444,18 +447,21 @@ async function createJob(client, params) {
444
447
  target_function,
445
448
  abi,
446
449
  args,
447
- }
450
+ },
451
+ headers: {
452
+ 'X-Trace-ID': feesTraceId,
453
+ },
448
454
  });
449
455
  // The API returns total fee in wei: { total_fee: "<wei>" } or nested under data
450
456
  let totalFeeRaw;
451
- if (feeRes && feeRes.current_total_fee !== undefined) {
457
+ if (feeRes && feeRes.current_total_fee !== undefined && feeRes.current_total_fee !== 0) {
452
458
  totalFeeRaw = feeRes.current_total_fee;
453
459
  }
454
- else if (feeRes && feeRes.data && feeRes.data.current_total_fee !== undefined) {
460
+ else if (feeRes && feeRes.data && feeRes.data.current_total_fee !== undefined && feeRes.data.current_total_fee !== 0) {
455
461
  totalFeeRaw = feeRes.data.current_total_fee;
456
462
  }
457
463
  maxtotalFeeRaw = feeRes.total_fee;
458
- if (totalFeeRaw === undefined) {
464
+ if (totalFeeRaw === undefined || totalFeeRaw === 0) {
459
465
  return (0, errors_1.createErrorResponse)(new errors_1.ApiError('Invalid response from /api/fees: missing total_fee', { response: feeRes }), 'API error');
460
466
  }
461
467
  // Support both number and string representations of wei
@@ -569,8 +575,14 @@ async function createJob(client, params) {
569
575
  ether_balance: typeof jobData.ether_balance === 'bigint' ? Number(jobData.ether_balance) : Number(jobData.ether_balance),
570
576
  token_balance: typeof jobData.token_balance === 'bigint' ? Number(jobData.token_balance) : Number(jobData.token_balance),
571
577
  };
578
+ // Generate trace ID for /api/jobs request
579
+ const jobsTraceId = (0, traceId_1.generateTraceId)('post', '/api/jobs', userAddress);
572
580
  const res = await client.post('/api/jobs', [jobDataForApi], {
573
- headers: { 'Content-Type': 'application/json', 'X-API-KEY': apiKey },
581
+ headers: {
582
+ 'Content-Type': 'application/json',
583
+ 'X-API-KEY': apiKey,
584
+ 'X-Trace-ID': jobsTraceId,
585
+ },
574
586
  });
575
587
  res.requiredETH = requiredETH;
576
588
  res.maxtotalFeeRaw = maxtotalFeeRaw;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Generate a trace ID for request tracing.
3
+ * Format: <METHOD>-<ROUTE>-<USER_ADDRESS>-<RANDOMNESS>
4
+ *
5
+ * @param method - HTTP method (get, post, put, delete)
6
+ * @param route - API route being called
7
+ * @param userAddress - User's wallet address
8
+ * @returns Formatted trace ID string
9
+ *
10
+ * @example
11
+ * generateTraceId('get', '/api/jobs/user/0x1234567890abcdef', '0x1234567890abcdef')
12
+ * // Returns: 'get-jobsuser-abcdef-a1b2c3d4'
13
+ */
14
+ export declare function generateTraceId(method: string, route: string, userAddress: string): string;
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateTraceId = generateTraceId;
4
+ /**
5
+ * Generate a trace ID for request tracing.
6
+ * Format: <METHOD>-<ROUTE>-<USER_ADDRESS>-<RANDOMNESS>
7
+ *
8
+ * @param method - HTTP method (get, post, put, delete)
9
+ * @param route - API route being called
10
+ * @param userAddress - User's wallet address
11
+ * @returns Formatted trace ID string
12
+ *
13
+ * @example
14
+ * generateTraceId('get', '/api/jobs/user/0x1234567890abcdef', '0x1234567890abcdef')
15
+ * // Returns: 'get-jobsuser-abcdef-a1b2c3d4'
16
+ */
17
+ function generateTraceId(method, route, userAddress) {
18
+ // Normalize method to lowercase
19
+ const normalizedMethod = method.toLowerCase();
20
+ // Sanitize route: remove leading slash, api prefix, and special characters
21
+ // Also remove any address-like segments and truncate
22
+ let sanitizedRoute = route
23
+ .replace(/^\/+/, '') // Remove leading slashes
24
+ .replace(/^api\//, '') // Remove 'api/' prefix
25
+ .replace(/0x[a-fA-F0-9]+/g, '') // Remove ethereum addresses
26
+ .replace(/[\/\-_]+/g, '') // Remove slashes, dashes, underscores
27
+ .toLowerCase();
28
+ // Truncate route to max 20 chars
29
+ if (sanitizedRoute.length > 20) {
30
+ sanitizedRoute = sanitizedRoute.substring(0, 20);
31
+ }
32
+ // Extract last 6 characters of user address (without 0x prefix handling)
33
+ const addressSuffix = userAddress
34
+ ? userAddress.toLowerCase().slice(-6)
35
+ : '000000';
36
+ // Generate randomness using crypto-safe random
37
+ const randomness = generateRandomSuffix();
38
+ return `${normalizedMethod}-${sanitizedRoute}-${addressSuffix}-${randomness}`;
39
+ }
40
+ /**
41
+ * Generate a random suffix for the trace ID.
42
+ * Uses crypto.randomUUID() if available, falls back to Math.random().
43
+ */
44
+ function generateRandomSuffix() {
45
+ // Use crypto.randomUUID() if available (Node.js 14.17+, browsers)
46
+ if (typeof crypto !== 'undefined' && crypto.randomUUID) {
47
+ const uuid = crypto.randomUUID();
48
+ // Take first 8 characters of UUID for shorter trace ID
49
+ return uuid.replace(/-/g, '').substring(0, 8);
50
+ }
51
+ // Fallback to Math.random-based generation
52
+ return Math.random().toString(36).substring(2, 10);
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sdk-triggerx",
3
- "version": "0.1.34",
3
+ "version": "0.1.36",
4
4
  "description": "SDK for interacting with the TriggerX backend and smart contracts.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -42,4 +42,4 @@
42
42
  "ts-jest": "^29.4.5",
43
43
  "typescript": "^5.8.3"
44
44
  }
45
- }
45
+ }