Skip to content
This repository was archived by the owner on Oct 12, 2025. It is now read-only.

Commit 4a7414d

Browse files
committed
guard scrypt and add fallack balance provider
1 parent 57d6d3e commit 4a7414d

File tree

5 files changed

+95
-10
lines changed

5 files changed

+95
-10
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ecency/sdk",
33
"private": false,
4-
"version": "1.1.18",
4+
"version": "1.1.19",
55
"type": "module",
66
"license": "MIT",
77
"main": "./dist/ecency-sdk.umd.js",

packages/wallets/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ecency/wallets",
33
"private": false,
4-
"version": "1.3.18",
4+
"version": "1.3.19",
55
"type": "module",
66
"license": "MIT",
77
"main": "./dist/ecency-sdk.umd.js",

packages/wallets/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
import { rememberScryptBsvVersion } from "./internal/scrypt-guard";
2+
3+
rememberScryptBsvVersion();
4+
15
export * from "./modules/wallets";
26
export * from "./modules/assets";
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const globalLike = globalThis as Record<string, unknown> & {
2+
_scrypt_bsv?: unknown;
3+
__scryptBsvVersion?: unknown;
4+
__scryptWarningsPatched?: boolean;
5+
};
6+
7+
const knownVersion = globalLike.__scryptBsvVersion;
8+
if (typeof knownVersion === "string" && globalLike._scrypt_bsv === knownVersion) {
9+
delete globalLike._scrypt_bsv;
10+
}
11+
12+
if (!globalLike.__scryptWarningsPatched) {
13+
const originalWarn = console.warn.bind(console);
14+
console.warn = (...args: unknown[]) => {
15+
const [first] = args;
16+
if (typeof first === "string") {
17+
if (first.includes("More than one instance of scrypt bsv found")) {
18+
return;
19+
}
20+
if (first.includes("bigint: Failed to load bindings")) {
21+
return;
22+
}
23+
}
24+
originalWarn(...(args as Parameters<typeof console.warn>));
25+
};
26+
globalLike.__scryptWarningsPatched = true;
27+
}
28+
29+
export function rememberScryptBsvVersion() {
30+
if (typeof globalLike._scrypt_bsv !== "undefined") {
31+
globalLike.__scryptBsvVersion = globalLike._scrypt_bsv;
32+
}
33+
}

packages/wallets/src/modules/wallets/queries/use-get-external-wallet-query.ts

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,62 @@ export function useGetExternalWalletBalanceQuery(
117117
throw new Error("Private API host is not configured");
118118
}
119119

120-
const response = await fetch(
121-
`${CONFIG.privateApiHost}/private-api/balance/${chain}/${encodeURIComponent(
122-
address,
123-
)}`,
124-
);
125-
126-
if (!response.ok) {
127-
throw new Error("Private API request failed");
120+
const baseUrl = `${CONFIG.privateApiHost}/private-api/balance/${chain}/${encodeURIComponent(
121+
address,
122+
)}`;
123+
124+
let primaryResponse: Response | undefined;
125+
let primaryError: unknown;
126+
127+
try {
128+
primaryResponse = await fetch(baseUrl);
129+
} catch (error) {
130+
primaryError = error;
131+
}
132+
133+
let response = primaryResponse;
134+
135+
if (!response || !response.ok) {
136+
const fallbackUrl = `${baseUrl}?provider=chainz`;
137+
let fallbackError: unknown;
138+
139+
try {
140+
const fallbackResponse = await fetch(fallbackUrl);
141+
142+
if (fallbackResponse.ok) {
143+
response = fallbackResponse;
144+
} else {
145+
fallbackError = new Error(
146+
`Fallback provider responded with status ${fallbackResponse.status}`,
147+
);
148+
}
149+
} catch (error) {
150+
fallbackError = error;
151+
}
152+
153+
if (!response || !response.ok) {
154+
const failureReasons: string[] = [];
155+
156+
if (primaryError) {
157+
const message =
158+
primaryError instanceof Error ? primaryError.message : String(primaryError);
159+
failureReasons.push(`primary provider failed: ${message}`);
160+
} else if (primaryResponse && !primaryResponse.ok) {
161+
failureReasons.push(`primary provider status ${primaryResponse.status}`);
162+
}
163+
164+
if (fallbackError) {
165+
const message =
166+
fallbackError instanceof Error ? fallbackError.message : String(fallbackError);
167+
failureReasons.push(`fallback provider failed: ${message}`);
168+
}
169+
170+
if (failureReasons.length === 0) {
171+
failureReasons.push("unknown error");
172+
}
173+
174+
throw new Error(`Private API request failed (${failureReasons.join(", ")})`);
175+
}
128176
}
129177

130178
const result = (await response.json()) as unknown;

0 commit comments

Comments
 (0)