@covalenthq/client-sdk 0.6.1 → 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.
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
@@ -5867,7 +5868,7 @@ class XykService {
5867
5868
  }
5868
5869
  }
5869
5870
 
5870
- const userAgent = "com.covalenthq.sdk.typescript/0.6.1";
5871
+ const userAgent = "com.covalenthq.sdk.typescript/0.6.2";
5871
5872
  /**
5872
5873
  * CovalentClient Class
5873
5874
  */
@@ -5902,6 +5903,28 @@ class Client {
5902
5903
  }
5903
5904
  }
5904
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
+
5905
5927
  exports.Client = Client;
5906
5928
  exports.CovalentClient = CovalentClient;
5929
+ exports.calculatePrettyBalance = calculatePrettyBalance;
5907
5930
  //# sourceMappingURL=index.js.map