@alephium/web3 0.5.1 → 0.5.3

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.
@@ -9,7 +9,7 @@ export declare class ExplorerProvider {
9
9
  readonly tokens: ExplorerApi<string>['tokens'];
10
10
  readonly charts: ExplorerApi<string>['charts'];
11
11
  readonly utils: ExplorerApi<string>['utils'];
12
- constructor(baseUrl: string, apiKey?: string);
12
+ constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch);
13
13
  constructor(provider: ExplorerProvider);
14
14
  constructor(handler: ApiRequestHandler);
15
15
  request: (args: ApiRequestArguments) => Promise<any>;
@@ -20,23 +20,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.ExplorerProvider = void 0;
21
21
  const types_1 = require("./types");
22
22
  const api_explorer_1 = require("./api-explorer");
23
- function initializeExplorerApi(baseUrl, apiKey) {
23
+ const utils_1 = require("./utils");
24
+ function initializeExplorerApi(baseUrl, apiKey, customFetch) {
24
25
  const explorerApi = new api_explorer_1.Api({
25
26
  baseUrl: baseUrl,
26
27
  baseApiParams: { secure: true },
27
- securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
28
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
29
+ customFetch: customFetch ?? utils_1.DEFAULT_THROTTLE_FETCH
28
30
  });
29
31
  explorerApi.setSecurityData(apiKey ?? null);
30
32
  return explorerApi;
31
33
  }
32
34
  class ExplorerProvider {
33
- constructor(param0, apiKey) {
35
+ constructor(param0, apiKey, customFetch) {
34
36
  this.request = (args) => {
35
37
  return (0, types_1.request)(this, args);
36
38
  };
37
39
  let explorerApi;
38
40
  if (typeof param0 === 'string') {
39
- explorerApi = initializeExplorerApi(param0, apiKey);
41
+ explorerApi = initializeExplorerApi(param0, apiKey, customFetch);
40
42
  }
41
43
  else if (typeof param0 === 'function') {
42
44
  explorerApi = new ExplorerProvider('https://1.2.3.4:0');
@@ -3,3 +3,4 @@ export * from './explorer-provider';
3
3
  export * as node from './api-alephium';
4
4
  export * as explorer from './api-explorer';
5
5
  export * from './types';
6
+ export * from './utils';
@@ -49,3 +49,4 @@ __exportStar(require("./explorer-provider"), exports);
49
49
  exports.node = __importStar(require("./api-alephium"));
50
50
  exports.explorer = __importStar(require("./api-explorer"));
51
51
  __exportStar(require("./types"), exports);
52
+ __exportStar(require("./utils"), exports);
@@ -12,7 +12,7 @@ export declare class NodeProvider {
12
12
  readonly utils: NodeApi<string>['utils'];
13
13
  readonly miners: NodeApi<string>['miners'];
14
14
  readonly events: NodeApi<string>['events'];
15
- constructor(baseUrl: string, apiKey?: string);
15
+ constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch);
16
16
  constructor(provider: NodeProvider);
17
17
  constructor(handler: ApiRequestHandler);
18
18
  request: (args: ApiRequestArguments) => Promise<any>;
@@ -20,23 +20,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.NodeProvider = void 0;
21
21
  const types_1 = require("./types");
22
22
  const api_alephium_1 = require("./api-alephium");
23
- function initializeNodeApi(baseUrl, apiKey) {
23
+ const utils_1 = require("./utils");
24
+ function initializeNodeApi(baseUrl, apiKey, customFetch) {
24
25
  const nodeApi = new api_alephium_1.Api({
25
26
  baseUrl: baseUrl,
26
27
  baseApiParams: { secure: true },
27
- securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
28
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
29
+ customFetch: customFetch ?? utils_1.DEFAULT_THROTTLE_FETCH
28
30
  });
29
31
  nodeApi.setSecurityData(apiKey ?? null);
30
32
  return nodeApi;
31
33
  }
32
34
  class NodeProvider {
33
- constructor(param0, apiKey) {
35
+ constructor(param0, apiKey, customFetch) {
34
36
  this.request = (args) => {
35
37
  return (0, types_1.request)(this, args);
36
38
  };
37
39
  let nodeApi;
38
40
  if (typeof param0 === 'string') {
39
- nodeApi = initializeNodeApi(param0, apiKey);
41
+ nodeApi = initializeNodeApi(param0, apiKey, customFetch);
40
42
  }
41
43
  else if (typeof param0 === 'function') {
42
44
  nodeApi = new NodeProvider('https://1.2.3.4:0');
@@ -1,6 +1,12 @@
1
+ import 'cross-fetch/polyfill';
1
2
  export declare function convertHttpResponse<T>(response: {
2
3
  data: T;
3
4
  error?: {
4
5
  detail: string;
5
6
  };
6
7
  }): T;
8
+ export declare function retryFetch(...fetchParams: Parameters<typeof fetch>): ReturnType<typeof fetch>;
9
+ export declare function throttledFetch(ratePerSec: number): typeof fetch;
10
+ export declare const RETRY_LIMIT_WHEN_429 = 3;
11
+ export declare const DEFAULT_RATE_LIMIT = 3;
12
+ export declare const DEFAULT_THROTTLE_FETCH: typeof fetch;
@@ -17,13 +17,43 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.convertHttpResponse = void 0;
20
+ exports.DEFAULT_THROTTLE_FETCH = exports.DEFAULT_RATE_LIMIT = exports.RETRY_LIMIT_WHEN_429 = exports.throttledFetch = exports.retryFetch = exports.convertHttpResponse = void 0;
21
+ require("cross-fetch/polyfill");
22
+ const utils_1 = require("../utils");
23
+ const async_sema_1 = require("async-sema");
21
24
  function convertHttpResponse(response) {
22
25
  if (response.error) {
23
- throw new Error(`[Node API Error] - ${response.error.detail}`);
26
+ throw new Error(`[API Error] - ${response.error.detail}`);
24
27
  }
25
28
  else {
26
29
  return response.data;
27
30
  }
28
31
  }
29
32
  exports.convertHttpResponse = convertHttpResponse;
33
+ async function retryFetch(...fetchParams) {
34
+ const retry = async (retryCount) => {
35
+ const response = await fetch(...fetchParams);
36
+ if (response.status === 429 && retryCount < exports.RETRY_LIMIT_WHEN_429) {
37
+ const nextCount = retryCount + 1;
38
+ await (0, utils_1.sleep)(nextCount * 500);
39
+ return await retry(nextCount);
40
+ }
41
+ else {
42
+ return response;
43
+ }
44
+ };
45
+ return retry(0);
46
+ }
47
+ exports.retryFetch = retryFetch;
48
+ function throttledFetch(ratePerSec) {
49
+ const limit = (0, async_sema_1.RateLimit)(ratePerSec);
50
+ const customFetch = async (...fetchParams) => {
51
+ await limit();
52
+ return retryFetch(...fetchParams);
53
+ };
54
+ return customFetch;
55
+ }
56
+ exports.throttledFetch = throttledFetch;
57
+ exports.RETRY_LIMIT_WHEN_429 = 3;
58
+ exports.DEFAULT_RATE_LIMIT = 3;
59
+ exports.DEFAULT_THROTTLE_FETCH = throttledFetch(exports.DEFAULT_RATE_LIMIT);
@@ -1,7 +1,7 @@
1
1
  import { ExplorerProvider, NodeProvider } from './api';
2
2
  export declare function setCurrentNodeProvider(provider: NodeProvider): void;
3
- export declare function setCurrentNodeProvider(baseUrl: string, apiKey?: string): void;
3
+ export declare function setCurrentNodeProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void;
4
4
  export declare function getCurrentNodeProvider(): NodeProvider;
5
5
  export declare function setCurrentExplorerProvider(provider: ExplorerProvider): void;
6
- export declare function setCurrentExplorerProvider(baseUrl: string, apiKey?: string): void;
6
+ export declare function setCurrentExplorerProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void;
7
7
  export declare function getCurrentExplorerProvider(): ExplorerProvider | undefined;
@@ -20,9 +20,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
20
20
  exports.getCurrentExplorerProvider = exports.setCurrentExplorerProvider = exports.getCurrentNodeProvider = exports.setCurrentNodeProvider = void 0;
21
21
  const api_1 = require("./api");
22
22
  let _currentNodeProvider = undefined;
23
- function setCurrentNodeProvider(provider, apiKey) {
23
+ function setCurrentNodeProvider(provider, apiKey, customFetch) {
24
24
  if (typeof provider == 'string') {
25
- _currentNodeProvider = new api_1.NodeProvider(provider, apiKey);
25
+ _currentNodeProvider = new api_1.NodeProvider(provider, apiKey, customFetch);
26
26
  }
27
27
  else {
28
28
  _currentNodeProvider = provider;
@@ -37,9 +37,9 @@ function getCurrentNodeProvider() {
37
37
  }
38
38
  exports.getCurrentNodeProvider = getCurrentNodeProvider;
39
39
  let _currentExplorerProvider = undefined;
40
- function setCurrentExplorerProvider(provider, apiKey) {
40
+ function setCurrentExplorerProvider(provider, apiKey, customFetch) {
41
41
  if (typeof provider == 'string') {
42
- _currentExplorerProvider = new api_1.ExplorerProvider(provider, apiKey);
42
+ _currentExplorerProvider = new api_1.ExplorerProvider(provider, apiKey, customFetch);
43
43
  }
44
44
  else {
45
45
  _currentExplorerProvider = provider;
@@ -3,7 +3,7 @@ import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScri
3
3
  export declare abstract class TransactionBuilder {
4
4
  abstract get nodeProvider(): NodeProvider;
5
5
  static from(nodeProvider: NodeProvider): TransactionBuilder;
6
- static from(baseUrl: string, apiKey?: string): TransactionBuilder;
6
+ static from(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): TransactionBuilder;
7
7
  private static validatePublicKey;
8
8
  buildTransferTx(params: SignTransferTxParams, publicKey: string): Promise<Omit<SignTransferTxResult, 'signature'>>;
9
9
  buildDeployContractTx(params: SignDeployContractTxParams, publicKey: string): Promise<Omit<SignDeployContractTxResult, 'signature'>>;
@@ -23,8 +23,8 @@ const api_1 = require("../api");
23
23
  const utils_1 = require("../utils");
24
24
  const signer_1 = require("./signer");
25
25
  class TransactionBuilder {
26
- static from(param0, param1) {
27
- const nodeProvider = typeof param0 === 'string' ? new api_1.NodeProvider(param0, param1) : param0;
26
+ static from(param0, param1, customFetch) {
27
+ const nodeProvider = typeof param0 === 'string' ? new api_1.NodeProvider(param0, param1, customFetch) : param0;
28
28
  return new (class extends TransactionBuilder {
29
29
  get nodeProvider() {
30
30
  return nodeProvider;
@@ -14,5 +14,5 @@ export declare function prettifyTokenAmount(amount: Number256, decimals: number)
14
14
  export declare function prettifyExactAmount(amount: Number256, decimals: number): string | undefined;
15
15
  export declare function prettifyNumber(amount: Number256, decimals: number, config: IPrettifyNumberConfig): string | undefined;
16
16
  export declare function convertAmountWithDecimals(amount: string | number, decimals: number): bigint | undefined;
17
- export declare function convertAlphAmount(amount: string | number): bigint | undefined;
17
+ export declare function convertAlphAmountWithDecimals(amount: string | number): bigint | undefined;
18
18
  export declare function number256ToBigint(number: Number256): bigint;
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
20
20
  return (mod && mod.__esModule) ? mod : { "default": mod };
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.number256ToBigint = exports.convertAlphAmount = exports.convertAmountWithDecimals = exports.prettifyNumber = exports.prettifyExactAmount = exports.prettifyTokenAmount = exports.prettifyAttoAlphAmount = exports.prettifyNumberConfig = exports.isNumeric = void 0;
23
+ exports.number256ToBigint = exports.convertAlphAmountWithDecimals = exports.convertAmountWithDecimals = exports.prettifyNumber = exports.prettifyExactAmount = exports.prettifyTokenAmount = exports.prettifyAttoAlphAmount = exports.prettifyNumberConfig = exports.isNumeric = void 0;
24
24
  // Credits:
25
25
  // 1. https://github.com/argentlabs/argent-x/blob/e63affa7f28b27333dca4081a3dcd375bb2da40b/packages/extension/src/shared/utils/number.ts
26
26
  // 2. https://github.com/ethers-io/ethers.js/blob/724881f34d428406488a1c9f9dbebe54b6edecda/src.ts/utils/fixednumber.ts
@@ -138,10 +138,11 @@ function convertAmountWithDecimals(amount, decimals) {
138
138
  }
139
139
  }
140
140
  exports.convertAmountWithDecimals = convertAmountWithDecimals;
141
- function convertAlphAmount(amount) {
141
+ // E.g. `1.23 ALPH` will be converted to `1230000000000000000`
142
+ function convertAlphAmountWithDecimals(amount) {
142
143
  return convertAmountWithDecimals(amount, 18);
143
144
  }
144
- exports.convertAlphAmount = convertAlphAmount;
145
+ exports.convertAlphAmountWithDecimals = convertAlphAmountWithDecimals;
145
146
  function number256ToBigint(number) {
146
147
  return typeof number === 'string' ? BigInt(number) : number;
147
148
  }
@@ -22,7 +22,7 @@ export declare function contractIdFromTx(txId: string, outputIndex: number): str
22
22
  export declare function subContractId(parentContractId: string, pathInHex: string, group: number): string;
23
23
  export declare function stringToHex(str: string): string;
24
24
  export declare function hexToString(str: string): string;
25
- export declare function timeout(ms: number): Promise<void>;
25
+ export declare function sleep(ms: number): Promise<void>;
26
26
  declare type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
27
27
  export declare type Eq<X, Y> = _Eq<{
28
28
  [P in keyof X]: X[P];
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
20
20
  return (mod && mod.__esModule) ? mod : { "default": mod };
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.assertType = exports.timeout = exports.hexToString = exports.stringToHex = exports.subContractId = exports.contractIdFromTx = exports.addressFromContractId = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.groupOfPrivateKey = exports.binToHex = exports.hexToBinUnsafe = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isHexString = exports.xorByte = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = void 0;
23
+ exports.assertType = exports.sleep = exports.hexToString = exports.stringToHex = exports.subContractId = exports.contractIdFromTx = exports.addressFromContractId = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.groupOfPrivateKey = exports.binToHex = exports.hexToBinUnsafe = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isHexString = exports.xorByte = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = void 0;
24
24
  const elliptic_1 = require("elliptic");
25
25
  const bn_js_1 = __importDefault(require("bn.js"));
26
26
  const blakejs_1 = __importDefault(require("blakejs"));
@@ -222,10 +222,10 @@ function hexToString(str) {
222
222
  return buffer_1.Buffer.from(str, 'hex').toString();
223
223
  }
224
224
  exports.hexToString = hexToString;
225
- function timeout(ms) {
225
+ function sleep(ms) {
226
226
  return new Promise((resolve) => setTimeout(resolve, ms));
227
227
  }
228
- exports.timeout = timeout;
228
+ exports.sleep = sleep;
229
229
  // eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
230
230
  function assertType() { }
231
231
  exports.assertType = assertType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -41,13 +41,14 @@
41
41
  "type": "commonjs",
42
42
  "dependencies": {
43
43
  "@noble/secp256k1": "1.7.1",
44
+ "async-sema": "^3.1.1",
44
45
  "base-x": "4.0.0",
46
+ "bignumber.js": "^9.0.2",
45
47
  "blakejs": "1.2.1",
46
48
  "buffer": "^6.0.3",
47
49
  "cross-fetch": "^3.1.5",
48
50
  "elliptic": "6.5.4",
49
- "eventemitter3": "^4.0.7",
50
- "bignumber.js": "^9.0.2"
51
+ "eventemitter3": "^4.0.7"
51
52
  },
52
53
  "devDependencies": {
53
54
  "@babel/eslint-parser": "^7.18.9",
@@ -18,12 +18,14 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
20
20
  import { Api as ExplorerApi } from './api-explorer'
21
+ import { DEFAULT_THROTTLE_FETCH } from './utils'
21
22
 
22
- function initializeExplorerApi(baseUrl: string, apiKey?: string): ExplorerApi<string> {
23
+ function initializeExplorerApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): ExplorerApi<string> {
23
24
  const explorerApi = new ExplorerApi<string>({
24
25
  baseUrl: baseUrl,
25
26
  baseApiParams: { secure: true },
26
- securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
27
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
28
+ customFetch: customFetch ?? DEFAULT_THROTTLE_FETCH
27
29
  })
28
30
  explorerApi.setSecurityData(apiKey ?? null)
29
31
  return explorerApi
@@ -39,13 +41,13 @@ export class ExplorerProvider {
39
41
  readonly charts: ExplorerApi<string>['charts']
40
42
  readonly utils: ExplorerApi<string>['utils']
41
43
 
42
- constructor(baseUrl: string, apiKey?: string)
44
+ constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch)
43
45
  constructor(provider: ExplorerProvider)
44
46
  constructor(handler: ApiRequestHandler)
45
- constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string) {
47
+ constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string, customFetch?: typeof fetch) {
46
48
  let explorerApi: ExplorerProvider
47
49
  if (typeof param0 === 'string') {
48
- explorerApi = initializeExplorerApi(param0, apiKey)
50
+ explorerApi = initializeExplorerApi(param0, apiKey, customFetch)
49
51
  } else if (typeof param0 === 'function') {
50
52
  explorerApi = new ExplorerProvider('https://1.2.3.4:0')
51
53
  forwardRequests(explorerApi, param0 as ApiRequestHandler)
package/src/api/index.ts CHANGED
@@ -21,3 +21,4 @@ export * from './explorer-provider'
21
21
  export * as node from './api-alephium'
22
22
  export * as explorer from './api-explorer'
23
23
  export * from './types'
24
+ export * from './utils'
@@ -18,12 +18,14 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
20
20
  import { Api as NodeApi } from './api-alephium'
21
+ import { DEFAULT_THROTTLE_FETCH } from './utils'
21
22
 
22
- function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
23
+ function initializeNodeApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): NodeApi<string> {
23
24
  const nodeApi = new NodeApi<string>({
24
25
  baseUrl: baseUrl,
25
26
  baseApiParams: { secure: true },
26
- securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
27
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
28
+ customFetch: customFetch ?? DEFAULT_THROTTLE_FETCH
27
29
  })
28
30
  nodeApi.setSecurityData(apiKey ?? null)
29
31
  return nodeApi
@@ -42,13 +44,13 @@ export class NodeProvider {
42
44
  readonly miners: NodeApi<string>['miners']
43
45
  readonly events: NodeApi<string>['events']
44
46
 
45
- constructor(baseUrl: string, apiKey?: string)
47
+ constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch)
46
48
  constructor(provider: NodeProvider)
47
49
  constructor(handler: ApiRequestHandler)
48
- constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
50
+ constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string, customFetch?: typeof fetch) {
49
51
  let nodeApi: NodeProvider
50
52
  if (typeof param0 === 'string') {
51
- nodeApi = initializeNodeApi(param0, apiKey)
53
+ nodeApi = initializeNodeApi(param0, apiKey, customFetch)
52
54
  } else if (typeof param0 === 'function') {
53
55
  nodeApi = new NodeProvider('https://1.2.3.4:0')
54
56
  forwardRequests(nodeApi, param0 as ApiRequestHandler)
package/src/api/utils.ts CHANGED
@@ -16,10 +16,41 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
+ import 'cross-fetch/polyfill'
20
+ import { sleep } from '../utils'
21
+ import { RateLimit } from 'async-sema'
22
+
19
23
  export function convertHttpResponse<T>(response: { data: T; error?: { detail: string } }): T {
20
24
  if (response.error) {
21
- throw new Error(`[Node API Error] - ${response.error.detail}`)
25
+ throw new Error(`[API Error] - ${response.error.detail}`)
22
26
  } else {
23
27
  return response.data
24
28
  }
25
29
  }
30
+
31
+ export async function retryFetch(...fetchParams: Parameters<typeof fetch>): ReturnType<typeof fetch> {
32
+ const retry = async (retryCount: number): ReturnType<typeof fetch> => {
33
+ const response = await fetch(...fetchParams)
34
+ if (response.status === 429 && retryCount < RETRY_LIMIT_WHEN_429) {
35
+ const nextCount = retryCount + 1
36
+ await sleep(nextCount * 500)
37
+ return await retry(nextCount)
38
+ } else {
39
+ return response
40
+ }
41
+ }
42
+ return retry(0)
43
+ }
44
+
45
+ export function throttledFetch(ratePerSec: number): typeof fetch {
46
+ const limit = RateLimit(ratePerSec)
47
+ const customFetch = async (...fetchParams: Parameters<typeof fetch>): ReturnType<typeof fetch> => {
48
+ await limit()
49
+ return retryFetch(...fetchParams)
50
+ }
51
+ return customFetch
52
+ }
53
+
54
+ export const RETRY_LIMIT_WHEN_429 = 3
55
+ export const DEFAULT_RATE_LIMIT = 3
56
+ export const DEFAULT_THROTTLE_FETCH = throttledFetch(DEFAULT_RATE_LIMIT)
package/src/global.ts CHANGED
@@ -21,10 +21,14 @@ import { ExplorerProvider, NodeProvider } from './api'
21
21
  let _currentNodeProvider: NodeProvider | undefined = undefined
22
22
 
23
23
  export function setCurrentNodeProvider(provider: NodeProvider): void
24
- export function setCurrentNodeProvider(baseUrl: string, apiKey?: string): void
25
- export function setCurrentNodeProvider(provider: NodeProvider | string, apiKey?: string): void {
24
+ export function setCurrentNodeProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void
25
+ export function setCurrentNodeProvider(
26
+ provider: NodeProvider | string,
27
+ apiKey?: string,
28
+ customFetch?: typeof fetch
29
+ ): void {
26
30
  if (typeof provider == 'string') {
27
- _currentNodeProvider = new NodeProvider(provider, apiKey)
31
+ _currentNodeProvider = new NodeProvider(provider, apiKey, customFetch)
28
32
  } else {
29
33
  _currentNodeProvider = provider
30
34
  }
@@ -40,10 +44,14 @@ export function getCurrentNodeProvider(): NodeProvider {
40
44
  let _currentExplorerProvider: ExplorerProvider | undefined = undefined
41
45
 
42
46
  export function setCurrentExplorerProvider(provider: ExplorerProvider): void
43
- export function setCurrentExplorerProvider(baseUrl: string, apiKey?: string): void
44
- export function setCurrentExplorerProvider(provider: ExplorerProvider | string, apiKey?: string): void {
47
+ export function setCurrentExplorerProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void
48
+ export function setCurrentExplorerProvider(
49
+ provider: ExplorerProvider | string,
50
+ apiKey?: string,
51
+ customFetch?: typeof fetch
52
+ ): void {
45
53
  if (typeof provider == 'string') {
46
- _currentExplorerProvider = new ExplorerProvider(provider, apiKey)
54
+ _currentExplorerProvider = new ExplorerProvider(provider, apiKey, customFetch)
47
55
  } else {
48
56
  _currentExplorerProvider = provider
49
57
  }
@@ -37,9 +37,9 @@ export abstract class TransactionBuilder {
37
37
  abstract get nodeProvider(): NodeProvider
38
38
 
39
39
  static from(nodeProvider: NodeProvider): TransactionBuilder
40
- static from(baseUrl: string, apiKey?: string): TransactionBuilder
41
- static from(param0: string | NodeProvider, param1?: string): TransactionBuilder {
42
- const nodeProvider = typeof param0 === 'string' ? new NodeProvider(param0, param1) : (param0 as NodeProvider)
40
+ static from(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): TransactionBuilder
41
+ static from(param0: string | NodeProvider, param1?: string, customFetch?: typeof fetch): TransactionBuilder {
42
+ const nodeProvider = typeof param0 === 'string' ? new NodeProvider(param0, param1, customFetch) : (param0 as NodeProvider)
43
43
  return new (class extends TransactionBuilder {
44
44
  get nodeProvider(): NodeProvider {
45
45
  return nodeProvider
@@ -153,7 +153,8 @@ export function convertAmountWithDecimals(amount: string | number, decimals: num
153
153
  }
154
154
  }
155
155
 
156
- export function convertAlphAmount(amount: string | number): bigint | undefined {
156
+ // E.g. `1.23 ALPH` will be converted to `1230000000000000000`
157
+ export function convertAlphAmountWithDecimals(amount: string | number): bigint | undefined {
157
158
  return convertAmountWithDecimals(amount, 18)
158
159
  }
159
160
 
@@ -227,7 +227,7 @@ export function hexToString(str: string): string {
227
227
  return Buffer.from(str, 'hex').toString()
228
228
  }
229
229
 
230
- export function timeout(ms: number): Promise<void> {
230
+ export function sleep(ms: number): Promise<void> {
231
231
  return new Promise((resolve) => setTimeout(resolve, ms))
232
232
  }
233
233