@covalenthq/client-sdk 0.6.0 → 0.6.2

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.
Files changed (35) hide show
  1. package/README.md +10 -0
  2. package/dist/cjs/index.d.ts +1 -0
  3. package/dist/cjs/index.js +39 -1
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/services/BalanceService.d.ts +20 -0
  6. package/dist/cjs/services/CovalentClient.d.ts +1 -1
  7. package/dist/cjs/util/CalculatePrettyBalance.d.ts +1 -0
  8. package/dist/cjs/util/types/BalanceServiceTypes.d.ts +18 -0
  9. package/dist/es/index.d.ts +1 -0
  10. package/dist/es/index.js +39 -2
  11. package/dist/es/index.js.map +1 -1
  12. package/dist/es/services/BalanceService.d.ts +20 -0
  13. package/dist/es/services/CovalentClient.d.ts +1 -1
  14. package/dist/es/util/CalculatePrettyBalance.d.ts +1 -0
  15. package/dist/es/util/types/BalanceServiceTypes.d.ts +18 -0
  16. package/dist/esm/index.d.ts +1 -0
  17. package/dist/esm/index.js +39 -2
  18. package/dist/esm/index.js.map +1 -1
  19. package/dist/esm/services/BalanceService.d.ts +20 -0
  20. package/dist/esm/services/CovalentClient.d.ts +1 -1
  21. package/dist/esm/util/CalculatePrettyBalance.d.ts +1 -0
  22. package/dist/esm/util/types/BalanceServiceTypes.d.ts +18 -0
  23. package/dist/index.d.ts +1 -0
  24. package/dist/index.js +1 -0
  25. package/dist/index.js.map +1 -1
  26. package/dist/services/BalanceService.d.ts +20 -0
  27. package/dist/services/BalanceService.js +15 -0
  28. package/dist/services/BalanceService.js.map +1 -1
  29. package/dist/services/CovalentClient.d.ts +1 -1
  30. package/dist/services/CovalentClient.js +1 -1
  31. package/dist/util/CalculatePrettyBalance.d.ts +1 -0
  32. package/dist/util/CalculatePrettyBalance.js +22 -0
  33. package/dist/util/CalculatePrettyBalance.js.map +1 -0
  34. package/dist/util/types/BalanceServiceTypes.d.ts +18 -0
  35. package/package.json +3 -1
