@ecency/wallets 1.3.16 → 1.3.17

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/README.md CHANGED
@@ -61,5 +61,12 @@ const { data: btcBalance } = useGetExternalWalletBalanceQuery(
61
61
  EcencyWalletCurrency.BTC,
62
62
  '1BitcoinAddress...'
63
63
  );
64
+
65
+ // query with Chainstack priority before Bitquery and public APIs
66
+ const { data: btcBalanceChainstackFirst } = useGetExternalWalletBalanceQuery(
67
+ EcencyWalletCurrency.BTC,
68
+ '1BitcoinAddress...',
69
+ { reverseProviderOrder: true }
70
+ );
64
71
  ```
65
72
 
@@ -74,87 +74,92 @@ const A0 = {
74
74
  scale: 1e8
75
75
  }
76
76
  };
77
- function Op(i, c) {
77
+ function Op(i, c, b = {}) {
78
+ const { reverseProviderOrder: y = !1 } = b;
78
79
  return Yt({
79
80
  queryKey: ["ecency-wallets", "external-wallet-balance", i, c],
80
81
  queryFn: async () => {
81
- const b = async (s) => {
82
- const a = A0[i];
83
- if (!a)
84
- throw new Error(`Unsupported currency for ${s}`);
85
- const p = s === "bitquery" ? Ve.bitqueryApiUrl : Ve.chainstackApiUrl;
86
- if (!p)
82
+ const s = async (p) => {
83
+ const f = A0[i];
84
+ if (!f)
85
+ throw new Error(`Unsupported currency for ${p}`);
86
+ const l = p === "bitquery" ? Ve.bitqueryApiUrl : Ve.chainstackApiUrl;
87
+ if (!l)
87
88
  throw new Error(
88
- s === "bitquery" ? "Bitquery API URL is not configured" : "Chainstack API URL is not configured"
89
+ p === "bitquery" ? "Bitquery API URL is not configured" : "Chainstack API URL is not configured"
89
90
  );
90
- const f = {
91
+ const n = {
91
92
  "Content-Type": "application/json"
92
93
  };
93
- s === "bitquery" ? f["X-API-KEY"] = Ve.bitqueryApiKey || "" : Ve.chainstackApiKey && (f["X-API-KEY"] = Ve.chainstackApiKey);
94
- const l = await fetch(p, {
94
+ p === "bitquery" ? n["X-API-KEY"] = Ve.bitqueryApiKey || "" : Ve.chainstackApiKey && (n["X-API-KEY"] = Ve.chainstackApiKey);
95
+ const r = await fetch(l, {
95
96
  method: "POST",
96
- headers: f,
97
- body: a.query(c)
97
+ headers: n,
98
+ body: f.query(c)
98
99
  });
99
- if (!l.ok)
100
+ if (!r.ok)
100
101
  throw new Error(
101
- s === "bitquery" ? "Bitquery request failed" : "Chainstack request failed"
102
+ p === "bitquery" ? "Bitquery request failed" : "Chainstack request failed"
102
103
  );
103
- const n = await l.json(), r = a.parse(n.data);
104
- if (typeof r != "number")
104
+ const e = await r.json(), t = f.parse(e.data);
105
+ if (typeof t != "number")
105
106
  throw new Error(
106
- s === "bitquery" ? "Bitquery returned invalid balance" : "Chainstack returned invalid balance"
107
+ p === "bitquery" ? "Bitquery returned invalid balance" : "Chainstack returned invalid balance"
107
108
  );
108
- return r / a.scale;
109
- }, y = async (s) => {
110
- try {
111
- return await s();
112
- } catch (a) {
109
+ return t / f.scale;
110
+ }, a = async (p) => {
111
+ const f = y ? [
112
+ () => s("chainstack"),
113
+ () => s("bitquery"),
114
+ p
115
+ ] : [
116
+ p,
117
+ () => s("bitquery"),
118
+ () => s("chainstack")
119
+ ];
120
+ let l;
121
+ for (const n of f)
113
122
  try {
114
- return await b("bitquery");
115
- } catch (p) {
116
- try {
117
- return await b("chainstack");
118
- } catch (f) {
119
- throw f instanceof Error ? f : p instanceof Error ? p : a;
120
- }
123
+ return await n();
124
+ } catch (r) {
125
+ l = r;
121
126
  }
122
- }
127
+ throw l instanceof Error ? l : new Error("Failed to fetch external wallet balance");
123
128
  };
124
129
  switch (i) {
125
130
  case Oe.BTC:
126
- return y(async () => {
127
- const s = await fetch(
131
+ return a(async () => {
132
+ const p = await fetch(
128
133
  `https://mempool.space/api/address/${c}`
129
134
  );
130
- if (!s.ok) throw new Error("Mempool API request failed");
131
- const a = await s.json();
132
- return (a.chain_stats.funded_txo_sum - a.chain_stats.spent_txo_sum) / 1e8;
135
+ if (!p.ok) throw new Error("Mempool API request failed");
136
+ const f = await p.json();
137
+ return (f.chain_stats.funded_txo_sum - f.chain_stats.spent_txo_sum) / 1e8;
133
138
  });
