@alephium/web3 1.4.0 → 1.5.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.
Files changed (106) hide show
  1. package/dist/alephium-web3.min.js +1 -1
  2. package/dist/alephium-web3.min.js.map +1 -1
  3. package/dist/src/address/address.js +5 -5
  4. package/dist/src/api/api-alephium.d.ts +1 -1
  5. package/dist/src/api/api-alephium.js +1 -1
  6. package/dist/src/codec/array-codec.d.ts +4 -12
  7. package/dist/src/codec/array-codec.js +15 -28
  8. package/dist/src/codec/asset-output-codec.d.ts +6 -11
  9. package/dist/src/codec/asset-output-codec.js +32 -71
  10. package/dist/src/codec/bigint-codec.d.ts +2 -1
  11. package/dist/src/codec/bigint-codec.js +14 -2
  12. package/dist/src/codec/bytestring-codec.d.ts +6 -11
  13. package/dist/src/codec/bytestring-codec.js +9 -23
  14. package/dist/src/codec/codec.d.ts +54 -5
  15. package/dist/src/codec/codec.js +112 -14
  16. package/dist/src/codec/compact-int-codec.d.ts +65 -44
  17. package/dist/src/codec/compact-int-codec.js +222 -204
  18. package/dist/src/codec/contract-codec.d.ts +5 -8
  19. package/dist/src/codec/contract-codec.js +15 -29
  20. package/dist/src/codec/contract-output-codec.d.ts +4 -10
  21. package/dist/src/codec/contract-output-codec.js +20 -40
  22. package/dist/src/codec/contract-output-ref-codec.d.ts +2 -8
  23. package/dist/src/codec/contract-output-ref-codec.js +7 -17
  24. package/dist/src/codec/either-codec.d.ts +8 -15
  25. package/dist/src/codec/either-codec.js +5 -46
  26. package/dist/src/codec/index.d.ts +4 -3
  27. package/dist/src/codec/index.js +7 -4
  28. package/dist/src/codec/input-codec.d.ts +4 -10
  29. package/dist/src/codec/input-codec.js +11 -46
  30. package/dist/src/codec/instr-codec.d.ts +633 -40
  31. package/dist/src/codec/instr-codec.js +1040 -434
  32. package/dist/src/codec/int-as-4bytes-codec.d.ts +7 -0
  33. package/dist/src/codec/{signed-int-codec.js → int-as-4bytes-codec.js} +6 -12
  34. package/dist/src/codec/lockup-script-codec.d.ts +23 -26
  35. package/dist/src/codec/lockup-script-codec.js +12 -58
  36. package/dist/src/codec/method-codec.d.ts +6 -18
  37. package/dist/src/codec/method-codec.js +20 -48
  38. package/dist/src/codec/option-codec.d.ts +8 -13
  39. package/dist/src/codec/option-codec.js +14 -32
  40. package/dist/src/codec/output-codec.d.ts +2 -2
  41. package/dist/src/codec/output-codec.js +1 -1
  42. package/dist/src/codec/reader.d.ts +8 -0
  43. package/dist/src/codec/reader.js +48 -0
  44. package/dist/src/codec/script-codec.d.ts +6 -14
  45. package/dist/src/codec/script-codec.js +6 -22
  46. package/dist/src/codec/signature-codec.d.ts +4 -12
  47. package/dist/src/codec/signature-codec.js +3 -15
  48. package/dist/src/codec/timestamp-codec.d.ts +8 -0
  49. package/dist/src/codec/timestamp-codec.js +39 -0
  50. package/dist/src/codec/token-codec.d.ts +3 -10
  51. package/dist/src/codec/token-codec.js +6 -24
  52. package/dist/src/codec/transaction-codec.d.ts +6 -11
  53. package/dist/src/codec/transaction-codec.js +24 -49
  54. package/dist/src/codec/unlock-script-codec.d.ts +25 -36
  55. package/dist/src/codec/unlock-script-codec.js +26 -147
  56. package/dist/src/codec/unsigned-tx-codec.d.ts +8 -14
  57. package/dist/src/codec/unsigned-tx-codec.js +24 -66
  58. package/dist/src/codec/val.d.ts +27 -0
  59. package/dist/src/codec/val.js +33 -0
  60. package/dist/src/contract/contract.js +6 -6
  61. package/dist/src/contract/index.d.ts +1 -0
  62. package/dist/src/contract/index.js +1 -0
  63. package/dist/src/contract/ralph.d.ts +0 -4
  64. package/dist/src/contract/ralph.js +50 -179
  65. package/dist/src/contract/script-simulator.d.ts +27 -0
  66. package/dist/src/contract/script-simulator.js +757 -0
  67. package/dist/src/exchange/exchange.js +1 -1
  68. package/package.json +3 -4
  69. package/src/address/address.ts +8 -8
  70. package/src/api/api-alephium.ts +1 -1
  71. package/src/codec/array-codec.ts +16 -34
  72. package/src/codec/asset-output-codec.ts +38 -83
  73. package/src/codec/bigint-codec.ts +16 -2
  74. package/src/codec/bytestring-codec.ts +10 -28
  75. package/src/codec/codec.ts +121 -15
  76. package/src/codec/compact-int-codec.ts +230 -207
  77. package/src/codec/contract-codec.ts +20 -33
  78. package/src/codec/contract-output-codec.ts +22 -48
  79. package/src/codec/contract-output-ref-codec.ts +6 -17
  80. package/src/codec/either-codec.ts +4 -53
  81. package/src/codec/index.ts +4 -3
  82. package/src/codec/input-codec.ts +14 -36
  83. package/src/codec/instr-codec.ts +1229 -455
  84. package/src/codec/{signed-int-codec.ts → int-as-4bytes-codec.ts} +6 -10
  85. package/src/codec/lockup-script-codec.ts +28 -76
  86. package/src/codec/method-codec.ts +23 -61
  87. package/src/codec/option-codec.ts +13 -36
  88. package/src/codec/output-codec.ts +2 -2
  89. package/src/codec/reader.ts +56 -0
  90. package/src/codec/script-codec.ts +9 -31
  91. package/src/codec/signature-codec.ts +3 -18
  92. package/src/codec/timestamp-codec.ts +42 -0
  93. package/src/codec/token-codec.ts +7 -26
  94. package/src/codec/transaction-codec.ts +29 -58
  95. package/src/codec/unlock-script-codec.ts +44 -171
  96. package/src/codec/unsigned-tx-codec.ts +34 -63
  97. package/src/codec/val.ts +40 -0
  98. package/src/contract/contract.ts +9 -13
  99. package/src/contract/index.ts +1 -0
  100. package/src/contract/ralph.ts +76 -172
  101. package/src/contract/script-simulator.ts +838 -0
  102. package/src/exchange/exchange.ts +1 -1
  103. package/dist/src/codec/long-codec.d.ts +0 -8
  104. package/dist/src/codec/long-codec.js +0 -55
  105. package/dist/src/codec/signed-int-codec.d.ts +0 -8
  106. package/src/codec/long-codec.ts +0 -58
