envio 2.17.0 → 2.17.1
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/package.json +5 -5
- package/src/ContractAddressingMap.res +115 -0
- package/src/ErrorHandling.res +61 -0
- package/src/EventUtils.res +87 -0
- package/src/Internal.res +2 -0
- package/src/LoadManager.res +174 -0
- package/src/Logging.res +179 -0
- package/src/Time.res +41 -0
- package/src/Utils.res +15 -0
- package/src/db/EntityHistory.res +4 -4
- package/src/db/Table.res +5 -2
- package/src/sources/Fuel.res +37 -0
- package/src/sources/HyperFuel.res +260 -0
- package/src/sources/HyperFuel.resi +59 -0
- package/src/sources/HyperFuelClient.res +408 -0
- package/src/sources/HyperSync.res +349 -0
- package/src/sources/HyperSync.resi +69 -0
- package/src/sources/vendored-fuel-abi-coder.js +1847 -0
|
@@ -0,0 +1,1847 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Copyright (C) Fuel Labs <contact@fuel.sh> (https://fuel.network/)
|
|
4
|
+
|
|
5
|
+
// This is a vendored compiled file from @fuel-ts/abi-coder@0.86.0 package
|
|
6
|
+
// We want to change a few decoding functions, to avoid having our own decoders
|
|
7
|
+
// and optimise the decodign process as much as possible.
|
|
8
|
+
// As a more maintainable option we might consider forking the repo and publishing
|
|
9
|
+
// a patched version under the @envio-dev org.
|
|
10
|
+
// Initially I've tried using pnpm patch,
|
|
11
|
+
// but it didn't work for us, since it include patch in the root of the repo, which is user's indexer.
|
|
12
|
+
|
|
13
|
+
// Here's the list of the changes:
|
|
14
|
+
// 1. Changed enum decoder to always return data in a format {case: <Variant name>, payload: <Payload data>}.
|
|
15
|
+
// Where Payload data should be unit if it's not provided.
|
|
16
|
+
// 1.1. Adjust OptionCoder to return variant with payload instead of T | undefined
|
|
17
|
+
// 2. Changed BigNumberCoder to return BigInt instead of BN.js
|
|
18
|
+
// 3. Exposed AbiCoder and added getLogDecoder static method, to do all prep work once
|
|
19
|
+
// 4. Added transpileAbi function to convert json abi to old fuel abi
|
|
20
|
+
|
|
21
|
+
// Here's the generated diff from pnpm patch
|
|
22
|
+
|
|
23
|
+
// diff --git a/dist/index.js b/dist/index.js
|
|
24
|
+
// index bbb0bdfd7d506d311d2160f81b22a59ded3e4f70..653e0b400358e2e67c84c27bb2e120a2e4115717 100644
|
|
25
|
+
// --- a/dist/index.js
|
|
26
|
+
// +++ b/dist/index.js
|
|
27
|
+
// @@ -25,6 +25,7 @@ var __publicField = (obj, key, value) => {
|
|
28
|
+
// // src/index.ts
|
|
29
|
+
// var src_exports = {};
|
|
30
|
+
// __export(src_exports, {
|
|
31
|
+
// + AbiCoder: () => AbiCoder,
|
|
32
|
+
// + transpileAbi: () => transpileAbi,
|
|
33
|
+
// ASSET_ID_LEN: () => ASSET_ID_LEN,
|
|
34
|
+
// ArrayCoder: () => ArrayCoder,
|
|
35
|
+
// B256Coder: () => B256Coder,
|
|
36
|
+
// @@ -272,7 +273,7 @@ var BigNumberCoder = class extends Coder {
|
|
37
|
+
// if (bytes.length !== this.encodedLength) {
|
|
38
|
+
// throw new import_errors4.FuelError(import_errors4.ErrorCode.DECODE_ERROR, `Invalid ${this.type} byte data size.`);
|
|
39
|
+
// }
|
|
40
|
+
// - return [(0, import_math3.bn)(bytes), offset + this.encodedLength];
|
|
41
|
+
// + return [BigInt(import_math3.bn(bytes)), offset + this.encodedLength];
|
|
42
|
+
// }
|
|
43
|
+
// };
|
|
44
|
+
|
|
45
|
+
// @@ -408,7 +409,7 @@ var EnumCoder = class extends Coder {
|
|
46
|
+
// - if (this.#isNativeEnum(this.coders[caseKey])) {
|
|
47
|
+
// - return this.#decodeNativeEnum(caseKey, newOffset);
|
|
48
|
+
// - }
|
|
49
|
+
// - return [{ [caseKey]: decoded }, newOffset];
|
|
50
|
+
// + return [{ case: caseKey, payload: decoded }, newOffset];
|
|
51
|
+
// }
|
|
52
|
+
// };
|
|
53
|
+
|
|
54
|
+
// @@ -1035,6 +1036,20 @@ var AbiCoder = class {
|
|
55
|
+
// const resolvedAbiType = new ResolvedAbiType(abi, argument);
|
|
56
|
+
// return getCoderForEncoding(options.encoding)(resolvedAbiType, options);
|
|
57
|
+
// }
|
|
58
|
+
// + static getLogDecoder(abi, logId, options = {
|
|
59
|
+
// + padToWordSize: false
|
|
60
|
+
// + }) {
|
|
61
|
+
// + const loggedType = abi.loggedTypes.find((type) => type.logId === logId);
|
|
62
|
+
// + if (!loggedType) {
|
|
63
|
+
// + throw new import_errors20.FuelError(
|
|
64
|
+
// + import_errors20.ErrorCode.LOG_TYPE_NOT_FOUND,
|
|
65
|
+
// + `Log type with logId '${logId}' doesn't exist in the ABI.`
|
|
66
|
+
// + );
|
|
67
|
+
// + }
|
|
68
|
+
// + const resolvedAbiType = new ResolvedAbiType(abi, loggedType.loggedType);
|
|
69
|
+
// + const internalCoder = getCoderForEncoding(options.encoding)(resolvedAbiType, options);
|
|
70
|
+
// + return (data) => internalCoder.decode(import_utils12.arrayify(data), 0)[0];
|
|
71
|
+
// + }
|
|
72
|
+
// static encode(abi, argument, value, options) {
|
|
73
|
+
// return this.getCoder(abi, argument, options).encode(value);
|
|
74
|
+
// }
|
|
75
|
+
// @@ -1239,8 +1254,10 @@ var Interface = class {
|
|
76
|
+
// return findTypeById(this.jsonAbi, typeId);
|
|
77
|
+
// }
|
|
78
|
+
// };
|
|
79
|
+
// +
|
|
80
|
+
// // Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
// 0 && (module.exports = {
|
|
82
|
+
// + transpileAbi,
|
|
83
|
+
// + AbiCoder,
|
|
84
|
+
// ASSET_ID_LEN,
|
|
85
|
+
// ArrayCoder,
|
|
86
|
+
// B256Coder,
|
|
87
|
+
|
|
88
|
+
var __defProp = Object.defineProperty;
|
|
89
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
90
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
91
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
92
|
+
var __defNormalProp = (obj, key, value) =>
|
|
93
|
+
key in obj
|
|
94
|
+
? __defProp(obj, key, {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
configurable: true,
|
|
97
|
+
writable: true,
|
|
98
|
+
value,
|
|
99
|
+
})
|
|
100
|
+
: (obj[key] = value);
|
|
101
|
+
var __export = (target, all) => {
|
|
102
|
+
for (var name in all)
|
|
103
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
104
|
+
};
|
|
105
|
+
var __copyProps = (to, from, except, desc) => {
|
|
106
|
+
if ((from && typeof from === "object") || typeof from === "function") {
|
|
107
|
+
for (let key of __getOwnPropNames(from))
|
|
108
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
109
|
+
__defProp(to, key, {
|
|
110
|
+
get: () => from[key],
|
|
111
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return to;
|
|
115
|
+
};
|
|
116
|
+
var __toCommonJS = (mod) =>
|
|
117
|
+
__copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
118
|
+
var __publicField = (obj, key, value) => {
|
|
119
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
120
|
+
return value;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// src/index.ts
|
|
124
|
+
var src_exports = {};
|
|
125
|
+
__export(src_exports, {
|
|
126
|
+
ASSET_ID_LEN: () => ASSET_ID_LEN,
|
|
127
|
+
AbiCoder: () => AbiCoder,
|
|
128
|
+
transpileAbi: () => transpileAbi,
|
|
129
|
+
ArrayCoder: () => ArrayCoder,
|
|
130
|
+
B256Coder: () => B256Coder,
|
|
131
|
+
B512Coder: () => B512Coder,
|
|
132
|
+
BYTES_32: () => BYTES_32,
|
|
133
|
+
BigNumberCoder: () => BigNumberCoder,
|
|
134
|
+
BooleanCoder: () => BooleanCoder,
|
|
135
|
+
ByteCoder: () => ByteCoder,
|
|
136
|
+
CONTRACT_ID_LEN: () => CONTRACT_ID_LEN,
|
|
137
|
+
Coder: () => Coder,
|
|
138
|
+
ENCODING_V1: () => ENCODING_V1,
|
|
139
|
+
EnumCoder: () => EnumCoder,
|
|
140
|
+
INPUT_COIN_FIXED_SIZE: () => INPUT_COIN_FIXED_SIZE,
|
|
141
|
+
Interface: () => Interface,
|
|
142
|
+
NumberCoder: () => NumberCoder,
|
|
143
|
+
OptionCoder: () => OptionCoder,
|
|
144
|
+
RawSliceCoder: () => RawSliceCoder,
|
|
145
|
+
SCRIPT_FIXED_SIZE: () => SCRIPT_FIXED_SIZE,
|
|
146
|
+
StdStringCoder: () => StdStringCoder,
|
|
147
|
+
StrSliceCoder: () => StrSliceCoder,
|
|
148
|
+
StringCoder: () => StringCoder,
|
|
149
|
+
StructCoder: () => StructCoder,
|
|
150
|
+
TupleCoder: () => TupleCoder,
|
|
151
|
+
UTXO_ID_LEN: () => UTXO_ID_LEN,
|
|
152
|
+
VecCoder: () => VecCoder,
|
|
153
|
+
WORD_SIZE: () => WORD_SIZE,
|
|
154
|
+
calculateVmTxMemory: () => calculateVmTxMemory,
|
|
155
|
+
});
|
|
156
|
+
module.exports = __toCommonJS(src_exports);
|
|
157
|
+
|
|
158
|
+
// src/encoding/coders/AbstractCoder.ts
|
|
159
|
+
var Coder = class {
|
|
160
|
+
name;
|
|
161
|
+
type;
|
|
162
|
+
encodedLength;
|
|
163
|
+
constructor(name, type, encodedLength) {
|
|
164
|
+
this.name = name;
|
|
165
|
+
this.type = type;
|
|
166
|
+
this.encodedLength = encodedLength;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// src/encoding/coders/ArrayCoder.ts
|
|
171
|
+
var import_errors = require("@fuel-ts/errors");
|
|
172
|
+
var import_utils = require("@fuel-ts/utils");
|
|
173
|
+
|
|
174
|
+
// src/utils/constants.ts
|
|
175
|
+
var U8_CODER_TYPE = "u8";
|
|
176
|
+
var U16_CODER_TYPE = "u16";
|
|
177
|
+
var U32_CODER_TYPE = "u32";
|
|
178
|
+
var U64_CODER_TYPE = "u64";
|
|
179
|
+
var U256_CODER_TYPE = "u256";
|
|
180
|
+
var RAW_PTR_CODER_TYPE = "raw untyped ptr";
|
|
181
|
+
var RAW_SLICE_CODER_TYPE = "raw untyped slice";
|
|
182
|
+
var BOOL_CODER_TYPE = "bool";
|
|
183
|
+
var B256_CODER_TYPE = "b256";
|
|
184
|
+
var B512_CODER_TYPE = "struct std::b512::B512";
|
|
185
|
+
var OPTION_CODER_TYPE = "enum std::option::Option";
|
|
186
|
+
var VEC_CODER_TYPE = "struct std::vec::Vec";
|
|
187
|
+
var BYTES_CODER_TYPE = "struct std::bytes::Bytes";
|
|
188
|
+
var STD_STRING_CODER_TYPE = "struct std::string::String";
|
|
189
|
+
var STR_SLICE_CODER_TYPE = "str";
|
|
190
|
+
var VOID_TYPE = "()";
|
|
191
|
+
var optionRegEx = /^enum (std::option::)?Option$/m;
|
|
192
|
+
var stringRegEx = /^str\[(?<length>[0-9]+)\]/;
|
|
193
|
+
var arrayRegEx = /^\[(?<item>[\w\s\\[\]]+);\s*(?<length>[0-9]+)\]/;
|
|
194
|
+
var structRegEx = /^struct.+/;
|
|
195
|
+
var enumRegEx = /^enum.+$/;
|
|
196
|
+
var tupleRegEx = /^\((?<items>.*)\)$/;
|
|
197
|
+
var genericRegEx = /^generic.+$/;
|
|
198
|
+
var fullNameRegExMatch = /([^\s]+)$/m;
|
|
199
|
+
var ENCODING_V1 = "1";
|
|
200
|
+
var WORD_SIZE = 8;
|
|
201
|
+
var BYTES_32 = 32;
|
|
202
|
+
var UTXO_ID_LEN = BYTES_32 + 2;
|
|
203
|
+
var ASSET_ID_LEN = BYTES_32;
|
|
204
|
+
var CONTRACT_ID_LEN = BYTES_32;
|
|
205
|
+
var ADDRESS_LEN = BYTES_32;
|
|
206
|
+
var NONCE_LEN = BYTES_32;
|
|
207
|
+
var TX_LEN = WORD_SIZE * 4;
|
|
208
|
+
var TX_POINTER_LEN = WORD_SIZE * 2;
|
|
209
|
+
var MAX_BYTES = 2 ** 32 - 1;
|
|
210
|
+
var calculateVmTxMemory = ({ maxInputs }) =>
|
|
211
|
+
BYTES_32 + // Tx ID
|
|
212
|
+
ASSET_ID_LEN + // Base asset ID
|
|
213
|
+
// Asset ID/Balance coin input pairs
|
|
214
|
+
maxInputs * (ASSET_ID_LEN + WORD_SIZE) +
|
|
215
|
+
WORD_SIZE;
|
|
216
|
+
var SCRIPT_FIXED_SIZE =
|
|
217
|
+
WORD_SIZE + // Identifier
|
|
218
|
+
WORD_SIZE + // Gas limit
|
|
219
|
+
WORD_SIZE + // Script size
|
|
220
|
+
WORD_SIZE + // Script data size
|
|
221
|
+
WORD_SIZE + // Policies
|
|
222
|
+
WORD_SIZE + // Inputs size
|
|
223
|
+
WORD_SIZE + // Outputs size
|
|
224
|
+
WORD_SIZE + // Witnesses size
|
|
225
|
+
BYTES_32;
|
|
226
|
+
var INPUT_COIN_FIXED_SIZE =
|
|
227
|
+
WORD_SIZE + // Identifier
|
|
228
|
+
TX_LEN + // Utxo Length
|
|
229
|
+
WORD_SIZE + // Output Index
|
|
230
|
+
ADDRESS_LEN + // Owner
|
|
231
|
+
WORD_SIZE + // Amount
|
|
232
|
+
ASSET_ID_LEN + // Asset id
|
|
233
|
+
TX_POINTER_LEN + // TxPointer
|
|
234
|
+
WORD_SIZE + // Witnesses index
|
|
235
|
+
WORD_SIZE + // Predicate size
|
|
236
|
+
WORD_SIZE + // Predicate data size
|
|
237
|
+
WORD_SIZE;
|
|
238
|
+
var INPUT_MESSAGE_FIXED_SIZE =
|
|
239
|
+
WORD_SIZE + // Identifier
|
|
240
|
+
ADDRESS_LEN + // Sender
|
|
241
|
+
ADDRESS_LEN + // Recipient
|
|
242
|
+
WORD_SIZE + // Amount
|
|
243
|
+
NONCE_LEN + // Nonce
|
|
244
|
+
WORD_SIZE + // witness_index
|
|
245
|
+
WORD_SIZE + // Data size
|
|
246
|
+
WORD_SIZE + // Predicate size
|
|
247
|
+
WORD_SIZE + // Predicate data size
|
|
248
|
+
WORD_SIZE;
|
|
249
|
+
|
|
250
|
+
// src/utils/utilities.ts
|
|
251
|
+
var isUint8Array = (value) => value instanceof Uint8Array;
|
|
252
|
+
var hasNestedOption = (coders) => {
|
|
253
|
+
const array = Array.isArray(coders) ? coders : Object.values(coders);
|
|
254
|
+
for (const node of array) {
|
|
255
|
+
if (node.type === OPTION_CODER_TYPE) {
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
if ("coder" in node && node.coder.type === OPTION_CODER_TYPE) {
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
if ("coders" in node) {
|
|
262
|
+
const child = hasNestedOption(node.coders);
|
|
263
|
+
if (child) {
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return false;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// src/encoding/coders/ArrayCoder.ts
|
|
272
|
+
var ArrayCoder = class extends Coder {
|
|
273
|
+
coder;
|
|
274
|
+
length;
|
|
275
|
+
#hasNestedOption;
|
|
276
|
+
constructor(coder, length) {
|
|
277
|
+
super("array", `[${coder.type}; ${length}]`, length * coder.encodedLength);
|
|
278
|
+
this.coder = coder;
|
|
279
|
+
this.length = length;
|
|
280
|
+
this.#hasNestedOption = hasNestedOption([coder]);
|
|
281
|
+
}
|
|
282
|
+
encode(value) {
|
|
283
|
+
if (!Array.isArray(value)) {
|
|
284
|
+
throw new import_errors.FuelError(
|
|
285
|
+
import_errors.ErrorCode.ENCODE_ERROR,
|
|
286
|
+
`Expected array value.`
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
if (this.length !== value.length) {
|
|
290
|
+
throw new import_errors.FuelError(
|
|
291
|
+
import_errors.ErrorCode.ENCODE_ERROR,
|
|
292
|
+
`Types/values length mismatch.`
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
return (0, import_utils.concat)(
|
|
296
|
+
Array.from(value).map((v) => this.coder.encode(v))
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
decode(data, offset) {
|
|
300
|
+
if (
|
|
301
|
+
(!this.#hasNestedOption && data.length < this.encodedLength) ||
|
|
302
|
+
data.length > MAX_BYTES
|
|
303
|
+
) {
|
|
304
|
+
throw new import_errors.FuelError(
|
|
305
|
+
import_errors.ErrorCode.DECODE_ERROR,
|
|
306
|
+
`Invalid array data size.`
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
let newOffset = offset;
|
|
310
|
+
const decodedValue = Array(this.length)
|
|
311
|
+
.fill(0)
|
|
312
|
+
.map(() => {
|
|
313
|
+
let decoded;
|
|
314
|
+
[decoded, newOffset] = this.coder.decode(data, newOffset);
|
|
315
|
+
return decoded;
|
|
316
|
+
});
|
|
317
|
+
return [decodedValue, newOffset];
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// src/encoding/coders/B256Coder.ts
|
|
322
|
+
var import_errors2 = require("@fuel-ts/errors");
|
|
323
|
+
var import_math = require("@fuel-ts/math");
|
|
324
|
+
var import_utils2 = require("@fuel-ts/utils");
|
|
325
|
+
var B256Coder = class extends Coder {
|
|
326
|
+
constructor() {
|
|
327
|
+
super("b256", "b256", WORD_SIZE * 4);
|
|
328
|
+
}
|
|
329
|
+
encode(value) {
|
|
330
|
+
let encodedValue;
|
|
331
|
+
try {
|
|
332
|
+
encodedValue = (0, import_utils2.arrayify)(value);
|
|
333
|
+
} catch (error) {
|
|
334
|
+
throw new import_errors2.FuelError(
|
|
335
|
+
import_errors2.ErrorCode.ENCODE_ERROR,
|
|
336
|
+
`Invalid ${this.type}.`
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
if (encodedValue.length !== this.encodedLength) {
|
|
340
|
+
throw new import_errors2.FuelError(
|
|
341
|
+
import_errors2.ErrorCode.ENCODE_ERROR,
|
|
342
|
+
`Invalid ${this.type}.`
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
return encodedValue;
|
|
346
|
+
}
|
|
347
|
+
decode(data, offset) {
|
|
348
|
+
if (data.length < this.encodedLength) {
|
|
349
|
+
throw new import_errors2.FuelError(
|
|
350
|
+
import_errors2.ErrorCode.DECODE_ERROR,
|
|
351
|
+
`Invalid b256 data size.`
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
let bytes = data.slice(offset, offset + this.encodedLength);
|
|
355
|
+
const decoded = (0, import_math.bn)(bytes);
|
|
356
|
+
if (decoded.isZero()) {
|
|
357
|
+
bytes = new Uint8Array(32);
|
|
358
|
+
}
|
|
359
|
+
if (bytes.length !== this.encodedLength) {
|
|
360
|
+
throw new import_errors2.FuelError(
|
|
361
|
+
import_errors2.ErrorCode.DECODE_ERROR,
|
|
362
|
+
`Invalid b256 byte data size.`
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
return [(0, import_math.toHex)(bytes, 32), offset + 32];
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
// src/encoding/coders/B512Coder.ts
|
|
370
|
+
var import_errors3 = require("@fuel-ts/errors");
|
|
371
|
+
var import_math2 = require("@fuel-ts/math");
|
|
372
|
+
var import_utils3 = require("@fuel-ts/utils");
|
|
373
|
+
var B512Coder = class extends Coder {
|
|
374
|
+
constructor() {
|
|
375
|
+
super("b512", "struct B512", WORD_SIZE * 8);
|
|
376
|
+
}
|
|
377
|
+
encode(value) {
|
|
378
|
+
let encodedValue;
|
|
379
|
+
try {
|
|
380
|
+
encodedValue = (0, import_utils3.arrayify)(value);
|
|
381
|
+
} catch (error) {
|
|
382
|
+
throw new import_errors3.FuelError(
|
|
383
|
+
import_errors3.ErrorCode.ENCODE_ERROR,
|
|
384
|
+
`Invalid ${this.type}.`
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
if (encodedValue.length !== this.encodedLength) {
|
|
388
|
+
throw new import_errors3.FuelError(
|
|
389
|
+
import_errors3.ErrorCode.ENCODE_ERROR,
|
|
390
|
+
`Invalid ${this.type}.`
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
return encodedValue;
|
|
394
|
+
}
|
|
395
|
+
decode(data, offset) {
|
|
396
|
+
if (data.length < this.encodedLength) {
|
|
397
|
+
throw new import_errors3.FuelError(
|
|
398
|
+
import_errors3.ErrorCode.DECODE_ERROR,
|
|
399
|
+
`Invalid b512 data size.`
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
let bytes = data.slice(offset, offset + this.encodedLength);
|
|
403
|
+
const decoded = (0, import_math2.bn)(bytes);
|
|
404
|
+
if (decoded.isZero()) {
|
|
405
|
+
bytes = new Uint8Array(64);
|
|
406
|
+
}
|
|
407
|
+
if (bytes.length !== this.encodedLength) {
|
|
408
|
+
throw new import_errors3.FuelError(
|
|
409
|
+
import_errors3.ErrorCode.DECODE_ERROR,
|
|
410
|
+
`Invalid b512 byte data size.`
|
|
411
|
+
);
|
|
412
|
+
}
|
|
413
|
+
return [
|
|
414
|
+
(0, import_math2.toHex)(bytes, this.encodedLength),
|
|
415
|
+
offset + this.encodedLength,
|
|
416
|
+
];
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
// src/encoding/coders/BigNumberCoder.ts
|
|
421
|
+
var import_errors4 = require("@fuel-ts/errors");
|
|
422
|
+
var import_math3 = require("@fuel-ts/math");
|
|
423
|
+
var encodedLengths = {
|
|
424
|
+
u64: WORD_SIZE,
|
|
425
|
+
u256: WORD_SIZE * 4,
|
|
426
|
+
};
|
|
427
|
+
var BigNumberCoder = class extends Coder {
|
|
428
|
+
constructor(baseType) {
|
|
429
|
+
super("bigNumber", baseType, encodedLengths[baseType]);
|
|
430
|
+
}
|
|
431
|
+
encode(value) {
|
|
432
|
+
let bytes;
|
|
433
|
+
try {
|
|
434
|
+
bytes = (0, import_math3.toBytes)(value, this.encodedLength);
|
|
435
|
+
} catch (error) {
|
|
436
|
+
throw new import_errors4.FuelError(
|
|
437
|
+
import_errors4.ErrorCode.ENCODE_ERROR,
|
|
438
|
+
`Invalid ${this.type}.`
|
|
439
|
+
);
|
|
440
|
+
}
|
|
441
|
+
return bytes;
|
|
442
|
+
}
|
|
443
|
+
decode(data, offset) {
|
|
444
|
+
if (data.length < this.encodedLength) {
|
|
445
|
+
throw new import_errors4.FuelError(
|
|
446
|
+
import_errors4.ErrorCode.DECODE_ERROR,
|
|
447
|
+
`Invalid ${this.type} data size.`
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
let bytes = data.slice(offset, offset + this.encodedLength);
|
|
451
|
+
bytes = bytes.slice(0, this.encodedLength);
|
|
452
|
+
if (bytes.length !== this.encodedLength) {
|
|
453
|
+
throw new import_errors4.FuelError(
|
|
454
|
+
import_errors4.ErrorCode.DECODE_ERROR,
|
|
455
|
+
`Invalid ${this.type} byte data size.`
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
return [BigInt(import_math3.bn(bytes)), offset + this.encodedLength];
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// src/encoding/coders/BooleanCoder.ts
|
|
463
|
+
var import_errors5 = require("@fuel-ts/errors");
|
|
464
|
+
var import_math4 = require("@fuel-ts/math");
|
|
465
|
+
var BooleanCoder = class extends Coder {
|
|
466
|
+
options;
|
|
467
|
+
constructor(
|
|
468
|
+
options = {
|
|
469
|
+
padToWordSize: false,
|
|
470
|
+
}
|
|
471
|
+
) {
|
|
472
|
+
const encodedLength = options.padToWordSize ? WORD_SIZE : 1;
|
|
473
|
+
super("boolean", "boolean", encodedLength);
|
|
474
|
+
this.options = options;
|
|
475
|
+
}
|
|
476
|
+
encode(value) {
|
|
477
|
+
const isTrueBool = value === true || value === false;
|
|
478
|
+
if (!isTrueBool) {
|
|
479
|
+
throw new import_errors5.FuelError(
|
|
480
|
+
import_errors5.ErrorCode.ENCODE_ERROR,
|
|
481
|
+
`Invalid boolean value.`
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
return (0, import_math4.toBytes)(value ? 1 : 0, this.encodedLength);
|
|
485
|
+
}
|
|
486
|
+
decode(data, offset) {
|
|
487
|
+
if (data.length < this.encodedLength) {
|
|
488
|
+
throw new import_errors5.FuelError(
|
|
489
|
+
import_errors5.ErrorCode.DECODE_ERROR,
|
|
490
|
+
`Invalid boolean data size.`
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
const bytes = (0, import_math4.bn)(
|
|
494
|
+
data.slice(offset, offset + this.encodedLength)
|
|
495
|
+
);
|
|
496
|
+
if (bytes.isZero()) {
|
|
497
|
+
return [false, offset + this.encodedLength];
|
|
498
|
+
}
|
|
499
|
+
if (!bytes.eq((0, import_math4.bn)(1))) {
|
|
500
|
+
throw new import_errors5.FuelError(
|
|
501
|
+
import_errors5.ErrorCode.DECODE_ERROR,
|
|
502
|
+
`Invalid boolean value.`
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
return [true, offset + this.encodedLength];
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// src/encoding/coders/ByteCoder.ts
|
|
510
|
+
var import_errors6 = require("@fuel-ts/errors");
|
|
511
|
+
var import_math5 = require("@fuel-ts/math");
|
|
512
|
+
var ByteCoder = class extends Coder {
|
|
513
|
+
constructor() {
|
|
514
|
+
super("struct", "struct Bytes", WORD_SIZE);
|
|
515
|
+
}
|
|
516
|
+
encode(value) {
|
|
517
|
+
const bytes = value instanceof Uint8Array ? value : new Uint8Array(value);
|
|
518
|
+
const lengthBytes = new BigNumberCoder("u64").encode(bytes.length);
|
|
519
|
+
return new Uint8Array([...lengthBytes, ...bytes]);
|
|
520
|
+
}
|
|
521
|
+
decode(data, offset) {
|
|
522
|
+
if (data.length < WORD_SIZE) {
|
|
523
|
+
throw new import_errors6.FuelError(
|
|
524
|
+
import_errors6.ErrorCode.DECODE_ERROR,
|
|
525
|
+
`Invalid byte data size.`
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
const offsetAndLength = offset + WORD_SIZE;
|
|
529
|
+
const lengthBytes = data.slice(offset, offsetAndLength);
|
|
530
|
+
const length = (0, import_math5.bn)(
|
|
531
|
+
new BigNumberCoder("u64").decode(lengthBytes, 0)[0]
|
|
532
|
+
).toNumber();
|
|
533
|
+
const dataBytes = data.slice(offsetAndLength, offsetAndLength + length);
|
|
534
|
+
if (dataBytes.length !== length) {
|
|
535
|
+
throw new import_errors6.FuelError(
|
|
536
|
+
import_errors6.ErrorCode.DECODE_ERROR,
|
|
537
|
+
`Invalid bytes byte data size.`
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
return [dataBytes, offsetAndLength + length];
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
__publicField(ByteCoder, "memorySize", 1);
|
|
544
|
+
|
|
545
|
+
// src/encoding/coders/EnumCoder.ts
|
|
546
|
+
var import_errors7 = require("@fuel-ts/errors");
|
|
547
|
+
var import_math6 = require("@fuel-ts/math");
|
|
548
|
+
var import_utils4 = require("@fuel-ts/utils");
|
|
549
|
+
var EnumCoder = class extends Coder {
|
|
550
|
+
name;
|
|
551
|
+
coders;
|
|
552
|
+
#caseIndexCoder;
|
|
553
|
+
#encodedValueSize;
|
|
554
|
+
#shouldValidateLength;
|
|
555
|
+
constructor(name, coders) {
|
|
556
|
+
const caseIndexCoder = new BigNumberCoder("u64");
|
|
557
|
+
const encodedValueSize = Object.values(coders).reduce(
|
|
558
|
+
(min, coder) => Math.min(min, coder.encodedLength),
|
|
559
|
+
0
|
|
560
|
+
);
|
|
561
|
+
super(
|
|
562
|
+
`enum ${name}`,
|
|
563
|
+
`enum ${name}`,
|
|
564
|
+
caseIndexCoder.encodedLength + encodedValueSize
|
|
565
|
+
);
|
|
566
|
+
this.name = name;
|
|
567
|
+
this.coders = coders;
|
|
568
|
+
this.#caseIndexCoder = caseIndexCoder;
|
|
569
|
+
this.#encodedValueSize = encodedValueSize;
|
|
570
|
+
this.#shouldValidateLength = !(
|
|
571
|
+
optionRegEx.test(this.type) || hasNestedOption(coders)
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
// Checks that we're handling a native enum that is of type void.
|
|
575
|
+
#isNativeEnum(coder) {
|
|
576
|
+
return this.type !== OPTION_CODER_TYPE && coder.type === VOID_TYPE;
|
|
577
|
+
}
|
|
578
|
+
#encodeNativeEnum(value) {
|
|
579
|
+
const valueCoder = this.coders[value];
|
|
580
|
+
const encodedValue = valueCoder.encode([]);
|
|
581
|
+
const caseIndex = Object.keys(this.coders).indexOf(value);
|
|
582
|
+
const padding = new Uint8Array(
|
|
583
|
+
this.#encodedValueSize - valueCoder.encodedLength
|
|
584
|
+
);
|
|
585
|
+
return (0, import_utils4.concat)([
|
|
586
|
+
this.#caseIndexCoder.encode(caseIndex),
|
|
587
|
+
padding,
|
|
588
|
+
encodedValue,
|
|
589
|
+
]);
|
|
590
|
+
}
|
|
591
|
+
encode(value) {
|
|
592
|
+
if (typeof value === "string" && this.coders[value]) {
|
|
593
|
+
return this.#encodeNativeEnum(value);
|
|
594
|
+
}
|
|
595
|
+
const [caseKey, ...empty] = Object.keys(value);
|
|
596
|
+
if (!caseKey) {
|
|
597
|
+
throw new import_errors7.FuelError(
|
|
598
|
+
import_errors7.ErrorCode.INVALID_DECODE_VALUE,
|
|
599
|
+
"A field for the case must be provided."
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
if (empty.length !== 0) {
|
|
603
|
+
throw new import_errors7.FuelError(
|
|
604
|
+
import_errors7.ErrorCode.INVALID_DECODE_VALUE,
|
|
605
|
+
"Only one field must be provided."
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
const valueCoder = this.coders[caseKey];
|
|
609
|
+
const caseIndex = Object.keys(this.coders).indexOf(caseKey);
|
|
610
|
+
if (caseIndex === -1) {
|
|
611
|
+
const validCases = Object.keys(this.coders)
|
|
612
|
+
.map((v) => `'${v}'`)
|
|
613
|
+
.join(", ");
|
|
614
|
+
throw new import_errors7.FuelError(
|
|
615
|
+
import_errors7.ErrorCode.INVALID_DECODE_VALUE,
|
|
616
|
+
`Invalid case '${caseKey}'. Valid cases: ${validCases}.`
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
const encodedValue = valueCoder.encode(value[caseKey]);
|
|
620
|
+
return new Uint8Array([
|
|
621
|
+
...this.#caseIndexCoder.encode(caseIndex),
|
|
622
|
+
...encodedValue,
|
|
623
|
+
]);
|
|
624
|
+
}
|
|
625
|
+
#decodeNativeEnum(caseKey, newOffset) {
|
|
626
|
+
return [caseKey, newOffset];
|
|
627
|
+
}
|
|
628
|
+
decode(data, offset) {
|
|
629
|
+
if (this.#shouldValidateLength && data.length < this.encodedLength) {
|
|
630
|
+
throw new import_errors7.FuelError(
|
|
631
|
+
import_errors7.ErrorCode.DECODE_ERROR,
|
|
632
|
+
`Invalid enum data size.`
|
|
633
|
+
);
|
|
634
|
+
}
|
|
635
|
+
const caseBytes = new BigNumberCoder("u64").decode(data, offset)[0];
|
|
636
|
+
const caseIndex = (0, import_math6.toNumber)(caseBytes);
|
|
637
|
+
const caseKey = Object.keys(this.coders)[caseIndex];
|
|
638
|
+
if (!caseKey) {
|
|
639
|
+
throw new import_errors7.FuelError(
|
|
640
|
+
import_errors7.ErrorCode.INVALID_DECODE_VALUE,
|
|
641
|
+
`Invalid caseIndex "${caseIndex}". Valid cases: ${Object.keys(
|
|
642
|
+
this.coders
|
|
643
|
+
)}.`
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
const valueCoder = this.coders[caseKey];
|
|
647
|
+
const offsetAndCase = offset + this.#caseIndexCoder.encodedLength;
|
|
648
|
+
if (
|
|
649
|
+
this.#shouldValidateLength &&
|
|
650
|
+
data.length < offsetAndCase + valueCoder.encodedLength
|
|
651
|
+
) {
|
|
652
|
+
throw new import_errors7.FuelError(
|
|
653
|
+
import_errors7.ErrorCode.DECODE_ERROR,
|
|
654
|
+
`Invalid enum data size.`
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
const [decoded, newOffset] = valueCoder.decode(data, offsetAndCase);
|
|
658
|
+
return [{ case: caseKey, payload: decoded }, newOffset];
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
|
|
662
|
+
// src/encoding/coders/NumberCoder.ts
|
|
663
|
+
var import_errors8 = require("@fuel-ts/errors");
|
|
664
|
+
var import_math7 = require("@fuel-ts/math");
|
|
665
|
+
var getLength = (baseType) => {
|
|
666
|
+
switch (baseType) {
|
|
667
|
+
case "u8":
|
|
668
|
+
return 1;
|
|
669
|
+
case "u16":
|
|
670
|
+
return 2;
|
|
671
|
+
case "u32":
|
|
672
|
+
return 4;
|
|
673
|
+
default:
|
|
674
|
+
throw new import_errors8.FuelError(
|
|
675
|
+
import_errors8.ErrorCode.TYPE_NOT_SUPPORTED,
|
|
676
|
+
`Invalid number type: ${baseType}`
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
};
|
|
680
|
+
var NumberCoder = class extends Coder {
|
|
681
|
+
baseType;
|
|
682
|
+
options;
|
|
683
|
+
constructor(
|
|
684
|
+
baseType,
|
|
685
|
+
options = {
|
|
686
|
+
padToWordSize: false,
|
|
687
|
+
}
|
|
688
|
+
) {
|
|
689
|
+
const length = options.padToWordSize ? WORD_SIZE : getLength(baseType);
|
|
690
|
+
super("number", baseType, length);
|
|
691
|
+
this.baseType = baseType;
|
|
692
|
+
this.options = options;
|
|
693
|
+
}
|
|
694
|
+
encode(value) {
|
|
695
|
+
let bytes;
|
|
696
|
+
try {
|
|
697
|
+
bytes = (0, import_math7.toBytes)(value);
|
|
698
|
+
} catch (error) {
|
|
699
|
+
throw new import_errors8.FuelError(
|
|
700
|
+
import_errors8.ErrorCode.ENCODE_ERROR,
|
|
701
|
+
`Invalid ${this.baseType}.`
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
if (bytes.length > this.encodedLength) {
|
|
705
|
+
throw new import_errors8.FuelError(
|
|
706
|
+
import_errors8.ErrorCode.ENCODE_ERROR,
|
|
707
|
+
`Invalid ${this.baseType}, too many bytes.`
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
return (0, import_math7.toBytes)(bytes, this.encodedLength);
|
|
711
|
+
}
|
|
712
|
+
decode(data, offset) {
|
|
713
|
+
if (data.length < this.encodedLength) {
|
|
714
|
+
throw new import_errors8.FuelError(
|
|
715
|
+
import_errors8.ErrorCode.DECODE_ERROR,
|
|
716
|
+
`Invalid number data size.`
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
const bytes = data.slice(offset, offset + this.encodedLength);
|
|
720
|
+
if (bytes.length !== this.encodedLength) {
|
|
721
|
+
throw new import_errors8.FuelError(
|
|
722
|
+
import_errors8.ErrorCode.DECODE_ERROR,
|
|
723
|
+
`Invalid number byte data size.`
|
|
724
|
+
);
|
|
725
|
+
}
|
|
726
|
+
return [(0, import_math7.toNumber)(bytes), offset + this.encodedLength];
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
// src/encoding/coders/OptionCoder.ts
|
|
731
|
+
var OptionCoder = class extends EnumCoder {
|
|
732
|
+
encode(value) {
|
|
733
|
+
const result = super.encode(this.toSwayOption(value));
|
|
734
|
+
return result;
|
|
735
|
+
}
|
|
736
|
+
toSwayOption(input) {
|
|
737
|
+
if (input !== void 0) {
|
|
738
|
+
return { Some: input };
|
|
739
|
+
}
|
|
740
|
+
return { None: [] };
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
// src/encoding/coders/RawSliceCoder.ts
|
|
745
|
+
var import_errors9 = require("@fuel-ts/errors");
|
|
746
|
+
var import_math8 = require("@fuel-ts/math");
|
|
747
|
+
var RawSliceCoder = class extends Coder {
|
|
748
|
+
constructor() {
|
|
749
|
+
super("raw untyped slice", "raw untyped slice", WORD_SIZE);
|
|
750
|
+
}
|
|
751
|
+
encode(value) {
|
|
752
|
+
if (!Array.isArray(value)) {
|
|
753
|
+
throw new import_errors9.FuelError(
|
|
754
|
+
import_errors9.ErrorCode.ENCODE_ERROR,
|
|
755
|
+
`Expected array value.`
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
const internalCoder = new ArrayCoder(new NumberCoder("u8"), value.length);
|
|
759
|
+
const bytes = internalCoder.encode(value);
|
|
760
|
+
const lengthBytes = new BigNumberCoder("u64").encode(bytes.length);
|
|
761
|
+
return new Uint8Array([...lengthBytes, ...bytes]);
|
|
762
|
+
}
|
|
763
|
+
decode(data, offset) {
|
|
764
|
+
if (data.length < this.encodedLength) {
|
|
765
|
+
throw new import_errors9.FuelError(
|
|
766
|
+
import_errors9.ErrorCode.DECODE_ERROR,
|
|
767
|
+
`Invalid raw slice data size.`
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
const offsetAndLength = offset + WORD_SIZE;
|
|
771
|
+
const lengthBytes = data.slice(offset, offsetAndLength);
|
|
772
|
+
const length = (0, import_math8.bn)(
|
|
773
|
+
new BigNumberCoder("u64").decode(lengthBytes, 0)[0]
|
|
774
|
+
).toNumber();
|
|
775
|
+
const dataBytes = data.slice(offsetAndLength, offsetAndLength + length);
|
|
776
|
+
if (dataBytes.length !== length) {
|
|
777
|
+
throw new import_errors9.FuelError(
|
|
778
|
+
import_errors9.ErrorCode.DECODE_ERROR,
|
|
779
|
+
`Invalid raw slice byte data size.`
|
|
780
|
+
);
|
|
781
|
+
}
|
|
782
|
+
const internalCoder = new ArrayCoder(new NumberCoder("u8"), length);
|
|
783
|
+
const [decodedValue] = internalCoder.decode(dataBytes, 0);
|
|
784
|
+
return [decodedValue, offsetAndLength + length];
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
// src/encoding/coders/StdStringCoder.ts
|
|
789
|
+
var import_errors10 = require("@fuel-ts/errors");
|
|
790
|
+
var import_math9 = require("@fuel-ts/math");
|
|
791
|
+
var import_utils5 = require("@fuel-ts/utils");
|
|
792
|
+
var StdStringCoder = class extends Coder {
|
|
793
|
+
constructor() {
|
|
794
|
+
super("struct", "struct String", WORD_SIZE);
|
|
795
|
+
}
|
|
796
|
+
encode(value) {
|
|
797
|
+
const bytes = (0, import_utils5.toUtf8Bytes)(value);
|
|
798
|
+
const lengthBytes = new BigNumberCoder("u64").encode(value.length);
|
|
799
|
+
return new Uint8Array([...lengthBytes, ...bytes]);
|
|
800
|
+
}
|
|
801
|
+
decode(data, offset) {
|
|
802
|
+
if (data.length < this.encodedLength) {
|
|
803
|
+
throw new import_errors10.FuelError(
|
|
804
|
+
import_errors10.ErrorCode.DECODE_ERROR,
|
|
805
|
+
`Invalid std string data size.`
|
|
806
|
+
);
|
|
807
|
+
}
|
|
808
|
+
const offsetAndLength = offset + WORD_SIZE;
|
|
809
|
+
const lengthBytes = data.slice(offset, offsetAndLength);
|
|
810
|
+
const length = (0, import_math9.bn)(
|
|
811
|
+
new BigNumberCoder("u64").decode(lengthBytes, 0)[0]
|
|
812
|
+
).toNumber();
|
|
813
|
+
const dataBytes = data.slice(offsetAndLength, offsetAndLength + length);
|
|
814
|
+
if (dataBytes.length !== length) {
|
|
815
|
+
throw new import_errors10.FuelError(
|
|
816
|
+
import_errors10.ErrorCode.DECODE_ERROR,
|
|
817
|
+
`Invalid std string byte data size.`
|
|
818
|
+
);
|
|
819
|
+
}
|
|
820
|
+
return [
|
|
821
|
+
(0, import_utils5.toUtf8String)(dataBytes),
|
|
822
|
+
offsetAndLength + length,
|
|
823
|
+
];
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
__publicField(StdStringCoder, "memorySize", 1);
|
|
827
|
+
|
|
828
|
+
// src/encoding/coders/StrSliceCoder.ts
|
|
829
|
+
var import_errors11 = require("@fuel-ts/errors");
|
|
830
|
+
var import_math10 = require("@fuel-ts/math");
|
|
831
|
+
var import_utils6 = require("@fuel-ts/utils");
|
|
832
|
+
var StrSliceCoder = class extends Coder {
|
|
833
|
+
constructor() {
|
|
834
|
+
super("strSlice", "str", WORD_SIZE);
|
|
835
|
+
}
|
|
836
|
+
encode(value) {
|
|
837
|
+
const bytes = (0, import_utils6.toUtf8Bytes)(value);
|
|
838
|
+
const lengthBytes = new BigNumberCoder("u64").encode(value.length);
|
|
839
|
+
return new Uint8Array([...lengthBytes, ...bytes]);
|
|
840
|
+
}
|
|
841
|
+
decode(data, offset) {
|
|
842
|
+
if (data.length < this.encodedLength) {
|
|
843
|
+
throw new import_errors11.FuelError(
|
|
844
|
+
import_errors11.ErrorCode.DECODE_ERROR,
|
|
845
|
+
`Invalid string slice data size.`
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
const offsetAndLength = offset + WORD_SIZE;
|
|
849
|
+
const lengthBytes = data.slice(offset, offsetAndLength);
|
|
850
|
+
const length = (0, import_math10.bn)(
|
|
851
|
+
new BigNumberCoder("u64").decode(lengthBytes, 0)[0]
|
|
852
|
+
).toNumber();
|
|
853
|
+
const bytes = data.slice(offsetAndLength, offsetAndLength + length);
|
|
854
|
+
if (bytes.length !== length) {
|
|
855
|
+
throw new import_errors11.FuelError(
|
|
856
|
+
import_errors11.ErrorCode.DECODE_ERROR,
|
|
857
|
+
`Invalid string slice byte data size.`
|
|
858
|
+
);
|
|
859
|
+
}
|
|
860
|
+
return [(0, import_utils6.toUtf8String)(bytes), offsetAndLength + length];
|
|
861
|
+
}
|
|
862
|
+
};
|
|
863
|
+
__publicField(StrSliceCoder, "memorySize", 1);
|
|
864
|
+
|
|
865
|
+
// src/encoding/coders/StringCoder.ts
|
|
866
|
+
var import_errors12 = require("@fuel-ts/errors");
|
|
867
|
+
var import_utils7 = require("@fuel-ts/utils");
|
|
868
|
+
var StringCoder = class extends Coder {
|
|
869
|
+
constructor(length) {
|
|
870
|
+
super("string", `str[${length}]`, length);
|
|
871
|
+
}
|
|
872
|
+
encode(value) {
|
|
873
|
+
if (value.length !== this.encodedLength) {
|
|
874
|
+
throw new import_errors12.FuelError(
|
|
875
|
+
import_errors12.ErrorCode.ENCODE_ERROR,
|
|
876
|
+
`Value length mismatch during encode.`
|
|
877
|
+
);
|
|
878
|
+
}
|
|
879
|
+
return (0, import_utils7.toUtf8Bytes)(value);
|
|
880
|
+
}
|
|
881
|
+
decode(data, offset) {
|
|
882
|
+
if (data.length < this.encodedLength) {
|
|
883
|
+
throw new import_errors12.FuelError(
|
|
884
|
+
import_errors12.ErrorCode.DECODE_ERROR,
|
|
885
|
+
`Invalid string data size.`
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
const bytes = data.slice(offset, offset + this.encodedLength);
|
|
889
|
+
if (bytes.length !== this.encodedLength) {
|
|
890
|
+
throw new import_errors12.FuelError(
|
|
891
|
+
import_errors12.ErrorCode.DECODE_ERROR,
|
|
892
|
+
`Invalid string byte data size.`
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
return [
|
|
896
|
+
(0, import_utils7.toUtf8String)(bytes),
|
|
897
|
+
offset + this.encodedLength,
|
|
898
|
+
];
|
|
899
|
+
}
|
|
900
|
+
};
|
|
901
|
+
|
|
902
|
+
// src/encoding/coders/StructCoder.ts
|
|
903
|
+
var import_errors13 = require("@fuel-ts/errors");
|
|
904
|
+
var import_utils8 = require("@fuel-ts/utils");
|
|
905
|
+
var StructCoder = class extends Coder {
|
|
906
|
+
name;
|
|
907
|
+
coders;
|
|
908
|
+
#hasNestedOption;
|
|
909
|
+
constructor(name, coders) {
|
|
910
|
+
const encodedLength = Object.values(coders).reduce(
|
|
911
|
+
(acc, coder) => acc + coder.encodedLength,
|
|
912
|
+
0
|
|
913
|
+
);
|
|
914
|
+
super("struct", `struct ${name}`, encodedLength);
|
|
915
|
+
this.name = name;
|
|
916
|
+
this.coders = coders;
|
|
917
|
+
this.#hasNestedOption = hasNestedOption(coders);
|
|
918
|
+
}
|
|
919
|
+
encode(value) {
|
|
920
|
+
return (0, import_utils8.concatBytes)(
|
|
921
|
+
Object.keys(this.coders).map((fieldName) => {
|
|
922
|
+
const fieldCoder = this.coders[fieldName];
|
|
923
|
+
const fieldValue = value[fieldName];
|
|
924
|
+
if (!(fieldCoder instanceof OptionCoder) && fieldValue == null) {
|
|
925
|
+
throw new import_errors13.FuelError(
|
|
926
|
+
import_errors13.ErrorCode.ENCODE_ERROR,
|
|
927
|
+
`Invalid ${this.type}. Field "${fieldName}" not present.`
|
|
928
|
+
);
|
|
929
|
+
}
|
|
930
|
+
return fieldCoder.encode(fieldValue);
|
|
931
|
+
})
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
decode(data, offset) {
|
|
935
|
+
if (!this.#hasNestedOption && data.length < this.encodedLength) {
|
|
936
|
+
throw new import_errors13.FuelError(
|
|
937
|
+
import_errors13.ErrorCode.DECODE_ERROR,
|
|
938
|
+
`Invalid struct data size.`
|
|
939
|
+
);
|
|
940
|
+
}
|
|
941
|
+
let newOffset = offset;
|
|
942
|
+
const decodedValue = Object.keys(this.coders).reduce((obj, fieldName) => {
|
|
943
|
+
const fieldCoder = this.coders[fieldName];
|
|
944
|
+
let decoded;
|
|
945
|
+
[decoded, newOffset] = fieldCoder.decode(data, newOffset);
|
|
946
|
+
obj[fieldName] = decoded;
|
|
947
|
+
return obj;
|
|
948
|
+
}, {});
|
|
949
|
+
return [decodedValue, newOffset];
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
|
|
953
|
+
// src/encoding/coders/TupleCoder.ts
|
|
954
|
+
var import_errors14 = require("@fuel-ts/errors");
|
|
955
|
+
var import_utils9 = require("@fuel-ts/utils");
|
|
956
|
+
var TupleCoder = class extends Coder {
|
|
957
|
+
coders;
|
|
958
|
+
#hasNestedOption;
|
|
959
|
+
constructor(coders) {
|
|
960
|
+
const encodedLength = coders.reduce(
|
|
961
|
+
(acc, coder) => acc + coder.encodedLength,
|
|
962
|
+
0
|
|
963
|
+
);
|
|
964
|
+
super(
|
|
965
|
+
"tuple",
|
|
966
|
+
`(${coders.map((coder) => coder.type).join(", ")})`,
|
|
967
|
+
encodedLength
|
|
968
|
+
);
|
|
969
|
+
this.coders = coders;
|
|
970
|
+
this.#hasNestedOption = hasNestedOption(coders);
|
|
971
|
+
}
|
|
972
|
+
encode(value) {
|
|
973
|
+
if (this.coders.length !== value.length) {
|
|
974
|
+
throw new import_errors14.FuelError(
|
|
975
|
+
import_errors14.ErrorCode.ENCODE_ERROR,
|
|
976
|
+
`Types/values length mismatch.`
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
return (0, import_utils9.concatBytes)(
|
|
980
|
+
this.coders.map((coder, i) => coder.encode(value[i]))
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
decode(data, offset) {
|
|
984
|
+
if (!this.#hasNestedOption && data.length < this.encodedLength) {
|
|
985
|
+
throw new import_errors14.FuelError(
|
|
986
|
+
import_errors14.ErrorCode.DECODE_ERROR,
|
|
987
|
+
`Invalid tuple data size.`
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
let newOffset = offset;
|
|
991
|
+
const decodedValue = this.coders.map((coder) => {
|
|
992
|
+
let decoded;
|
|
993
|
+
[decoded, newOffset] = coder.decode(data, newOffset);
|
|
994
|
+
return decoded;
|
|
995
|
+
});
|
|
996
|
+
return [decodedValue, newOffset];
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
|
|
1000
|
+
// src/encoding/coders/VecCoder.ts
|
|
1001
|
+
var import_errors15 = require("@fuel-ts/errors");
|
|
1002
|
+
var import_math11 = require("@fuel-ts/math");
|
|
1003
|
+
var import_utils10 = require("@fuel-ts/utils");
|
|
1004
|
+
var VecCoder = class extends Coder {
|
|
1005
|
+
coder;
|
|
1006
|
+
#hasNestedOption;
|
|
1007
|
+
constructor(coder) {
|
|
1008
|
+
super("struct", `struct Vec`, WORD_SIZE);
|
|
1009
|
+
this.coder = coder;
|
|
1010
|
+
this.#hasNestedOption = hasNestedOption([coder]);
|
|
1011
|
+
}
|
|
1012
|
+
encode(value) {
|
|
1013
|
+
if (!Array.isArray(value) && !isUint8Array(value)) {
|
|
1014
|
+
throw new import_errors15.FuelError(
|
|
1015
|
+
import_errors15.ErrorCode.ENCODE_ERROR,
|
|
1016
|
+
`Expected array value, or a Uint8Array. You can use arrayify to convert a value to a Uint8Array.`
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
const lengthCoder = new BigNumberCoder("u64");
|
|
1020
|
+
if (isUint8Array(value)) {
|
|
1021
|
+
return new Uint8Array([...lengthCoder.encode(value.length), ...value]);
|
|
1022
|
+
}
|
|
1023
|
+
const bytes = value.map((v) => this.coder.encode(v));
|
|
1024
|
+
const lengthBytes = lengthCoder.encode(value.length);
|
|
1025
|
+
return new Uint8Array([
|
|
1026
|
+
...lengthBytes,
|
|
1027
|
+
...(0, import_utils10.concatBytes)(bytes),
|
|
1028
|
+
]);
|
|
1029
|
+
}
|
|
1030
|
+
decode(data, offset) {
|
|
1031
|
+
if (
|
|
1032
|
+
(!this.#hasNestedOption && data.length < this.encodedLength) ||
|
|
1033
|
+
data.length > MAX_BYTES
|
|
1034
|
+
) {
|
|
1035
|
+
throw new import_errors15.FuelError(
|
|
1036
|
+
import_errors15.ErrorCode.DECODE_ERROR,
|
|
1037
|
+
`Invalid vec data size.`
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
const offsetAndLength = offset + WORD_SIZE;
|
|
1041
|
+
const lengthBytes = data.slice(offset, offsetAndLength);
|
|
1042
|
+
const length = (0, import_math11.bn)(
|
|
1043
|
+
new BigNumberCoder("u64").decode(lengthBytes, 0)[0]
|
|
1044
|
+
).toNumber();
|
|
1045
|
+
const dataLength = length * this.coder.encodedLength;
|
|
1046
|
+
const dataBytes = data.slice(offsetAndLength, offsetAndLength + dataLength);
|
|
1047
|
+
if (!this.#hasNestedOption && dataBytes.length !== dataLength) {
|
|
1048
|
+
throw new import_errors15.FuelError(
|
|
1049
|
+
import_errors15.ErrorCode.DECODE_ERROR,
|
|
1050
|
+
`Invalid vec byte data size.`
|
|
1051
|
+
);
|
|
1052
|
+
}
|
|
1053
|
+
let newOffset = offsetAndLength;
|
|
1054
|
+
const chunks = [];
|
|
1055
|
+
for (let i = 0; i < length; i++) {
|
|
1056
|
+
const [decoded, optionOffset] = this.coder.decode(data, newOffset);
|
|
1057
|
+
chunks.push(decoded);
|
|
1058
|
+
newOffset = optionOffset;
|
|
1059
|
+
}
|
|
1060
|
+
return [chunks, newOffset];
|
|
1061
|
+
}
|
|
1062
|
+
};
|
|
1063
|
+
|
|
1064
|
+
// src/Interface.ts
|
|
1065
|
+
var import_errors21 = require("@fuel-ts/errors");
|
|
1066
|
+
var import_utils12 = require("@fuel-ts/utils");
|
|
1067
|
+
|
|
1068
|
+
// src/ResolvedAbiType.ts
|
|
1069
|
+
var import_errors17 = require("@fuel-ts/errors");
|
|
1070
|
+
|
|
1071
|
+
// src/utils/json-abi.ts
|
|
1072
|
+
var import_errors16 = require("@fuel-ts/errors");
|
|
1073
|
+
var getEncodingVersion = (encoding) => {
|
|
1074
|
+
switch (encoding) {
|
|
1075
|
+
case void 0:
|
|
1076
|
+
case ENCODING_V1:
|
|
1077
|
+
return ENCODING_V1;
|
|
1078
|
+
default:
|
|
1079
|
+
throw new import_errors16.FuelError(
|
|
1080
|
+
import_errors16.ErrorCode.UNSUPPORTED_ENCODING_VERSION,
|
|
1081
|
+
`Encoding version '${encoding}' is unsupported.`
|
|
1082
|
+
);
|
|
1083
|
+
}
|
|
1084
|
+
};
|
|
1085
|
+
var findTypeById = (abi, typeId) => {
|
|
1086
|
+
const type = abi.types.find((t) => t.typeId === typeId);
|
|
1087
|
+
if (!type) {
|
|
1088
|
+
throw new import_errors16.FuelError(
|
|
1089
|
+
import_errors16.ErrorCode.TYPE_NOT_FOUND,
|
|
1090
|
+
`Type with typeId '${typeId}' doesn't exist in the ABI.`
|
|
1091
|
+
);
|
|
1092
|
+
}
|
|
1093
|
+
return type;
|
|
1094
|
+
};
|
|
1095
|
+
var findNonVoidInputs = (abi, inputs) =>
|
|
1096
|
+
inputs.filter((input) => findTypeById(abi, input.type).type !== VOID_TYPE);
|
|
1097
|
+
var findVectorBufferArgument = (components) => {
|
|
1098
|
+
const bufferComponent = components.find((c) => c.name === "buf");
|
|
1099
|
+
const bufferTypeArgument = bufferComponent?.originalTypeArguments?.[0];
|
|
1100
|
+
if (!bufferComponent || !bufferTypeArgument) {
|
|
1101
|
+
throw new import_errors16.FuelError(
|
|
1102
|
+
import_errors16.ErrorCode.INVALID_COMPONENT,
|
|
1103
|
+
`The Vec type provided is missing or has a malformed 'buf' component.`
|
|
1104
|
+
);
|
|
1105
|
+
}
|
|
1106
|
+
return bufferTypeArgument;
|
|
1107
|
+
};
|
|
1108
|
+
|
|
1109
|
+
// src/ResolvedAbiType.ts
|
|
1110
|
+
var ResolvedAbiType = class {
|
|
1111
|
+
abi;
|
|
1112
|
+
name;
|
|
1113
|
+
type;
|
|
1114
|
+
originalTypeArguments;
|
|
1115
|
+
components;
|
|
1116
|
+
constructor(abi, argument) {
|
|
1117
|
+
this.abi = abi;
|
|
1118
|
+
this.name = argument.name;
|
|
1119
|
+
const jsonABIType = findTypeById(abi, argument.type);
|
|
1120
|
+
if (jsonABIType.type.length > 256) {
|
|
1121
|
+
throw new import_errors17.FuelError(
|
|
1122
|
+
import_errors17.ErrorCode.INVALID_COMPONENT,
|
|
1123
|
+
`The provided ABI type is too long: ${jsonABIType.type}.`
|
|
1124
|
+
);
|
|
1125
|
+
}
|
|
1126
|
+
this.type = jsonABIType.type;
|
|
1127
|
+
this.originalTypeArguments = argument.typeArguments;
|
|
1128
|
+
this.components = ResolvedAbiType.getResolvedGenericComponents(
|
|
1129
|
+
abi,
|
|
1130
|
+
argument,
|
|
1131
|
+
jsonABIType.components,
|
|
1132
|
+
jsonABIType.typeParameters ??
|
|
1133
|
+
ResolvedAbiType.getImplicitGenericTypeParameters(
|
|
1134
|
+
abi,
|
|
1135
|
+
jsonABIType.components
|
|
1136
|
+
)
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
static getResolvedGenericComponents(abi, arg, components, typeParameters) {
|
|
1140
|
+
if (components === null) {
|
|
1141
|
+
return null;
|
|
1142
|
+
}
|
|
1143
|
+
if (typeParameters === null || typeParameters.length === 0) {
|
|
1144
|
+
return components.map((c) => new ResolvedAbiType(abi, c));
|
|
1145
|
+
}
|
|
1146
|
+
const typeParametersAndArgsMap = typeParameters.reduce(
|
|
1147
|
+
(obj, typeParameter, typeParameterIndex) => {
|
|
1148
|
+
const o = { ...obj };
|
|
1149
|
+
o[typeParameter] = structuredClone(
|
|
1150
|
+
arg.typeArguments?.[typeParameterIndex]
|
|
1151
|
+
);
|
|
1152
|
+
return o;
|
|
1153
|
+
},
|
|
1154
|
+
{}
|
|
1155
|
+
);
|
|
1156
|
+
const resolvedComponents = this.resolveGenericArgTypes(
|
|
1157
|
+
abi,
|
|
1158
|
+
components,
|
|
1159
|
+
typeParametersAndArgsMap
|
|
1160
|
+
);
|
|
1161
|
+
return resolvedComponents.map((c) => new ResolvedAbiType(abi, c));
|
|
1162
|
+
}
|
|
1163
|
+
static resolveGenericArgTypes(abi, args, typeParametersAndArgsMap) {
|
|
1164
|
+
return args.map((arg) => {
|
|
1165
|
+
if (typeParametersAndArgsMap[arg.type] !== void 0) {
|
|
1166
|
+
return {
|
|
1167
|
+
...typeParametersAndArgsMap[arg.type],
|
|
1168
|
+
name: arg.name,
|
|
1169
|
+
};
|
|
1170
|
+
}
|
|
1171
|
+
if (arg.typeArguments) {
|
|
1172
|
+
return {
|
|
1173
|
+
...structuredClone(arg),
|
|
1174
|
+
typeArguments: this.resolveGenericArgTypes(
|
|
1175
|
+
abi,
|
|
1176
|
+
arg.typeArguments,
|
|
1177
|
+
typeParametersAndArgsMap
|
|
1178
|
+
),
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
const argType = findTypeById(abi, arg.type);
|
|
1182
|
+
const implicitTypeParameters = this.getImplicitGenericTypeParameters(
|
|
1183
|
+
abi,
|
|
1184
|
+
argType.components
|
|
1185
|
+
);
|
|
1186
|
+
if (implicitTypeParameters && implicitTypeParameters.length > 0) {
|
|
1187
|
+
return {
|
|
1188
|
+
...structuredClone(arg),
|
|
1189
|
+
typeArguments: implicitTypeParameters.map(
|
|
1190
|
+
(itp) => typeParametersAndArgsMap[itp]
|
|
1191
|
+
),
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
return arg;
|
|
1195
|
+
});
|
|
1196
|
+
}
|
|
1197
|
+
static getImplicitGenericTypeParameters(
|
|
1198
|
+
abi,
|
|
1199
|
+
args,
|
|
1200
|
+
implicitGenericParametersParam
|
|
1201
|
+
) {
|
|
1202
|
+
if (!Array.isArray(args)) {
|
|
1203
|
+
return null;
|
|
1204
|
+
}
|
|
1205
|
+
const implicitGenericParameters = implicitGenericParametersParam ?? [];
|
|
1206
|
+
args.forEach((a) => {
|
|
1207
|
+
const argType = findTypeById(abi, a.type);
|
|
1208
|
+
if (genericRegEx.test(argType.type)) {
|
|
1209
|
+
implicitGenericParameters.push(argType.typeId);
|
|
1210
|
+
return;
|
|
1211
|
+
}
|
|
1212
|
+
if (!Array.isArray(a.typeArguments)) {
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
this.getImplicitGenericTypeParameters(
|
|
1216
|
+
abi,
|
|
1217
|
+
a.typeArguments,
|
|
1218
|
+
implicitGenericParameters
|
|
1219
|
+
);
|
|
1220
|
+
});
|
|
1221
|
+
return implicitGenericParameters.length > 0
|
|
1222
|
+
? implicitGenericParameters
|
|
1223
|
+
: null;
|
|
1224
|
+
}
|
|
1225
|
+
getSignature() {
|
|
1226
|
+
const prefix = this.getArgSignaturePrefix();
|
|
1227
|
+
const content = this.getArgSignatureContent();
|
|
1228
|
+
return `${prefix}${content}`;
|
|
1229
|
+
}
|
|
1230
|
+
getArgSignaturePrefix() {
|
|
1231
|
+
const structMatch = structRegEx.test(this.type);
|
|
1232
|
+
if (structMatch) {
|
|
1233
|
+
return "s";
|
|
1234
|
+
}
|
|
1235
|
+
const arrayMatch = arrayRegEx.test(this.type);
|
|
1236
|
+
if (arrayMatch) {
|
|
1237
|
+
return "a";
|
|
1238
|
+
}
|
|
1239
|
+
const enumMatch = enumRegEx.test(this.type);
|
|
1240
|
+
if (enumMatch) {
|
|
1241
|
+
return "e";
|
|
1242
|
+
}
|
|
1243
|
+
return "";
|
|
1244
|
+
}
|
|
1245
|
+
getArgSignatureContent() {
|
|
1246
|
+
if (this.type === "raw untyped ptr") {
|
|
1247
|
+
return "rawptr";
|
|
1248
|
+
}
|
|
1249
|
+
if (this.type === "raw untyped slice") {
|
|
1250
|
+
return "rawslice";
|
|
1251
|
+
}
|
|
1252
|
+
const strMatch = stringRegEx.exec(this.type)?.groups;
|
|
1253
|
+
if (strMatch) {
|
|
1254
|
+
return `str[${strMatch.length}]`;
|
|
1255
|
+
}
|
|
1256
|
+
if (this.components === null) {
|
|
1257
|
+
return this.type;
|
|
1258
|
+
}
|
|
1259
|
+
const arrayMatch = arrayRegEx.exec(this.type)?.groups;
|
|
1260
|
+
if (arrayMatch) {
|
|
1261
|
+
return `[${this.components[0].getSignature()};${arrayMatch.length}]`;
|
|
1262
|
+
}
|
|
1263
|
+
const typeArgumentsSignature =
|
|
1264
|
+
this.originalTypeArguments !== null
|
|
1265
|
+
? `<${this.originalTypeArguments
|
|
1266
|
+
.map((a) => new ResolvedAbiType(this.abi, a).getSignature())
|
|
1267
|
+
.join(",")}>`
|
|
1268
|
+
: "";
|
|
1269
|
+
const componentsSignature = `(${this.components
|
|
1270
|
+
.map((c) => c.getSignature())
|
|
1271
|
+
.join(",")})`;
|
|
1272
|
+
return `${typeArgumentsSignature}${componentsSignature}`;
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
|
|
1276
|
+
// src/encoding/strategies/getCoderForEncoding.ts
|
|
1277
|
+
var import_errors19 = require("@fuel-ts/errors");
|
|
1278
|
+
|
|
1279
|
+
// src/encoding/strategies/getCoderV1.ts
|
|
1280
|
+
var import_errors18 = require("@fuel-ts/errors");
|
|
1281
|
+
|
|
1282
|
+
// src/encoding/coders/VoidCoder.ts
|
|
1283
|
+
var VoidCoder = class extends Coder {
|
|
1284
|
+
constructor() {
|
|
1285
|
+
super("void", VOID_TYPE, 0);
|
|
1286
|
+
}
|
|
1287
|
+
encode(_value) {
|
|
1288
|
+
return new Uint8Array([]);
|
|
1289
|
+
}
|
|
1290
|
+
decode(_data, offset) {
|
|
1291
|
+
return [void 0, offset];
|
|
1292
|
+
}
|
|
1293
|
+
};
|
|
1294
|
+
|
|
1295
|
+
// src/encoding/strategies/getCoders.ts
|
|
1296
|
+
function getCoders(components, options) {
|
|
1297
|
+
const { getCoder: getCoder2 } = options;
|
|
1298
|
+
return components.reduce((obj, component) => {
|
|
1299
|
+
const o = obj;
|
|
1300
|
+
o[component.name] = getCoder2(component, options);
|
|
1301
|
+
return o;
|
|
1302
|
+
}, {});
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
// src/encoding/strategies/getCoderV1.ts
|
|
1306
|
+
var getCoder = (resolvedAbiType, _options) => {
|
|
1307
|
+
switch (resolvedAbiType.type) {
|
|
1308
|
+
case U8_CODER_TYPE:
|
|
1309
|
+
case U16_CODER_TYPE:
|
|
1310
|
+
case U32_CODER_TYPE:
|
|
1311
|
+
return new NumberCoder(resolvedAbiType.type);
|
|
1312
|
+
case U64_CODER_TYPE:
|
|
1313
|
+
case RAW_PTR_CODER_TYPE:
|
|
1314
|
+
return new BigNumberCoder("u64");
|
|
1315
|
+
case U256_CODER_TYPE:
|
|
1316
|
+
return new BigNumberCoder("u256");
|
|
1317
|
+
case RAW_SLICE_CODER_TYPE:
|
|
1318
|
+
return new RawSliceCoder();
|
|
1319
|
+
case BOOL_CODER_TYPE:
|
|
1320
|
+
return new BooleanCoder();
|
|
1321
|
+
case B256_CODER_TYPE:
|
|
1322
|
+
return new B256Coder();
|
|
1323
|
+
case B512_CODER_TYPE:
|
|
1324
|
+
return new B512Coder();
|
|
1325
|
+
case BYTES_CODER_TYPE:
|
|
1326
|
+
return new ByteCoder();
|
|
1327
|
+
case STD_STRING_CODER_TYPE:
|
|
1328
|
+
return new StdStringCoder();
|
|
1329
|
+
case STR_SLICE_CODER_TYPE:
|
|
1330
|
+
return new StrSliceCoder();
|
|
1331
|
+
case VOID_TYPE:
|
|
1332
|
+
return new VoidCoder();
|
|
1333
|
+
default:
|
|
1334
|
+
break;
|
|
1335
|
+
}
|
|
1336
|
+
const stringMatch = stringRegEx.exec(resolvedAbiType.type)?.groups;
|
|
1337
|
+
if (stringMatch) {
|
|
1338
|
+
const length = parseInt(stringMatch.length, 10);
|
|
1339
|
+
return new StringCoder(length);
|
|
1340
|
+
}
|
|
1341
|
+
const components = resolvedAbiType.components;
|
|
1342
|
+
const arrayMatch = arrayRegEx.exec(resolvedAbiType.type)?.groups;
|
|
1343
|
+
if (arrayMatch) {
|
|
1344
|
+
const length = parseInt(arrayMatch.length, 10);
|
|
1345
|
+
const arg = components[0];
|
|
1346
|
+
if (!arg) {
|
|
1347
|
+
throw new import_errors18.FuelError(
|
|
1348
|
+
import_errors18.ErrorCode.INVALID_COMPONENT,
|
|
1349
|
+
`The provided Array type is missing an item of 'component'.`
|
|
1350
|
+
);
|
|
1351
|
+
}
|
|
1352
|
+
const arrayElementCoder = getCoder(arg);
|
|
1353
|
+
return new ArrayCoder(arrayElementCoder, length);
|
|
1354
|
+
}
|
|
1355
|
+
if (resolvedAbiType.type === VEC_CODER_TYPE) {
|
|
1356
|
+
const arg = findVectorBufferArgument(components);
|
|
1357
|
+
const argType = new ResolvedAbiType(resolvedAbiType.abi, arg);
|
|
1358
|
+
const itemCoder = getCoder(argType, { encoding: ENCODING_V1 });
|
|
1359
|
+
return new VecCoder(itemCoder);
|
|
1360
|
+
}
|
|
1361
|
+
const coderName = resolvedAbiType.type.match(fullNameRegExMatch)?.[0];
|
|
1362
|
+
const structMatch = structRegEx.test(resolvedAbiType.type);
|
|
1363
|
+
if (structMatch && coderName) {
|
|
1364
|
+
const coders = getCoders(components, { getCoder });
|
|
1365
|
+
return new StructCoder(coderName, coders);
|
|
1366
|
+
}
|
|
1367
|
+
const enumMatch = enumRegEx.test(resolvedAbiType.type);
|
|
1368
|
+
if (enumMatch && coderName) {
|
|
1369
|
+
const coders = getCoders(components, { getCoder });
|
|
1370
|
+
const isOptionEnum = resolvedAbiType.type === OPTION_CODER_TYPE;
|
|
1371
|
+
if (isOptionEnum) {
|
|
1372
|
+
return new OptionCoder(coderName, coders);
|
|
1373
|
+
}
|
|
1374
|
+
return new EnumCoder(coderName, coders);
|
|
1375
|
+
}
|
|
1376
|
+
const tupleMatch = tupleRegEx.exec(resolvedAbiType.type)?.groups;
|
|
1377
|
+
if (tupleMatch) {
|
|
1378
|
+
const coders = components.map((component) =>
|
|
1379
|
+
getCoder(component, { encoding: ENCODING_V1 })
|
|
1380
|
+
);
|
|
1381
|
+
return new TupleCoder(coders);
|
|
1382
|
+
}
|
|
1383
|
+
throw new import_errors18.FuelError(
|
|
1384
|
+
import_errors18.ErrorCode.CODER_NOT_FOUND,
|
|
1385
|
+
`Coder not found: ${JSON.stringify(resolvedAbiType)}.`
|
|
1386
|
+
);
|
|
1387
|
+
};
|
|
1388
|
+
|
|
1389
|
+
// src/encoding/strategies/getCoderForEncoding.ts
|
|
1390
|
+
function getCoderForEncoding(encoding = ENCODING_V1) {
|
|
1391
|
+
switch (encoding) {
|
|
1392
|
+
case ENCODING_V1:
|
|
1393
|
+
return getCoder;
|
|
1394
|
+
default:
|
|
1395
|
+
throw new import_errors19.FuelError(
|
|
1396
|
+
import_errors19.ErrorCode.UNSUPPORTED_ENCODING_VERSION,
|
|
1397
|
+
`Encoding version ${encoding} is unsupported.`
|
|
1398
|
+
);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
// src/AbiCoder.ts
|
|
1403
|
+
var AbiCoder = class {
|
|
1404
|
+
static getCoder(
|
|
1405
|
+
abi,
|
|
1406
|
+
argument,
|
|
1407
|
+
options = {
|
|
1408
|
+
padToWordSize: false,
|
|
1409
|
+
}
|
|
1410
|
+
) {
|
|
1411
|
+
const resolvedAbiType = new ResolvedAbiType(abi, argument);
|
|
1412
|
+
return getCoderForEncoding(options.encoding)(resolvedAbiType, options);
|
|
1413
|
+
}
|
|
1414
|
+
static getLogDecoder(
|
|
1415
|
+
abi,
|
|
1416
|
+
logId,
|
|
1417
|
+
options = {
|
|
1418
|
+
padToWordSize: false,
|
|
1419
|
+
}
|
|
1420
|
+
) {
|
|
1421
|
+
const loggedType = abi.loggedTypes.find((type) => type.logId === logId);
|
|
1422
|
+
if (!loggedType) {
|
|
1423
|
+
throw new import_errors20.FuelError(
|
|
1424
|
+
import_errors20.ErrorCode.LOG_TYPE_NOT_FOUND,
|
|
1425
|
+
`Log type with logId '${logId}' doesn't exist in the ABI.`
|
|
1426
|
+
);
|
|
1427
|
+
}
|
|
1428
|
+
const resolvedAbiType = new ResolvedAbiType(abi, loggedType.loggedType);
|
|
1429
|
+
const internalCoder = getCoderForEncoding(options.encoding)(
|
|
1430
|
+
resolvedAbiType,
|
|
1431
|
+
options
|
|
1432
|
+
);
|
|
1433
|
+
return (data) => internalCoder.decode(import_utils12.arrayify(data), 0)[0];
|
|
1434
|
+
}
|
|
1435
|
+
static encode(abi, argument, value, options) {
|
|
1436
|
+
return this.getCoder(abi, argument, options).encode(value);
|
|
1437
|
+
}
|
|
1438
|
+
static decode(abi, argument, data, offset, options) {
|
|
1439
|
+
return this.getCoder(abi, argument, options).decode(data, offset);
|
|
1440
|
+
}
|
|
1441
|
+
};
|
|
1442
|
+
|
|
1443
|
+
// src/FunctionFragment.ts
|
|
1444
|
+
var import_crypto = require("@fuel-ts/crypto");
|
|
1445
|
+
var import_errors20 = require("@fuel-ts/errors");
|
|
1446
|
+
var import_hasher = require("@fuel-ts/hasher");
|
|
1447
|
+
var import_math12 = require("@fuel-ts/math");
|
|
1448
|
+
var import_utils11 = require("@fuel-ts/utils");
|
|
1449
|
+
|
|
1450
|
+
// src/utils/getFunctionInputs.ts
|
|
1451
|
+
var getFunctionInputs = (params) => {
|
|
1452
|
+
const { jsonAbi, inputs } = params;
|
|
1453
|
+
let isMandatory = false;
|
|
1454
|
+
return inputs.reduceRight((result, input) => {
|
|
1455
|
+
const type = findTypeById(jsonAbi, input.type);
|
|
1456
|
+
isMandatory =
|
|
1457
|
+
isMandatory || (type.type !== VOID_TYPE && !optionRegEx.test(type.type));
|
|
1458
|
+
return [{ ...input, isOptional: !isMandatory }, ...result];
|
|
1459
|
+
}, []);
|
|
1460
|
+
};
|
|
1461
|
+
|
|
1462
|
+
// src/utils/padValuesWithUndefined.ts
|
|
1463
|
+
var padValuesWithUndefined = (values, inputs) => {
|
|
1464
|
+
if (values.length >= inputs.length) {
|
|
1465
|
+
return values;
|
|
1466
|
+
}
|
|
1467
|
+
const paddedValues = values.slice();
|
|
1468
|
+
paddedValues.length = inputs.length;
|
|
1469
|
+
paddedValues.fill(void 0, values.length);
|
|
1470
|
+
return paddedValues;
|
|
1471
|
+
};
|
|
1472
|
+
|
|
1473
|
+
// src/FunctionFragment.ts
|
|
1474
|
+
var FunctionFragment = class {
|
|
1475
|
+
signature;
|
|
1476
|
+
selector;
|
|
1477
|
+
selectorBytes;
|
|
1478
|
+
encoding;
|
|
1479
|
+
name;
|
|
1480
|
+
jsonFn;
|
|
1481
|
+
attributes;
|
|
1482
|
+
jsonAbiOld;
|
|
1483
|
+
jsonFnOld;
|
|
1484
|
+
constructor(jsonAbi, fn) {
|
|
1485
|
+
this.jsonFn = fn;
|
|
1486
|
+
this.jsonAbiOld = jsonAbi;
|
|
1487
|
+
this.jsonFnOld = jsonAbi.functions.find((f) => f.name === fn.name);
|
|
1488
|
+
this.name = fn.name;
|
|
1489
|
+
this.signature = FunctionFragment.getSignature(
|
|
1490
|
+
this.jsonAbiOld,
|
|
1491
|
+
this.jsonFnOld
|
|
1492
|
+
);
|
|
1493
|
+
this.selector = FunctionFragment.getFunctionSelector(this.signature);
|
|
1494
|
+
this.selectorBytes = new StdStringCoder().encode(this.name);
|
|
1495
|
+
this.encoding = getEncodingVersion(jsonAbi.encoding);
|
|
1496
|
+
this.attributes = this.jsonFn.attributes ?? [];
|
|
1497
|
+
}
|
|
1498
|
+
static getSignature(abi, fn) {
|
|
1499
|
+
const inputsSignatures = fn.inputs.map((input) =>
|
|
1500
|
+
new ResolvedAbiType(abi, input).getSignature()
|
|
1501
|
+
);
|
|
1502
|
+
return `${fn.name}(${inputsSignatures.join(",")})`;
|
|
1503
|
+
}
|
|
1504
|
+
static getFunctionSelector(functionSignature) {
|
|
1505
|
+
const hashedFunctionSignature = (0, import_hasher.sha256)(
|
|
1506
|
+
(0, import_crypto.bufferFromString)(functionSignature, "utf-8")
|
|
1507
|
+
);
|
|
1508
|
+
return (0, import_math12.bn)(hashedFunctionSignature.slice(0, 10)).toHex(8);
|
|
1509
|
+
}
|
|
1510
|
+
encodeArguments(values) {
|
|
1511
|
+
const inputs = getFunctionInputs({
|
|
1512
|
+
jsonAbi: this.jsonAbiOld,
|
|
1513
|
+
inputs: this.jsonFnOld.inputs,
|
|
1514
|
+
});
|
|
1515
|
+
const mandatoryInputLength = inputs.filter((i) => !i.isOptional).length;
|
|
1516
|
+
if (values.length < mandatoryInputLength) {
|
|
1517
|
+
throw new import_errors20.FuelError(
|
|
1518
|
+
import_errors20.ErrorCode.ABI_TYPES_AND_VALUES_MISMATCH,
|
|
1519
|
+
`Invalid number of arguments. Expected a minimum of ${mandatoryInputLength} arguments, received ${values.length}`
|
|
1520
|
+
);
|
|
1521
|
+
}
|
|
1522
|
+
const coders = this.jsonFnOld.inputs.map((t) =>
|
|
1523
|
+
AbiCoder.getCoder(this.jsonAbiOld, t, {
|
|
1524
|
+
encoding: this.encoding,
|
|
1525
|
+
})
|
|
1526
|
+
);
|
|
1527
|
+
const argumentValues = padValuesWithUndefined(values, this.jsonFn.inputs);
|
|
1528
|
+
return new TupleCoder(coders).encode(argumentValues);
|
|
1529
|
+
}
|
|
1530
|
+
decodeArguments(data) {
|
|
1531
|
+
const bytes = (0, import_utils11.arrayify)(data);
|
|
1532
|
+
const nonVoidInputs = findNonVoidInputs(
|
|
1533
|
+
this.jsonAbiOld,
|
|
1534
|
+
this.jsonFnOld.inputs
|
|
1535
|
+
);
|
|
1536
|
+
if (nonVoidInputs.length === 0) {
|
|
1537
|
+
if (bytes.length === 0) {
|
|
1538
|
+
return void 0;
|
|
1539
|
+
}
|
|
1540
|
+
throw new import_errors20.FuelError(
|
|
1541
|
+
import_errors20.ErrorCode.DECODE_ERROR,
|
|
1542
|
+
`Types/values length mismatch during decode. ${JSON.stringify({
|
|
1543
|
+
count: {
|
|
1544
|
+
types: this.jsonFn.inputs.length,
|
|
1545
|
+
nonVoidInputs: nonVoidInputs.length,
|
|
1546
|
+
values: bytes.length,
|
|
1547
|
+
},
|
|
1548
|
+
value: {
|
|
1549
|
+
args: this.jsonFn.inputs,
|
|
1550
|
+
nonVoidInputs,
|
|
1551
|
+
values: bytes,
|
|
1552
|
+
},
|
|
1553
|
+
})}`
|
|
1554
|
+
);
|
|
1555
|
+
}
|
|
1556
|
+
const result = this.jsonFnOld.inputs.reduce(
|
|
1557
|
+
(obj, input) => {
|
|
1558
|
+
const coder = AbiCoder.getCoder(this.jsonAbiOld, input, {
|
|
1559
|
+
encoding: this.encoding,
|
|
1560
|
+
});
|
|
1561
|
+
const [decodedValue, decodedValueByteSize] = coder.decode(
|
|
1562
|
+
bytes,
|
|
1563
|
+
obj.offset
|
|
1564
|
+
);
|
|
1565
|
+
return {
|
|
1566
|
+
decoded: [...obj.decoded, decodedValue],
|
|
1567
|
+
offset: obj.offset + decodedValueByteSize,
|
|
1568
|
+
};
|
|
1569
|
+
},
|
|
1570
|
+
{ decoded: [], offset: 0 }
|
|
1571
|
+
);
|
|
1572
|
+
return result.decoded;
|
|
1573
|
+
}
|
|
1574
|
+
decodeOutput(data) {
|
|
1575
|
+
const bytes = (0, import_utils11.arrayify)(data);
|
|
1576
|
+
const coder = AbiCoder.getCoder(this.jsonAbiOld, this.jsonFnOld.output, {
|
|
1577
|
+
encoding: this.encoding,
|
|
1578
|
+
});
|
|
1579
|
+
return coder.decode(bytes, 0);
|
|
1580
|
+
}
|
|
1581
|
+
/**
|
|
1582
|
+
* Checks if the function is read-only i.e. it only reads from storage, does not write to it.
|
|
1583
|
+
*
|
|
1584
|
+
* @returns True if the function is read-only or pure, false otherwise.
|
|
1585
|
+
*/
|
|
1586
|
+
isReadOnly() {
|
|
1587
|
+
const storageAttribute = this.attributes.find(
|
|
1588
|
+
(attr) => attr.name === "storage"
|
|
1589
|
+
);
|
|
1590
|
+
return !storageAttribute?.arguments?.includes("write");
|
|
1591
|
+
}
|
|
1592
|
+
};
|
|
1593
|
+
|
|
1594
|
+
// src/utils/transpile-abi.ts
|
|
1595
|
+
var findTypeByConcreteId = (types, id) =>
|
|
1596
|
+
types.find((x) => x.concreteTypeId === id);
|
|
1597
|
+
var findConcreteTypeById = (abi, id) =>
|
|
1598
|
+
abi.concreteTypes.find((x) => x.concreteTypeId === id);
|
|
1599
|
+
function finsertTypeIdByConcreteTypeId(abi, types, id) {
|
|
1600
|
+
const concreteType = findConcreteTypeById(abi, id);
|
|
1601
|
+
if (concreteType.metadataTypeId !== void 0) {
|
|
1602
|
+
return concreteType.metadataTypeId;
|
|
1603
|
+
}
|
|
1604
|
+
const type = findTypeByConcreteId(types, id);
|
|
1605
|
+
if (type) {
|
|
1606
|
+
return type.typeId;
|
|
1607
|
+
}
|
|
1608
|
+
types.push({
|
|
1609
|
+
typeId: types.length,
|
|
1610
|
+
type: concreteType.type,
|
|
1611
|
+
components: parseComponents(concreteType.components),
|
|
1612
|
+
concreteTypeId: id,
|
|
1613
|
+
typeParameters: concreteType.typeParameters ?? null,
|
|
1614
|
+
originalConcreteTypeId: concreteType?.concreteTypeId,
|
|
1615
|
+
});
|
|
1616
|
+
return types.length - 1;
|
|
1617
|
+
}
|
|
1618
|
+
function parseFunctionTypeArguments(abi, types, concreteType) {
|
|
1619
|
+
return (
|
|
1620
|
+
concreteType.typeArguments?.map((cTypeId) => {
|
|
1621
|
+
const self = findConcreteTypeById(abi, cTypeId);
|
|
1622
|
+
const type = !isNaN(cTypeId)
|
|
1623
|
+
? cTypeId
|
|
1624
|
+
: finsertTypeIdByConcreteTypeId(abi, types, cTypeId);
|
|
1625
|
+
return {
|
|
1626
|
+
name: "",
|
|
1627
|
+
type,
|
|
1628
|
+
// originalTypeId: cTypeId,
|
|
1629
|
+
typeArguments: parseFunctionTypeArguments(abi, types, self),
|
|
1630
|
+
};
|
|
1631
|
+
}) ?? null
|
|
1632
|
+
);
|
|
1633
|
+
}
|
|
1634
|
+
function parseConcreteType(abi, types, concreteTypeId, name) {
|
|
1635
|
+
const type = finsertTypeIdByConcreteTypeId(abi, types, concreteTypeId);
|
|
1636
|
+
const concrete = findConcreteTypeById(abi, concreteTypeId);
|
|
1637
|
+
return {
|
|
1638
|
+
name: name ?? "",
|
|
1639
|
+
type,
|
|
1640
|
+
// concreteTypeId,
|
|
1641
|
+
typeArguments: parseFunctionTypeArguments(abi, types, concrete),
|
|
1642
|
+
};
|
|
1643
|
+
}
|
|
1644
|
+
function parseComponents(abi, types, components) {
|
|
1645
|
+
return (
|
|
1646
|
+
components?.map((component) => {
|
|
1647
|
+
const { typeId, name, typeArguments } = component;
|
|
1648
|
+
const type = !isNaN(typeId)
|
|
1649
|
+
? typeId
|
|
1650
|
+
: finsertTypeIdByConcreteTypeId(abi, types, typeId);
|
|
1651
|
+
return {
|
|
1652
|
+
name,
|
|
1653
|
+
type,
|
|
1654
|
+
// originalTypeId: typeId,
|
|
1655
|
+
typeArguments: parseComponents(abi, types, typeArguments),
|
|
1656
|
+
};
|
|
1657
|
+
}) ?? null
|
|
1658
|
+
);
|
|
1659
|
+
}
|
|
1660
|
+
function transpileAbi(abi) {
|
|
1661
|
+
if (!abi.specVersion) {
|
|
1662
|
+
return abi;
|
|
1663
|
+
}
|
|
1664
|
+
const types = [];
|
|
1665
|
+
abi.metadataTypes.forEach((m) => {
|
|
1666
|
+
const t = {
|
|
1667
|
+
typeId: m.metadataTypeId,
|
|
1668
|
+
type: m.type,
|
|
1669
|
+
components: m.components ?? (m.type === "()" ? [] : null),
|
|
1670
|
+
typeParameters: m.typeParameters ?? null,
|
|
1671
|
+
};
|
|
1672
|
+
types.push(t);
|
|
1673
|
+
});
|
|
1674
|
+
types.forEach((t) => {
|
|
1675
|
+
t.components = parseComponents(abi, types, t.components);
|
|
1676
|
+
});
|
|
1677
|
+
const functions = abi.functions.map((fn) => {
|
|
1678
|
+
const inputs = fn.inputs.map(({ concreteTypeId, name }) =>
|
|
1679
|
+
parseConcreteType(abi, types, concreteTypeId, name)
|
|
1680
|
+
);
|
|
1681
|
+
const output = parseConcreteType(abi, types, fn.output, "");
|
|
1682
|
+
return { ...fn, inputs, output };
|
|
1683
|
+
});
|
|
1684
|
+
const configurables = abi.configurables.map((conf) => ({
|
|
1685
|
+
name: conf.name,
|
|
1686
|
+
configurableType: parseConcreteType(abi, types, conf.concreteTypeId),
|
|
1687
|
+
offset: conf.offset,
|
|
1688
|
+
}));
|
|
1689
|
+
const loggedTypes = abi.loggedTypes.map((log) => ({
|
|
1690
|
+
logId: log.logId,
|
|
1691
|
+
loggedType: parseConcreteType(abi, types, log.concreteTypeId),
|
|
1692
|
+
}));
|
|
1693
|
+
const transpiled = {
|
|
1694
|
+
encoding: abi.encodingVersion,
|
|
1695
|
+
types,
|
|
1696
|
+
functions,
|
|
1697
|
+
loggedTypes,
|
|
1698
|
+
messagesTypes: abi.messagesTypes,
|
|
1699
|
+
configurables,
|
|
1700
|
+
};
|
|
1701
|
+
return transpiled;
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
// src/Interface.ts
|
|
1705
|
+
var Interface = class {
|
|
1706
|
+
functions;
|
|
1707
|
+
configurables;
|
|
1708
|
+
jsonAbi;
|
|
1709
|
+
encoding;
|
|
1710
|
+
jsonAbiOld;
|
|
1711
|
+
constructor(jsonAbi) {
|
|
1712
|
+
this.jsonAbi = jsonAbi;
|
|
1713
|
+
this.encoding = getEncodingVersion(jsonAbi.encodingVersion);
|
|
1714
|
+
this.jsonAbiOld = transpileAbi(jsonAbi);
|
|
1715
|
+
this.functions = Object.fromEntries(
|
|
1716
|
+
this.jsonAbi.functions.map((fn) => [
|
|
1717
|
+
fn.name,
|
|
1718
|
+
new FunctionFragment(this.jsonAbiOld, fn),
|
|
1719
|
+
])
|
|
1720
|
+
);
|
|
1721
|
+
this.configurables = Object.fromEntries(
|
|
1722
|
+
this.jsonAbi.configurables.map((x) => [x.name, x])
|
|
1723
|
+
);
|
|
1724
|
+
}
|
|
1725
|
+
/**
|
|
1726
|
+
* Returns function fragment for a dynamic input.
|
|
1727
|
+
* @param nameOrSignatureOrSelector - name (e.g. 'transfer'), signature (e.g. 'transfer(address,uint256)') or selector (e.g. '0x00000000a9059cbb') of the function fragment
|
|
1728
|
+
*/
|
|
1729
|
+
getFunction(nameOrSignatureOrSelector) {
|
|
1730
|
+
const fn = Object.values(this.functions).find(
|
|
1731
|
+
(f) =>
|
|
1732
|
+
f.name === nameOrSignatureOrSelector ||
|
|
1733
|
+
f.signature === nameOrSignatureOrSelector ||
|
|
1734
|
+
f.selector === nameOrSignatureOrSelector
|
|
1735
|
+
);
|
|
1736
|
+
if (fn !== void 0) {
|
|
1737
|
+
return fn;
|
|
1738
|
+
}
|
|
1739
|
+
throw new import_errors21.FuelError(
|
|
1740
|
+
import_errors21.ErrorCode.FUNCTION_NOT_FOUND,
|
|
1741
|
+
`function ${nameOrSignatureOrSelector} not found: ${JSON.stringify(fn)}.`
|
|
1742
|
+
);
|
|
1743
|
+
}
|
|
1744
|
+
// Decode the result of a function call
|
|
1745
|
+
decodeFunctionResult(functionFragment, data) {
|
|
1746
|
+
const fragment =
|
|
1747
|
+
typeof functionFragment === "string"
|
|
1748
|
+
? this.getFunction(functionFragment)
|
|
1749
|
+
: functionFragment;
|
|
1750
|
+
return fragment.decodeOutput(data);
|
|
1751
|
+
}
|
|
1752
|
+
decodeLog(data, logId) {
|
|
1753
|
+
const loggedType = this.jsonAbiOld.loggedTypes.find(
|
|
1754
|
+
(type) => type.logId === logId
|
|
1755
|
+
);
|
|
1756
|
+
if (!loggedType) {
|
|
1757
|
+
throw new import_errors21.FuelError(
|
|
1758
|
+
import_errors21.ErrorCode.LOG_TYPE_NOT_FOUND,
|
|
1759
|
+
`Log type with logId '${logId}' doesn't exist in the ABI.`
|
|
1760
|
+
);
|
|
1761
|
+
}
|
|
1762
|
+
return AbiCoder.decode(
|
|
1763
|
+
this.jsonAbiOld,
|
|
1764
|
+
loggedType.loggedType,
|
|
1765
|
+
(0, import_utils12.arrayify)(data),
|
|
1766
|
+
0,
|
|
1767
|
+
{
|
|
1768
|
+
encoding: this.encoding,
|
|
1769
|
+
}
|
|
1770
|
+
);
|
|
1771
|
+
}
|
|
1772
|
+
encodeConfigurable(name, value) {
|
|
1773
|
+
const configurable = this.jsonAbiOld.configurables.find(
|
|
1774
|
+
(c) => c.name === name
|
|
1775
|
+
);
|
|
1776
|
+
if (!configurable) {
|
|
1777
|
+
throw new import_errors21.FuelError(
|
|
1778
|
+
import_errors21.ErrorCode.CONFIGURABLE_NOT_FOUND,
|
|
1779
|
+
`A configurable with the '${name}' was not found in the ABI.`
|
|
1780
|
+
);
|
|
1781
|
+
}
|
|
1782
|
+
return AbiCoder.encode(
|
|
1783
|
+
this.jsonAbiOld,
|
|
1784
|
+
configurable.configurableType,
|
|
1785
|
+
value,
|
|
1786
|
+
{
|
|
1787
|
+
encoding: this.encoding,
|
|
1788
|
+
}
|
|
1789
|
+
);
|
|
1790
|
+
}
|
|
1791
|
+
encodeType(concreteTypeId, value) {
|
|
1792
|
+
const typeArg = parseConcreteType(
|
|
1793
|
+
this.jsonAbi,
|
|
1794
|
+
this.jsonAbiOld.types,
|
|
1795
|
+
concreteTypeId,
|
|
1796
|
+
""
|
|
1797
|
+
);
|
|
1798
|
+
return AbiCoder.encode(this.jsonAbiOld, typeArg, value, {
|
|
1799
|
+
encoding: this.encoding,
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
decodeType(concreteTypeId, data) {
|
|
1803
|
+
const typeArg = parseConcreteType(
|
|
1804
|
+
this.jsonAbi,
|
|
1805
|
+
this.jsonAbiOld.types,
|
|
1806
|
+
concreteTypeId,
|
|
1807
|
+
""
|
|
1808
|
+
);
|
|
1809
|
+
return AbiCoder.decode(this.jsonAbiOld, typeArg, data, 0, {
|
|
1810
|
+
encoding: this.encoding,
|
|
1811
|
+
});
|
|
1812
|
+
}
|
|
1813
|
+
};
|
|
1814
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1815
|
+
0 &&
|
|
1816
|
+
(module.exports = {
|
|
1817
|
+
AbiCoder,
|
|
1818
|
+
transpileAbi,
|
|
1819
|
+
ASSET_ID_LEN,
|
|
1820
|
+
ArrayCoder,
|
|
1821
|
+
B256Coder,
|
|
1822
|
+
B512Coder,
|
|
1823
|
+
BYTES_32,
|
|
1824
|
+
BigNumberCoder,
|
|
1825
|
+
BooleanCoder,
|
|
1826
|
+
ByteCoder,
|
|
1827
|
+
CONTRACT_ID_LEN,
|
|
1828
|
+
Coder,
|
|
1829
|
+
ENCODING_V1,
|
|
1830
|
+
EnumCoder,
|
|
1831
|
+
INPUT_COIN_FIXED_SIZE,
|
|
1832
|
+
Interface,
|
|
1833
|
+
NumberCoder,
|
|
1834
|
+
OptionCoder,
|
|
1835
|
+
RawSliceCoder,
|
|
1836
|
+
SCRIPT_FIXED_SIZE,
|
|
1837
|
+
StdStringCoder,
|
|
1838
|
+
StrSliceCoder,
|
|
1839
|
+
StringCoder,
|
|
1840
|
+
StructCoder,
|
|
1841
|
+
TupleCoder,
|
|
1842
|
+
UTXO_ID_LEN,
|
|
1843
|
+
VecCoder,
|
|
1844
|
+
WORD_SIZE,
|
|
1845
|
+
calculateVmTxMemory,
|
|
1846
|
+
});
|
|
1847
|
+
//# sourceMappingURL=index.js.map
|