package/README.md CHANGED
@@ -97,6 +97,16 @@ The `BalanceService` class refers to the [balances API endpoints](https://www.co
97
97
  - `getTokenHoldersV2ForTokenAddressByPage()`: 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. (Nonpaginated)
98
98
  - `getNativeTokenBalance()`: Get the native token balance for an address. This endpoint is required because native tokens are usually not ERC20 tokens and sometimes you want something lightweight.
99
99
 
100
+ The `calculatePrettyBalance` function is designed to take up to 4 inputs: the `balance` field obtained from the `tokenBalance` endpoint and the `contract_decimals`. The function also includes two optional fields, `roundOff` and `precision`, to allow developers to round the unscaled balance to a certain decimal precision. The primary purpose of this function is to convert the scaled token balance (the balance parameter) into its unscaled, human-readable form. The scaled balance needs to be divided by 10^(contractDecimals) to remove the scaling factor.
101
+
102
+ ```ts
103
+ import { CovalentClient, calculatePrettyBalance } from "@covalenthq/client-sdk";
104
+
105
+ const client = new CovalentClient("YOUR_API_KEY"); // Replace with your Covalent API key.
106
+ const resp = await client.BalanceService.getTokenBalancesForWalletAddress("eth-mainnet", "WALLET_ADDRESS");
107
+ const prettyBalance = calculatePrettyBalance(resp.data.items[0].balance, resp.data.items[0].contract_decimals);
108
+ ```
109
+
100
110
  ### BaseService
101
111
 
102
112
  The `BaseService` class refers to the [address activity, log events, chain status and block retrieval API endpoints](https://www.covalenthq.com/docs/api/base/get-address-activity/):
@@ -7,3 +7,4 @@ export * from "./util/types/SecurityServiceTypes";
7
7
  export * from "./util/types/XykServiceTypes";
8
8
  export * from "./util/types/GenericTypes";
9
9
  export * from "./util/types/TransactionServiceTypes";
10
+ export { calculatePrettyBalance } from "./util/CalculatePrettyBalance";
package/dist/cjs/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  var chalk = require('chalk');
4
4
  var dateFns = require('date-fns');
5
+ var Big = require('big.js');
5
6
 
6
7
  const DEFAULT_BACKOFF_MAX_RETRIES = 5;
7
8
  const BASE_DELAY_MS = 1000; // Base delay in milliseconds
@@ -524,6 +525,7 @@ class BalanceItem {
524
525
  this.contract_name = data.contract_name;
525
526
  this.contract_ticker_symbol = data.contract_ticker_symbol;
526
527
  this.contract_address = data.contract_address;
528
+ this.contract_display_name = data.contract_display_name;
527
529
  this.supports_erc = data.supports_erc;
528
530
  this.logo_url = data.logo_url;
529
531
  this.last_transferred_at = data.last_transferred_at && data.last_transferred_at !== null ? dateFns.parseISO(data.last_transferred_at.toString()) : null;
@@ -538,9 +540,23 @@ class BalanceItem {
538
540
  this.quote_24h = data.quote_24h;
539
541
  this.pretty_quote = data.pretty_quote;
540
542
  this.pretty_quote_24h = data.pretty_quote_24h;
543
+ this.logo_urls = data.logo_urls && data.logo_urls !== null ? new LogoUrls(data.logo_urls) : null;
544
+ this.protocol_metadata = data.protocol_metadata && data.protocol_metadata !== null ? new ProtocolMetadata(data.protocol_metadata) : null;
541
545
  this.nft_data = data.nft_data && data.nft_data !== null ? data.nft_data.map((itemData) => new NftData$1(itemData)) : null;
542
546
  }
543
547
  }
548
+ class LogoUrls {
549
+ constructor(data) {
550
+ this.token_logo_url = data.token_logo_url;
551
+ this.protocol_logo_url = data.protocol_logo_url;
552
+ this.chain_logo_url = data.chain_logo_url;
553
+ }
554
+ }
555
+ class ProtocolMetadata {
556
+ constructor(data) {
557
+ this.protocol_name = data.protocol_name;
558
+ }
559
+ }
544
560
  let NftData$1 = class NftData {
545
561
  constructor(data) {
546
562
  this.token_id = data.token_id && data.token_id !== null ? BigInt(data.token_id) : null;
@@ -5852,7 +5868,7 @@ class XykService {
5852
5868
  }
5853
5869
  }
5854
5870
 
5855
- const userAgent = "com.covalenthq.sdk.typescript/0.6.0";
5871
+ const userAgent = "com.covalenthq.sdk.typescript/0.6.2";
5856
5872
  /**
5857
5873
  * CovalentClient Class
5858
5874
  */
@@ -5887,6 +5903,28 @@ class Client {
5887
5903
  }
5888
5904
  }
5889
5905
 
5906
+ const calculatePrettyBalance = (value, decimals = 18, roundOff = true, precision = 0) => {
5907
+ const bigIntValue = BigInt(value);
5908
+ const bigDecimalValue = new Big(bigIntValue.toString());
5909
+ const _decimals = decimals || 18;
5910
+ const _expoValue = BigInt(Math.pow(10, _decimals));
5911
+ const bigDecimalExpo = new Big(_expoValue.toString());
5912
+ const _calculated = bigDecimalValue.div(bigDecimalExpo);
5913
+ // removes the decimal places, true by default so it adds decimals
5914
+ if (!roundOff) {
5915
+ return _calculated.toString();
5916
+ }
5917
+ let _decimalFixed = precision;
5918
+ if (precision === 0) {
5919
+ _decimalFixed = 2;
5920
+ if (_calculated.lt(100)) {
5921
+ _decimalFixed = 6;
5922
+ }
5923
+ }
5924
+ return _calculated.toFixed(_decimalFixed);
5925
+ };
5926
+
5890
5927
  exports.Client = Client;
5891
5928
  exports.CovalentClient = CovalentClient;
5929
+ exports.calculatePrettyBalance = calculatePrettyBalance;
5892
5930
  //# sourceMappingURL=index.js.map