Added numberFormatter

This commit is contained in:
Tornado Contrib 2024-10-18 13:23:14 +00:00
parent 8600bd8842
commit 6ab73c7d59
Signed by: tornadocontrib
GPG Key ID: 60B4DF1A076C64B1
8 changed files with 84 additions and 6 deletions

17
dist/index.js vendored

@ -124,6 +124,20 @@ function substring(str, length = 10) {
async function digest(bytes, algo = "SHA-384") { async function digest(bytes, algo = "SHA-384") {
return new Uint8Array(await crypto.subtle.digest(algo, bytes)); return new Uint8Array(await crypto.subtle.digest(algo, bytes));
} }
function numberFormatter(num, digits = 3) {
const lookup = [
{ value: 1, symbol: "" },
{ value: 1e3, symbol: "K" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "G" },
{ value: 1e12, symbol: "T" },
{ value: 1e15, symbol: "P" },
{ value: 1e18, symbol: "E" }
];
const regexp = /\.0+$|(?<=\.[0-9]*[1-9])0+$/;
const item = lookup.slice().reverse().find((item2) => Number(num) >= item2.value);
return item ? (Number(num) / item.value).toFixed(digits).replace(regexp, "").concat(item.symbol) : "0";
}
const defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0"; const defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0";
const fetch = crossFetch; const fetch = crossFetch;
@ -6541,7 +6555,7 @@ function gasZipInput(to, shorts) {
data += "01"; data += "01";
} }
for (const i in shorts) { for (const i in shorts) {
data += "0x" + Number(shorts[i]).toString(16).slice(2).padStart(4, "0"); data += Number(shorts[i]).toString(16).padStart(4, "0");
} }
return data; return data;
} }
@ -7561,6 +7575,7 @@ exports.loadDBEvents = loadDBEvents;
exports.loadRemoteEvents = loadRemoteEvents; exports.loadRemoteEvents = loadRemoteEvents;
exports.mimc = mimc; exports.mimc = mimc;
exports.multicall = multicall; exports.multicall = multicall;
exports.numberFormatter = numberFormatter;
exports.packEncryptedMessage = packEncryptedMessage; exports.packEncryptedMessage = packEncryptedMessage;
exports.pedersen = pedersen; exports.pedersen = pedersen;
exports.pickWeightedRandomRelayer = pickWeightedRandomRelayer; exports.pickWeightedRandomRelayer = pickWeightedRandomRelayer;

18
dist/index.mjs vendored