@@ -0,0 +1,33 @@
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.valsCodec = exports.valCodec = void 0;
21
+ const compact_int_codec_1 = require("./compact-int-codec");
22
+ const bytestring_codec_1 = require("./bytestring-codec");
23
+ const codec_1 = require("./codec");
24
+ const lockup_script_codec_1 = require("./lockup-script-codec");
25
+ const array_codec_1 = require("./array-codec");
26
+ exports.valCodec = new codec_1.EnumCodec('val', {
27
+ Bool: codec_1.boolCodec,
28
+ I256: compact_int_codec_1.i256Codec,
29
+ U256: compact_int_codec_1.u256Codec,
30
+ ByteVec: bytestring_codec_1.byteStringCodec,
31
+ Address: lockup_script_codec_1.lockupScriptCodec
32
+ });
33
+ exports.valsCodec = new array_codec_1.ArrayCodec(exports.valCodec);
@@ -1100,6 +1100,9 @@ function getBytecodeTemplate(methodIndex, methodUsePreapprovedAssets, functionSi
1100
1100
  const modifier = scriptUseApprovedAssets ? '03' : '00';
1101
1101
  const argsLength = '00';
1102
1102
  const returnsLength = '00';
1103
+ if (methodUsePreapprovedAssets && !scriptUseApprovedAssets) {
1104
+ throw new Error('The contract call requires preapproved assets but none are provided');
1105
+ }
1103
1106
  const [templateVarStoreLocalInstrs, templateVarsLength] = getTemplateVarStoreLocalInstrs(functionSig, structs);
1104
1107
  const approveAlphInstrs = getApproveAlphInstrs(methodUsePreapprovedAssets ? attoAlphAmount : undefined);
1105
1108
  const approveTokensInstrs = getApproveTokensInstrs(methodUsePreapprovedAssets ? tokens : undefined);
@@ -1153,10 +1156,7 @@ function getApproveTokensInstrs(tokens) {
1153
1156
  if (tokens) {
1154
1157
  tokens.forEach((token) => {
1155
1158
  const tokenIdBin = (0, utils_1.hexToBinUnsafe)(token.id);
1156
- approveTokensInstrs.push(encodeInstr((0, codec_1.ByteConst)({
1157
- length: codec_1.compactSignedIntCodec.fromI32(tokenIdBin.length),
1158
- value: tokenIdBin
1159
- })));
1159
+ approveTokensInstrs.push(encodeInstr((0, codec_1.BytesConst)(tokenIdBin)));
1160
1160
  approveTokensInstrs.push(encodeU256Const(BigInt(token.amount)));
1161
1161
  approveTokensInstrs.push(encodeInstr(codec_1.ApproveToken));
1162
1162
  });
@@ -1225,7 +1225,7 @@ function encodeLoadLocalInstr(index) {
1225
1225
  return encodeInstr((0, codec_1.LoadLocal)(index));
1226
1226
  }
1227
1227
  function encodeI32(value) {
1228
- return (0, utils_1.binToHex)(codec_1.compactSignedIntCodec.encodeI32(value));
1228
+ return (0, utils_1.binToHex)(codec_1.i32Codec.encode(value));
1229
1229
  }
1230
1230
  function encodeU256Const(value) {
1231
1231
  if (value < 0) {
@@ -1235,7 +1235,7 @@ function encodeU256Const(value) {
1235
1235
  return (BigInt(0x0c) + value).toString(16).padStart(2, '0');
1236
1236
  }
1237
1237
  else {
1238
- return encodeInstr((0, codec_1.U256Const)(codec_1.compactUnsignedIntCodec.fromU256(BigInt(value))));
1238
+ return encodeInstr((0, codec_1.U256Const)(value));
1239
1239
  }
1240
1240
  }
1241
1241
  function encodeInstr(instr) {
@@ -1,3 +1,4 @@
1
1
  export * from './ralph';
2
2
  export * from './contract';
3
3
  export * from './events';
4
+ export * from './script-simulator';
@@ -34,3 +34,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
34
34
  __exportStar(require("./ralph"), exports);
35
35
  __exportStar(require("./contract"), exports);
36
36
  __exportStar(require("./events"), exports);
37
+ __exportStar(require("./script-simulator"), exports);
@@ -1,10 +1,6 @@
1
1
  import { Val } from '../api';
2
2
  import { HexString } from '../utils';
3
3
  import { Fields, FieldsSig, Struct } from './contract';
4
- export declare function encodeBool(bool: boolean): Uint8Array;
5
- export declare function decodeBool(bytes: Uint8Array): boolean;
6
- export declare function encodeI256(i256: bigint): Uint8Array;
7
- export declare function encodeU256(u256: bigint): Uint8Array;
8
4
  export declare function encodeByteVec(hex: string): Uint8Array;
9
5
  export declare function encodeAddress(address: string): Uint8Array;
10
6
  export declare enum VmValType {
@@ -16,155 +16,18 @@ GNU Lesser General Public License for more details.
16
16
  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
- var _a;
20
19
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.flattenFields = exports.typeLength = exports.encodeMapKey = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = exports.encodeU256 = exports.encodeI256 = exports.decodeBool = exports.encodeBool = void 0;
20
+ exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.flattenFields = exports.typeLength = exports.encodeMapKey = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = void 0;
22
21
  const api_1 = require("../api");
23
22
  const utils_1 = require("../utils");
24
23
  const codec_1 = require("../codec");
25
- const bigIntZero = BigInt(0);
26
- class UnSigned {
27
- }
28
- UnSigned.oneByteBound = BigInt(0x40);
29
- UnSigned.twoByteBound = UnSigned.oneByteBound << BigInt(8);
30
- UnSigned.fourByteBound = UnSigned.oneByteBound << BigInt(8 * 3);
31
- UnSigned.u256UpperBound = BigInt(1) << BigInt(256);
32
- class Signed {
33
- }
34
- _a = Signed;
35
- Signed.oneByteBound = BigInt(0x20);
36
- Signed.twoByteBound = Signed.oneByteBound << BigInt(8);
37
- Signed.fourByteBound = Signed.oneByteBound << BigInt(8 * 3);
38
- Signed.i256UpperBound = BigInt(1) << BigInt(255);
39
- Signed.i256LowerBound = -_a.i256UpperBound;
40
- class CompactInt {
41
- }
42
- CompactInt.oneBytePrefix = 0x00;
43
- CompactInt.oneByteNegPrefix = 0xc0;
44
- CompactInt.twoBytePrefix = 0x40;
45
- CompactInt.twoByteNegPrefix = 0x80;
46
- CompactInt.fourBytePrefix = 0x80;
47
- CompactInt.fourByteNegPrefix = 0x40;
48
- CompactInt.multiBytePrefix = 0xc0;
49
- function encodeBool(bool) {
50
- return bool ? Uint8Array.from([1]) : Uint8Array.from([0]);
51
- }
52
- exports.encodeBool = encodeBool;
53
- function decodeBool(bytes) {
54
- if (bytes.length !== 1) {
55
- throw new Error(`Expected one byte for encoded bool, got ${bytes.length}`);
56
- }
57
- return bytes[0] === 1 ? true : false;
58
- }
59
- exports.decodeBool = decodeBool;
60
- function encodeI256(i256) {
61
- if (i256 >= bigIntZero) {
62
- return encodeI256Positive(i256);
63
- }
64
- else {
65
- return encodeI256Negative(i256);
66
- }
67
- }
68
- exports.encodeI256 = encodeI256;
69
- // n should be positive
70
- function toByteArray(n, signed, notBit) {
71
- let hex = n.toString(16);
72
- if (hex.length % 2 === 1) {
73
- hex = '0' + hex;
74
- }
75
- else if (signed && hex[0] >= '8') {
76
- hex = '00' + hex; // add the byte for sign
77
- }
78
- const byteLength = hex.length / 2;
79
- const bytes = new Uint8Array(byteLength + 1);
80
- for (let index = 0; index < byteLength; index++) {
81
- const offset = index * 2;
82
- const byte = parseInt(hex.slice(offset, offset + 2), 16);
83
- bytes[`${index + 1}`] = notBit ? ~byte : byte;
84
- }
85
- const header = byteLength - 4 + CompactInt.multiBytePrefix;
86
- bytes[0] = header;
87
- return bytes;
88
- }
89
- function encodeI256Positive(i256) {
90
- if (i256 < Signed.oneByteBound) {
91
- return new Uint8Array([Number(i256) + CompactInt.oneBytePrefix]);
92
- }
93
- else if (i256 < Signed.twoByteBound) {
94
- const num = Number(i256);
95
- return new Uint8Array([(num >> 8) + CompactInt.twoBytePrefix, num & 0xff]);
96
- }
97
- else if (i256 < Signed.fourByteBound) {
98
- const num = Number(i256);
99
- return new Uint8Array([(num >> 24) + CompactInt.fourBytePrefix, (num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff]);
100
- }
101
- else if (i256 < Signed.i256UpperBound) {
102
- return toByteArray(i256, true, false);
103
- }
104
- else {
105
- throw Error(`Too large number for i256: ${i256}`);
106
- }
107
- }
108
- function encodeI256Negative(i256) {
109
- if (i256 >= -Signed.oneByteBound) {
110
- const num = Number(i256);
111
- return new Uint8Array([(num ^ CompactInt.oneByteNegPrefix) & 0xff]);
112
- }
113
- else if (i256 >= -Signed.twoByteBound) {
114
- const num = Number(i256);
115
- return new Uint8Array([((num >> 8) ^ CompactInt.twoByteNegPrefix) & 0xff, num & 0xff]);
116
- }
117
- else if (i256 >= -Signed.fourByteBound) {
118
- const num = Number(i256);
119
- return new Uint8Array([
120
- ((num >> 24) ^ CompactInt.fourByteNegPrefix) & 0xff,
121
- (num >> 16) & 0xff,
122
- (num >> 8) & 0xff,
123
- num & 0xff
124
- ]);
125
- }
126
- else if (i256 >= Signed.i256LowerBound) {
127
- return toByteArray(~i256, true, true);
128
- }
129
- else {
130
- throw Error(`Too small number for i256: ${i256}`);
131
- }
132
- }
133
- function encodeU256(u256) {
134
- if (u256 < bigIntZero) {
135
- throw Error(`Negative number for U256: ${u256}`);
136
- }
137
- else if (u256 < UnSigned.oneByteBound) {
138
- return new Uint8Array([Number(u256) + CompactInt.oneBytePrefix]);
139
- }
140
- else if (u256 < UnSigned.twoByteBound) {
141
- const num = Number(u256);
142
- return new Uint8Array([((num >> 8) & 0xff) + CompactInt.twoBytePrefix, num & 0xff]);
143
- }
144
- else if (u256 < UnSigned.fourByteBound) {
145
- const num = Number(u256);
146
- return new Uint8Array([
147
- ((num >> 24) & 0xff) + CompactInt.fourBytePrefix,
148
- (num >> 16) & 0xff,
149
- (num >> 8) & 0xff,
150
- num & 0xff
151
- ]);
152
- }
153
- else if (u256 < UnSigned.u256UpperBound) {
154
- return toByteArray(u256, false, false);
155
- }
156
- else {
157
- throw Error(`Too large number for U256: ${u256}`);
158
- }
159
- }
160
- exports.encodeU256 = encodeU256;
24
+ const codec_2 = require("../codec/codec");
161
25
  function encodeByteVec(hex) {
162
26
  if (!(0, utils_1.isHexString)(hex)) {
163
27
  throw Error(`Given value ${hex} is not a valid hex string`);
164
28
  }
165
- const bytes0 = (0, utils_1.hexToBinUnsafe)(hex);
166
- const bytes1 = encodeI256(BigInt(bytes0.length));
167
- return (0, utils_1.concatBytes)([bytes1, bytes0]);
29
+ const bytes = (0, utils_1.hexToBinUnsafe)(hex);
30
+ return codec_1.byteStringCodec.encode(bytes);
168
31
  }
169
32
  exports.encodeByteVec = encodeByteVec;
170
33
  function encodeAddress(address) {
@@ -180,15 +43,15 @@ var VmValType;
180
43
  VmValType[VmValType["Address"] = 4] = "Address";
181
44
  })(VmValType = exports.VmValType || (exports.VmValType = {}));
182
45
  function encodeVmBool(bool) {
183
- return new Uint8Array([VmValType.Bool, ...encodeBool(bool)]);
46
+ return new Uint8Array([VmValType.Bool, ...codec_2.boolCodec.encode(bool)]);
184
47
  }
185
48
  exports.encodeVmBool = encodeVmBool;
186
49
  function encodeVmI256(i256) {
187
- return new Uint8Array([VmValType.I256, ...encodeI256(i256)]);
50
+ return new Uint8Array([VmValType.I256, ...codec_1.i256Codec.encode(i256)]);
188
51
  }
189
52
  exports.encodeVmI256 = encodeVmI256;
190
53
  function encodeVmU256(u256) {
191
- return new Uint8Array([VmValType.U256, ...encodeU256(u256)]);
54
+ return new Uint8Array([VmValType.U256, ...codec_1.u256Codec.encode(u256)]);
192
55
  }
193
56
  exports.encodeVmU256 = encodeVmU256;
194
57
  function encodeVmByteVec(bytes) {
@@ -226,35 +89,43 @@ exports.encodePrimitiveValues = encodePrimitiveValues;
226
89
  function invalidScriptField(tpe, value) {
227
90
  return Error(`Invalid script field ${value} for type ${tpe}`);
228
91
  }
229
- var Instruction;
230
- (function (Instruction) {
231
- Instruction[Instruction["trueConst"] = 3] = "trueConst";
232
- Instruction[Instruction["falseConst"] = 4] = "falseConst";
233
- Instruction[Instruction["i256Const0"] = 5] = "i256Const0";
234
- Instruction[Instruction["i256Const1"] = 6] = "i256Const1";
235
- Instruction[Instruction["i256Const2"] = 7] = "i256Const2";
236
- Instruction[Instruction["i256Const3"] = 8] = "i256Const3";
237
- Instruction[Instruction["i256Const4"] = 9] = "i256Const4";
238
- Instruction[Instruction["i256Const5"] = 10] = "i256Const5";
239
- Instruction[Instruction["i256ConstN1"] = 11] = "i256ConstN1";
240
- Instruction[Instruction["u256Const0"] = 12] = "u256Const0";
241
- Instruction[Instruction["u256Const1"] = 13] = "u256Const1";
242
- Instruction[Instruction["u256Const2"] = 14] = "u256Const2";
243
- Instruction[Instruction["u256Const3"] = 15] = "u256Const3";
244
- Instruction[Instruction["u256Const4"] = 16] = "u256Const4";
245
- Instruction[Instruction["u256Const5"] = 17] = "u256Const5";
246
- Instruction[Instruction["i256Const"] = 18] = "i256Const";
247
- Instruction[Instruction["u256Const"] = 19] = "u256Const";
248
- Instruction[Instruction["bytesConst"] = 20] = "bytesConst";
249
- Instruction[Instruction["addressConst"] = 21] = "addressConst";
250
- })(Instruction || (Instruction = {}));
251
- // TODO: optimize
252
92
  function encodeScriptFieldI256(value) {
253
- return new Uint8Array([Instruction.i256Const, ...encodeI256(value)]);
93
+ switch (value) {
94
+ case 0n:
95
+ return codec_1.instrCodec.encode(codec_1.I256Const0);
96
+ case 1n:
97
+ return codec_1.instrCodec.encode(codec_1.I256Const1);
98
+ case 2n:
99
+ return codec_1.instrCodec.encode(codec_1.I256Const2);
100
+ case 3n:
101
+ return codec_1.instrCodec.encode(codec_1.I256Const3);
102
+ case 4n:
103
+ return codec_1.instrCodec.encode(codec_1.I256Const4);
104
+ case 5n:
105
+ return codec_1.instrCodec.encode(codec_1.I256Const5);
106
+ case -1n:
107
+ return codec_1.instrCodec.encode(codec_1.I256ConstN1);
108
+ default:
109
+ return codec_1.instrCodec.encode((0, codec_1.I256Const)(value));
110
+ }
254
111
  }
255
- // TODO: optimize
256
112
  function encodeScriptFieldU256(value) {
257
- return new Uint8Array([Instruction.u256Const, ...encodeU256(value)]);
113
+ switch (value) {
114
+ case 0n:
115
+ return codec_1.instrCodec.encode(codec_1.U256Const0);
116
+ case 1n:
117
+ return codec_1.instrCodec.encode(codec_1.U256Const1);
118
+ case 2n:
119
+ return codec_1.instrCodec.encode(codec_1.U256Const2);
120
+ case 3n:
121
+ return codec_1.instrCodec.encode(codec_1.U256Const3);
122
+ case 4n:
123
+ return codec_1.instrCodec.encode(codec_1.U256Const4);
124
+ case 5n:
125
+ return codec_1.instrCodec.encode(codec_1.U256Const5);
126
+ default:
127
+ return codec_1.instrCodec.encode((0, codec_1.U256Const)(value));
128
+ }
258
129
  }
259
130
  function encodeScriptFieldAsString(tpe, value) {
260
131
  return (0, utils_1.binToHex)(encodeScriptField(tpe, value));
@@ -263,7 +134,7 @@ exports.encodeScriptFieldAsString = encodeScriptFieldAsString;
263
134
  function encodeScriptField(tpe, value) {
264
135
  switch (tpe) {
265
136
  case 'Bool':
266
- const byte = (0, api_1.toApiBoolean)(value) ? Instruction.trueConst : Instruction.falseConst;
137
+ const byte = (0, api_1.toApiBoolean)(value) ? codec_1.ConstTrue.code : codec_1.ConstFalse.code;
267
138
  return new Uint8Array([byte]);
268
139
  case 'I256':
269
140
  const i256 = (0, api_1.toApiNumber256)(value);
@@ -273,10 +144,10 @@ function encodeScriptField(tpe, value) {
273
144
  return encodeScriptFieldU256(BigInt(u256));
274
145
  case 'Address':
275
146
  const address = (0, api_1.toApiAddress)(value);
276
- return new Uint8Array([Instruction.addressConst, ...encodeAddress(address)]);
147
+ return new Uint8Array([codec_1.AddressConstCode, ...encodeAddress(address)]);
277
148
  default: // ByteVec or Contract
278
149
  const hexStr = (0, api_1.toApiByteVec)(value);
279
- return new Uint8Array([Instruction.bytesConst, ...encodeByteVec(hexStr)]);
150
+ return new Uint8Array([codec_1.BytesConstCode, ...encodeByteVec(hexStr)]);
280
151
  }
281
152
  throw invalidScriptField(tpe, value);
282
153
  }
@@ -366,11 +237,11 @@ exports.tryDecodeMapDebugLog = tryDecodeMapDebugLog;
366
237
  function decodePrimitive(value, type) {
367
238
  switch (type) {
368
239
  case 'Bool':
369
- return decodeBool(value);
240
+ return codec_2.boolCodec.decode(value);
370
241
  case 'I256':
371
- return codec_1.compactSignedIntCodec.decodeI256(value);
242
+ return codec_1.i256Codec.decode(value);
372
243
  case 'U256':
373
- return codec_1.compactUnsignedIntCodec.decodeU256(value);
244
+ return codec_1.u256Codec.decode(value);
374
245
  case 'ByteVec':
375
246
  return (0, utils_1.binToHex)(value);
376
247
  case 'Address':
@@ -387,10 +258,10 @@ function encodeMapKey(value, type) {
387
258
  return new Uint8Array([byte]);
388
259
  case 'I256':
389
260
  const i256 = (0, api_1.toApiNumber256)(value);
390
- return encodeI256(BigInt(i256));
261
+ return codec_1.i256Codec.encode(BigInt(i256));
391
262
  case 'U256':
392
263
  const u256 = (0, api_1.toApiNumber256)(value);
393
- return encodeU256(BigInt(u256));
264
+ return codec_1.u256Codec.encode(BigInt(u256));
394
265
  case 'ByteVec':
395
266
  const hexStr = (0, api_1.toApiByteVec)(value);
396
267
  return (0, utils_1.hexToBinUnsafe)(hexStr);
@@ -493,7 +364,7 @@ function _encodeField(fieldName, encodeFunc) {
493
364
  }
494
365
  }
495
366
  function encodeFields(fields) {
496
- const prefix = encodeI256(BigInt(fields.length));
367
+ const prefix = codec_1.i32Codec.encode(fields.length);
497
368
  return fields.reduce((acc, field) => {
498
369
  const encoded = _encodeField(field.name, () => encodeContractField(field.type, field.value));
499
370
  const bytes = new Uint8Array(acc.byteLength + encoded.byteLength);
@@ -0,0 +1,27 @@
1
+ import { Token } from '../api';
2
+ import { Method } from '../codec';
3
+ import { Script } from '../codec/script-codec';
4
+ import { HexString } from '../utils';
5
+ /**
6
+ * Contract call extracted from a script
7
+ * @param contractAddress the address of the contract
8
+ * @param approvedAttoAlphAmount the amount of ALPH approved to the contract
9
+ * - undefined if no ALPH is approved
10
+ * - 'unknown' if the amount cannot be determined
11
+ * - a number if the amount is known
12
+ * @param approvedTokens the tokens approved to the contract
13
+ * - undefined if no tokens are approved
14
+ * - 'unknown' if the tokens cannot be determined
15
+ * - an array of tokens if the tokens are known
16
+ */
17
+ export interface ContractCall {
18
+ contractAddress: string;
19
+ approvedAttoAlphAmount?: bigint | 'unknown';
20
+ approvedTokens?: Token[] | 'unknown';
21
+ }
22
+ export declare class ScriptSimulator {
23
+ static extractContractCalls(unsignedTx: HexString): ContractCall[];
24
+ static extractContractCallsWithErrors(unsignedTx: HexString): ContractCall[];
25
+ static extractContractCallsFromScript(script: Script): ContractCall[];
26
+ static extractContractCallsFromMainMethod(mainMethod: Method): ContractCall[];
27
+ }