@alephium/web3 0.15.0 → 0.16.0
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/api-explorer.d.ts +38 -33
- package/dist/src/api/api-explorer.js +14 -0
- package/dist/src/api/types.js +18 -12
- package/dist/src/api/utils.d.ts +2 -0
- package/dist/src/api/utils.js +25 -1
- package/dist/src/contract/contract.js +12 -2
- package/dist/src/contract/ralph.js +35 -60
- package/package.json +2 -2
- package/src/api/api-explorer.ts +57 -33
- package/src/api/types.ts +17 -11
- package/src/api/utils.ts +25 -0
- package/src/contract/contract.ts +10 -2
- package/src/contract/ralph.ts +35 -59
|
@@ -29,6 +29,14 @@ export interface AddressInfo {
|
|
|
29
29
|
/** @format int32 */
|
|
30
30
|
txNumber: number;
|
|
31
31
|
}
|
|
32
|
+
export interface AddressTokenBalance {
|
|
33
|
+
/** @format 32-byte-hash */
|
|
34
|
+
tokenId: string;
|
|
35
|
+
/** @format uint256 */
|
|
36
|
+
balance: string;
|
|
37
|
+
/** @format uint256 */
|
|
38
|
+
lockedBalance: string;
|
|
39
|
+
}
|
|
32
40
|
export interface AssetOutput {
|
|
33
41
|
/** @format int32 */
|
|
34
42
|
hint: number;
|
|
@@ -55,9 +63,9 @@ export interface BlockEntryLite {
|
|
|
55
63
|
hash: string;
|
|
56
64
|
/** @format int64 */
|
|
57
65
|
timestamp: number;
|
|
58
|
-
/** @format
|
|
66
|
+
/** @format group-index */
|
|
59
67
|
chainFrom: number;
|
|
60
|
-
/** @format
|
|
68
|
+
/** @format group-index */
|
|
61
69
|
chainTo: number;
|
|
62
70
|
/** @format int32 */
|
|
63
71
|
height: number;
|
|
@@ -101,6 +109,10 @@ export interface Event {
|
|
|
101
109
|
export interface ExplorerInfo {
|
|
102
110
|
releaseVersion: string;
|
|
103
111
|
commit: string;
|
|
112
|
+
/** @format int32 */
|
|
113
|
+
migrationsVersion: number;
|
|
114
|
+
/** @format int64 */
|
|
115
|
+
lastFinalizedInputTime: number;
|
|
104
116
|
}
|
|
105
117
|
export interface Hashrate {
|
|
106
118
|
/** @format int64 */
|
|
@@ -139,9 +151,9 @@ export interface LogbackValue {
|
|
|
139
151
|
export interface MempoolTransaction {
|
|
140
152
|
/** @format 32-byte-hash */
|
|
141
153
|
hash: string;
|
|
142
|
-
/** @format
|
|
154
|
+
/** @format group-index */
|
|
143
155
|
chainFrom: number;
|
|
144
|
-
/** @format
|
|
156
|
+
/** @format group-index */
|
|
145
157
|
chainTo: number;
|
|
146
158
|
inputs?: Input[];
|
|
147
159
|
outputs?: Output[];
|
|
@@ -166,9 +178,9 @@ export interface OutputRef {
|
|
|
166
178
|
export interface PendingTransaction {
|
|
167
179
|
/** @format 32-byte-hash */
|
|
168
180
|
hash: string;
|
|
169
|
-
/** @format
|
|
181
|
+
/** @format group-index */
|
|
170
182
|
chainFrom: number;
|
|
171
|
-
/** @format
|
|
183
|
+
/** @format group-index */
|
|
172
184
|
chainTo: number;
|
|
173
185
|
inputs?: Input[];
|
|
174
186
|
outputs?: Output[];
|
|
@@ -406,8 +418,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
406
418
|
* @format int32
|
|
407
419
|
*/
|
|
408
420
|
limit?: number;
|
|
409
|
-
/** Reverse pagination */
|
|
410
|
-
reverse?: boolean;
|
|
411
421
|
}, params?: RequestParams) => Promise<Transaction[]>;
|
|
412
422
|
};
|
|
413
423
|
transactions: {
|
|
@@ -447,8 +457,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
447
457
|
* @format int32
|
|
448
458
|
*/
|
|
449
459
|
limit?: number;
|
|
450
|
-
/** Reverse pagination */
|
|
451
|
-
reverse?: boolean;
|
|
452
460
|
}, params?: RequestParams) => Promise<Transaction[]>;
|
|
453
461
|
/**
|
|
454
462
|
* @description List transactions for given addresses
|
|
@@ -468,8 +476,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
468
476
|
* @format int32
|
|
469
477
|
*/
|
|
470
478
|
limit?: number;
|
|
471
|
-
/** Reverse pagination */
|
|
472
|
-
reverse?: boolean;
|
|
473
479
|
}, data?: string[], params?: RequestParams) => Promise<Transaction[]>;
|
|
474
480
|
/**
|
|
475
481
|
* @description List transactions of a given address within a time-range
|
|
@@ -499,8 +505,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
499
505
|
* @format int32
|
|
500
506
|
*/
|
|
501
507
|
limit?: number;
|
|
502
|
-
/** Reverse pagination */
|
|
503
|
-
reverse?: boolean;
|
|
504
508
|
}, params?: RequestParams) => Promise<Transaction[]>;
|
|
505
509
|
/**
|
|
506
510
|
* @description Get total transactions of a given address
|
|
@@ -544,8 +548,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
544
548
|
* @format int32
|
|
545
549
|
*/
|
|
546
550
|
limit?: number;
|
|
547
|
-
/** Reverse pagination */
|
|
548
|
-
reverse?: boolean;
|
|
549
551
|
}, params?: RequestParams) => Promise<string[]>;
|
|
550
552
|
/**
|
|
551
553
|
* @description List address tokens
|
|
@@ -565,8 +567,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
565
567
|
* @format int32
|
|
566
568
|
*/
|
|
567
569
|
limit?: number;
|
|
568
|
-
/** Reverse pagination */
|
|
569
|
-
reverse?: boolean;
|
|
570
570
|
}, params?: RequestParams) => Promise<Transaction[]>;
|
|
571
571
|
/**
|
|
572
572
|
* @description Get address balance of given token
|
|
@@ -575,7 +575,26 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
575
575
|
* @name GetAddressesAddressTokensTokenIdBalance
|
|
576
576
|
* @request GET:/addresses/{address}/tokens/{token_id}/balance
|
|
577
577
|
*/
|
|
578
|
-
getAddressesAddressTokensTokenIdBalance: (address: string, tokenId: string, params?: RequestParams) => Promise<
|
|
578
|
+
getAddressesAddressTokensTokenIdBalance: (address: string, tokenId: string, params?: RequestParams) => Promise<AddressTokenBalance>;
|
|
579
|
+
/**
|
|
580
|
+
* @description Get address tokens with balance
|
|
581
|
+
*
|
|
582
|
+
* @tags Addresses
|
|
583
|
+
* @name GetAddressesAddressTokensBalance
|
|
584
|
+
* @request GET:/addresses/{address}/tokens-balance
|
|
585
|
+
*/
|
|
586
|
+
getAddressesAddressTokensBalance: (address: string, query?: {
|
|
587
|
+
/**
|
|
588
|
+
* Page number
|
|
589
|
+
* @format int32
|
|
590
|
+
*/
|
|
591
|
+
page?: number;
|
|
592
|
+
/**
|
|
593
|
+
* Number of items per page
|
|
594
|
+
* @format int32
|
|
595
|
+
*/
|
|
596
|
+
limit?: number;
|
|
597
|
+
}, params?: RequestParams) => Promise<AddressTokenBalance[]>;
|
|
579
598
|
/**
|
|
580
599
|
* @description Are the addresses used (at least 1 transaction)
|
|
581
600
|
*
|
|
@@ -659,8 +678,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
659
678
|
* @format int32
|
|
660
679
|
*/
|
|
661
680
|
limit?: number;
|
|
662
|
-
/** Reverse pagination */
|
|
663
|
-
reverse?: boolean;
|
|
664
681
|
}, params?: RequestParams) => Promise<TokenSupply[]>;
|
|
665
682
|
/**
|
|
666
683
|
* @description Get the ALPH total supply
|
|
@@ -730,8 +747,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
730
747
|
* @format int32
|
|
731
748
|
*/
|
|
732
749
|
limit?: number;
|
|
733
|
-
/** Reverse pagination */
|
|
734
|
-
reverse?: boolean;
|
|
735
750
|
}, params?: RequestParams) => Promise<MempoolTransaction[]>;
|
|
736
751
|
};
|
|
737
752
|
tokens: {
|
|
@@ -753,8 +768,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
753
768
|
* @format int32
|
|
754
769
|
*/
|
|
755
770
|
limit?: number;
|
|
756
|
-
/** Reverse pagination */
|
|
757
|
-
reverse?: boolean;
|
|
758
771
|
}, params?: RequestParams) => Promise<string[]>;
|
|
759
772
|
/**
|
|
760
773
|
* @description List token transactions
|
|
@@ -774,8 +787,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
774
787
|
* @format int32
|
|
775
788
|
*/
|
|
776
789
|
limit?: number;
|
|
777
|
-
/** Reverse pagination */
|
|
778
|
-
reverse?: boolean;
|
|
779
790
|
}, params?: RequestParams) => Promise<Transaction[]>;
|
|
780
791
|
};
|
|
781
792
|
charts: {
|
|
@@ -870,8 +881,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
870
881
|
* @format int32
|
|
871
882
|
*/
|
|
872
883
|
limit?: number;
|
|
873
|
-
/** Reverse pagination */
|
|
874
|
-
reverse?: boolean;
|
|
875
884
|
}, params?: RequestParams) => Promise<Event[]>;
|
|
876
885
|
/**
|
|
877
886
|
* @description Get contract events by contract and input addresses
|
|
@@ -891,8 +900,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
891
900
|
* @format int32
|
|
892
901
|
*/
|
|
893
902
|
limit?: number;
|
|
894
|
-
/** Reverse pagination */
|
|
895
|
-
reverse?: boolean;
|
|
896
903
|
}, params?: RequestParams) => Promise<Event[]>;
|
|
897
904
|
};
|
|
898
905
|
contracts: {
|
|
@@ -922,8 +929,6 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
|
|
|
922
929
|
* @format int32
|
|
923
930
|
*/
|
|
924
931
|
limit?: number;
|
|
925
|
-
/** Reverse pagination */
|
|
926
|
-
reverse?: boolean;
|
|
927
932
|
}, params?: RequestParams) => Promise<SubContracts>;
|
|
928
933
|
};
|
|
929
934
|
utils: {
|
|
@@ -357,6 +357,20 @@ class Api extends HttpClient {
|
|
|
357
357
|
format: 'json',
|
|
358
358
|
...params
|
|
359
359
|
}).then(utils_1.convertHttpResponse),
|
|
360
|
+
/**
|
|
361
|
+
* @description Get address tokens with balance
|
|
362
|
+
*
|
|
363
|
+
* @tags Addresses
|
|
364
|
+
* @name GetAddressesAddressTokensBalance
|
|
365
|
+
* @request GET:/addresses/{address}/tokens-balance
|
|
366
|
+
*/
|
|
367
|
+
getAddressesAddressTokensBalance: (address, query, params = {}) => this.request({
|
|
368
|
+
path: `/addresses/${address}/tokens-balance`,
|
|
369
|
+
method: 'GET',
|
|
370
|
+
query: query,
|
|
371
|
+
format: 'json',
|
|
372
|
+
...params
|
|
373
|
+
}).then(utils_1.convertHttpResponse),
|
|
360
374
|
/**
|
|
361
375
|
* @description Are the addresses used (at least 1 transaction)
|
|
362
376
|
*
|
package/dist/src/api/types.js
CHANGED
|
@@ -51,11 +51,16 @@ function toApiNumber256(v) {
|
|
|
51
51
|
return v.toString();
|
|
52
52
|
}
|
|
53
53
|
else if (typeof v === 'string') {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
try {
|
|
55
|
+
if (BigInt(v).toString() === v) {
|
|
56
|
+
return v;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (_) {
|
|
60
|
+
throw new Error(`Invalid value: ${v}, expected a 256 bit number`);
|
|
61
|
+
}
|
|
58
62
|
}
|
|
63
|
+
throw new Error(`Invalid value: ${v}, expected a 256 bit number`);
|
|
59
64
|
}
|
|
60
65
|
exports.toApiNumber256 = toApiNumber256;
|
|
61
66
|
function toApiNumber256Optional(v) {
|
|
@@ -66,9 +71,13 @@ function fromApiNumber256(n) {
|
|
|
66
71
|
return BigInt(n);
|
|
67
72
|
}
|
|
68
73
|
exports.fromApiNumber256 = fromApiNumber256;
|
|
69
|
-
// TODO: check hex string
|
|
70
74
|
function toApiByteVec(v) {
|
|
71
|
-
if (typeof v
|
|
75
|
+
if (typeof v !== 'string') {
|
|
76
|
+
throw new Error(`Invalid value: ${v}, expected a hex-string`);
|
|
77
|
+
}
|
|
78
|
+
if ((0, utils_1.isHexString)(v))
|
|
79
|
+
return v;
|
|
80
|
+
if ((0, utils_1.isBase58)(v)) {
|
|
72
81
|
// try to convert from address to contract id
|
|
73
82
|
try {
|
|
74
83
|
const address = utils_1.bs58.decode(v);
|
|
@@ -77,13 +86,10 @@ function toApiByteVec(v) {
|
|
|
77
86
|
}
|
|
78
87
|
}
|
|
79
88
|
catch (_) {
|
|
80
|
-
|
|
89
|
+
throw new Error(`Invalid hex-string: ${v}`);
|
|
81
90
|
}
|
|
82
|
-
return v;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
throw new Error(`Invalid string: ${v}`);
|
|
86
91
|
}
|
|
92
|
+
throw new Error(`Invalid hex-string: ${v}`);
|
|
87
93
|
}
|
|
88
94
|
exports.toApiByteVec = toApiByteVec;
|
|
89
95
|
function toApiAddress(v) {
|
|
@@ -97,7 +103,7 @@ function toApiAddress(v) {
|
|
|
97
103
|
}
|
|
98
104
|
}
|
|
99
105
|
else {
|
|
100
|
-
throw new Error(`Invalid
|
|
106
|
+
throw new Error(`Invalid value: ${v}, expected a base58 string`);
|
|
101
107
|
}
|
|
102
108
|
}
|
|
103
109
|
exports.toApiAddress = toApiAddress;
|
package/dist/src/api/utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import 'cross-fetch/polyfill';
|
|
2
|
+
import { node } from '..';
|
|
2
3
|
export declare function convertHttpResponse<T>(response: {
|
|
3
4
|
status: number;
|
|
4
5
|
data: T;
|
|
@@ -6,3 +7,4 @@ export declare function convertHttpResponse<T>(response: {
|
|
|
6
7
|
detail: string;
|
|
7
8
|
};
|
|
8
9
|
}): T;
|
|
10
|
+
export declare function isBalanceEqual(b0: node.Balance, b1: node.Balance): boolean;
|
package/dist/src/api/utils.js
CHANGED
|
@@ -17,7 +17,7 @@ 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.isBalanceEqual = exports.convertHttpResponse = void 0;
|
|
21
21
|
require("cross-fetch/polyfill");
|
|
22
22
|
function convertHttpResponse(response) {
|
|
23
23
|
if (response.error) {
|
|
@@ -29,3 +29,27 @@ function convertHttpResponse(response) {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
exports.convertHttpResponse = convertHttpResponse;
|
|
32
|
+
function isBalanceEqual(b0, b1) {
|
|
33
|
+
const isTokenBalanceEqual = (tokens0, tokens1) => {
|
|
34
|
+
const tokens0Size = tokens0?.length ?? 0;
|
|
35
|
+
const tokens1Size = tokens1?.length ?? 0;
|
|
36
|
+
if (tokens0Size !== tokens1Size)
|
|
37
|
+
return false;
|
|
38
|
+
if (tokens0Size === 0)
|
|
39
|
+
return true;
|
|
40
|
+
const _tokens1 = tokens1.map((t) => ({ ...t, used: false }));
|
|
41
|
+
return tokens0.every((t0) => {
|
|
42
|
+
const t1 = _tokens1.find((t) => !t.used && t0.id === t.id && t0.amount === t.amount);
|
|
43
|
+
if (t1 === undefined)
|
|
44
|
+
return false;
|
|
45
|
+
t1.used = true;
|
|
46
|
+
return true;
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
const isAlphBalanceEqual = b0.balance === b1.balance && b0.lockedBalance === b1.lockedBalance;
|
|
50
|
+
return (b0.utxoNum === b1.utxoNum &&
|
|
51
|
+
isAlphBalanceEqual &&
|
|
52
|
+
isTokenBalanceEqual(b0.tokenBalances, b1.tokenBalances) &&
|
|
53
|
+
isTokenBalanceEqual(b0.lockedTokenBalances, b1.lockedTokenBalances));
|
|
54
|
+
}
|
|
55
|
+
exports.isBalanceEqual = isBalanceEqual;
|
|
@@ -704,7 +704,12 @@ class Contract extends Artifact {
|
|
|
704
704
|
return signerParams;
|
|
705
705
|
}
|
|
706
706
|
buildByteCodeToDeploy(initialFields) {
|
|
707
|
-
|
|
707
|
+
try {
|
|
708
|
+
return ralph.buildContractByteCode(this.bytecode, initialFields, this.fieldsSig);
|
|
709
|
+
}
|
|
710
|
+
catch (error) {
|
|
711
|
+
throw new Error(`Failed to build bytecode for contract ${this.name}, error: ${error}`);
|
|
712
|
+
}
|
|
708
713
|
}
|
|
709
714
|
static fromApiEvents(events, addressToCodeHash, txId, getContractByCodeHash) {
|
|
710
715
|
return events.map((event) => {
|
|
@@ -808,7 +813,12 @@ class Script extends Artifact {
|
|
|
808
813
|
return signerParams;
|
|
809
814
|
}
|
|
810
815
|
buildByteCodeToDeploy(initialFields) {
|
|
811
|
-
|
|
816
|
+
try {
|
|
817
|
+
return ralph.buildScriptByteCode(this.bytecodeTemplate, initialFields, this.fieldsSig);
|
|
818
|
+
}
|
|
819
|
+
catch (error) {
|
|
820
|
+
throw new Error(`Failed to build bytecode for script ${this.name}, error: ${error}`);
|
|
821
|
+
}
|
|
812
822
|
}
|
|
813
823
|
}
|
|
814
824
|
exports.Script = Script;
|
|
@@ -20,6 +20,7 @@ var _a;
|
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
21
|
exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.buildScriptByteCode = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodeAddress = exports.encodeByteVec = exports.encodeU256 = exports.encodeI256 = exports.encodeBool = void 0;
|
|
22
22
|
const buffer_1 = require("buffer/");
|
|
23
|
+
const api_1 = require("../api");
|
|
23
24
|
const utils_1 = require("../utils");
|
|
24
25
|
const bigIntZero = BigInt(0);
|
|
25
26
|
class UnSigned {
|
|
@@ -200,40 +201,24 @@ function encodeScriptFieldAsString(tpe, value) {
|
|
|
200
201
|
return buffer_1.Buffer.from(encodeScriptField(tpe, value)).toString('hex');
|
|
201
202
|
}
|
|
202
203
|
exports.encodeScriptFieldAsString = encodeScriptFieldAsString;
|
|
204
|
+
// TODO: support array type
|
|
203
205
|
function encodeScriptField(tpe, value) {
|
|
204
206
|
switch (tpe) {
|
|
205
207
|
case 'Bool':
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return new Uint8Array([byte]);
|
|
209
|
-
}
|
|
210
|
-
break;
|
|
208
|
+
const byte = (0, api_1.toApiBoolean)(value) ? Instruction.trueConst : Instruction.falseConst;
|
|
209
|
+
return new Uint8Array([byte]);
|
|
211
210
|
case 'I256':
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
else if (typeof value === 'bigint') {
|
|
216
|
-
return encodeScriptFieldI256(value);
|
|
217
|
-
}
|
|
218
|
-
break;
|
|
211
|
+
const i256 = (0, api_1.toApiNumber256)(value);
|
|
212
|
+
return encodeScriptFieldI256(BigInt(i256));
|
|
219
213
|
case 'U256':
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
else if (typeof value === 'bigint') {
|
|
224
|
-
return encodeScriptFieldU256(value);
|
|
225
|
-
}
|
|
226
|
-
break;
|
|
214
|
+
const u256 = (0, api_1.toApiNumber256)(value);
|
|
215
|
+
return encodeScriptFieldU256(BigInt(u256));
|
|
227
216
|
case 'ByteVec':
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
break;
|
|
217
|
+
const hexStr = (0, api_1.toApiByteVec)(value);
|
|
218
|
+
return new Uint8Array([Instruction.bytesConst, ...encodeByteVec(hexStr)]);
|
|
232
219
|
case 'Address':
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
break;
|
|
220
|
+
const address = (0, api_1.toApiAddress)(value);
|
|
221
|
+
return new Uint8Array([Instruction.addressConst, ...encodeAddress(address)]);
|
|
237
222
|
}
|
|
238
223
|
throw invalidScriptField(tpe, value);
|
|
239
224
|
}
|
|
@@ -245,7 +230,7 @@ function buildScriptByteCode(bytecodeTemplate, fields, fieldsSig) {
|
|
|
245
230
|
const fieldType = fieldsSig.types[`${fieldIndex}`];
|
|
246
231
|
if (fieldName in fields) {
|
|
247
232
|
const fieldValue = fields[`${fieldName}`];
|
|
248
|
-
return encodeScriptFieldAsString(fieldType, fieldValue);
|
|
233
|
+
return _encodeField(fieldName, () => encodeScriptFieldAsString(fieldType, fieldValue));
|
|
249
234
|
}
|
|
250
235
|
else {
|
|
251
236
|
throw new Error(`The value of field ${fieldName} is not provided`);
|
|
@@ -253,6 +238,17 @@ function buildScriptByteCode(bytecodeTemplate, fields, fieldsSig) {
|
|
|
253
238
|
});
|
|
254
239
|
}
|
|
255
240
|
exports.buildScriptByteCode = buildScriptByteCode;
|
|
241
|
+
function _encodeField(fieldName, encodeFunc) {
|
|
242
|
+
try {
|
|
243
|
+
return encodeFunc();
|
|
244
|
+
}
|
|
245
|
+
catch (error) {
|
|
246
|
+
if (error instanceof Error) {
|
|
247
|
+
throw new Error(`Invalid ${fieldName}, error: ${error.message}`);
|
|
248
|
+
}
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
256
252
|
function encodeFields(fields, fieldsSig, mutable) {
|
|
257
253
|
const fieldIndexes = fieldsSig.isMutable
|
|
258
254
|
.map((_, index) => index)
|
|
@@ -262,7 +258,7 @@ function encodeFields(fields, fieldsSig, mutable) {
|
|
|
262
258
|
const fieldType = fieldsSig.types[`${fieldIndex}`];
|
|
263
259
|
if (fieldName in fields) {
|
|
264
260
|
const fieldValue = fields[`${fieldName}`];
|
|
265
|
-
return encodeContractField(fieldType, fieldValue);
|
|
261
|
+
return _encodeField(fieldName, () => encodeContractField(fieldType, fieldValue));
|
|
266
262
|
}
|
|
267
263
|
else {
|
|
268
264
|
throw new Error(`The value of field ${fieldName} is not provided`);
|
|
@@ -311,47 +307,26 @@ function encodeContractFieldArray(tpe, val) {
|
|
|
311
307
|
function encodeContractField(tpe, value) {
|
|
312
308
|
switch (tpe) {
|
|
313
309
|
case 'Bool':
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
return [new Uint8Array([ApiValType.Bool, byte])];
|
|
317
|
-
}
|
|
318
|
-
break;
|
|
310
|
+
const byte = (0, api_1.toApiBoolean)(value) ? 1 : 0;
|
|
311
|
+
return [new Uint8Array([ApiValType.Bool, byte])];
|
|
319
312
|
case 'I256':
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
}
|
|
323
|
-
else if (typeof value === 'bigint') {
|
|
324
|
-
return [encodeContractFieldI256(value)];
|
|
325
|
-
}
|
|
326
|
-
break;
|
|
313
|
+
const i256 = (0, api_1.toApiNumber256)(value);
|
|
314
|
+
return [encodeContractFieldI256(BigInt(i256))];
|
|
327
315
|
case 'U256':
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
}
|
|
331
|
-
else if (typeof value === 'bigint') {
|
|
332
|
-
return [encodeContractFieldU256(value)];
|
|
333
|
-
}
|
|
334
|
-
break;
|
|
316
|
+
const u256 = (0, api_1.toApiNumber256)(value);
|
|
317
|
+
return [encodeContractFieldU256(BigInt(u256))];
|
|
335
318
|
case 'ByteVec':
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
break;
|
|
319
|
+
const hexStr = (0, api_1.toApiByteVec)(value);
|
|
320
|
+
return [new Uint8Array([ApiValType.ByteVec, ...encodeByteVec(hexStr)])];
|
|
340
321
|
case 'Address':
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
break;
|
|
322
|
+
const address = (0, api_1.toApiAddress)(value);
|
|
323
|
+
return [new Uint8Array([ApiValType.Address, ...encodeAddress(address)])];
|
|
345
324
|
default:
|
|
346
325
|
// Array type
|
|
347
326
|
return encodeContractFieldArray(tpe, value);
|
|
348
327
|
}
|
|
349
|
-
throw invalidVal(tpe, value);
|
|
350
328
|
}
|
|
351
329
|
exports.encodeContractField = encodeContractField;
|
|
352
|
-
function invalidVal(tpe, value) {
|
|
353
|
-
return Error(`Invalid API value ${value} for type ${tpe}`);
|
|
354
|
-
}
|
|
355
330
|
function buildDebugBytecode(bytecode, bytecodePatch) {
|
|
356
331
|
if (bytecodePatch === '') {
|
|
357
332
|
return bytecode;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
5
|
"license": "GPL",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"author": "Alephium dev <dev@alephium.org>",
|
|
29
29
|
"config": {
|
|
30
30
|
"alephium_version": "2.3.5",
|
|
31
|
-
"explorer_backend_version": "1.
|
|
31
|
+
"explorer_backend_version": "1.15.1"
|
|
32
32
|
},
|
|
33
33
|
"type": "commonjs",
|
|
34
34
|
"dependencies": {
|