@xandeum/web3.js 0.8.0 → 0.8.1

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.
@@ -55,40 +55,60 @@ function sleep(ms) {
55
55
  */
56
56
  function getXandeumResult(connection, signature) {
57
57
  return __awaiter(this, void 0, void 0, function () {
58
- var url, requestBody, response, errorText, data;
58
+ var url, maxAttempts, delayMs, attempt, response, text, data;
59
59
  return __generator(this, function (_a) {
60
60
  switch (_a.label) {
61
61
  case 0:
62
62
  url = connection.rpcEndpoint;
63
- requestBody = {
64
- jsonrpc: '2.0',
65
- id: 1,
66
- method: 'getXandeumResult',
67
- params: [signature]
68
- };
69
- // sleeping To let the transaction process
70
- return [4 /*yield*/, sleep(5000)];
63
+ maxAttempts = 3;
64
+ delayMs = 5000;
65
+ attempt = 1;
66
+ _a.label = 1;
71
67
  case 1:
72
- // sleeping To let the transaction process
68
+ if (!(attempt <= maxAttempts)) return [3 /*break*/, 8];
69
+ // Wait before every attempt (including the first one)
70
+ return [4 /*yield*/, sleep(delayMs)];
71
+ case 2:
72
+ // Wait before every attempt (including the first one)
73
73
  _a.sent();
74
74
  return [4 /*yield*/, fetch(url, {
75
- method: 'POST',
76
- headers: {
77
- 'Content-Type': 'application/json'
78
- },
79
- body: JSON.stringify(requestBody)
75
+ method: "POST",
76
+ headers: { "Content-Type": "application/json" },
77
+ body: JSON.stringify({
78
+ jsonrpc: "2.0",
79
+ id: 1,
80
+ method: "getXandeumResult",
81
+ params: [signature],
82
+ }),
80
83
  })];
81
- case 2:
84
+ case 3:
82
85
  response = _a.sent();
83
- if (!!response.ok) return [3 /*break*/, 4];
86
+ if (!!response.ok) return [3 /*break*/, 5];
84
87
  return [4 /*yield*/, response.text()];
85
- case 3:
86
- errorText = _a.sent();
87
- return [2 /*return*/, new Error("Error! status: ".concat(response.status, ", message: ").concat(errorText))];
88
- case 4: return [4 /*yield*/, response.json()];
89
- case 5:
88
+ case 4:
89
+ text = _a.sent();
90
+ console.error("Attempt ".concat(attempt, "/").concat(maxAttempts, " failed: ").concat(response.status, " ").concat(text));
91
+ if (attempt === maxAttempts) {
92
+ return [2 /*return*/, { result: null, error: "HTTP ".concat(response.status) }];
93
+ }
94
+ return [3 /*break*/, 7];
95
+ case 5: return [4 /*yield*/, response.json()];
96
+ case 6:
90
97
  data = _a.sent();
91
- return [2 /*return*/, data];
98
+ // Change this condition if Xandeum uses a different "not ready" value
99
+ if ((data === null || data === void 0 ? void 0 : data.result) != null && data.result !== "pending") {
100
+ console.log("Got result on attempt ".concat(attempt));
101
+ return [2 /*return*/, data]; // Success — return immediately
102
+ }
103
+ console.log("Attempt ".concat(attempt, "/").concat(maxAttempts, ": still pending..."));
104
+ _a.label = 7;
105
+ case 7:
106
+ attempt++;
107
+ return [3 /*break*/, 1];
108
+ case 8:
109
+ // All 3 attempts done, still no result
110
+ console.log("Max attempts reached — result still not available");
111
+ return [2 /*return*/, { result: null, error: "Result not ready after 3 attempts" }];
92
112
  }
93
113
  });
94
114
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xandeum/web3.js",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "Xandeum javascript api",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,31 +18,50 @@ function sleep(ms: number) {
18
18
  * @returns A `Promise<any>` resolving to the parsed JSON response from the RPC server,
19
19
  * which includes the result of the transaction if available.
20
20
  */
21
- export async function getXandeumResult( connection: Connection, signature: string): Promise<any> {
21
+ export async function getXandeumResult(
22
+ connection: Connection,
23
+ signature: string
24
+ ): Promise<any> {
22
25
  const url = connection.rpcEndpoint;
23
- const requestBody = {
24
- jsonrpc: '2.0',
25
- id: 1,
26
- method: 'getXandeumResult',
27
- params: [signature]
26
+ const maxAttempts = 3;
27
+ const delayMs = 5000;
28
+
29
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
30
+ // Wait before every attempt (including the first one)
31
+ await sleep(delayMs);
32
+
33
+ const response = await fetch(url, {
34
+ method: "POST",
35
+ headers: { "Content-Type": "application/json" },
36
+ body: JSON.stringify({
37
+ jsonrpc: "2.0",
38
+ id: 1,
39
+ method: "getXandeumResult",
40
+ params: [signature],
41
+ }),
42
+ });
43
+
44
+ if (!response.ok) {
45
+ const text = await response.text();
46
+ console.error(`Attempt ${attempt}/${maxAttempts} failed: ${response.status} ${text}`);
47
+ if (attempt === maxAttempts) {
48
+ return { result: null, error: `HTTP ${response.status}` };
49
+ }
50
+ continue;
51
+ }
52
+
53
+ const data = await response.json();
54
+
55
+ // Change this condition if Xandeum uses a different "not ready" value
56
+ if (data?.result != null && data.result !== "pending") {
57
+ console.log(`Got result on attempt ${attempt}`);
58
+ return data; // Success — return immediately
59
+ }
60
+
61
+ console.log(`Attempt ${attempt}/${maxAttempts}: still pending...`);
28
62
  }
29
-
30
- // sleeping To let the transaction process
31
- await sleep(5000);
32
-
33
- const response = await fetch(url, {
34
- method: 'POST',
35
- headers: {
36
- 'Content-Type': 'application/json'
37
- },
38
- body: JSON.stringify(requestBody)
39
- })
40
-
41
- if (!response.ok) {
42
- const errorText = await response.text();
43
- return new Error(`Error! status: ${response.status}, message: ${errorText}`);
44
- }
45
-
46
- const data = await response.json()
47
- return data
48
- }
63
+
64
+ // All 3 attempts done, still no result
65
+ console.log("Max attempts reached — result still not available");
66
+ return { result: null, error: "Result not ready after 3 attempts" };
67
+ }