@covalenthq/client-sdk 0.2.9 → 0.5.0
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 +117 -46
- package/dist/cjs/index.d.ts +9 -0
- package/dist/cjs/index.js +5226 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/services/BalanceService.d.ts +543 -0
- package/dist/cjs/services/BaseService.d.ts +357 -0
- package/dist/cjs/services/CovalentClient.d.ts +65 -0
- package/dist/cjs/services/NftService.d.ts +620 -0
- package/dist/cjs/services/PricingService.d.ts +88 -0
- package/dist/cjs/services/SecurityService.d.ts +158 -0
- package/dist/cjs/services/TransactionService.d.ts +457 -0
- package/dist/cjs/services/XykService.d.ts +686 -0
- package/dist/cjs/util/ApiHelpers.d.ts +5 -0
- package/dist/cjs/util/backoff.d.ts +11 -0
- package/dist/cjs/util/types/BalanceServiceTypes.d.ts +322 -0
- package/dist/cjs/util/types/BaseServiceTypes.d.ts +180 -0
- package/dist/cjs/util/types/GenericTypes.d.ts +96 -0
- package/dist/cjs/util/types/NftServiceTypes.d.ts +297 -0
- package/dist/cjs/util/types/PricingServiceTypes.d.ts +31 -0
- package/dist/cjs/util/types/SecurityServiceTypes.d.ts +121 -0
- package/dist/cjs/util/types/TransactionServiceTypes.d.ts +322 -0
- package/dist/cjs/util/types/XykServiceTypes.d.ts +479 -0
- package/dist/es/index.d.ts +9 -0
- package/dist/es/index.js +5223 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/services/BalanceService.d.ts +543 -0
- package/dist/es/services/BaseService.d.ts +357 -0
- package/dist/es/services/CovalentClient.d.ts +65 -0
- package/dist/es/services/NftService.d.ts +620 -0
- package/dist/es/services/PricingService.d.ts +88 -0
- package/dist/es/services/SecurityService.d.ts +158 -0
- package/dist/es/services/TransactionService.d.ts +457 -0
- package/dist/es/services/XykService.d.ts +686 -0
- package/dist/es/util/ApiHelpers.d.ts +5 -0
- package/dist/es/util/backoff.d.ts +11 -0
- package/dist/es/util/types/BalanceServiceTypes.d.ts +322 -0
- package/dist/es/util/types/BaseServiceTypes.d.ts +180 -0
- package/dist/es/util/types/GenericTypes.d.ts +96 -0
- package/dist/es/util/types/NftServiceTypes.d.ts +297 -0
- package/dist/es/util/types/PricingServiceTypes.d.ts +31 -0
- package/dist/es/util/types/SecurityServiceTypes.d.ts +121 -0
- package/dist/es/util/types/TransactionServiceTypes.d.ts +322 -0
- package/dist/es/util/types/XykServiceTypes.d.ts +479 -0
- package/dist/esm/index.d.ts +9 -0
- package/dist/esm/index.js +5223 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/services/BalanceService.d.ts +543 -0
- package/dist/esm/services/BaseService.d.ts +357 -0
- package/dist/esm/services/CovalentClient.d.ts +65 -0
- package/dist/esm/services/NftService.d.ts +620 -0
- package/dist/esm/services/PricingService.d.ts +88 -0
- package/dist/esm/services/SecurityService.d.ts +158 -0
- package/dist/esm/services/TransactionService.d.ts +457 -0
- package/dist/esm/services/XykService.d.ts +686 -0
- package/dist/esm/util/ApiHelpers.d.ts +5 -0
- package/dist/esm/util/backoff.d.ts +11 -0
- package/dist/esm/util/types/BalanceServiceTypes.d.ts +322 -0
- package/dist/esm/util/types/BaseServiceTypes.d.ts +180 -0
- package/dist/esm/util/types/GenericTypes.d.ts +96 -0
- package/dist/esm/util/types/NftServiceTypes.d.ts +297 -0
- package/dist/esm/util/types/PricingServiceTypes.d.ts +31 -0
- package/dist/esm/util/types/SecurityServiceTypes.d.ts +121 -0
- package/dist/esm/util/types/TransactionServiceTypes.d.ts +322 -0
- package/dist/esm/util/types/XykServiceTypes.d.ts +479 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -27
- package/dist/index.js.map +1 -1
- package/dist/services/BalanceService.d.ts +149 -11
- package/dist/services/BalanceService.js +300 -84
- package/dist/services/BalanceService.js.map +1 -1
- package/dist/services/BaseService.d.ts +46 -10
- package/dist/services/BaseService.js +199 -151
- package/dist/services/BaseService.js.map +1 -1
- package/dist/services/CovalentClient.d.ts +65 -0
- package/dist/services/CovalentClient.js +44 -0
- package/dist/services/CovalentClient.js.map +1 -0
- package/dist/services/NftService.d.ts +182 -16
- package/dist/services/NftService.js +490 -211
- package/dist/services/NftService.js.map +1 -1
- package/dist/services/PricingService.d.ts +8 -3
- package/dist/services/PricingService.js +44 -38
- package/dist/services/PricingService.js.map +1 -1
- package/dist/services/SecurityService.d.ts +71 -3
- package/dist/services/SecurityService.js +140 -32
- package/dist/services/SecurityService.js.map +1 -1
- package/dist/services/TransactionService.d.ts +58 -9
- package/dist/services/TransactionService.js +152 -103
- package/dist/services/TransactionService.js.map +1 -1
- package/dist/services/XykService.d.ts +85 -19
- package/dist/services/XykService.js +470 -291
- package/dist/services/XykService.js.map +1 -1
- package/dist/util/ApiHelpers.d.ts +2 -1
- package/dist/util/ApiHelpers.js +57 -36
- package/dist/util/ApiHelpers.js.map +1 -1
- package/dist/util/backoff.d.ts +5 -3
- package/dist/util/backoff.js +35 -13
- package/dist/util/backoff.js.map +1 -1
- package/dist/util/types/BalanceServiceTypes.d.ts +63 -0
- package/dist/util/types/BalanceServiceTypes.js +1 -2
- package/dist/util/types/BaseServiceTypes.d.ts +8 -0
- package/dist/util/types/BaseServiceTypes.js +1 -2
- package/dist/util/types/GenericTypes.d.ts +7 -0
- package/dist/util/types/GenericTypes.js +1 -2
- package/dist/util/types/NftServiceTypes.d.ts +86 -2
- package/dist/util/types/NftServiceTypes.js +1 -2
- package/dist/util/types/PricingServiceTypes.js +1 -2
- package/dist/util/types/SecurityServiceTypes.d.ts +51 -0
- package/dist/util/types/SecurityServiceTypes.js +1 -2
- package/dist/util/types/TransactionServiceTypes.d.ts +24 -2
- package/dist/util/types/TransactionServiceTypes.js +1 -2
- package/dist/util/types/XykServiceTypes.d.ts +20 -6
- package/dist/util/types/XykServiceTypes.js +1 -2
- package/package.json +19 -4
- package/dist/services/Client.d.ts +0 -29
- package/dist/services/Client.js +0 -30
- package/dist/services/Client.js.map +0 -1
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const date_fns_1 = require("date-fns");
|
|
7
|
-
const backoff_1 = require("../util/backoff");
|
|
1
|
+
import { userAgent } from "./CovalentClient";
|
|
2
|
+
import { checkAndModifyResponse, debugOutput, paginateEndpoint } from "../util/ApiHelpers";
|
|
3
|
+
import { parseISO } from "date-fns";
|
|
4
|
+
import { ExponentialBackoff } from "../util/backoff";
|
|
5
|
+
import pLimit from "p-limit";
|
|
8
6
|
class BalancesResponse {
|
|
9
7
|
constructor(data) {
|
|
10
8
|
this.address = data.address;
|
|
11
9
|
this.chain_id = data.chain_id;
|
|
12
10
|
this.chain_name = data.chain_name;
|
|
13
11
|
this.quote_currency = data.quote_currency;
|
|
14
|
-
this.updated_at = data.updated_at && data.updated_at !== null ?
|
|
12
|
+
this.updated_at = data.updated_at && data.updated_at !== null ? parseISO(data.updated_at.toString()) : null;
|
|
15
13
|
this.items = data.items && data.items !== null ? data.items.map((itemData) => new BalanceItem(itemData)) : null;
|
|
16
14
|
}
|
|
17
15
|
}
|
|
@@ -23,14 +21,18 @@ class BalanceItem {
|
|
|
23
21
|
this.contract_address = data.contract_address;
|
|
24
22
|
this.supports_erc = data.supports_erc;
|
|
25
23
|
this.logo_url = data.logo_url;
|
|
26
|
-
this.last_transferred_at = data.last_transferred_at && data.last_transferred_at !== null ?
|
|
24
|
+
this.last_transferred_at = data.last_transferred_at && data.last_transferred_at !== null ? parseISO(data.last_transferred_at.toString()) : null;
|
|
27
25
|
this.native_token = data.native_token;
|
|
28
26
|
this.type = data.type;
|
|
29
27
|
this.is_spam = data.is_spam;
|
|
30
28
|
this.balance = data.balance && data.balance !== null ? BigInt(data.balance) : null;
|
|
29
|
+
this.balance_24h = data.balance_24h && data.balance_24h !== null ? BigInt(data.balance_24h) : null;
|
|
31
30
|
this.quote_rate = data.quote_rate;
|
|
31
|
+
this.quote_rate_24h = data.quote_rate_24h;
|
|
32
32
|
this.quote = data.quote;
|
|
33
|
+
this.quote_24h = data.quote_24h;
|
|
33
34
|
this.pretty_quote = data.pretty_quote;
|
|
35
|
+
this.pretty_quote_24h = data.pretty_quote_24h;
|
|
34
36
|
this.nft_data = data.nft_data && data.nft_data !== null ? data.nft_data.map((itemData) => new NftData(itemData)) : null;
|
|
35
37
|
}
|
|
36
38
|
}
|
|
@@ -70,7 +72,7 @@ class NftCollectionAttribute {
|
|
|
70
72
|
class PortfolioResponse {
|
|
71
73
|
constructor(data) {
|
|
72
74
|
this.address = data.address;
|
|
73
|
-
this.updated_at = data.updated_at && data.updated_at !== null ?
|
|
75
|
+
this.updated_at = data.updated_at && data.updated_at !== null ? parseISO(data.updated_at.toString()) : null;
|
|
74
76
|
this.quote_currency = data.quote_currency;
|
|
75
77
|
this.chain_id = data.chain_id;
|
|
76
78
|
this.chain_name = data.chain_name;
|
|
@@ -90,7 +92,7 @@ class PortfolioItem {
|
|
|
90
92
|
class HoldingItem {
|
|
91
93
|
constructor(data) {
|
|
92
94
|
this.quote_rate = data.quote_rate;
|
|
93
|
-
this.timestamp = data.timestamp && data.timestamp !== null ?
|
|
95
|
+
this.timestamp = data.timestamp && data.timestamp !== null ? parseISO(data.timestamp.toString()) : null;
|
|
94
96
|
this.close = data.close && data.close !== null ? new OhlcItem(data.close) : null;
|
|
95
97
|
this.high = data.high && data.high !== null ? new OhlcItem(data.high) : null;
|
|
96
98
|
this.low = data.low && data.low !== null ? new OhlcItem(data.low) : null;
|
|
@@ -107,7 +109,7 @@ class OhlcItem {
|
|
|
107
109
|
class Erc20TransfersResponse {
|
|
108
110
|
constructor(data) {
|
|
109
111
|
this.address = data.address;
|
|
110
|
-
this.updated_at = data.updated_at && data.updated_at !== null ?
|
|
112
|
+
this.updated_at = data.updated_at && data.updated_at !== null ? parseISO(data.updated_at.toString()) : null;
|
|
111
113
|
this.quote_currency = data.quote_currency;
|
|
112
114
|
this.chain_id = data.chain_id;
|
|
113
115
|
this.chain_name = data.chain_name;
|
|
@@ -117,7 +119,7 @@ class Erc20TransfersResponse {
|
|
|
117
119
|
}
|
|
118
120
|
class BlockTransactionWithContractTransfers {
|
|
119
121
|
constructor(data) {
|
|
120
|
-
this.block_signed_at = data.block_signed_at && data.block_signed_at !== null ?
|
|
122
|
+
this.block_signed_at = data.block_signed_at && data.block_signed_at !== null ? parseISO(data.block_signed_at.toString()) : null;
|
|
121
123
|
this.block_height = data.block_height;
|
|
122
124
|
this.tx_hash = data.tx_hash;
|
|
123
125
|
this.tx_offset = data.tx_offset;
|
|
@@ -149,7 +151,7 @@ class Pagination {
|
|
|
149
151
|
}
|
|
150
152
|
class TokenTransferItem {
|
|
151
153
|
constructor(data) {
|
|
152
|
-
this.block_signed_at = data.block_signed_at && data.block_signed_at !== null ?
|
|
154
|
+
this.block_signed_at = data.block_signed_at && data.block_signed_at !== null ? parseISO(data.block_signed_at.toString()) : null;
|
|
153
155
|
this.tx_hash = data.tx_hash;
|
|
154
156
|
this.from_address = data.from_address;
|
|
155
157
|
this.from_address_label = data.from_address_label;
|
|
@@ -178,7 +180,7 @@ class MethodCallsForTransfers {
|
|
|
178
180
|
}
|
|
179
181
|
class TokenHoldersResponse {
|
|
180
182
|
constructor(data) {
|
|
181
|
-
this.updated_at = data.updated_at && data.updated_at !== null ?
|
|
183
|
+
this.updated_at = data.updated_at && data.updated_at !== null ? parseISO(data.updated_at.toString()) : null;
|
|
182
184
|
this.chain_id = data.chain_id;
|
|
183
185
|
this.chain_name = data.chain_name;
|
|
184
186
|
this.items = data.items && data.items !== null ? data.items.map((itemData) => new TokenHolder(itemData)) : null;
|
|
@@ -202,7 +204,7 @@ class TokenHolder {
|
|
|
202
204
|
class HistoricalBalancesResponse {
|
|
203
205
|
constructor(data) {
|
|
204
206
|
this.address = data.address;
|
|
205
|
-
this.updated_at = data.updated_at && data.updated_at !== null ?
|
|
207
|
+
this.updated_at = data.updated_at && data.updated_at !== null ? parseISO(data.updated_at.toString()) : null;
|
|
206
208
|
this.quote_currency = data.quote_currency;
|
|
207
209
|
this.chain_id = data.chain_id;
|
|
208
210
|
this.chain_name = data.chain_name;
|
|
@@ -219,7 +221,7 @@ class HistoricalBalanceItem {
|
|
|
219
221
|
this.logo_url = data.logo_url;
|
|
220
222
|
this.block_height = data.block_height;
|
|
221
223
|
this.last_transferred_block_height = data.last_transferred_block_height;
|
|
222
|
-
this.last_transferred_at = data.last_transferred_at && data.last_transferred_at !== null ?
|
|
224
|
+
this.last_transferred_at = data.last_transferred_at && data.last_transferred_at !== null ? parseISO(data.last_transferred_at.toString()) : null;
|
|
223
225
|
this.native_token = data.native_token;
|
|
224
226
|
this.type = data.type;
|
|
225
227
|
this.is_spam = data.is_spam;
|
|
@@ -230,15 +232,45 @@ class HistoricalBalanceItem {
|
|
|
230
232
|
this.nft_data = data.nft_data && data.nft_data !== null ? data.nft_data.map((itemData) => new NftData(itemData)) : null;
|
|
231
233
|
}
|
|
232
234
|
}
|
|
235
|
+
class TokenBalanceNativeResponse {
|
|
236
|
+
constructor(data) {
|
|
237
|
+
this.address = data.address;
|
|
238
|
+
this.updated_at = data.updated_at && data.updated_at !== null ? parseISO(data.updated_at.toString()) : null;
|
|
239
|
+
this.quote_currency = data.quote_currency;
|
|
240
|
+
this.chain_id = data.chain_id;
|
|
241
|
+
this.chain_name = data.chain_name;
|
|
242
|
+
this.items = data.items && data.items !== null ? data.items.map((itemData) => new NativeBalanceItem(itemData)) : null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
class NativeBalanceItem {
|
|
246
|
+
constructor(data) {
|
|
247
|
+
this.contract_decimals = data.contract_decimals;
|
|
248
|
+
this.contract_name = data.contract_name;
|
|
249
|
+
this.contract_ticker_symbol = data.contract_ticker_symbol;
|
|
250
|
+
this.contract_address = data.contract_address;
|
|
251
|
+
this.supports_erc = data.supports_erc;
|
|
252
|
+
this.logo_url = data.logo_url;
|
|
253
|
+
this.block_height = data.block_height;
|
|
254
|
+
this.balance = data.balance && data.balance !== null ? BigInt(data.balance) : null;
|
|
255
|
+
this.quote_rate = data.quote_rate;
|
|
256
|
+
this.quote = data.quote;
|
|
257
|
+
this.pretty_quote = data.pretty_quote;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
233
260
|
/**
|
|
234
261
|
* Balances APIs
|
|
235
262
|
*
|
|
236
263
|
*/
|
|
237
|
-
class BalanceService {
|
|
238
|
-
constructor(apiKey) {
|
|
264
|
+
export class BalanceService {
|
|
265
|
+
constructor(apiKey, debug = false, threadCount = 3) {
|
|
239
266
|
this.apiKey = apiKey;
|
|
267
|
+
this.debug = debug;
|
|
268
|
+
this.threadCount = threadCount;
|
|
269
|
+
this.LIMIT = pLimit(this.threadCount);
|
|
240
270
|
}
|
|
241
271
|
/**
|
|
272
|
+
*
|
|
273
|
+
* Commonly used to fetch the native, fungible (ERC20), and non-fungible (ERC721 & ERC1155) tokens held by an address. Response includes spot prices and other metadata.
|
|
242
274
|
*
|
|
243
275
|
* @param {string} chainName - The chain name eg: `eth-mainnet`.
|
|
244
276
|
* @param {string} walletAddress - The requested address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
@@ -254,7 +286,7 @@ class BalanceService {
|
|
|
254
286
|
let success = false;
|
|
255
287
|
let data;
|
|
256
288
|
let response;
|
|
257
|
-
const backoff = new
|
|
289
|
+
const backoff = new ExponentialBackoff(this.apiKey, this.debug);
|
|
258
290
|
while (!success) {
|
|
259
291
|
try {
|
|
260
292
|
const urlParams = new URLSearchParams();
|
|
@@ -273,38 +305,43 @@ class BalanceService {
|
|
|
273
305
|
if (queryParamOpts?.noNftAssetMetadata !== undefined) {
|
|
274
306
|
urlParams.append("no-nft-asset-metadata", queryParamOpts?.noNftAssetMetadata.toString());
|
|
275
307
|
}
|
|
276
|
-
|
|
308
|
+
let startTime;
|
|
309
|
+
if (this.debug) {
|
|
310
|
+
startTime = typeof performance !== 'undefined' ? performance.now() : process.hrtime();
|
|
311
|
+
}
|
|
312
|
+
response = await this.LIMIT(() => fetch(`https://api.covalenthq.com/v1/${chainName}/address/${walletAddress}/balances_v2/?${urlParams}`, {
|
|
277
313
|
headers: {
|
|
278
314
|
"Authorization": `Bearer ${this.apiKey}`,
|
|
279
|
-
"X-Requested-With":
|
|
315
|
+
"X-Requested-With": userAgent
|
|
280
316
|
}
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
if (
|
|
317
|
+
}));
|
|
318
|
+
debugOutput(response.url, response.status, startTime);
|
|
319
|
+
if (response.status === 429) {
|
|
284
320
|
try {
|
|
285
|
-
await backoff.backOff();
|
|
321
|
+
data = await this.LIMIT(() => backoff.backOff(response.url));
|
|
286
322
|
}
|
|
287
323
|
catch (error) {
|
|
288
324
|
success = true;
|
|
289
325
|
return {
|
|
290
326
|
data: null,
|
|
291
|
-
error:
|
|
292
|
-
error_code:
|
|
293
|
-
error_message:
|
|
327
|
+
error: true,
|
|
328
|
+
error_code: response.status,
|
|
329
|
+
error_message: error.message
|
|
294
330
|
};
|
|
295
331
|
}
|
|
296
332
|
}
|
|
297
333
|
else {
|
|
298
|
-
|
|
299
|
-
(0, ApiHelpers_1.checkAndModifyResponse)(dataClass);
|
|
300
|
-
success = true;
|
|
301
|
-
return {
|
|
302
|
-
data: dataClass,
|
|
303
|
-
error: data.error,
|
|
304
|
-
error_code: data ? data.error_code : response.status,
|
|
305
|
-
error_message: data ? data.error_message : "401 Authorization Required"
|
|
306
|
-
};
|
|
334
|
+
data = await response.json();
|
|
307
335
|
}
|
|
336
|
+
const dataClass = new BalancesResponse(data.data);
|
|
337
|
+
checkAndModifyResponse(dataClass);
|
|
338
|
+
success = true;
|
|
339
|
+
return {
|
|
340
|
+
data: dataClass,
|
|
341
|
+
error: data.error,
|
|
342
|
+
error_code: data ? data.error_code : response.status,
|
|
343
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
344
|
+
};
|
|
308
345
|
}
|
|
309
346
|
catch (error) {
|
|
310
347
|
success = true;
|
|
@@ -312,12 +349,14 @@ class BalanceService {
|
|
|
312
349
|
data: null,
|
|
313
350
|
error: true,
|
|
314
351
|
error_code: data ? data.error_code : response.status,
|
|
315
|
-
error_message: data ? data.error_message : "401 Authorization Required"
|
|
352
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
316
353
|
};
|
|
317
354
|
}
|
|
318
355
|
}
|
|
319
356
|
}
|
|
320
357
|
/**
|
|
358
|
+
*
|
|
359
|
+
* Commonly used to render a daily portfolio balance for an address broken down by the token. The timeframe is user-configurable, defaults to 30 days.
|
|
321
360
|
*
|
|
322
361
|
* @param {string} chainName - The chain name eg: `eth-mainnet`.
|
|
323
362
|
* @param {string} walletAddress - The requested address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
@@ -330,7 +369,7 @@ class BalanceService {
|
|
|
330
369
|
let success = false;
|
|
331
370
|
let data;
|
|
332
371
|
let response;
|
|
333
|
-
const backoff = new
|
|
372
|
+
const backoff = new ExponentialBackoff(this.apiKey, this.debug);
|
|
334
373
|
while (!success) {
|
|
335
374
|
try {
|
|
336
375
|
const urlParams = new URLSearchParams();
|
|
@@ -340,38 +379,43 @@ class BalanceService {
|
|
|
340
379
|
if (queryParamOpts?.days !== undefined) {
|
|
341
380
|
urlParams.append("days", queryParamOpts?.days.toString());
|
|
342
381
|
}
|
|
343
|
-
|
|
382
|
+
let startTime;
|
|
383
|
+
if (this.debug) {
|
|
384
|
+
startTime = typeof performance !== 'undefined' ? performance.now() : process.hrtime();
|
|
385
|
+
}
|
|
386
|
+
response = await this.LIMIT(() => fetch(`https://api.covalenthq.com/v1/${chainName}/address/${walletAddress}/portfolio_v2/?${urlParams}`, {
|
|
344
387
|
headers: {
|
|
345
388
|
"Authorization": `Bearer ${this.apiKey}`,
|
|
346
|
-
"X-Requested-With":
|
|
389
|
+
"X-Requested-With": userAgent
|
|
347
390
|
}
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
if (
|
|
391
|
+
}));
|
|
392
|
+
debugOutput(response.url, response.status, startTime);
|
|
393
|
+
if (response.status === 429) {
|
|
351
394
|
try {
|
|
352
|
-
await backoff.backOff();
|
|
395
|
+
data = await this.LIMIT(() => backoff.backOff(response.url));
|
|
353
396
|
}
|
|
354
397
|
catch (error) {
|
|
355
398
|
success = true;
|
|
356
399
|
return {
|
|
357
400
|
data: null,
|
|
358
|
-
error:
|
|
359
|
-
error_code:
|
|
360
|
-
error_message:
|
|
401
|
+
error: true,
|
|
402
|
+
error_code: response.status,
|
|
403
|
+
error_message: error.message
|
|
361
404
|
};
|
|
362
405
|
}
|
|
363
406
|
}
|
|
364
407
|
else {
|
|
365
|
-
|
|
366
|
-
(0, ApiHelpers_1.checkAndModifyResponse)(dataClass);
|
|
367
|
-
success = true;
|
|
368
|
-
return {
|
|
369
|
-
data: dataClass,
|
|
370
|
-
error: data.error,
|
|
371
|
-
error_code: data ? data.error_code : response.status,
|
|
372
|
-
error_message: data ? data.error_message : "401 Authorization Required"
|
|
373
|
-
};
|
|
408
|
+
data = await response.json();
|
|
374
409
|
}
|
|
410
|
+
const dataClass = new PortfolioResponse(data.data);
|
|
411
|
+
checkAndModifyResponse(dataClass);
|
|
412
|
+
success = true;
|
|
413
|
+
return {
|
|
414
|
+
data: dataClass,
|
|
415
|
+
error: data.error,
|
|
416
|
+
error_code: data ? data.error_code : response.status,
|
|
417
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
418
|
+
};
|
|
375
419
|
}
|
|
376
420
|
catch (error) {
|
|
377
421
|
success = true;
|
|
@@ -379,12 +423,14 @@ class BalanceService {
|
|
|
379
423
|
data: null,
|
|
380
424
|
error: true,
|
|
381
425
|
error_code: data ? data.error_code : response.status,
|
|
382
|
-
error_message: data ? data.error_message : "401 Authorization Required"
|
|
426
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
383
427
|
};
|
|
384
428
|
}
|
|
385
429
|
}
|
|
386
430
|
}
|
|
387
431
|
/**
|
|
432
|
+
*
|
|
433
|
+
* Commonly used to render the transfer-in and transfer-out of a token along with historical prices from an address.
|
|
388
434
|
*
|
|
389
435
|
* @param {string} chainName - The chain name eg: `eth-mainnet`.
|
|
390
436
|
* @param {string} walletAddress - The requested address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
@@ -421,18 +467,110 @@ class BalanceService {
|
|
|
421
467
|
if (queryParamOpts?.pageNumber !== undefined) {
|
|
422
468
|
urlParams.append("page-number", queryParamOpts?.pageNumber.toString());
|
|
423
469
|
}
|
|
424
|
-
for await (res of
|
|
470
|
+
for await (res of paginateEndpoint(`https://api.covalenthq.com/v1/${chainName}/address/${walletAddress}/transfers_v2/`, this.apiKey, urlParams, BlockTransactionWithContractTransfers, this.debug, this.threadCount)) {
|
|
425
471
|
yield res;
|
|
426
472
|
}
|
|
427
473
|
success = true;
|
|
428
474
|
}
|
|
429
475
|
catch (error) {
|
|
430
476
|
success = true;
|
|
431
|
-
|
|
477
|
+
throw new Error(error.message);
|
|
432
478
|
}
|
|
433
479
|
}
|
|
434
480
|
}
|
|
435
481
|
/**
|
|
482
|
+
*
|
|
483
|
+
* Commonly used to render the transfer-in and transfer-out of a token along with historical prices from an address.
|
|
484
|
+
*
|
|
485
|
+
* @param {string} chainName - The chain name eg: `eth-mainnet`.
|
|
486
|
+
* @param {string} walletAddress - The requested address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
487
|
+
* @param {GetErc20TransfersForWalletAddressQueryParamOpts} queryParamOpts
|
|
488
|
+
* - `quoteCurrency`: The currency to convert. Supports `USD`, `CAD`, `EUR`, `SGD`, `INR`, `JPY`, `VND`, `CNY`, `KRW`, `RUB`, `TRY`, `NGN`, `ARS`, `AUD`, `CHF`, and `GBP`.
|
|
489
|
+
* - `contractAddress`: The requested contract address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
490
|
+
* - `startingBlock`: The block height to start from, defaults to `0`.
|
|
491
|
+
* - `endingBlock`: The block height to end at, defaults to current block height.
|
|
492
|
+
* - `pageSize`: Number of items per page. Omitting this parameter defaults to 100.
|
|
493
|
+
* - `pageNumber`: 0-indexed page number to begin pagination.
|
|
494
|
+
*
|
|
495
|
+
*/
|
|
496
|
+
async getErc20TransfersForWalletAddressByPage(chainName, walletAddress, queryParamOpts) {
|
|
497
|
+
let success = false;
|
|
498
|
+
let data;
|
|
499
|
+
let response;
|
|
500
|
+
const backoff = new ExponentialBackoff(this.apiKey, this.debug);
|
|
501
|
+
while (!success) {
|
|
502
|
+
try {
|
|
503
|
+
const urlParams = new URLSearchParams();
|
|
504
|
+
if (queryParamOpts?.quoteCurrency !== undefined) {
|
|
505
|
+
urlParams.append("quote-currency", queryParamOpts?.quoteCurrency.toString());
|
|
506
|
+
}
|
|
507
|
+
if (queryParamOpts?.contractAddress !== undefined) {
|
|
508
|
+
urlParams.append("contract-address", queryParamOpts?.contractAddress.toString());
|
|
509
|
+
}
|
|
510
|
+
if (queryParamOpts?.startingBlock !== undefined) {
|
|
511
|
+
urlParams.append("starting-block", queryParamOpts?.startingBlock.toString());
|
|
512
|
+
}
|
|
513
|
+
if (queryParamOpts?.endingBlock !== undefined) {
|
|
514
|
+
urlParams.append("ending-block", queryParamOpts?.endingBlock.toString());
|
|
515
|
+
}
|
|
516
|
+
if (queryParamOpts?.pageSize !== undefined) {
|
|
517
|
+
urlParams.append("page-size", queryParamOpts?.pageSize.toString());
|
|
518
|
+
}
|
|
519
|
+
if (queryParamOpts?.pageNumber !== undefined) {
|
|
520
|
+
urlParams.append("page-number", queryParamOpts?.pageNumber.toString());
|
|
521
|
+
}
|
|
522
|
+
let startTime;
|
|
523
|
+
if (this.debug) {
|
|
524
|
+
startTime = typeof performance !== 'undefined' ? performance.now() : process.hrtime();
|
|
525
|
+
}
|
|
526
|
+
response = await this.LIMIT(() => fetch(`https://api.covalenthq.com/v1/${chainName}/address/${walletAddress}/transfers_v2/?${urlParams}`, {
|
|
527
|
+
headers: {
|
|
528
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
529
|
+
"X-Requested-With": userAgent
|
|
530
|
+
}
|
|
531
|
+
}));
|
|
532
|
+
debugOutput(response.url, response.status, startTime);
|
|
533
|
+
if (response.status === 429) {
|
|
534
|
+
try {
|
|
535
|
+
data = await this.LIMIT(() => backoff.backOff(response.url));
|
|
536
|
+
}
|
|
537
|
+
catch (error) {
|
|
538
|
+
success = true;
|
|
539
|
+
return {
|
|
540
|
+
data: null,
|
|
541
|
+
error: true,
|
|
542
|
+
error_code: response.status,
|
|
543
|
+
error_message: error.message
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
data = await response.json();
|
|
549
|
+
}
|
|
550
|
+
const dataClass = new Erc20TransfersResponse(data.data);
|
|
551
|
+
checkAndModifyResponse(dataClass);
|
|
552
|
+
success = true;
|
|
553
|
+
return {
|
|
554
|
+
data: dataClass,
|
|
555
|
+
error: data.error,
|
|
556
|
+
error_code: data ? data.error_code : response.status,
|
|
557
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
catch (error) {
|
|
561
|
+
success = true;
|
|
562
|
+
return {
|
|
563
|
+
data: null,
|
|
564
|
+
error: true,
|
|
565
|
+
error_code: data ? data.error_code : response.status,
|
|
566
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
*
|
|
573
|
+
* Commonly used to get a list of all the token holders for a specified ERC20 or ERC721 token. Returns historic token holders when block-height is set (defaults to `latest`). Useful for building pie charts of token holders.
|
|
436
574
|
*
|
|
437
575
|
* @param {string} chainName - The chain name eg: `eth-mainnet`.
|
|
438
576
|
* @param {string} tokenAddress - The requested address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
@@ -457,18 +595,20 @@ class BalanceService {
|
|
|
457
595
|
if (queryParamOpts?.pageNumber !== undefined) {
|
|
458
596
|
urlParams.append("page-number", queryParamOpts?.pageNumber.toString());
|
|
459
597
|
}
|
|
460
|
-
for await (res of
|
|
598
|
+
for await (res of paginateEndpoint(`https://api.covalenthq.com/v1/${chainName}/tokens/${tokenAddress}/token_holders_v2/`, this.apiKey, urlParams, TokenHolder, this.debug, this.threadCount)) {
|
|
461
599
|
yield res;
|
|
462
600
|
}
|
|
463
601
|
success = true;
|
|
464
602
|
}
|
|
465
603
|
catch (error) {
|
|
466
604
|
success = true;
|
|
467
|
-
|
|
605
|
+
throw new Error(error.message);
|
|
468
606
|
}
|
|
469
607
|
}
|
|
470
608
|
}
|
|
471
609
|
/**
|
|
610
|
+
*
|
|
611
|
+
* Commonly used to fetch the historical native, fungible (ERC20), and non-fungible (ERC721 & ERC1155) tokens held by an address at a given block height or date. Response includes daily prices and other metadata.
|
|
472
612
|
*
|
|
473
613
|
* @param {string} chainName - The chain name eg: `eth-mainnet`.
|
|
474
614
|
* @param {string} walletAddress - The requested address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
@@ -486,7 +626,7 @@ class BalanceService {
|
|
|
486
626
|
let success = false;
|
|
487
627
|
let data;
|
|
488
628
|
let response;
|
|
489
|
-
const backoff = new
|
|
629
|
+
const backoff = new ExponentialBackoff(this.apiKey, this.debug);
|
|
490
630
|
while (!success) {
|
|
491
631
|
try {
|
|
492
632
|
const urlParams = new URLSearchParams();
|
|
@@ -511,38 +651,115 @@ class BalanceService {
|
|
|
511
651
|
if (queryParamOpts?.date !== undefined) {
|
|
512
652
|
urlParams.append("date", queryParamOpts?.date.toString());
|
|
513
653
|
}
|
|
514
|
-
|
|
654
|
+
let startTime;
|
|
655
|
+
if (this.debug) {
|
|
656
|
+
startTime = typeof performance !== 'undefined' ? performance.now() : process.hrtime();
|
|
657
|
+
}
|
|
658
|
+
response = await this.LIMIT(() => fetch(`https://api.covalenthq.com/v1/${chainName}/address/${walletAddress}/historical_balances/?${urlParams}`, {
|
|
515
659
|
headers: {
|
|
516
660
|
"Authorization": `Bearer ${this.apiKey}`,
|
|
517
|
-
"X-Requested-With":
|
|
661
|
+
"X-Requested-With": userAgent
|
|
518
662
|
}
|
|
519
|
-
});
|
|
520
|
-
|
|
521
|
-
if (
|
|
663
|
+
}));
|
|
664
|
+
debugOutput(response.url, response.status, startTime);
|
|
665
|
+
if (response.status === 429) {
|
|
522
666
|
try {
|
|
523
|
-
await backoff.backOff();
|
|
667
|
+
data = await this.LIMIT(() => backoff.backOff(response.url));
|
|
524
668
|
}
|
|
525
669
|
catch (error) {
|
|
526
670
|
success = true;
|
|
527
671
|
return {
|
|
528
672
|
data: null,
|
|
529
|
-
error:
|
|
530
|
-
error_code:
|
|
531
|
-
error_message:
|
|
673
|
+
error: true,
|
|
674
|
+
error_code: response.status,
|
|
675
|
+
error_message: error.message
|
|
532
676
|
};
|
|
533
677
|
}
|
|
534
678
|
}
|
|
535
679
|
else {
|
|
536
|
-
|
|
537
|
-
(0, ApiHelpers_1.checkAndModifyResponse)(dataClass);
|
|
538
|
-
success = true;
|
|
539
|
-
return {
|
|
540
|
-
data: dataClass,
|
|
541
|
-
error: data.error,
|
|
542
|
-
error_code: data ? data.error_code : response.status,
|
|
543
|
-
error_message: data ? data.error_message : "401 Authorization Required"
|
|
544
|
-
};
|
|
680
|
+
data = await response.json();
|
|
545
681
|
}
|
|
682
|
+
const dataClass = new HistoricalBalancesResponse(data.data);
|
|
683
|
+
checkAndModifyResponse(dataClass);
|
|
684
|
+
success = true;
|
|
685
|
+
return {
|
|
686
|
+
data: dataClass,
|
|
687
|
+
error: data.error,
|
|
688
|
+
error_code: data ? data.error_code : response.status,
|
|
689
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
catch (error) {
|
|
693
|
+
success = true;
|
|
694
|
+
return {
|
|
695
|
+
data: null,
|
|
696
|
+
error: true,
|
|
697
|
+
error_code: data ? data.error_code : response.status,
|
|
698
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
/**
|
|
704
|
+
*
|
|
705
|
+
* @param {string} chainName - The chain name eg: `eth-mainnet`.
|
|
706
|
+
* @param {string} walletAddress - The requested address. Passing in an `ENS`, `RNS`, `Lens Handle`, or an `Unstoppable Domain` resolves automatically.
|
|
707
|
+
* @param {GetNativeTokenBalanceQueryParamOpts} queryParamOpts
|
|
708
|
+
* - `quoteCurrency`: The currency to convert. Supports `USD`, `CAD`, `EUR`, `SGD`, `INR`, `JPY`, `VND`, `CNY`, `KRW`, `RUB`, `TRY`, `NGN`, `ARS`, `AUD`, `CHF`, and `GBP`.
|
|
709
|
+
* - `blockHeight`: Ending block to define a block range. Omitting this parameter defaults to the latest block height.
|
|
710
|
+
*
|
|
711
|
+
*/
|
|
712
|
+
async getNativeTokenBalance(chainName, walletAddress, queryParamOpts) {
|
|
713
|
+
let success = false;
|
|
714
|
+
let data;
|
|
715
|
+
let response;
|
|
716
|
+
const backoff = new ExponentialBackoff(this.apiKey, this.debug);
|
|
717
|
+
while (!success) {
|
|
718
|
+
try {
|
|
719
|
+
const urlParams = new URLSearchParams();
|
|
720
|
+
if (queryParamOpts?.quoteCurrency !== undefined) {
|
|
721
|
+
urlParams.append("quote-currency", queryParamOpts?.quoteCurrency.toString());
|
|
722
|
+
}
|
|
723
|
+
if (queryParamOpts?.blockHeight !== undefined) {
|
|
724
|
+
urlParams.append("block-height", queryParamOpts?.blockHeight.toString());
|
|
725
|
+
}
|
|
726
|
+
let startTime;
|
|
727
|
+
if (this.debug) {
|
|
728
|
+
startTime = typeof performance !== 'undefined' ? performance.now() : process.hrtime();
|
|
729
|
+
}
|
|
730
|
+
response = await this.LIMIT(() => fetch(`https://api.covalenthq.com/v1/${chainName}/address/${walletAddress}/balances_native/?${urlParams}`, {
|
|
731
|
+
headers: {
|
|
732
|
+
"Authorization": `Bearer ${this.apiKey}`,
|
|
733
|
+
"X-Requested-With": userAgent
|
|
734
|
+
}
|
|
735
|
+
}));
|
|
736
|
+
debugOutput(response.url, response.status, startTime);
|
|
737
|
+
if (response.status === 429) {
|
|
738
|
+
try {
|
|
739
|
+
data = await this.LIMIT(() => backoff.backOff(response.url));
|
|
740
|
+
}
|
|
741
|
+
catch (error) {
|
|
742
|
+
success = true;
|
|
743
|
+
return {
|
|
744
|
+
data: null,
|
|
745
|
+
error: true,
|
|
746
|
+
error_code: response.status,
|
|
747
|
+
error_message: error.message
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
else {
|
|
752
|
+
data = await response.json();
|
|
753
|
+
}
|
|
754
|
+
const dataClass = new TokenBalanceNativeResponse(data.data);
|
|
755
|
+
checkAndModifyResponse(dataClass);
|
|
756
|
+
success = true;
|
|
757
|
+
return {
|
|
758
|
+
data: dataClass,
|
|
759
|
+
error: data.error,
|
|
760
|
+
error_code: data ? data.error_code : response.status,
|
|
761
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
762
|
+
};
|
|
546
763
|
}
|
|
547
764
|
catch (error) {
|
|
548
765
|
success = true;
|
|
@@ -550,11 +767,10 @@ class BalanceService {
|
|
|
550
767
|
data: null,
|
|
551
768
|
error: true,
|
|
552
769
|
error_code: data ? data.error_code : response.status,
|
|
553
|
-
error_message: data ? data.error_message : "401 Authorization Required"
|
|
770
|
+
error_message: data ? data.error_message : response.status === 500 ? "Internal server error" : "401 Authorization Required"
|
|
554
771
|
};
|
|
555
772
|
}
|
|
556
773
|
}
|
|
557
774
|
}
|
|
558
775
|
}
|
|
559
|
-
exports.BalanceService = BalanceService;
|
|
560
776
|
//# sourceMappingURL=BalanceService.js.map
|