@ -103,6 +103,20 @@ function substring(str, length = 10) {
async function digest(bytes, algo = "SHA-384") { async function digest(bytes, algo = "SHA-384") {
return new Uint8Array(await crypto.subtle.digest(algo, bytes)); return new Uint8Array(await crypto.subtle.digest(algo, bytes));
} }
function numberFormatter(num, digits = 3) {
const lookup = [
{ value: 1, symbol: "" },
{ value: 1e3, symbol: "K" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "G" },
{ value: 1e12, symbol: "T" },
{ value: 1e15, symbol: "P" },
{ value: 1e18, symbol: "E" }
];
const regexp = /\.0+$|(?<=\.[0-9]*[1-9])0+$/;
const item = lookup.slice().reverse().find((item2) => Number(num) >= item2.value);
return item ? (Number(num) / item.value).toFixed(digits).replace(regexp, "").concat(item.symbol) : "0";
}
const defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0"; const defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/115.0";
const fetch = crossFetch; const fetch = crossFetch;
@ -6520,7 +6534,7 @@ function gasZipInput(to, shorts) {
data += "01"; data += "01";
} }
for (const i in shorts) { for (const i in shorts) {
data += "0x" + Number(shorts[i]).toString(16).slice(2).padStart(4, "0"); data += Number(shorts[i]).toString(16).padStart(4, "0");
} }
return data; return data;
} }
@ -7411,4 +7425,4 @@ async function calculateSnarkProof(input, circuit, provingKey) {
return { proof, args }; return { proof, args };
} }
export { BaseEchoService, BaseEncryptedNotesService, BaseEventsService, BaseGovernanceService, BaseRegistryService, BaseTornadoService, BatchBlockService, BatchEventsService, BatchTransactionService, DBTornadoService, DEPOSIT, Deposit, ENS__factory, ERC20__factory, GET_DEPOSITS, GET_ECHO_EVENTS, GET_ENCRYPTED_NOTES, GET_GOVERNANCE_APY, GET_GOVERNANCE_EVENTS, GET_NOTE_ACCOUNTS, GET_REGISTERED, GET_STATISTIC, GET_WITHDRAWALS, INDEX_DB_ERROR, IndexedDB, Invoice, MAX_FEE, MAX_TOVARISH_EVENTS, MIN_FEE, MIN_STAKE_BALANCE, MerkleTreeService, Mimc, Multicall__factory, NetId, NoteAccount, OffchainOracle__factory, OvmGasPriceOracle__factory, Pedersen, RelayerClient, ReverseRecords__factory, TokenPriceOracle, TornadoBrowserProvider, TornadoFeeOracle, TornadoRpcSigner, TornadoVoidSigner, TornadoWallet, TovarishClient, WITHDRAWAL, _META, addNetwork, addressSchemaType, ajv, base64ToBytes, bigIntReplacer, bnSchemaType, bnToBytes, buffPedersenHash, bufferToBytes, bytes32BNSchemaType, bytes32SchemaType, bytesToBN, bytesToBase64, bytesToHex, calculateScore, calculateSnarkProof, chunk, concatBytes, convertETHToTokenAmount, createDeposit, crypto, customConfig, defaultConfig, defaultUserAgent, depositsEventsSchema, digest, downloadZip, echoEventsSchema, enabledChains, encryptedNotesSchema, index as factories, fetch, fetchData, fetchGetUrlFunc, gasZipID, gasZipInbounds, gasZipInput, gasZipMinMax, getActiveTokenInstances, getActiveTokens, getAllDeposits, getAllEncryptedNotes, getAllGovernanceEvents, getAllGraphEchoEvents, getAllRegisters, getAllWithdrawals, getConfig, getDeposits, getEncryptedNotes, getEventsSchemaValidator, getGovernanceEvents, getGraphEchoEvents, getHttpAgent, getIndexedDB, getInstanceByAddress, getMeta, getNetworkConfig, getNoteAccounts, getProvider, getProviderWithNetId, getRegisters, getRelayerEnsSubdomains, getStatistic, getStatusSchema, getSupportedInstances, getTokenBalances, getTovarishNetworks, getWeightRandom, getWithdrawals, governanceEventsSchema, hexToBytes, initGroth16, isNode, jobRequestSchema, jobsSchema, leBuff2Int, leInt2Buff, loadDBEvents, loadRemoteEvents, mimc, multicall, packEncryptedMessage, pedersen, pickWeightedRandomRelayer, populateTransaction, proofSchemaType, proposalState, queryGraph, rBigInt, registeredEventsSchema, saveDBEvents, sleep, substring, toFixedHex, toFixedLength, unpackEncryptedMessage, unzipAsync, validateUrl, withdrawalsEventsSchema, zipAsync }; export { BaseEchoService, BaseEncryptedNotesService, BaseEventsService, BaseGovernanceService, BaseRegistryService, BaseTornadoService, BatchBlockService, BatchEventsService, BatchTransactionService, DBTornadoService, DEPOSIT, Deposit, ENS__factory, ERC20__factory, GET_DEPOSITS, GET_ECHO_EVENTS, GET_ENCRYPTED_NOTES, GET_GOVERNANCE_APY, GET_GOVERNANCE_EVENTS, GET_NOTE_ACCOUNTS, GET_REGISTERED, GET_STATISTIC, GET_WITHDRAWALS, INDEX_DB_ERROR, IndexedDB, Invoice, MAX_FEE, MAX_TOVARISH_EVENTS, MIN_FEE, MIN_STAKE_BALANCE, MerkleTreeService, Mimc, Multicall__factory, NetId, NoteAccount, OffchainOracle__factory, OvmGasPriceOracle__factory, Pedersen, RelayerClient, ReverseRecords__factory, TokenPriceOracle, TornadoBrowserProvider, TornadoFeeOracle, TornadoRpcSigner, TornadoVoidSigner, TornadoWallet, TovarishClient, WITHDRAWAL, _META, addNetwork, addressSchemaType, ajv, base64ToBytes, bigIntReplacer, bnSchemaType, bnToBytes, buffPedersenHash, bufferToBytes, bytes32BNSchemaType, bytes32SchemaType, bytesToBN, bytesToBase64, bytesToHex, calculateScore, calculateSnarkProof, chunk, concatBytes, convertETHToTokenAmount, createDeposit, crypto, customConfig, defaultConfig, defaultUserAgent, depositsEventsSchema, digest, downloadZip, echoEventsSchema, enabledChains, encryptedNotesSchema, index as factories, fetch, fetchData, fetchGetUrlFunc, gasZipID, gasZipInbounds, gasZipInput, gasZipMinMax, getActiveTokenInstances, getActiveTokens, getAllDeposits, getAllEncryptedNotes, getAllGovernanceEvents, getAllGraphEchoEvents, getAllRegisters, getAllWithdrawals, getConfig, getDeposits, getEncryptedNotes, getEventsSchemaValidator, getGovernanceEvents, getGraphEchoEvents, getHttpAgent, getIndexedDB, getInstanceByAddress, getMeta, getNetworkConfig, getNoteAccounts, getProvider, getProviderWithNetId, getRegisters, getRelayerEnsSubdomains, getStatistic, getStatusSchema, getSupportedInstances, getTokenBalances, getTovarishNetworks, getWeightRandom, getWithdrawals, governanceEventsSchema, hexToBytes, initGroth16, isNode, jobRequestSchema, jobsSchema, leBuff2Int, leInt2Buff, loadDBEvents, loadRemoteEvents, mimc, multicall, numberFormatter, packEncryptedMessage, pedersen, pickWeightedRandomRelayer, populateTransaction, proofSchemaType, proposalState, queryGraph, rBigInt, registeredEventsSchema, saveDBEvents, sleep, substring, toFixedHex, toFixedLength, unpackEncryptedMessage, unzipAsync, validateUrl, withdrawalsEventsSchema, zipAsync };