134
139
  case Oe.ETH:
135
- return y(async () => {
136
- const s = await fetch(
140
+ return a(async () => {
141
+ const p = await fetch(
137
142
  `https://api.ethplorer.io/getAddressInfo/${c}?apiKey=freekey`
138
143
  );
139
- if (!s.ok)
144
+ if (!p.ok)
140
145
  throw new Error("Ethplorer API request failed");
141
- return +(await s.json()).ETH.balance;
146
+ return +(await p.json()).ETH.balance;
142
147
  });
143
148
  case Oe.BNB:
144
- return y(async () => {
145
- const s = await fetch(
149
+ return a(async () => {
150
+ const p = await fetch(
146
151
  `https://api.bscscan.com/api?module=account&action=balance&address=${c}&tag=latest&apikey=YourApiKeyToken`
147
152
  );
148
- if (!s.ok)
153
+ if (!p.ok)
149
154
  throw new Error("BscScan API request failed");
150
- const a = await s.json();
151
- if (a.status !== "1")
155
+ const f = await p.json();
156
+ if (f.status !== "1")
152
157
  throw new Error("BscScan API returned an error");
153
- return parseInt(a.result, 10) / 1e18;
158
+ return parseInt(f.result, 10) / 1e18;
154
159
  });
155
160
  case Oe.SOL:
156
- return y(async () => {
157
- const s = await fetch(
161
+ return a(async () => {
162
+ const p = await fetch(
158
163
  `https://rpc.helius.xyz/?api-key=${Ve.heliusApiKey}`,
159
164
  {
160
165
  method: "POST",
@@ -169,36 +174,36 @@ function Op(i, c) {
169
174
  }
170
175
  }
171
176
  );
172
- if (!s.ok) throw new Error("Helius API request failed");
173
- return (await s.json()).result.value / 1e9;
177
+ if (!p.ok) throw new Error("Helius API request failed");
178
+ return (await p.json()).result.value / 1e9;
174
179
  });
175
180
  case Oe.TRON:
176
- return y(async () => {
177
- const s = await fetch(
181
+ return a(async () => {
182
+ const p = await fetch(
178
183
  `https://api.trongrid.io/v1/accounts/${c}`
179
184
  );
180
- if (!s.ok)
185
+ if (!p.ok)
181
186
  throw new Error("Trongrid API request failed");
182
- return (await s.json()).data[0].balance / 1e6;
187
+ return (await p.json()).data[0].balance / 1e6;
183
188
  });
184
189
  case Oe.TON:
185
- return y(async () => {
186
- const s = await fetch(
190
+ return a(async () => {
191
+ const p = await fetch(
187
192
  `https://tonapi.io/v1/blockchain/getAccount?account=${c}`
188
193
  );
189
- if (!s.ok) throw new Error("Ton API request failed");
190
- return (await s.json()).balance / 1e9;
194
+ if (!p.ok) throw new Error("Ton API request failed");
195
+ return (await p.json()).balance / 1e9;
191
196
  });
192
197
  case Oe.APT:
193
- return y(async () => {
194
- const s = await fetch(
198
+ return a(async () => {
199
+ const p = await fetch(
195
200
  `https://fullnode.mainnet.aptoslabs.com/v1/accounts/${c}/resources`
196
201
  );
197
- if (!s.ok) throw new Error("Aptos API request failed");
198
- const p = (await s.json()).find(
199
- (f) => f.type.includes("coin::CoinStore")
202
+ if (!p.ok) throw new Error("Aptos API request failed");
203
+ const l = (await p.json()).find(
204
+ (n) => n.type.includes("coin::CoinStore")
200
205
  );
201
- return p ? parseInt(p.data.coin.value) / 1e8 : 0;
206
+ return l ? parseInt(l.data.coin.value) / 1e8 : 0;
202
207
  });
203
208
  default:
204
209
  throw new Error(`Unsupported currency ${i}`);
@@ -6,4 +6,11 @@ import { EcencyWalletCurrency } from '../enums';
6
6
  * @param address
7
7
  * @returns
8
8
  */
9
- export declare function useGetExternalWalletBalanceQuery(currency: EcencyWalletCurrency, address: string): import('@tanstack/react-query').UseQueryResult<number, Error>;
9
+ interface UseGetExternalWalletBalanceQueryOptions {
10
+ /**
11
+ * When true, Chainstack will be queried before Bitquery and public APIs.
12
+ */
13
+ reverseProviderOrder?: boolean;
14
+ }
15
+ export declare function useGetExternalWalletBalanceQuery(currency: EcencyWalletCurrency, address: string, options?: UseGetExternalWalletBalanceQueryOptions): import('@tanstack/react-query').UseQueryResult<number, Error>;
16
+ export {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ecency/wallets",
3
3
  "private": false,
4
- "version": "1.3.16",
4
+ "version": "1.3.17",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "main": "./dist/ecency-sdk.umd.js",