@covalenthq/client-sdk 0.6.4 → 0.6.6

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 +72 -2
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/services/BalanceService.d.ts +2 -2
  6. package/dist/cjs/services/CovalentClient.d.ts +1 -1
  7. package/dist/cjs/util/prettifyCurrency.d.ts +2 -0
  8. package/dist/cjs/util/types/BalanceServiceTypes.d.ts +2 -2
  9. package/dist/es/index.d.ts +1 -0
  10. package/dist/es/index.js +72 -3
  11. package/dist/es/index.js.map +1 -1
  12. package/dist/es/services/BalanceService.d.ts +2 -2
  13. package/dist/es/services/CovalentClient.d.ts +1 -1
  14. package/dist/es/util/prettifyCurrency.d.ts +2 -0
  15. package/dist/es/util/types/BalanceServiceTypes.d.ts +2 -2
  16. package/dist/esm/index.d.ts +1 -0
  17. package/dist/esm/index.js +72 -3
  18. package/dist/esm/index.js.map +1 -1
  19. package/dist/esm/services/BalanceService.d.ts +2 -2
  20. package/dist/esm/services/CovalentClient.d.ts +1 -1
  21. package/dist/esm/util/prettifyCurrency.d.ts +2 -0
  22. package/dist/esm/util/types/BalanceServiceTypes.d.ts +2 -2
  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 +2 -2
  27. package/dist/services/BalanceService.js +1 -1
  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/prettifyCurrency.d.ts +2 -0
  32. package/dist/util/prettifyCurrency.js +69 -0
  33. package/dist/util/prettifyCurrency.js.map +1 -0
  34. package/dist/util/types/BalanceServiceTypes.d.ts +2 -2
  35. package/package.json +1 -1
