@alephium/web3 0.9.0-rc.0 → 0.9.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/alephium-web3.min.js +3 -0
  2. package/dist/alephium-web3.min.js.LICENSE.txt +12 -0
  3. package/dist/alephium-web3.min.js.map +1 -0
  4. package/dist/scripts/check-versions.d.ts +1 -0
  5. package/dist/scripts/check-versions.js +39 -0
  6. package/dist/scripts/header.d.ts +0 -0
  7. package/dist/scripts/header.js +18 -0
  8. package/dist/src/api/api-alephium.d.ts +1717 -0
  9. package/dist/src/api/api-alephium.js +1284 -0
  10. package/dist/src/api/api-explorer.d.ts +923 -0
  11. package/dist/src/api/api-explorer.js +711 -0
  12. package/dist/src/api/explorer-provider.d.ts +20 -0
  13. package/dist/src/api/explorer-provider.js +69 -0
  14. package/dist/src/api/index.d.ts +6 -0
  15. package/dist/src/api/index.js +52 -0
  16. package/dist/src/api/node-provider.d.ts +38 -0
  17. package/dist/src/api/node-provider.js +99 -0
  18. package/dist/src/api/types.d.ts +38 -0
  19. package/dist/src/api/types.js +259 -0
  20. package/dist/src/api/utils.d.ts +12 -0
  21. package/dist/src/api/utils.js +59 -0
  22. package/dist/src/constants.d.ts +6 -0
  23. package/dist/src/constants.js +26 -0
  24. package/dist/src/contract/contract.d.ts +297 -0
  25. package/dist/src/contract/contract.js +1097 -0
  26. package/dist/src/contract/events.d.ts +11 -0
  27. package/dist/src/contract/events.js +65 -0
  28. package/dist/src/contract/index.d.ts +3 -0
  29. package/dist/src/contract/index.js +36 -0
  30. package/dist/src/contract/ralph.d.ts +13 -0
  31. package/dist/src/contract/ralph.js +397 -0
  32. package/dist/src/global.d.ts +7 -0
  33. package/dist/src/global.js +54 -0
  34. package/dist/src/index.d.ts +9 -0
  35. package/dist/src/index.js +58 -0
  36. package/dist/src/signer/index.d.ts +3 -0
  37. package/dist/src/signer/index.js +36 -0
  38. package/dist/src/signer/signer.d.ts +60 -0
  39. package/dist/src/signer/signer.js +236 -0
  40. package/dist/src/signer/tx-builder.d.ts +12 -0
  41. package/dist/src/signer/tx-builder.js +96 -0
  42. package/dist/src/signer/types.d.ts +115 -0
  43. package/dist/src/signer/types.js +30 -0
  44. package/dist/src/token/index.d.ts +1 -0
  45. package/dist/src/token/index.js +34 -0
  46. package/dist/src/token/nft.d.ts +10 -0
  47. package/dist/src/token/nft.js +19 -0
  48. package/dist/src/transaction/index.d.ts +2 -0
  49. package/dist/src/transaction/index.js +35 -0
  50. package/dist/src/transaction/sign-verify.d.ts +3 -0
  51. package/dist/src/transaction/sign-verify.js +52 -0
  52. package/dist/src/transaction/status.d.ts +11 -0
  53. package/dist/src/transaction/status.js +49 -0
  54. package/dist/src/utils/address.d.ts +1 -0
  55. package/dist/src/utils/address.js +42 -0
  56. package/dist/src/utils/bs58.d.ts +5 -0
  57. package/dist/src/utils/bs58.js +40 -0
  58. package/dist/src/utils/djb2.d.ts +1 -0
  59. package/dist/src/utils/djb2.js +27 -0
  60. package/dist/src/utils/index.d.ts +7 -0
  61. package/dist/src/utils/index.js +40 -0
  62. package/dist/src/utils/number.d.ts +18 -0
  63. package/dist/src/utils/number.fixture.d.ts +12 -0
  64. package/dist/src/utils/number.fixture.js +189 -0
  65. package/dist/src/utils/number.js +149 -0
  66. package/dist/src/utils/sign.d.ts +3 -0
  67. package/dist/src/utils/sign.js +89 -0
  68. package/dist/src/utils/subscription.d.ts +21 -0
  69. package/dist/src/utils/subscription.js +51 -0
  70. package/dist/src/utils/utils.d.ts +40 -0
  71. package/dist/src/utils/utils.js +244 -0
  72. package/package.json +1 -1
  73. package/LICENSE +0 -165
