@avalabs/core-utils-sdk 2.8.0-alpha.197
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/LICENSE +9 -0
- package/README.md +0 -0
- package/dist/index.d.ts +266 -0
- package/dist/index.js +1 -0
- package/esm/balanceToDisplayValue.d.ts +10 -0
- package/esm/balanceToDisplayValue.js +1 -0
- package/esm/bech32ToHex.d.ts +7 -0
- package/esm/bech32ToHex.js +1 -0
- package/esm/bigConstants.d.ts +6 -0
- package/esm/bigConstants.js +1 -0
- package/esm/bigIntToString.d.ts +8 -0
- package/esm/bigIntToString.js +1 -0
- package/esm/bigToBN.d.ts +6 -0
- package/esm/bigToBN.js +1 -0
- package/esm/bigToBigInt.d.ts +5 -0
- package/esm/bigToBigInt.js +1 -0
- package/esm/bigToLocaleString.d.ts +5 -0
- package/esm/bigToLocaleString.js +1 -0
- package/esm/bigintToBig.d.ts +5 -0
- package/esm/bigintToBig.js +1 -0
- package/esm/bnAvaxCrossChain.d.ts +12 -0
- package/esm/bnAvaxCrossChain.js +1 -0
- package/esm/bnToAvax.d.ts +29 -0
- package/esm/bnToAvax.js +1 -0
- package/esm/bnToBig.d.ts +10 -0
- package/esm/bnToBig.js +1 -0
- package/esm/bnToBigAvax.d.ts +8 -0
- package/esm/bnToBigAvax.js +1 -0
- package/esm/bnToLocaleString.d.ts +18 -0
- package/esm/bnToLocaleString.js +1 -0
- package/esm/encryptAnalyticsData.d.ts +7 -0
- package/esm/encryptAnalyticsData.js +1 -0
- package/esm/hexToBN.d.ts +14 -0
- package/esm/hexToBN.js +1 -0
- package/esm/hexToBytes.d.ts +3 -0
- package/esm/hexToBytes.js +1 -0
- package/esm/httpClient.d.ts +16 -0
- package/esm/httpClient.js +1 -0
- package/esm/index.d.ts +32 -0
- package/esm/index.js +1 -0
- package/esm/ipfsResolver.d.ts +11 -0
- package/esm/ipfsResolver.js +1 -0
- package/esm/logFormatter.d.ts +9 -0
- package/esm/logFormatter.js +1 -0
- package/esm/noop.d.ts +3 -0
- package/esm/noop.js +1 -0
- package/esm/numberToBn.d.ts +10 -0
- package/esm/numberToBn.js +1 -0
- package/esm/numberToBnAvax.d.ts +7 -0
- package/esm/numberToBnAvax.js +1 -0
- package/esm/promiseResolveWithBackoff.d.ts +12 -0
- package/esm/promiseResolveWithBackoff.js +1 -0
- package/esm/promiseResolver.d.ts +10 -0
- package/esm/promiseResolver.js +1 -0
- package/esm/stringToBN.d.ts +15 -0
- package/esm/stringToBN.js +1 -0
- package/esm/strip0x.d.ts +7 -0
- package/esm/strip0x.js +1 -0
- package/esm/tokenUnit.d.ts +54 -0
- package/esm/tokenUnit.js +1 -0
- package/esm/trunateAddress.d.ts +3 -0
- package/esm/trunateAddress.js +1 -0
- package/esm/types/index.d.ts +3 -0
- package/esm/wait.d.ts +10 -0
- package/esm/wait.js +1 -0
- package/esm/weiToAvax.d.ts +5 -0
- package/esm/weiToAvax.js +1 -0
- package/esm/weiToNAvax.d.ts +5 -0
- package/esm/weiToNAvax.js +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Copyright (C) 2021, Ava Labs, Inc. All rights reserved.
|
|
2
|
+
|
|
3
|
+
Subject to the limited license below (**”License””), you may not, and you may not permit anyone else to, copy, reproduce, aggregate, republish, download, post, distribute, license, sublicense, reverse engineer, modify, or create derivative works based on this software (collectively, **“Software”**).
|
|
4
|
+
|
|
5
|
+
You are hereby granted a limited, non-exclusive, non-sublicensable and non-transferable license to download and use the Software as-is solely (i) for use in connection with the Avalanche Public Blockchain platform, having a NetworkID of 1 (Mainnet) or 5 (Fuji), and associated blockchains, comprised exclusively of the Avalanche X-Chain, C-Chain, P-Chain and any subnets linked to the P-Chain (**“Avalanche Authorized Platform”**) or (ii) for non-production, testing or research purposes without any commercial application within the Avalanche ecosystem (**“Non-Commercial Use”**); provided that, in each case, you may not use or allow use of the Software (a) in connection with any forks of the Avalanche Authorized Platform, (b) in any manner not operationally connected to the Avalanche Authorized Platform other than for Non-Commercial Use, or (c) to the extent the number of monthly active users or the number of total installs of any software that uses the Software across all versions thereof exceeds 10,000 at any time. You may not modify or alter the Software in any way.
|
|
6
|
+
|
|
7
|
+
You hereby acknowledge and agree to the terms set forth at www.avalabs.org/important-notice.
|
|
8
|
+
|
|
9
|
+
**TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED ON AN “AS IS” BASIS, AND AVA LABS EXPRESSLY DISCLAIMS AND EXCLUDES ALL REPRESENTATIONS, WARRANTIES AND OTHER TERMS AND CONDITIONS, WHETHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION BY OPERATION OF LAW OR BY CUSTOM, STATUTE OR OTHERWISE, AND INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED WARRANTY, TERM, OR CONDITION OF NON-INFRINGEMENT, MERCHANTABILITY, TITLE, OR FITNESS FOR PARTICULAR PURPOSE. YOU USE THE SOFTWARE AT YOUR OWN RISK. AVA LABS EXPRESSLY DISCLAIMS ALL LIABILITY (INCLUDING FOR ALL DIRECT, CONSEQUENTIAL OR OTHER DAMAGES OR LOSSES) RELATED TO ANY USE OF THE SOFTWARE.**
|
package/README.md
ADDED
|
File without changes
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import * as BN from 'bn.js';
|
|
2
|
+
import BN__default from 'bn.js';
|
|
3
|
+
import Big, { BigSource } from 'big.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Given a balance, returns a string for display
|
|
7
|
+
* @param val The balance in BN format.
|
|
8
|
+
* @param decimals Number of decimals token has.
|
|
9
|
+
*/
|
|
10
|
+
declare function balanceToDisplayValue(val: BN__default, decimals: number): string;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Return the hex format of a bech32 Avalanche address. `0x` is omitted on the return value.
|
|
14
|
+
* @param address Bech32 Avalanche address (X-avax..)
|
|
15
|
+
*/
|
|
16
|
+
declare function bech32ToHex(address: string): string;
|
|
17
|
+
|
|
18
|
+
declare const BIG_TEN: Big;
|
|
19
|
+
declare const BIG_ZERO: Big;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Pretty prints a native BigInt with the given decimal setting
|
|
23
|
+
* @param value
|
|
24
|
+
* @param decimals
|
|
25
|
+
*/
|
|
26
|
+
declare function bigIntToString(value: bigint, decimals: number): string;
|
|
27
|
+
|
|
28
|
+
declare function bigintToBig(amount: bigint, denomination: number): Big;
|
|
29
|
+
|
|
30
|
+
declare function bigToBN(val: Big, denom: number): BN__default;
|
|
31
|
+
|
|
32
|
+
declare function bigToBigInt(val: Big, denom: number): bigint;
|
|
33
|
+
|
|
34
|
+
declare function bigToLocaleString(bigVal: Big, decimals?: number): string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Converts a BN amount of 18 decimals to 9.
|
|
38
|
+
* Used for AVAX C <-> X,P conversions
|
|
39
|
+
* @param amount
|
|
40
|
+
*/
|
|
41
|
+
declare function avaxCtoX(amount: BN__default): BN__default;
|
|
42
|
+
declare function avaxXtoC(amount: BN__default): BN__default;
|
|
43
|
+
declare function avaxPtoC(amount: BN__default): BN__default;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parses the value using a denomination of 18
|
|
47
|
+
*
|
|
48
|
+
* @param val the amount to parse given in WEI
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```
|
|
52
|
+
* bnToAvaxC(new BN('22500000000000000000')
|
|
53
|
+
* // will return 22.5
|
|
54
|
+
*```
|
|
55
|
+
*
|
|
56
|
+
*/
|
|
57
|
+
declare function bnToAvaxC(val: BN__default): string;
|
|
58
|
+
/**
|
|
59
|
+
* Parses the value using a denomination of 9
|
|
60
|
+
*
|
|
61
|
+
* @param val the amount to parse given in nAVAX
|
|
62
|
+
*/
|
|
63
|
+
declare function bnToAvaxX(val: BN__default): string;
|
|
64
|
+
/**
|
|
65
|
+
* Parses the value using a denomination of 9
|
|
66
|
+
*
|
|
67
|
+
* @param val the amount to parse given in nAVAX
|
|
68
|
+
*/
|
|
69
|
+
declare function bnToAvaxP(val: BN__default): string;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @param val the amount to parse
|
|
73
|
+
* @param denomination number of decimal places to parse with
|
|
74
|
+
*/
|
|
75
|
+
declare function bnToBig(val: BN__default, denomination?: number): Big;
|
|
76
|
+
|
|
77
|
+
declare function bnToBigAvaxX(val: BN__default): Big;
|
|
78
|
+
declare function bnToBigAvaxP(val: BN__default): Big;
|
|
79
|
+
declare function bnToBigAvaxC(val: BN__default): Big;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @Remarks
|
|
83
|
+
* A helper method to convert BN numbers to human readable strings.
|
|
84
|
+
*
|
|
85
|
+
* @param val The amount to convert
|
|
86
|
+
* @param decimals Number of decimal places to parse the amount with
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```
|
|
90
|
+
* bnToLocaleString(new BN(100095),2)
|
|
91
|
+
* // will return '1,000.95'
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
declare function bnToLocaleString(val: BN__default, decimals?: number): string;
|
|
95
|
+
|
|
96
|
+
declare function encryptAnalyticsData(encryptionKeyID: string, encryptionKey: string, message?: string): Promise<{
|
|
97
|
+
data: string;
|
|
98
|
+
enc: string;
|
|
99
|
+
keyID: string;
|
|
100
|
+
}>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* BN has a bug when a converting hex to BN, if the hex starts with
|
|
104
|
+
* 0x BN doesnt know how to handle it and returns an incorrect value
|
|
105
|
+
*
|
|
106
|
+
* On the flip, BigInt cant parse hex without the 0x so we will use
|
|
107
|
+
* this helper function to deal with this
|
|
108
|
+
* @param hex the hex value
|
|
109
|
+
* @returns BN
|
|
110
|
+
*/
|
|
111
|
+
declare function hexToBN(hex: string): BN;
|
|
112
|
+
|
|
113
|
+
declare function hexToBytes(hex: string): number | null;
|
|
114
|
+
|
|
115
|
+
type HttpOptions = RequestInit & {
|
|
116
|
+
timeout?: number;
|
|
117
|
+
};
|
|
118
|
+
declare class HttpClient {
|
|
119
|
+
private readonly baseURL;
|
|
120
|
+
private TIMEOUT;
|
|
121
|
+
private headerOptions;
|
|
122
|
+
private nonHeaderOptions;
|
|
123
|
+
constructor(baseURL: string, options?: HttpOptions);
|
|
124
|
+
get<T>(path: string, params?: Record<string, any>, customOptions?: HttpOptions): Promise<T>;
|
|
125
|
+
post<T>(path: string, data: Record<string, any>, customOptions?: HttpOptions, params?: Record<string, any>): Promise<T>;
|
|
126
|
+
private handleResponse;
|
|
127
|
+
private fetchWithTimeout;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
declare const UNSUPPORTED_URL_ERROR = "unsupported ipfs URL pattern";
|
|
131
|
+
declare const urlContainsCID: (url: string) => {
|
|
132
|
+
containsCid: boolean;
|
|
133
|
+
cid: string;
|
|
134
|
+
} | {
|
|
135
|
+
containsCid: boolean;
|
|
136
|
+
cid: null;
|
|
137
|
+
};
|
|
138
|
+
declare const ipfsResolver: (sourceUrl: string, desiredGatewayPrefix: string) => string;
|
|
139
|
+
|
|
140
|
+
declare const repeat: (str: string, times: number) => string;
|
|
141
|
+
declare const padStart: (num: number, maxLength: number, char?: string) => string;
|
|
142
|
+
declare const formatTime: (time: Date) => string;
|
|
143
|
+
declare const now: () => string;
|
|
144
|
+
declare function formatAndLog(message: string, value: any, config?: {
|
|
145
|
+
color?: string;
|
|
146
|
+
}): void;
|
|
147
|
+
|
|
148
|
+
declare const noop: () => void;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
*
|
|
152
|
+
* @param val the number to parse
|
|
153
|
+
* @param decimals number of decimal places used to parse the number
|
|
154
|
+
*/
|
|
155
|
+
declare function numberToBN(val: number | string, decimals: number): BN__default;
|
|
156
|
+
|
|
157
|
+
declare function numberToBNAvaxX(val: number | string): BN;
|
|
158
|
+
declare function numberToBNAvaxP(val: number | string): BN;
|
|
159
|
+
declare function numberToBNAvaxC(val: number | string): BN;
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Simplify catching promise errors and returns a tuple of [result, error]
|
|
163
|
+
* @example
|
|
164
|
+
* const [result, error] = await resolve(promise)
|
|
165
|
+
* if (result) return result
|
|
166
|
+
* return handleError(error)
|
|
167
|
+
*/
|
|
168
|
+
declare function resolve<T>(promise: Promise<T>): Promise<[T, null] | [null, unknown]>;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Resolves promise with linear backoff policy for max of {@link maxAttempts} attempts
|
|
172
|
+
*
|
|
173
|
+
* @param prom Promise to resolve
|
|
174
|
+
* @param errorParser
|
|
175
|
+
* @param attempt Current attempt
|
|
176
|
+
* @param maxAttempts Max attempts
|
|
177
|
+
* @returns promise result
|
|
178
|
+
*/
|
|
179
|
+
declare function promiseResolveWithBackoff<T>(prom: () => Promise<T>, errorParser: (res: any) => boolean, attempt?: number, maxAttempts?: number): Promise<T>;
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Converts a string to a BN value of the given denomination.
|
|
183
|
+
* @param value The string value of the
|
|
184
|
+
* @param decimals
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```
|
|
188
|
+
* stringToBN('1.32', 5) // is same as BN(132000)
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
declare function stringToBN(value: string, decimals: number): BN__default;
|
|
192
|
+
|
|
193
|
+
declare const truncateAddress: (address: string, size?: number) => string;
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Promise friendly setTimeout
|
|
197
|
+
* @param ms number of milliseconds to wait
|
|
198
|
+
* @example
|
|
199
|
+
* await wait(1000);
|
|
200
|
+
* doSomething()
|
|
201
|
+
*/
|
|
202
|
+
declare function wait(ms: number): Promise<void>;
|
|
203
|
+
|
|
204
|
+
declare function weiToAvax(weiAmount: Big): Big;
|
|
205
|
+
|
|
206
|
+
declare function weiToNAvax(weiAmount: Big): Big;
|
|
207
|
+
|
|
208
|
+
/***
|
|
209
|
+
* Strip 0x from the beginning of a hex string.
|
|
210
|
+
* @param value The hex string to strip from.
|
|
211
|
+
*/
|
|
212
|
+
declare function strip0x(value: string): string;
|
|
213
|
+
|
|
214
|
+
type Maybe<T> = T | null | undefined;
|
|
215
|
+
|
|
216
|
+
type AcceptedTypes = BigSource | BN__default | bigint;
|
|
217
|
+
/**
|
|
218
|
+
* TokenUnit abstracts units in which crypto tokens are represented.
|
|
219
|
+
* It holds value of token in Big type in normal numeric representation (as opposed to exponential or any other).
|
|
220
|
+
* It also holds maxDecimals value which denotes smallest possible denomination for that token.
|
|
221
|
+
*
|
|
222
|
+
* For example, minimal denomination of Eth is Wei where 1 Eth = 1e18 Wei , which means maxDecimals is 18.
|
|
223
|
+
* Another example is Avax and nAvax where 1 Avax = 1e9 nAvax, therefore maxDecimals is 9.
|
|
224
|
+
*/
|
|
225
|
+
declare class TokenUnit {
|
|
226
|
+
/**
|
|
227
|
+
* In base unit
|
|
228
|
+
* @protected
|
|
229
|
+
*/
|
|
230
|
+
protected readonly value: Big;
|
|
231
|
+
protected readonly symbol: string;
|
|
232
|
+
protected readonly maxDecimals: number;
|
|
233
|
+
constructor(value: AcceptedTypes, maxDecimals: number, symbol: string);
|
|
234
|
+
getSymbol(): string;
|
|
235
|
+
getMaxDecimals(): number;
|
|
236
|
+
add(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
237
|
+
sub(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
238
|
+
mul(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
239
|
+
div(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
240
|
+
gt(value: TokenUnit | AcceptedTypes): boolean;
|
|
241
|
+
lt(value: TokenUnit | AcceptedTypes): boolean;
|
|
242
|
+
eq(value: TokenUnit | AcceptedTypes): boolean;
|
|
243
|
+
/**
|
|
244
|
+
* Tries to display token unit in most meaningful way.
|
|
245
|
+
* Precision of displaying token units makes sense only to up to
|
|
246
|
+
* *maxDecimals* decimal points, just as you would display US dollars up to 2 decimals.
|
|
247
|
+
* However, there's no point in showing all *maxDecimals* decimals if total
|
|
248
|
+
* value is huge, e.g. instead of displaying 1,000,000.000,000,001 we want to
|
|
249
|
+
* display 1,000,000. So in that effort this function will display maximum of
|
|
250
|
+
* **[maxDecimals - wholeDigits]** decimals, where *wholeDigits* is the number of digits
|
|
251
|
+
* of only whole portion of value.
|
|
252
|
+
*
|
|
253
|
+
* @param fixedDp See {@link Big.toFixed}
|
|
254
|
+
*/
|
|
255
|
+
toDisplay(fixedDp?: number): string;
|
|
256
|
+
isZero(): boolean;
|
|
257
|
+
/**
|
|
258
|
+
* Converts this base unit to the smallest unit defined by this.maxDecimals
|
|
259
|
+
* @param round
|
|
260
|
+
*/
|
|
261
|
+
toSubUnit(round?: boolean): bigint;
|
|
262
|
+
private static toBig;
|
|
263
|
+
private cloneWithValue;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export { AcceptedTypes, BIG_TEN, BIG_ZERO, HttpClient, HttpOptions, Maybe, TokenUnit, UNSUPPORTED_URL_ERROR, avaxCtoX, avaxPtoC, avaxXtoC, balanceToDisplayValue, bech32ToHex, bigIntToString, bigToBN, bigToBigInt, bigToLocaleString, bigintToBig, bnToAvaxC, bnToAvaxP, bnToAvaxX, bnToBig, bnToBigAvaxC, bnToBigAvaxP, bnToBigAvaxX, bnToLocaleString, encryptAnalyticsData, formatAndLog, formatTime, hexToBN, hexToBytes, ipfsResolver, noop, now, numberToBN, numberToBNAvaxC, numberToBNAvaxP, numberToBNAvaxX, padStart, promiseResolveWithBackoff, repeat, resolve, stringToBN, strip0x, truncateAddress, urlContainsCID, wait, weiToAvax, weiToNAvax };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var t=require("big.js"),e=require("@avalabs/avalanchejs"),n=require("bn.js"),r=require("@hpke/core"),o=require("is-ipfs");function i(e,n=0){const r=t(10).pow(n);return new t(e.toString()).div(r)}function s(t,e=9){const n=t.toFixed(e).split("."),r=parseInt(n[0]).toLocaleString("en-US");if(1===n.length)return r;{let t=n[1],o=t.charAt(t.length-1);for(;"0"===o;)t=t.substring(0,t.length-1),o=t.charAt(t.length-1);const i=t.substring(0,e);return i?`${r}.${i}`:r}}const a=new t(10),c=new t(0);function u(t){const e=new n(10).pow(new n(9));return t.mul(e)}function l(t,e=9){return s(i(t,e),e)}function p(t){return l(t,9)}function h(t){return i(t,9)}const f=new r.CipherSuite({kem:new r.DhkemP521HkdfSha512,kdf:new r.HkdfSha512,aead:new r.Aes256Gcm});const x=(t,e,n)=>{const r=(o=n,Object.fromEntries(Object.entries(o).filter((([t,e])=>void 0!==e))));var o;const i=new URLSearchParams(r).toString();return`${t}${e=i?`${e}?${i}`:e}`},d="unsupported ipfs URL pattern",g=t=>{if("string"!=typeof t)throw new Error("url is not string");const e=t.split(/\/|\?/);for(const t of e){if(o.cid(t))return{containsCid:!0,cid:t};const e=t.split(".")[0];if(o.cid(e))return{containsCid:!0,cid:e}}return{containsCid:!1,cid:null}},w=(t,e)=>new Array(e+1).join(t),m=(t,e,n=" ")=>w(n,e-t.toString().length)+t,b=t=>`${m(t.getHours(),2,"0")}:${m(t.getMinutes(),2,"0")}:${m(t.getSeconds(),2,"0")}.${m(t.getMilliseconds(),3,"0")}`,T=()=>b(new Date);function v(e,r){const o=t("string"==typeof e?e.replace(/,/gi,""):e),i=t(10).pow(r);return new n(o.times(i).toFixed(0))}function B(t){return v(t,9)}const y=new t("1e+18");const $=new t("1e+9");const S=t();S.PE=99,S.NE=-18;class E{value;symbol;maxDecimals;constructor(t,e,n){this.value=E.toBig(t).div(S(10).pow(e)),this.maxDecimals=e,this.symbol=n}getSymbol(){return this.symbol}getMaxDecimals(){return this.maxDecimals}add(t){return this.cloneWithValue(this.value.add(E.toBig(t)))}sub(t){return this.cloneWithValue(this.value.sub(E.toBig(t)))}mul(t){return this.cloneWithValue(this.value.mul(E.toBig(t)))}div(t){return this.cloneWithValue(this.value.div(E.toBig(t)))}gt(t){return this.value.gt(E.toBig(t))}lt(t){return this.value.lt(E.toBig(t))}eq(t){return this.value.eq(E.toBig(t))}toDisplay(e){const n=this.value.toFixed(0,t.roundDown).length,r=n>6?this.maxDecimals-n+1:this.maxDecimals;return this.value.round(r,t.roundHalfUp).toFixed(e,t.roundHalfUp)}isZero(){return this.value.eq(0)}toSubUnit(t){const e=t?this.value.round(this.maxDecimals):this.value;return BigInt(e.mul(new S(10).pow(this.maxDecimals)).toFixed(0))}static toBig(e){switch(typeof e){case"bigint":return S(BigInt(e).toString());case"string":case"number":return S(e);case"object":if(n.isBN(e))return S(e.toString());if(e instanceof t)return e}return e.value}cloneWithValue(t){return new E(t.mul(S(10).pow(this.maxDecimals)),this.maxDecimals,this.symbol)}}exports.BIG_TEN=a,exports.BIG_ZERO=c,exports.HttpClient=class{constructor(t,e={}){this.baseURL=t;const{headers:n,...r}=e;this.headerOptions=n,this.nonHeaderOptions=r}TIMEOUT=1e4;headerOptions;nonHeaderOptions;get(t,e={},n={}){const r=x(this.baseURL,t,e),{headers:o,...i}=n,s={method:"GET",headers:{"Content-Type":"application/json",...this.headerOptions,...o},...this.nonHeaderOptions,...i};return this.handleResponse(this.fetchWithTimeout(r,s))}post(t,e,n={},r={}){const o=x(this.baseURL,t,r),{headers:i,...s}=n,a={method:"POST",headers:{"Content-Type":"application/json",...this.headerOptions,...i},...this.nonHeaderOptions,...s,body:"string"==typeof e?e:JSON.stringify(e)};return this.handleResponse(this.fetchWithTimeout(o,a))}async handleResponse(t){const e=await t;if(e.ok)return await e.json();{const t=await e.text();return Promise.reject(new Error(t))}}async fetchWithTimeout(t,e={}){const{timeout:n=this.TIMEOUT}=e,r=new AbortController,o=setTimeout((()=>r.abort()),n),i=await fetch(t,{...e,signal:r.signal});return clearTimeout(o),i}},exports.TokenUnit=E,exports.UNSUPPORTED_URL_ERROR=d,exports.avaxCtoX=function(t){const e=new n(10).pow(new n(9));return t.div(e)},exports.avaxPtoC=function(t){return u(t)},exports.avaxXtoC=u,exports.balanceToDisplayValue=function(t,e){const n=i(t,e);return n.gte(1e3)?s(n,2):n.gte(1e-4)?s(n,4):s(n,e-t.toString().length+1)},exports.bech32ToHex=function(t){const[n,r,o]=e.utils.parse(t);return e.utils.strip0x(e.utils.bufferToHex(o))},exports.bigIntToString=function(t,e){e=Math.floor(e);const n=t.toString(),r=Math.max(n.length-e,0),o=n.slice(r).padStart(e,"0"),i=n.slice(0,r),s=BigInt(i).toLocaleString();return o.length?`${s}.${o}`:s},exports.bigToBN=function(e,r){const o=Math.floor(r);if(o<0)throw new Error("Denomination can not be less that 0.");const i=e.mul(t(10).pow(o)).toFixed(0,0);return new n(i)},exports.bigToBigInt=function(e,n){const r=Math.floor(n);if(r<0)throw new Error("Denomination can not be less that 0.");const o=e.mul(t(10).pow(r)).toFixed(0,0);return BigInt(o)},exports.bigToLocaleString=s,exports.bigintToBig=function(t,e){return i(new n.BN(t.toString()),e)},exports.bnToAvaxC=function(t){return l(t,18)},exports.bnToAvaxP=function(t){return p(t)},exports.bnToAvaxX=p,exports.bnToBig=i,exports.bnToBigAvaxC=function(t){return i(t,18)},exports.bnToBigAvaxP=function(t){return h(t)},exports.bnToBigAvaxX=h,exports.bnToLocaleString=l,exports.encryptAnalyticsData=async function(t,e,n){if(!t||!e)throw new Error("Encryption setting missing");const r=await f.kem.deserializePublicKey(Buffer.from(e,"base64")),o=await f.createSenderContext({recipientPublicKey:r}),i=(new TextEncoder).encode(t),s=(new TextEncoder).encode(n),a=await o.seal(s,i);return{data:Buffer.from(a).toString("base64"),enc:Buffer.from(o.enc).toString("base64"),keyID:t}},exports.formatAndLog=function(t,e,n){console.groupCollapsed("%c%s %s",((t,e=!0)=>`color:${t};font-weight:${e?"600":"300"};font-size:11px`)(n?.color??"#cccccc"),T(),t),console.log(e),console.groupEnd()},exports.formatTime=b,exports.hexToBN=function(t){return t.match("0x")?new n.BN(BigInt(t)):new n.BN(t,"hex")},exports.hexToBytes=function(t){return t?(t.length-2)/2:null},exports.ipfsResolver=(t,e)=>{const n=g(t);if(!n.cid||!0!==n.containsCid)throw new Error("url does not contain CID");const r=t.split(n.cid);if(t.includes(`ipfs://${n.cid}`))return`${e}/ipfs/${n.cid}${r[1]}`;if(t.includes(`/ipfs/${n.cid}`))return`${e}/ipfs/${n.cid}${r[1]}`;if(t.includes(`/ipns/${n.cid}`))return`${e}/ipns/${n.cid}${r[1]}`;if(t.includes(`${n.cid}.ipfs`)){const r=new URL(t).pathname;return`${e}/ipfs/${n.cid}${r}`}throw new Error(d)},exports.noop=()=>{},exports.now=T,exports.numberToBN=v,exports.numberToBNAvaxC=function(t){return v(t,18)},exports.numberToBNAvaxP=function(t){return B(t)},exports.numberToBNAvaxX=B,exports.padStart=m,exports.promiseResolveWithBackoff=async function t(e,n,r=0,o=10){try{const i=await function(t,e=0){return new Promise(((n,r)=>{setTimeout((()=>{t().then((t=>n(t))).catch((t=>r(t)))}),500*e)}))}(e,r);return o===r+1?i:n(i)?t(e,n,r+1,o):i}catch(i){if(o===r+1)throw"string"==typeof i?new Error(i):i;if(n(i))return t(e,n,r+1,o);throw"string"==typeof i?new Error(i):i}},exports.repeat=w,exports.resolve=async function(t){try{return[await t,null]}catch(t){return[null,t]}},exports.stringToBN=function(e,r){const o=t(e.replace(/,/gi,"")),i=t(10).pow(r),s=o.times(i).toFixed(0,0);return new n(s)},exports.strip0x=function(t){return"0x"===t.substring(0,2)?t.substring(2):t},exports.truncateAddress=(t,e=6)=>`${t.substring(0,e)}…${t.slice(-e/2)}`,exports.urlContainsCID=g,exports.wait=function(t){return new Promise((e=>setTimeout(e,t)))},exports.weiToAvax=function(t){return t.div(y)},exports.weiToNAvax=function(t){return t.div($)};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import BN__default from 'bn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Given a balance, returns a string for display
|
|
5
|
+
* @param val The balance in BN format.
|
|
6
|
+
* @param decimals Number of decimals token has.
|
|
7
|
+
*/
|
|
8
|
+
declare function balanceToDisplayValue(val: BN__default, decimals: number): string;
|
|
9
|
+
|
|
10
|
+
export { balanceToDisplayValue };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{bnToBig as t}from"./bnToBig.js";import{bigToLocaleString as r}from"./bigToLocaleString.js";function o(o,n){const e=t(o,n);if(e.gte(1e3))return r(e,2);if(e.gte(1e-4))return r(e,4);const i=o.toString().length;return r(e,n-i+1)}export{o as balanceToDisplayValue};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{utils as r}from"@avalabs/avalanchejs";function a(a){const[e,o,t]=r.parse(a);return r.strip0x(r.bufferToHex(t))}export{a as bech32ToHex};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import o from"big.js";const e=new o(10),n=new o(0);export{e as BIG_TEN,n as BIG_ZERO};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function t(t,n){n=Math.floor(n);const o=t.toString(),e=Math.max(o.length-n,0),r=o.slice(e).padStart(n,"0"),a=o.slice(0,e),i=BigInt(a).toLocaleString();return r.length?`${i}.${r}`:i}export{t as bigIntToString};
|
package/esm/bigToBN.d.ts
ADDED
package/esm/bigToBN.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import o from"big.js";import t from"bn.js";function n(n,r){const e=Math.floor(r);if(e<0)throw new Error("Denomination can not be less that 0.");const i=n.mul(o(10).pow(e)).toFixed(0,0);return new t(i)}export{n as bigToBN};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import o from"big.js";function t(t,n){const r=Math.floor(n);if(r<0)throw new Error("Denomination can not be less that 0.");const i=t.mul(o(10).pow(r)).toFixed(0,0);return BigInt(i)}export{t as bigToBigInt};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function t(t,n=9){const e=t.toFixed(n).split("."),r=parseInt(e[0]).toLocaleString("en-US");if(1===e.length)return r;{let t=e[1],o=t.charAt(t.length-1);for(;"0"===o;)t=t.substring(0,t.length-1),o=t.charAt(t.length-1);const s=t.substring(0,n);return s?`${r}.${s}`:r}}export{t as bigToLocaleString};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{bnToBig as o}from"./bnToBig.js";import{BN as r}from"bn.js";function n(n,t){return o(new r(n.toString()),t)}export{n as bigintToBig};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import BN__default from 'bn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a BN amount of 18 decimals to 9.
|
|
5
|
+
* Used for AVAX C <-> X,P conversions
|
|
6
|
+
* @param amount
|
|
7
|
+
*/
|
|
8
|
+
declare function avaxCtoX(amount: BN__default): BN__default;
|
|
9
|
+
declare function avaxXtoC(amount: BN__default): BN__default;
|
|
10
|
+
declare function avaxPtoC(amount: BN__default): BN__default;
|
|
11
|
+
|
|
12
|
+
export { avaxCtoX, avaxPtoC, avaxXtoC };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import n from"bn.js";function o(o){const t=new n(10).pow(new n(9));return o.div(t)}function t(o){const t=new n(10).pow(new n(9));return o.mul(t)}function r(n){return t(n)}export{o as avaxCtoX,r as avaxPtoC,t as avaxXtoC};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import BN__default from 'bn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parses the value using a denomination of 18
|
|
5
|
+
*
|
|
6
|
+
* @param val the amount to parse given in WEI
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```
|
|
10
|
+
* bnToAvaxC(new BN('22500000000000000000')
|
|
11
|
+
* // will return 22.5
|
|
12
|
+
*```
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
declare function bnToAvaxC(val: BN__default): string;
|
|
16
|
+
/**
|
|
17
|
+
* Parses the value using a denomination of 9
|
|
18
|
+
*
|
|
19
|
+
* @param val the amount to parse given in nAVAX
|
|
20
|
+
*/
|
|
21
|
+
declare function bnToAvaxX(val: BN__default): string;
|
|
22
|
+
/**
|
|
23
|
+
* Parses the value using a denomination of 9
|
|
24
|
+
*
|
|
25
|
+
* @param val the amount to parse given in nAVAX
|
|
26
|
+
*/
|
|
27
|
+
declare function bnToAvaxP(val: BN__default): string;
|
|
28
|
+
|
|
29
|
+
export { bnToAvaxC, bnToAvaxP, bnToAvaxX };
|
package/esm/bnToAvax.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{bnToLocaleString as n}from"./bnToLocaleString.js";function r(r){return n(r,18)}function t(r){return n(r,9)}function o(n){return t(n)}export{r as bnToAvaxC,o as bnToAvaxP,t as bnToAvaxX};
|
package/esm/bnToBig.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import BN__default from 'bn.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param val the amount to parse
|
|
6
|
+
* @param denomination number of decimal places to parse with
|
|
7
|
+
*/
|
|
8
|
+
declare function bnToBig(val: BN__default, denomination?: number): Big;
|
|
9
|
+
|
|
10
|
+
export { bnToBig };
|
package/esm/bnToBig.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import o from"big.js";function t(t,n=0){const r=o(10).pow(n);return new o(t.toString()).div(r)}export{t as bnToBig};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import Big from 'big.js';
|
|
2
|
+
import BN__default from 'bn.js';
|
|
3
|
+
|
|
4
|
+
declare function bnToBigAvaxX(val: BN__default): Big;
|
|
5
|
+
declare function bnToBigAvaxP(val: BN__default): Big;
|
|
6
|
+
declare function bnToBigAvaxC(val: BN__default): Big;
|
|
7
|
+
|
|
8
|
+
export { bnToBigAvaxC, bnToBigAvaxP, bnToBigAvaxX };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{bnToBig as n}from"./bnToBig.js";function r(r){return n(r,9)}function t(n){return r(n)}function o(r){return n(r,18)}export{o as bnToBigAvaxC,t as bnToBigAvaxP,r as bnToBigAvaxX};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import BN__default from 'bn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @Remarks
|
|
5
|
+
* A helper method to convert BN numbers to human readable strings.
|
|
6
|
+
*
|
|
7
|
+
* @param val The amount to convert
|
|
8
|
+
* @param decimals Number of decimal places to parse the amount with
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```
|
|
12
|
+
* bnToLocaleString(new BN(100095),2)
|
|
13
|
+
* // will return '1,000.95'
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
declare function bnToLocaleString(val: BN__default, decimals?: number): string;
|
|
17
|
+
|
|
18
|
+
export { bnToLocaleString };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{bigToLocaleString as o}from"./bigToLocaleString.js";import{bnToBig as r}from"./bnToBig.js";function t(t,i=9){const n=r(t,i);return o(n,i)}export{t as bnToLocaleString};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{CipherSuite as e,DhkemP521HkdfSha512 as n,HkdfSha512 as r,Aes256Gcm as t}from"@hpke/core";const o=new e({kem:new n,kdf:new r,aead:new t});async function i(e,n,r){if(!e||!n)throw new Error("Encryption setting missing");const t=await o.kem.deserializePublicKey(Buffer.from(n,"base64")),i=await o.createSenderContext({recipientPublicKey:t}),a=(new TextEncoder).encode(e),c=(new TextEncoder).encode(r),f=await i.seal(c,a);return{data:Buffer.from(f).toString("base64"),enc:Buffer.from(i.enc).toString("base64"),keyID:e}}export{i as encryptAnalyticsData};
|
package/esm/hexToBN.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as BN from 'bn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* BN has a bug when a converting hex to BN, if the hex starts with
|
|
5
|
+
* 0x BN doesnt know how to handle it and returns an incorrect value
|
|
6
|
+
*
|
|
7
|
+
* On the flip, BigInt cant parse hex without the 0x so we will use
|
|
8
|
+
* this helper function to deal with this
|
|
9
|
+
* @param hex the hex value
|
|
10
|
+
* @returns BN
|
|
11
|
+
*/
|
|
12
|
+
declare function hexToBN(hex: string): BN;
|
|
13
|
+
|
|
14
|
+
export { hexToBN };
|
package/esm/hexToBN.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{BN as n}from"bn.js";function t(t){return t.match("0x")?new n(BigInt(t)):new n(t,"hex")}export{t as hexToBN};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function n(n){return n?(n.length-2)/2:null}export{n as hexToBytes};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type HttpOptions = RequestInit & {
|
|
2
|
+
timeout?: number;
|
|
3
|
+
};
|
|
4
|
+
declare class HttpClient {
|
|
5
|
+
private readonly baseURL;
|
|
6
|
+
private TIMEOUT;
|
|
7
|
+
private headerOptions;
|
|
8
|
+
private nonHeaderOptions;
|
|
9
|
+
constructor(baseURL: string, options?: HttpOptions);
|
|
10
|
+
get<T>(path: string, params?: Record<string, any>, customOptions?: HttpOptions): Promise<T>;
|
|
11
|
+
post<T>(path: string, data: Record<string, any>, customOptions?: HttpOptions, params?: Record<string, any>): Promise<T>;
|
|
12
|
+
private handleResponse;
|
|
13
|
+
private fetchWithTimeout;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { HttpClient, HttpOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class t{constructor(t,e={}){this.baseURL=t;const{headers:s,...n}=e;this.headerOptions=s,this.nonHeaderOptions=n}TIMEOUT=1e4;headerOptions;nonHeaderOptions;get(t,s={},n={}){const o=e(this.baseURL,t,s),{headers:i,...r}=n,a={method:"GET",headers:{"Content-Type":"application/json",...this.headerOptions,...i},...this.nonHeaderOptions,...r};return this.handleResponse(this.fetchWithTimeout(o,a))}post(t,s,n={},o={}){const i=e(this.baseURL,t,o),{headers:r,...a}=n,h={method:"POST",headers:{"Content-Type":"application/json",...this.headerOptions,...r},...this.nonHeaderOptions,...a,body:"string"==typeof s?s:JSON.stringify(s)};return this.handleResponse(this.fetchWithTimeout(i,h))}async handleResponse(t){const e=await t;if(e.ok)return await e.json();{const t=await e.text();return Promise.reject(new Error(t))}}async fetchWithTimeout(t,e={}){const{timeout:s=this.TIMEOUT}=e,n=new AbortController,o=setTimeout((()=>n.abort()),s),i=await fetch(t,{...e,signal:n.signal});return clearTimeout(o),i}}const e=(t,e,s)=>{const n=(o=s,Object.fromEntries(Object.entries(o).filter((([t,e])=>void 0!==e))));var o;const i=new URLSearchParams(n).toString();return`${t}${e=i?`${e}?${i}`:e}`};export{t as HttpClient};
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export { balanceToDisplayValue } from './balanceToDisplayValue.js';
|
|
2
|
+
export { bech32ToHex } from './bech32ToHex.js';
|
|
3
|
+
export { BIG_TEN, BIG_ZERO } from './bigConstants.js';
|
|
4
|
+
export { bigIntToString } from './bigIntToString.js';
|
|
5
|
+
export { bigintToBig } from './bigintToBig.js';
|
|
6
|
+
export { bigToBN } from './bigToBN.js';
|
|
7
|
+
export { bigToBigInt } from './bigToBigInt.js';
|
|
8
|
+
export { bigToLocaleString } from './bigToLocaleString.js';
|
|
9
|
+
export { avaxCtoX, avaxPtoC, avaxXtoC } from './bnAvaxCrossChain.js';
|
|
10
|
+
export { bnToAvaxC, bnToAvaxP, bnToAvaxX } from './bnToAvax.js';
|
|
11
|
+
export { bnToBig } from './bnToBig.js';
|
|
12
|
+
export { bnToBigAvaxC, bnToBigAvaxP, bnToBigAvaxX } from './bnToBigAvax.js';
|
|
13
|
+
export { bnToLocaleString } from './bnToLocaleString.js';
|
|
14
|
+
export { encryptAnalyticsData } from './encryptAnalyticsData.js';
|
|
15
|
+
export { hexToBN } from './hexToBN.js';
|
|
16
|
+
export { hexToBytes } from './hexToBytes.js';
|
|
17
|
+
export { HttpClient, HttpOptions } from './httpClient.js';
|
|
18
|
+
export { UNSUPPORTED_URL_ERROR, ipfsResolver, urlContainsCID } from './ipfsResolver.js';
|
|
19
|
+
export { formatAndLog, formatTime, now, padStart, repeat } from './logFormatter.js';
|
|
20
|
+
export { noop } from './noop.js';
|
|
21
|
+
export { numberToBN } from './numberToBn.js';
|
|
22
|
+
export { numberToBNAvaxC, numberToBNAvaxP, numberToBNAvaxX } from './numberToBnAvax.js';
|
|
23
|
+
export { resolve } from './promiseResolver.js';
|
|
24
|
+
export { promiseResolveWithBackoff } from './promiseResolveWithBackoff.js';
|
|
25
|
+
export { stringToBN } from './stringToBN.js';
|
|
26
|
+
export { truncateAddress } from './trunateAddress.js';
|
|
27
|
+
export { wait } from './wait.js';
|
|
28
|
+
export { weiToAvax } from './weiToAvax.js';
|
|
29
|
+
export { weiToNAvax } from './weiToNAvax.js';
|
|
30
|
+
export { strip0x } from './strip0x.js';
|
|
31
|
+
export { Maybe } from './types/index.js';
|
|
32
|
+
export { AcceptedTypes, TokenUnit } from './tokenUnit.js';
|
package/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{balanceToDisplayValue}from"./balanceToDisplayValue.js";export{bech32ToHex}from"./bech32ToHex.js";export{BIG_TEN,BIG_ZERO}from"./bigConstants.js";export{bigIntToString}from"./bigIntToString.js";export{bigintToBig}from"./bigintToBig.js";export{bigToBN}from"./bigToBN.js";export{bigToBigInt}from"./bigToBigInt.js";export{bigToLocaleString}from"./bigToLocaleString.js";export{avaxCtoX,avaxPtoC,avaxXtoC}from"./bnAvaxCrossChain.js";export{bnToAvaxC,bnToAvaxP,bnToAvaxX}from"./bnToAvax.js";export{bnToBig}from"./bnToBig.js";export{bnToBigAvaxC,bnToBigAvaxP,bnToBigAvaxX}from"./bnToBigAvax.js";export{bnToLocaleString}from"./bnToLocaleString.js";export{encryptAnalyticsData}from"./encryptAnalyticsData.js";export{hexToBN}from"./hexToBN.js";export{hexToBytes}from"./hexToBytes.js";export{HttpClient}from"./httpClient.js";export{UNSUPPORTED_URL_ERROR,ipfsResolver,urlContainsCID}from"./ipfsResolver.js";export{formatAndLog,formatTime,now,padStart,repeat}from"./logFormatter.js";export{noop}from"./noop.js";export{numberToBN}from"./numberToBn.js";export{numberToBNAvaxC,numberToBNAvaxP,numberToBNAvaxX}from"./numberToBnAvax.js";export{resolve}from"./promiseResolver.js";export{promiseResolveWithBackoff}from"./promiseResolveWithBackoff.js";export{stringToBN}from"./stringToBN.js";export{truncateAddress}from"./trunateAddress.js";export{wait}from"./wait.js";export{weiToAvax}from"./weiToAvax.js";export{weiToNAvax}from"./weiToNAvax.js";export{strip0x}from"./strip0x.js";export{TokenUnit}from"./tokenUnit.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
declare const UNSUPPORTED_URL_ERROR = "unsupported ipfs URL pattern";
|
|
2
|
+
declare const urlContainsCID: (url: string) => {
|
|
3
|
+
containsCid: boolean;
|
|
4
|
+
cid: string;
|
|
5
|
+
} | {
|
|
6
|
+
containsCid: boolean;
|
|
7
|
+
cid: null;
|
|
8
|
+
};
|
|
9
|
+
declare const ipfsResolver: (sourceUrl: string, desiredGatewayPrefix: string) => string;
|
|
10
|
+
|
|
11
|
+
export { UNSUPPORTED_URL_ERROR, ipfsResolver, urlContainsCID };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{cid as i}from"is-ipfs";const n="unsupported ipfs URL pattern",r=n=>{if("string"!=typeof n)throw new Error("url is not string");const r=n.split(/\/|\?/);for(const n of r){if(i(n))return{containsCid:!0,cid:n};const r=n.split(".")[0];if(i(r))return{containsCid:!0,cid:r}}return{containsCid:!1,cid:null}},t=(i,t)=>{const s=r(i);if(!s.cid||!0!==s.containsCid)throw new Error("url does not contain CID");const c=i.split(s.cid);if(i.includes(`ipfs://${s.cid}`))return`${t}/ipfs/${s.cid}${c[1]}`;if(i.includes(`/ipfs/${s.cid}`))return`${t}/ipfs/${s.cid}${c[1]}`;if(i.includes(`/ipns/${s.cid}`))return`${t}/ipns/${s.cid}${c[1]}`;if(i.includes(`${s.cid}.ipfs`)){const n=new URL(i).pathname;return`${t}/ipfs/${s.cid}${n}`}throw new Error(n)};export{n as UNSUPPORTED_URL_ERROR,t as ipfsResolver,r as urlContainsCID};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
declare const repeat: (str: string, times: number) => string;
|
|
2
|
+
declare const padStart: (num: number, maxLength: number, char?: string) => string;
|
|
3
|
+
declare const formatTime: (time: Date) => string;
|
|
4
|
+
declare const now: () => string;
|
|
5
|
+
declare function formatAndLog(message: string, value: any, config?: {
|
|
6
|
+
color?: string;
|
|
7
|
+
}): void;
|
|
8
|
+
|
|
9
|
+
export { formatAndLog, formatTime, now, padStart, repeat };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const o=(o,e)=>new Array(e+1).join(o),e=(e,n,c=" ")=>o(c,n-e.toString().length)+e,n=o=>`${e(o.getHours(),2,"0")}:${e(o.getMinutes(),2,"0")}:${e(o.getSeconds(),2,"0")}.${e(o.getMilliseconds(),3,"0")}`,c=()=>n(new Date);function t(o,e,n){console.groupCollapsed("%c%s %s",((o,e=!0)=>`color:${o};font-weight:${e?"600":"300"};font-size:11px`)(n?.color??"#cccccc"),c(),o),console.log(e),console.groupEnd()}export{t as formatAndLog,n as formatTime,c as now,e as padStart,o as repeat};
|
package/esm/noop.d.ts
ADDED
package/esm/noop.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const o=()=>{};export{o as noop};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import BN__default from 'bn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param val the number to parse
|
|
6
|
+
* @param decimals number of decimal places used to parse the number
|
|
7
|
+
*/
|
|
8
|
+
declare function numberToBN(val: number | string, decimals: number): BN__default;
|
|
9
|
+
|
|
10
|
+
export { numberToBN };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import o from"big.js";import t from"bn.js";function r(r,e){const i=o("string"==typeof r?r.replace(/,/gi,""):r),n=o(10).pow(e);return new t(i.times(n).toFixed(0))}export{r as numberToBN};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as BN from 'bn.js';
|
|
2
|
+
|
|
3
|
+
declare function numberToBNAvaxX(val: number | string): BN;
|
|
4
|
+
declare function numberToBNAvaxP(val: number | string): BN;
|
|
5
|
+
declare function numberToBNAvaxC(val: number | string): BN;
|
|
6
|
+
|
|
7
|
+
export { numberToBNAvaxC, numberToBNAvaxP, numberToBNAvaxX };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{numberToBN as n}from"./numberToBn.js";function r(r){return n(r,9)}function t(n){return r(n)}function o(r){return n(r,18)}export{o as numberToBNAvaxC,t as numberToBNAvaxP,r as numberToBNAvaxX};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves promise with linear backoff policy for max of {@link maxAttempts} attempts
|
|
3
|
+
*
|
|
4
|
+
* @param prom Promise to resolve
|
|
5
|
+
* @param errorParser
|
|
6
|
+
* @param attempt Current attempt
|
|
7
|
+
* @param maxAttempts Max attempts
|
|
8
|
+
* @returns promise result
|
|
9
|
+
*/
|
|
10
|
+
declare function promiseResolveWithBackoff<T>(prom: () => Promise<T>, errorParser: (res: any) => boolean, attempt?: number, maxAttempts?: number): Promise<T>;
|
|
11
|
+
|
|
12
|
+
export { promiseResolveWithBackoff };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
async function t(r,n,e=0,o=10){try{const i=await function(t,r=0){return new Promise(((n,e)=>{setTimeout((()=>{t().then((t=>n(t))).catch((t=>e(t)))}),500*r)}))}(r,e);return o===e+1?i:n(i)?t(r,n,e+1,o):i}catch(i){if(o===e+1)throw"string"==typeof i?new Error(i):i;if(n(i))return t(r,n,e+1,o);throw"string"==typeof i?new Error(i):i}}export{t as promiseResolveWithBackoff};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Simplify catching promise errors and returns a tuple of [result, error]
|
|
3
|
+
* @example
|
|
4
|
+
* const [result, error] = await resolve(promise)
|
|
5
|
+
* if (result) return result
|
|
6
|
+
* return handleError(error)
|
|
7
|
+
*/
|
|
8
|
+
declare function resolve<T>(promise: Promise<T>): Promise<[T, null] | [null, unknown]>;
|
|
9
|
+
|
|
10
|
+
export { resolve };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
async function n(n){try{return[await n,null]}catch(n){return[null,n]}}export{n as resolve};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import BN__default from 'bn.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Converts a string to a BN value of the given denomination.
|
|
5
|
+
* @param value The string value of the
|
|
6
|
+
* @param decimals
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```
|
|
10
|
+
* stringToBN('1.32', 5) // is same as BN(132000)
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
declare function stringToBN(value: string, decimals: number): BN__default;
|
|
14
|
+
|
|
15
|
+
export { stringToBN };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import o from"big.js";import r from"bn.js";function t(t,e){const i=o(t.replace(/,/gi,"")),n=o(10).pow(e),m=i.times(n).toFixed(0,0);return new r(m)}export{t as stringToBN};
|
package/esm/strip0x.d.ts
ADDED
package/esm/strip0x.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function n(n){return"0x"===n.substring(0,2)?n.substring(2):n}export{n as strip0x};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import BN__default from 'bn.js';
|
|
2
|
+
import Big, { BigSource } from 'big.js';
|
|
3
|
+
|
|
4
|
+
type AcceptedTypes = BigSource | BN__default | bigint;
|
|
5
|
+
/**
|
|
6
|
+
* TokenUnit abstracts units in which crypto tokens are represented.
|
|
7
|
+
* It holds value of token in Big type in normal numeric representation (as opposed to exponential or any other).
|
|
8
|
+
* It also holds maxDecimals value which denotes smallest possible denomination for that token.
|
|
9
|
+
*
|
|
10
|
+
* For example, minimal denomination of Eth is Wei where 1 Eth = 1e18 Wei , which means maxDecimals is 18.
|
|
11
|
+
* Another example is Avax and nAvax where 1 Avax = 1e9 nAvax, therefore maxDecimals is 9.
|
|
12
|
+
*/
|
|
13
|
+
declare class TokenUnit {
|
|
14
|
+
/**
|
|
15
|
+
* In base unit
|
|
16
|
+
* @protected
|
|
17
|
+
*/
|
|
18
|
+
protected readonly value: Big;
|
|
19
|
+
protected readonly symbol: string;
|
|
20
|
+
protected readonly maxDecimals: number;
|
|
21
|
+
constructor(value: AcceptedTypes, maxDecimals: number, symbol: string);
|
|
22
|
+
getSymbol(): string;
|
|
23
|
+
getMaxDecimals(): number;
|
|
24
|
+
add(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
25
|
+
sub(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
26
|
+
mul(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
27
|
+
div(value: TokenUnit | AcceptedTypes): TokenUnit;
|
|
28
|
+
gt(value: TokenUnit | AcceptedTypes): boolean;
|
|
29
|
+
lt(value: TokenUnit | AcceptedTypes): boolean;
|
|
30
|
+
eq(value: TokenUnit | AcceptedTypes): boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Tries to display token unit in most meaningful way.
|
|
33
|
+
* Precision of displaying token units makes sense only to up to
|
|
34
|
+
* *maxDecimals* decimal points, just as you would display US dollars up to 2 decimals.
|
|
35
|
+
* However, there's no point in showing all *maxDecimals* decimals if total
|
|
36
|
+
* value is huge, e.g. instead of displaying 1,000,000.000,000,001 we want to
|
|
37
|
+
* display 1,000,000. So in that effort this function will display maximum of
|
|
38
|
+
* **[maxDecimals - wholeDigits]** decimals, where *wholeDigits* is the number of digits
|
|
39
|
+
* of only whole portion of value.
|
|
40
|
+
*
|
|
41
|
+
* @param fixedDp See {@link Big.toFixed}
|
|
42
|
+
*/
|
|
43
|
+
toDisplay(fixedDp?: number): string;
|
|
44
|
+
isZero(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Converts this base unit to the smallest unit defined by this.maxDecimals
|
|
47
|
+
* @param round
|
|
48
|
+
*/
|
|
49
|
+
toSubUnit(round?: boolean): bigint;
|
|
50
|
+
private static toBig;
|
|
51
|
+
private cloneWithValue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { AcceptedTypes, TokenUnit };
|
package/esm/tokenUnit.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import t from"bn.js";import i from"big.js";const e=i();e.PE=99,e.NE=-18;class s{value;symbol;maxDecimals;constructor(t,i,r){this.value=s.toBig(t).div(e(10).pow(i)),this.maxDecimals=i,this.symbol=r}getSymbol(){return this.symbol}getMaxDecimals(){return this.maxDecimals}add(t){return this.cloneWithValue(this.value.add(s.toBig(t)))}sub(t){return this.cloneWithValue(this.value.sub(s.toBig(t)))}mul(t){return this.cloneWithValue(this.value.mul(s.toBig(t)))}div(t){return this.cloneWithValue(this.value.div(s.toBig(t)))}gt(t){return this.value.gt(s.toBig(t))}lt(t){return this.value.lt(s.toBig(t))}eq(t){return this.value.eq(s.toBig(t))}toDisplay(t){const e=this.value.toFixed(0,i.roundDown).length,s=e>6?this.maxDecimals-e+1:this.maxDecimals;return this.value.round(s,i.roundHalfUp).toFixed(t,i.roundHalfUp)}isZero(){return this.value.eq(0)}toSubUnit(t){const i=t?this.value.round(this.maxDecimals):this.value;return BigInt(i.mul(new e(10).pow(this.maxDecimals)).toFixed(0))}static toBig(s){switch(typeof s){case"bigint":return e(BigInt(s).toString());case"string":case"number":return e(s);case"object":if(t.isBN(s))return e(s.toString());if(s instanceof i)return s}return s.value}cloneWithValue(t){return new s(t.mul(e(10).pow(this.maxDecimals)),this.maxDecimals,this.symbol)}}export{s as TokenUnit};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const s=(s,t=6)=>`${s.substring(0,t)}…${s.slice(-t/2)}`;export{s as truncateAddress};
|
package/esm/wait.d.ts
ADDED
package/esm/wait.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return new Promise((t=>setTimeout(t,e)))}export{e as wait};
|
package/esm/weiToAvax.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import n from"big.js";const o=new n("1e+18");function r(n){return n.div(o)}export{r as weiToAvax};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import n from"big.js";const o=new n("1e+9");function r(n){return n.div(o)}export{r as weiToNAvax};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@avalabs/core-utils-sdk",
|
|
3
|
+
"version": "2.8.0-alpha.197",
|
|
4
|
+
"license": "Limited Ecosystem License",
|
|
5
|
+
"private": false,
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "restricted"
|
|
8
|
+
},
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "esm/index.js",
|
|
11
|
+
"typings": "dist/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"esm"
|
|
15
|
+
],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"scripts": {
|
|
18
|
+
"start": "rollup -c --watch",
|
|
19
|
+
"build": "rollup -c",
|
|
20
|
+
"lint": "eslint --fix -c ./.eslintrc.cjs \"src/**/*.ts*\"",
|
|
21
|
+
"test": "jest",
|
|
22
|
+
"test:watch": "jest --watch"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@babel/preset-env": "7.22.20",
|
|
26
|
+
"@types/jest": "29.5.12",
|
|
27
|
+
"big.js": "6.2.1",
|
|
28
|
+
"bn.js": "5.2.1",
|
|
29
|
+
"ethers": "6.7.1",
|
|
30
|
+
"jest": "29.3.1"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@avalabs/avalanchejs": "4.0.5",
|
|
34
|
+
"@hpke/core": "1.2.5",
|
|
35
|
+
"is-ipfs": "6.0.2"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"big.js": "^6.2.1",
|
|
39
|
+
"bn.js": "^5.2.1",
|
|
40
|
+
"ethers": "^6.7.1"
|
|
41
|
+
}
|
|
42
|
+
}
|