@ -102097,6 +102097,20 @@ function substring(str, length = 10) {
async function digest(bytes, algo = "SHA-384") { async function digest(bytes, algo = "SHA-384") {
return new Uint8Array(await utils_crypto.subtle.digest(algo, bytes)); return new Uint8Array(await utils_crypto.subtle.digest(algo, bytes));
} }
function numberFormatter(num, digits = 3) {
const lookup = [
{ value: 1, symbol: "" },
{ value: 1e3, symbol: "K" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "G" },
{ value: 1e12, symbol: "T" },
{ value: 1e15, symbol: "P" },
{ value: 1e18, symbol: "E" }
];
const regexp = /\.0+$|(?<=\.[0-9]*[1-9])0+$/;
const item = lookup.slice().reverse().find((item2) => Number(num) >= item2.value);
return item ? (Number(num) / item.value).toFixed(digits).replace(regexp, "").concat(item.symbol) : "0";
}
;// ./src/merkleTreeWorker.ts ;// ./src/merkleTreeWorker.ts

18
dist/tornado.umd.js vendored

@ -60250,7 +60250,7 @@ function gasZipInput(to, shorts) {
data += "01"; data += "01";
} }
for (const i in shorts) { for (const i in shorts) {
data += "0x" + Number(shorts[i]).toString(16).slice(2).padStart(4, "0"); data += Number(shorts[i]).toString(16).padStart(4, "0");
} }
return data; return data;
} }
@ -75573,6 +75573,7 @@ class ReverseRecords__factory {
/* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ $W: () => (/* binding */ toFixedHex), /* harmony export */ $W: () => (/* binding */ toFixedHex),
/* harmony export */ EI: () => (/* binding */ leInt2Buff), /* harmony export */ EI: () => (/* binding */ leInt2Buff),
/* harmony export */ Eg: () => (/* binding */ numberFormatter),
/* harmony export */ Et: () => (/* binding */ crypto), /* harmony export */ Et: () => (/* binding */ crypto),
/* harmony export */ Id: () => (/* binding */ concatBytes), /* harmony export */ Id: () => (/* binding */ concatBytes),
/* harmony export */ Ju: () => (/* binding */ bytesToBN), /* harmony export */ Ju: () => (/* binding */ bytesToBN),
@ -75690,6 +75691,20 @@ function substring(str, length = 10) {
async function digest(bytes, algo = "SHA-384") { async function digest(bytes, algo = "SHA-384") {
return new Uint8Array(await crypto.subtle.digest(algo, bytes)); return new Uint8Array(await crypto.subtle.digest(algo, bytes));
} }
function numberFormatter(num, digits = 3) {
const lookup = [
{ value: 1, symbol: "" },
{ value: 1e3, symbol: "K" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "G" },
{ value: 1e12, symbol: "T" },
{ value: 1e15, symbol: "P" },
{ value: 1e18, symbol: "E" }
];
const regexp = /\.0+$|(?<=\.[0-9]*[1-9])0+$/;
const item = lookup.slice().reverse().find((item2) => Number(num) >= item2.value);
return item ? (Number(num) / item.value).toFixed(digits).replace(regexp, "").concat(item.symbol) : "0";
}
/***/ }), /***/ }),
@ -177010,6 +177025,7 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ leInt2Buff: () => (/* reexport safe */ _utils__WEBPACK_IMPORTED_MODULE_20__.EI), /* harmony export */ leInt2Buff: () => (/* reexport safe */ _utils__WEBPACK_IMPORTED_MODULE_20__.EI),
/* harmony export */ mimc: () => (/* reexport safe */ _mimc__WEBPACK_IMPORTED_MODULE_11__.f), /* harmony export */ mimc: () => (/* reexport safe */ _mimc__WEBPACK_IMPORTED_MODULE_11__.f),
/* harmony export */ multicall: () => (/* reexport safe */ _multicall__WEBPACK_IMPORTED_MODULE_12__.C), /* harmony export */ multicall: () => (/* reexport safe */ _multicall__WEBPACK_IMPORTED_MODULE_12__.C),
/* harmony export */ numberFormatter: () => (/* reexport safe */ _utils__WEBPACK_IMPORTED_MODULE_20__.Eg),
/* harmony export */ packEncryptedMessage: () => (/* reexport safe */ _encryptedNotes__WEBPACK_IMPORTED_MODULE_6__.Fr), /* harmony export */ packEncryptedMessage: () => (/* reexport safe */ _encryptedNotes__WEBPACK_IMPORTED_MODULE_6__.Fr),
/* harmony export */ pedersen: () => (/* reexport safe */ _pedersen__WEBPACK_IMPORTED_MODULE_14__.NO), /* harmony export */ pedersen: () => (/* reexport safe */ _pedersen__WEBPACK_IMPORTED_MODULE_14__.NO),
/* harmony export */ pickWeightedRandomRelayer: () => (/* reexport safe */ _relayerClient__WEBPACK_IMPORTED_MODULE_17__.sN), /* harmony export */ pickWeightedRandomRelayer: () => (/* reexport safe */ _relayerClient__WEBPACK_IMPORTED_MODULE_17__.sN),