@@ -0,0 +1,20 @@
1
+ import { ApiRequestArguments, ApiRequestHandler } from './types';
2
+ import { Api as ExplorerApi } from './api-explorer';
3
+ export declare class ExplorerProvider {
4
+ readonly blocks: ExplorerApi<string>['blocks'];
5
+ readonly transactions: ExplorerApi<string>['transactions'];
6
+ readonly addresses: ExplorerApi<string>['addresses'];
7
+ readonly infos: ExplorerApi<string>['infos'];
8
+ readonly mempool: ExplorerApi<string>['mempool'];
9
+ readonly tokens: ExplorerApi<string>['tokens'];
10
+ readonly charts: ExplorerApi<string>['charts'];
11
+ readonly contractEvents: ExplorerApi<string>['contractEvents'];
12
+ readonly contracts: ExplorerApi<string>['contracts'];
13
+ readonly utils: ExplorerApi<string>['utils'];
14
+ constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch);
15
+ constructor(provider: ExplorerProvider);
16
+ constructor(handler: ApiRequestHandler);
17
+ request: (args: ApiRequestArguments) => Promise<any>;
18
+ static Proxy(explorerProvider: ExplorerProvider): ExplorerProvider;
19
+ static Remote(handler: ApiRequestHandler): ExplorerProvider;
20
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.ExplorerProvider = void 0;
21
+ const types_1 = require("./types");
22
+ const api_explorer_1 = require("./api-explorer");
23
+ const utils_1 = require("./utils");
24
+ function initializeExplorerApi(baseUrl, apiKey, customFetch) {
25
+ const explorerApi = new api_explorer_1.Api({
26
+ baseUrl: baseUrl,
27
+ baseApiParams: { secure: true },
28
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
29
+ customFetch: customFetch ?? utils_1.DEFAULT_THROTTLE_FETCH
30
+ });
31
+ explorerApi.setSecurityData(apiKey ?? null);
32
+ return explorerApi;
33
+ }
34
+ class ExplorerProvider {
35
+ constructor(param0, apiKey, customFetch) {
36
+ this.request = (args) => {
37
+ return (0, types_1.request)(this, args);
38
+ };
39
+ let explorerApi;
40
+ if (typeof param0 === 'string') {
41
+ explorerApi = initializeExplorerApi(param0, apiKey, customFetch);
42
+ }
43
+ else if (typeof param0 === 'function') {
44
+ explorerApi = new ExplorerProvider('https://1.2.3.4:0');
45
+ (0, types_1.forwardRequests)(explorerApi, param0);
46
+ }
47
+ else {
48
+ explorerApi = param0;
49
+ }
50
+ this.blocks = { ...explorerApi.blocks };
51
+ this.transactions = { ...explorerApi.transactions };
52
+ this.addresses = { ...explorerApi.addresses };
53
+ this.infos = { ...explorerApi.infos };
54
+ this.mempool = { ...explorerApi.mempool };
55
+ this.tokens = { ...explorerApi.tokens };
56
+ this.charts = { ...explorerApi.charts };
57
+ this.utils = { ...explorerApi.utils };
58
+ this.contracts = { ...explorerApi.contracts };
59
+ this.contractEvents = { ...explorerApi.contractEvents };
60
+ }
61
+ // This can prevent the proxied explorer provider from being modified
62
+ static Proxy(explorerProvider) {
63
+ return new ExplorerProvider(explorerProvider);
64
+ }
65
+ static Remote(handler) {
66
+ return new ExplorerProvider(handler);
67
+ }
68
+ }
69
+ exports.ExplorerProvider = ExplorerProvider;
@@ -0,0 +1,6 @@
1
+ export * from './node-provider';
2
+ export * from './explorer-provider';
3
+ export * as node from './api-alephium';
4
+ export * as explorer from './api-explorer';
5
+ export * from './types';
6
+ export * from './utils';
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ var desc = Object.getOwnPropertyDescriptor(m, k);
22
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
+ desc = { enumerable: true, get: function() { return m[k]; } };
24
+ }
25
+ Object.defineProperty(o, k2, desc);
26
+ }) : (function(o, m, k, k2) {
27
+ if (k2 === undefined) k2 = k;
28
+ o[k2] = m[k];
29
+ }));
30
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
31
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
32
+ }) : function(o, v) {
33
+ o["default"] = v;
34
+ });
35
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
+ };
38
+ var __importStar = (this && this.__importStar) || function (mod) {
39
+ if (mod && mod.__esModule) return mod;
40
+ var result = {};
41
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
42
+ __setModuleDefault(result, mod);
43
+ return result;
44
+ };
45
+ Object.defineProperty(exports, "__esModule", { value: true });
46
+ exports.explorer = exports.node = void 0;
47
+ __exportStar(require("./node-provider"), exports);
48
+ __exportStar(require("./explorer-provider"), exports);
49
+ exports.node = __importStar(require("./api-alephium"));
50
+ exports.explorer = __importStar(require("./api-explorer"));
51
+ __exportStar(require("./types"), exports);
52
+ __exportStar(require("./utils"), exports);
@@ -0,0 +1,38 @@
1
+ import { ApiRequestArguments, ApiRequestHandler, TokenMetaData } from './types';
2
+ import { Api as NodeApi } from './api-alephium';
3
+ import { HexString } from '../contract';
4
+ interface NodeProviderApis {
5
+ wallets: NodeApi<string>['wallets'];
6
+ infos: NodeApi<string>['infos'];
7
+ blockflow: NodeApi<string>['blockflow'];
8
+ addresses: NodeApi<string>['addresses'];
9
+ transactions: NodeApi<string>['transactions'];
10
+ mempool: NodeApi<string>['mempool'];
11
+ contracts: NodeApi<string>['contracts'];
12
+ multisig: NodeApi<string>['multisig'];
13
+ utils: NodeApi<string>['utils'];
14
+ miners: NodeApi<string>['miners'];
15
+ events: NodeApi<string>['events'];
16
+ }
17
+ export declare class NodeProvider implements NodeProviderApis {
18
+ readonly wallets: NodeApi<string>['wallets'];
19
+ readonly infos: NodeApi<string>['infos'];
20
+ readonly blockflow: NodeApi<string>['blockflow'];
21
+ readonly addresses: NodeApi<string>['addresses'];
22
+ readonly transactions: NodeApi<string>['transactions'];
23
+ readonly mempool: NodeApi<string>['mempool'];
24
+ readonly contracts: NodeApi<string>['contracts'];
25
+ readonly multisig: NodeApi<string>['multisig'];
26
+ readonly utils: NodeApi<string>['utils'];
27
+ readonly miners: NodeApi<string>['miners'];
28
+ readonly events: NodeApi<string>['events'];
29
+ constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch);
30
+ constructor(provider: NodeProvider);
31
+ constructor(handler: ApiRequestHandler);
32
+ request: (args: ApiRequestArguments) => Promise<any>;
33
+ static Proxy(nodeProvider: NodeProvider): NodeProvider;
34
+ static Remote(handler: ApiRequestHandler): NodeProvider;
35
+ fetchStdTokenMetaData: (tokenId: HexString) => Promise<TokenMetaData>;
36
+ guessStdInterfaceId: (tokenId: HexString) => Promise<HexString | undefined>;
37
+ }
38
+ export {};
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.NodeProvider = void 0;
21
+ const types_1 = require("./types");
22
+ const api_alephium_1 = require("./api-alephium");
23
+ const utils_1 = require("./utils");
24
+ const utils_2 = require("../utils");
25
+ function initializeNodeApi(baseUrl, apiKey, customFetch) {
26
+ const nodeApi = new api_alephium_1.Api({
27
+ baseUrl: baseUrl,
28
+ baseApiParams: { secure: true },
29
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
30
+ customFetch: customFetch ?? utils_1.DEFAULT_THROTTLE_FETCH
31
+ });
32
+ nodeApi.setSecurityData(apiKey ?? null);
33
+ return nodeApi;
34
+ }
35
+ class NodeProvider {
36
+ constructor(param0, apiKey, customFetch) {
37
+ this.request = (args) => {
38
+ return (0, types_1.request)(this, args);
39
+ };
40
+ // Only use this when the token is following the standard token interface
41
+ this.fetchStdTokenMetaData = async (tokenId) => {
42
+ const group = 0;
43
+ const address = (0, utils_2.addressFromTokenId)(tokenId);
44
+ const calls = Array.from([0, 1, 2, 3], (index) => ({ methodIndex: index, group: group, address: address }));
45
+ const result = await this.contracts.postContractsMulticallContract({
46
+ calls: calls
47
+ });
48
+ return {
49
+ symbol: result.results[0].returns[0].value,
50
+ name: result.results[1].returns[0].value,
51
+ decimals: Number(result.results[2].returns[0].value),
52
+ totalSupply: BigInt(result.results[3].returns[0].value)
53
+ };
54
+ };
55
+ this.guessStdInterfaceId = async (tokenId) => {
56
+ const address = (0, utils_2.addressFromTokenId)(tokenId);
57
+ const group = (0, utils_2.groupOfAddress)(address);
58
+ const rawState = await this.contracts.getContractsAddressState((0, utils_2.addressFromTokenId)(tokenId), { group });
59
+ const lastImmField = rawState.immFields.slice(-1).pop()?.value;
60
+ const interfaceIdPrefix = '414c5048'; // the hex of 'ALPH'
61
+ if (typeof lastImmField === 'string' && lastImmField.startsWith(interfaceIdPrefix)) {
62
+ return lastImmField.slice(8);
63
+ }
64
+ else {
65
+ return undefined;
66
+ }
67
+ };
68
+ let nodeApi;
69
+ if (typeof param0 === 'string') {
70
+ nodeApi = initializeNodeApi(param0, apiKey, customFetch);
71
+ }
72
+ else if (typeof param0 === 'function') {
73
+ nodeApi = new NodeProvider('https://1.2.3.4:0');
74
+ (0, types_1.forwardRequests)(nodeApi, param0);
75
+ }
76
+ else {
77
+ nodeApi = param0;
78
+ }
79
+ this.wallets = { ...nodeApi.wallets };
80
+ this.infos = { ...nodeApi.infos };
81
+ this.blockflow = { ...nodeApi.blockflow };
82
+ this.addresses = { ...nodeApi.addresses };
83
+ this.transactions = { ...nodeApi.transactions };
84
+ this.mempool = { ...nodeApi.mempool };
85
+ this.contracts = { ...nodeApi.contracts };
86
+ this.multisig = { ...nodeApi.multisig };
87
+ this.utils = { ...nodeApi.utils };
88
+ this.miners = { ...nodeApi.miners };
89
+ this.events = { ...nodeApi.events };
90
+ }
91
+ // This can prevent the proxied node provider from being modified
92
+ static Proxy(nodeProvider) {
93
+ return new NodeProvider(nodeProvider);
94
+ }
95
+ static Remote(handler) {
96
+ return new NodeProvider(handler);
97
+ }
98
+ }
99
+ exports.NodeProvider = NodeProvider;
@@ -0,0 +1,38 @@
1
+ import * as node from './api-alephium';
2
+ export type Number256 = bigint | string;
3
+ export type Val = Number256 | boolean | string | Val[];
4
+ export type NamedVals = Record<string, Val>;
5
+ export interface Token {
6
+ id: string;
7
+ amount: Number256;
8
+ }
9
+ export declare function toApiToken(token: Token): node.Token;
10
+ export declare function toApiTokens(tokens?: Token[]): node.Token[] | undefined;
11
+ export declare function fromApiToken(token: node.Token): Token;
12
+ export declare function fromApiTokens(tokens?: node.Token[]): Token[] | undefined;
13
+ export declare function toApiBoolean(v: Val): boolean;
14
+ export declare function toApiNumber256(v: Val): string;
15
+ export declare function toApiNumber256Optional(v?: Val): string | undefined;
16
+ export declare function fromApiNumber256(n: string): bigint;
17
+ export declare function toApiByteVec(v: Val): string;
18
+ export declare function toApiAddress(v: Val): string;
19
+ export declare function toApiArray(tpe: string, v: Val): node.Val;
20
+ export declare function toApiVal(v: Val, tpe: string): node.Val;
21
+ export declare function fromApiVals(vals: node.Val[], names: string[], types: string[], systemEvent?: boolean): NamedVals;
22
+ export declare function fromApiArray(vals: node.Val[], types: string[]): Val[];
23
+ export declare function fromApiVal(v: node.Val, tpe: string): Val;
24
+ export declare function typeLength(tpe: string): number;
25
+ export interface ApiRequestArguments {
26
+ path: string;
27
+ method: string;
28
+ params: any[];
29
+ }
30
+ export type ApiRequestHandler = (args: ApiRequestArguments) => Promise<any>;
31
+ export declare function forwardRequests(api: Record<string, any>, handler: ApiRequestHandler): void;
32
+ export declare function request(provider: Record<string, any>, args: ApiRequestArguments): Promise<any>;
33
+ export interface TokenMetaData {
34
+ name: string;
35
+ symbol: string;
36
+ decimals: number;
37
+ totalSupply: Number256;
38
+ }
@@ -0,0 +1,259 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.request = exports.forwardRequests = exports.typeLength = exports.fromApiVal = exports.fromApiArray = exports.fromApiVals = exports.toApiVal = exports.toApiArray = exports.toApiAddress = exports.toApiByteVec = exports.fromApiNumber256 = exports.toApiNumber256Optional = exports.toApiNumber256 = exports.toApiBoolean = exports.fromApiTokens = exports.fromApiToken = exports.toApiTokens = exports.toApiToken = void 0;
21
+ const utils_1 = require("../utils");
22
+ utils_1.assertType;
23
+ function toApiToken(token) {
24
+ return { id: token.id, amount: toApiNumber256(token.amount) };
25
+ }
26
+ exports.toApiToken = toApiToken;
27
+ function toApiTokens(tokens) {
28
+ return tokens?.map(toApiToken);
29
+ }
30
+ exports.toApiTokens = toApiTokens;
31
+ function fromApiToken(token) {
32
+ return { id: token.id, amount: fromApiNumber256(token.amount) };
33
+ }
34
+ exports.fromApiToken = fromApiToken;
35
+ function fromApiTokens(tokens) {
36
+ return tokens?.map(fromApiToken);
37
+ }
38
+ exports.fromApiTokens = fromApiTokens;
39
+ function toApiBoolean(v) {
40
+ if (typeof v === 'boolean') {
41
+ return v;
42
+ }
43
+ else {
44
+ throw new Error(`Invalid boolean value: ${v}`);
45
+ }
46
+ }
47
+ exports.toApiBoolean = toApiBoolean;
48
+ // TODO: check integer bounds
49
+ function toApiNumber256(v) {
50
+ if ((typeof v === 'number' && Number.isInteger(v)) || typeof v === 'bigint') {
51
+ return v.toString();
52
+ }
53
+ else if (typeof v === 'string') {
54
+ return v;
55
+ }
56
+ else {
57
+ throw new Error(`Invalid 256 bit number: ${v}`);
58
+ }
59
+ }
60
+ exports.toApiNumber256 = toApiNumber256;
61
+ function toApiNumber256Optional(v) {
62
+ return v === undefined ? undefined : toApiNumber256(v);
63
+ }
64
+ exports.toApiNumber256Optional = toApiNumber256Optional;
65
+ function fromApiNumber256(n) {
66
+ return BigInt(n);
67
+ }
68
+ exports.fromApiNumber256 = fromApiNumber256;
69
+ // TODO: check hex string
70
+ function toApiByteVec(v) {
71
+ if (typeof v === 'string') {
72
+ // try to convert from address to contract id
73
+ try {
74
+ const address = utils_1.bs58.decode(v);
75
+ if (address.length == 33 && address[0] == 3) {
76
+ return Buffer.from(address.slice(1)).toString('hex');
77
+ }
78
+ }
79
+ catch (_) {
80
+ return v;
81
+ }
82
+ return v;
83
+ }
84
+ else {
85
+ throw new Error(`Invalid string: ${v}`);
86
+ }
87
+ }
88
+ exports.toApiByteVec = toApiByteVec;
89
+ function toApiAddress(v) {
90
+ if (typeof v === 'string') {
91
+ try {
92
+ utils_1.bs58.decode(v);
93
+ return v;
94
+ }
95
+ catch (error) {
96
+ throw new Error(`Invalid base58 string: ${v}`);
97
+ }
98
+ }
99
+ else {
100
+ throw new Error(`Invalid string: ${v}`);
101
+ }
102
+ }
103
+ exports.toApiAddress = toApiAddress;
104
+ function toApiArray(tpe, v) {
105
+ if (!Array.isArray(v)) {
106
+ throw new Error(`Expected array, got ${v}`);
107
+ }
108
+ const semiColonIndex = tpe.lastIndexOf(';');
109
+ if (semiColonIndex == -1) {
110
+ throw new Error(`Invalid Val type: ${tpe}`);
111
+ }
112
+ const subType = tpe.slice(1, semiColonIndex);
113
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
114
+ if (v.length != dim) {
115
+ throw new Error(`Invalid val dimension: ${v}`);
116
+ }
117
+ else {
118
+ return { value: v.map((v) => toApiVal(v, subType)), type: 'Array' };
119
+ }
120
+ }
121
+ exports.toApiArray = toApiArray;
122
+ function toApiVal(v, tpe) {
123
+ if (tpe === 'Bool') {
124
+ return { value: toApiBoolean(v), type: tpe };
125
+ }
126
+ else if (tpe === 'U256' || tpe === 'I256') {
127
+ return { value: toApiNumber256(v), type: tpe };
128
+ }
129
+ else if (tpe === 'ByteVec') {
130
+ return { value: toApiByteVec(v), type: tpe };
131
+ }
132
+ else if (tpe === 'Address') {
133
+ return { value: toApiAddress(v), type: tpe };
134
+ }
135
+ else {
136
+ return toApiArray(tpe, v);
137
+ }
138
+ }
139
+ exports.toApiVal = toApiVal;
140
+ function _fromApiVal(vals, valIndex, tpe, systemEvent = false) {
141
+ if (vals.length === 0) {
142
+ throw new Error('Not enough Vals');
143
+ }
144
+ const firstVal = vals[`${valIndex}`];
145
+ if (tpe === 'Bool' && firstVal.type === tpe) {
146
+ return [firstVal.value, valIndex + 1];
147
+ }
148
+ else if ((tpe === 'U256' || tpe === 'I256') && firstVal.type === tpe) {
149
+ return [fromApiNumber256(firstVal.value), valIndex + 1];
150
+ }
151
+ else if ((tpe === 'ByteVec' || tpe === 'Address') && (firstVal.type === tpe || systemEvent)) {
152
+ return [firstVal.value, valIndex + 1];
153
+ }
154
+ else {
155
+ const [baseType, dims] = decodeArrayType(tpe);
156
+ const arraySize = dims.reduce((a, b) => a * b);
157
+ const nextIndex = valIndex + arraySize;
158
+ const valsToUse = vals.slice(valIndex, nextIndex);
159
+ if (valsToUse.length == arraySize && valsToUse.every((val) => val.type === baseType)) {
160
+ const localVals = valsToUse.map((val) => fromApiVal(val, baseType));
161
+ return [foldVals(localVals, dims), nextIndex];
162
+ }
163
+ else {
164
+ throw new Error(`Invalid array Val type: ${valsToUse}, ${tpe}`);
165
+ }
166
+ }
167
+ }
168
+ function fromApiVals(vals, names, types, systemEvent = false) {
169
+ let valIndex = 0;
170
+ const result = {};
171
+ types.forEach((currentType, index) => {
172
+ const currentName = names[`${index}`];
173
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType, systemEvent);
174
+ valIndex = nextIndex;
175
+ result[`${currentName}`] = val;
176
+ });
177
+ return result;
178
+ }
179
+ exports.fromApiVals = fromApiVals;
180
+ function fromApiArray(vals, types) {
181
+ let valIndex = 0;
182
+ const result = [];
183
+ for (const currentType of types) {
184
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType);
185
+ result.push(val);
186
+ valIndex = nextIndex;
187
+ }
188
+ return result;
189
+ }
190
+ exports.fromApiArray = fromApiArray;
191
+ function fromApiVal(v, tpe) {
192
+ if (v.type === 'Bool' && v.type === tpe) {
193
+ return v.value;
194
+ }
195
+ else if ((v.type === 'U256' || v.type === 'I256') && v.type === tpe) {
196
+ return fromApiNumber256(v.value);
197
+ }
198
+ else if ((v.type === 'ByteVec' || v.type === 'Address') && v.type === tpe) {
199
+ return v.value;
200
+ }
201
+ else {
202
+ throw new Error(`Invalid node.Val type: ${v}`);
203
+ }
204
+ }
205
+ exports.fromApiVal = fromApiVal;
206
+ function decodeArrayType(tpe) {
207
+ const semiColonIndex = tpe.lastIndexOf(';');
208
+ if (semiColonIndex === -1) {
209
+ throw new Error(`Invalid Val type: ${tpe}`);
210
+ }
211
+ const subType = tpe.slice(1, semiColonIndex);
212
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
213
+ if (subType[0] == '[') {
214
+ const [baseType, subDim] = decodeArrayType(subType);
215
+ return [baseType, (subDim.unshift(dim), subDim)];
216
+ }
217
+ else {
218
+ return [subType, [dim]];
219
+ }
220
+ }
221
+ function foldVals(vals, dims) {
222
+ if (dims.length == 1) {
223
+ return vals;
224
+ }
225
+ else {
226
+ const result = [];
227
+ const chunkSize = vals.length / dims[0];
228
+ const chunkDims = dims.slice(1);
229
+ for (let i = 0; i < vals.length; i += chunkSize) {
230
+ const chunk = vals.slice(i, i + chunkSize);
231
+ result.push(foldVals(chunk, chunkDims));
232
+ }
233
+ return result;
234
+ }
235
+ }
236
+ function typeLength(tpe) {
237
+ if (tpe === 'U256' || tpe === 'I256' || tpe === 'Bool' || tpe === 'ByteVec' || tpe === 'Address') {
238
+ return 1;
239
+ }
240
+ const [, dims] = decodeArrayType(tpe);
241
+ return dims.reduce((a, b) => a * b);
242
+ }
243
+ exports.typeLength = typeLength;
244
+ function forwardRequests(api, handler) {
245
+ // Update class properties to forward requests
246
+ for (const [path, pathObject] of Object.entries(api)) {
247
+ for (const method of Object.keys(pathObject)) {
248
+ pathObject[`${method}`] = async (...params) => {
249
+ return handler({ path, method, params });
250
+ };
251
+ }
252
+ }
253
+ }
254
+ exports.forwardRequests = forwardRequests;
255
+ async function request(provider, args) {
256
+ const call = provider[`${args.path}`][`${args.method}`];
257
+ return call(...args.params);
258
+ }
259
+ exports.request = request;
@@ -0,0 +1,12 @@
1
+ import 'cross-fetch/polyfill';
2
+ export declare function convertHttpResponse<T>(response: {
3
+ data: T;
4
+ error?: {
5
+ detail: string;
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;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
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");
24
+ function convertHttpResponse(response) {
25
+ if (response.error) {
26
+ throw new Error(`[API Error] - ${response.error.detail}`);
27
+ }
28
+ else {
29
+ return response.data;
30
+ }
31
+ }
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);
@@ -0,0 +1,6 @@
1
+ export declare const TOTAL_NUMBER_OF_GROUPS = 4;
2
+ export declare const TOTAL_NUMBER_OF_CHAINS: number;
3
+ export declare const MIN_UTXO_SET_AMOUNT: bigint;
4
+ export declare const ALPH_TOKEN_ID: string;
5
+ export declare const ONE_ALPH: bigint;
6
+ export declare const DUST_AMOUNT: bigint;