@covalenthq/client-sdk 0.6.4 → 0.6.5
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 +10 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +71 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/services/CovalentClient.d.ts +1 -1
- package/dist/cjs/util/prettifyCurrency.d.ts +2 -0
- package/dist/es/index.d.ts +1 -0
- package/dist/es/index.js +71 -2
- package/dist/es/index.js.map +1 -1
- package/dist/es/services/CovalentClient.d.ts +1 -1
- package/dist/es/util/prettifyCurrency.d.ts +2 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +71 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/services/CovalentClient.d.ts +1 -1
- package/dist/esm/util/prettifyCurrency.d.ts +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/services/CovalentClient.d.ts +1 -1
- package/dist/services/CovalentClient.js +1 -1
- package/dist/util/prettifyCurrency.d.ts +2 -0
- package/dist/util/prettifyCurrency.js +69 -0
- package/dist/util/prettifyCurrency.js.map +1 -0
- 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/):
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -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
|
@@ -6248,7 +6248,7 @@ class XykService {
|
|
|
6248
6248
|
}
|
|
6249
6249
|
}
|
|
6250
6250
|
|
|
6251
|
-
const userAgent = "com.covalenthq.sdk.typescript/0.6.
|
|
6251
|
+
const userAgent = "com.covalenthq.sdk.typescript/0.6.5";
|
|
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
|