package/README.md CHANGED
@@ -107,6 +107,16 @@ const resp = await client.BalanceService.getTokenBalancesForWalletAddress("eth-m
107
107
  const prettyBalance = calculatePrettyBalance(resp.data.items[0].balance, resp.data.items[0].contract_decimals);
108
108
  ```
109
109
 
110
+ The `prettifyCurrency` function refines the presentation of a monetary value, accepting a numerical amount and a fiat currency code as parameters (with USD as the default currency). It simplifies currency formatting for developers, ensuring visually polished representations of financial information in user interfaces for an enhanced user experience.
111
+
112
+ ```ts
113
+ import { CovalentClient, prettifyCurrency } from "@covalenthq/client-sdk";
114
+
115
+ const client = new CovalentClient("YOUR_API_KEY"); // Replace with your Covalent API key.
116
+ const resp = await client.BalanceService.getTokenBalancesForWalletAddress("eth-mainnet", "WALLET_ADDRESS");
117
+ const prettyCurrency = prettifyCurrency(resp.data.items[0].quote_rate);
118
+ ```
119
+
110
120
  ### BaseService
111
121
 
112
122
  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/):
@@ -8,3 +8,4 @@ export * from "./util/types/XykServiceTypes";
8
8
  export * from "./util/types/GenericTypes";
9
9
  export * from "./util/types/TransactionServiceTypes";
10
10
  export { calculatePrettyBalance } from "./util/CalculatePrettyBalance";
11
+ export { prettifyCurrency } from "./util/prettifyCurrency";
package/dist/cjs/index.js CHANGED
@@ -659,7 +659,6 @@ class BlockTransactionWithContractTransfers {
659
659
  this.gas_quote = data.gas_quote;
660
660
  this.pretty_gas_quote = data.pretty_gas_quote;
661
661
  this.gas_quote_rate = data.gas_quote_rate;
662
- this.explorers = data.explorers && data.explorers !== null ? data.explorers.map((itemData) => new Explorer$1(itemData)) : null;
663
662
  this.transfers = data.transfers && data.transfers !== null ? data.transfers.map((itemData) => new TokenTransferItem(itemData)) : null;
664
663
  }
665
664
  }
@@ -698,6 +697,7 @@ class TokenTransferItem {
698
697
  this.pretty_delta_quote = data.pretty_delta_quote;
699
698
  this.balance_quote = data.balance_quote;
700
699
  this.method_calls = data.method_calls && data.method_calls !== null ? data.method_calls.map((itemData) => new MethodCallsForTransfers(itemData)) : null;
700
+ this.explorers = data.explorers && data.explorers !== null ? data.explorers.map((itemData) => new Explorer$1(itemData)) : null;
701
701
  }
702
702
  }
703
703
  class MethodCallsForTransfers {
@@ -6248,7 +6248,7 @@ class XykService {
6248
6248
  }
6249
6249
  }
6250
6250
 
6251
- const userAgent = "com.covalenthq.sdk.typescript/0.6.4";
6251
+ const userAgent = "com.covalenthq.sdk.typescript/0.6.6";
6252
6252
  /**
6253
6253
  * CovalentClient Class
6254
6254
  */
@@ -6311,7 +6311,77 @@ const calculatePrettyBalance = (value, decimals = 18, roundOff = true, precision
6311
6311
  return _calculated.toFixed(_decimalFixed);
6312
6312
  };
6313
6313
 
6314
+ const LESS_THAN_ZERO = "0.01";
6315
+ const ZERO = "0.00";
6316
+ const currencyMap = new Map([
6317
+ ["USD", "$"],
6318
+ ["CAD", "CA$"],
6319
+ ["EUR", "€"],
6320
+ ["SGD", "S$"],
6321
+ ["INR", "₹"],
6322
+ ["JPY", "¥"],
6323
+ ["VND", "₫"],
6324
+ ["CNY", "CN¥"],
6325
+ ["KRW", "₩"],
6326
+ ["RUB", "₽"],
6327
+ ["TRY", "₺"],
6328
+ ["NGN", "₦"],
6329
+ ["ARS", "ARS"],
6330
+ ["AUD", "A$"],
6331
+ ["CHF", "CHF"],
6332
+ ["GBP", "£"],
6333
+ ]);
6334
+ const prettifyCurrency = (value, decimals = 2, currency = "USD", ignoreSmallValue = false, ignoreMinus = true, ignoreZero = false) => {
6335
+ if (typeof value === "string") {
6336
+ value = Number(value);
6337
+ }
6338
+ let minus = "";
6339
+ let currencySuffix = "";
6340
+ // pass ignoreMinus false to get the negative number for currency formatter
6341
+ if (!ignoreMinus && value < 0) {
6342
+ value = Math.abs(value);
6343
+ minus = "-";
6344
+ }
6345
+ if (value === 0 || !value) {
6346
+ // if value is 0, pass ignoreZero true to get this string "<$0.01"
6347
+ if (ignoreZero) {
6348
+ return "<" + currencyMap.get(currency) + LESS_THAN_ZERO;
6349
+ }
6350
+ else {
6351
+ return currencyMap.get(currency) + ZERO;
6352
+ }
6353
+ }
6354
+ else if (value < 0 || value < 1) {
6355
+ if (value < 0.01 && ignoreSmallValue) {
6356
+ return "<" + currencyMap.get(currency) + LESS_THAN_ZERO;
6357
+ }
6358
+ }
6359
+ else if (value > 999999999) {
6360
+ value = value / 1000000000;
6361
+ currencySuffix = "B";
6362
+ }
6363
+ else if (value > 999999) {
6364
+ value = value / 1000000; // convert to M for number from > 1 million
6365
+ currencySuffix = "M";
6366
+ }
6367
+ // Added to round down the number
6368
+ const expo = Math.pow(10, decimals);
6369
+ value = Math.floor(value * expo) / expo;
6370
+ // generates the value with the inputted currency
6371
+ const formatter = new Intl.NumberFormat('en-US', {
6372
+ style: 'currency',
6373
+ currency: currency,
6374
+ maximumFractionDigits: decimals,
6375
+ currencyDisplay: "symbol"
6376
+ });
6377
+ //replace the occasional incorrect currency symbol from the formatter and replace with correct symbol from currencyMap
6378
+ const regex = new RegExp(`${currency}\\s?`);
6379
+ const _val = formatter.format(value).replace(regex, currencyMap.get(currency) ?? "$");
6380
+ return minus + _val + currencySuffix;
6381
+ };
6382
+
6314
6383
  exports.Client = Client;
6315
6384
  exports.CovalentClient = CovalentClient;
6316
6385
  exports.calculatePrettyBalance = calculatePrettyBalance;
6386
+ exports.prettifyCurrency = prettifyCurrency;
6317
6387
  //# sourceMappingURL=index.js.map