File diff suppressed because one or more lines are too long

1
dist/utils.d.ts vendored

@ -23,4 +23,5 @@ export declare function rBigInt(nbytes?: number): bigint;
export declare function bigIntReplacer(key: any, value: any): any; export declare function bigIntReplacer(key: any, value: any): any;
export declare function substring(str: string, length?: number): string; export declare function substring(str: string, length?: number): string;
export declare function digest(bytes: Uint8Array, algo?: string): Promise<Uint8Array>; export declare function digest(bytes: Uint8Array, algo?: string): Promise<Uint8Array>;
export declare function numberFormatter(num: string | number | bigint, digits?: number): string;
export {}; export {};

@ -39,7 +39,7 @@ export function gasZipInput(to: string, shorts: number[]): string | null {
} }
for (const i in shorts) { for (const i in shorts) {
data += '0x' + Number(shorts[i]).toString(16).slice(2).padStart(4, '0'); data += Number(shorts[i]).toString(16).padStart(4, '0');
} }
return data; return data;

@ -147,3 +147,21 @@ export function substring(str: string, length: number = 10) {
export async function digest(bytes: Uint8Array, algo: string = 'SHA-384') { export async function digest(bytes: Uint8Array, algo: string = 'SHA-384') {
return new Uint8Array(await crypto.subtle.digest(algo, bytes)); return new Uint8Array(await crypto.subtle.digest(algo, bytes));
} }
export function numberFormatter(num: string | number | bigint, digits: number = 3): string {
const lookup = [
{ value: 1, symbol: '' },
{ value: 1e3, symbol: 'K' },
{ value: 1e6, symbol: 'M' },
{ value: 1e9, symbol: 'G' },
{ value: 1e12, symbol: 'T' },
{ value: 1e15, symbol: 'P' },
{ value: 1e18, symbol: 'E' },
];
const regexp = /\.0+$|(?<=\.[0-9]*[1-9])0+$/;
const item = lookup
.slice()
.reverse()
.find((item) => Number(num) >= item.value);
return item ? (Number(num) / item.value).toFixed(digits).replace(regexp, '').concat(item.symbol) : '0';
}