1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.DataType = void 0;
|
4 | const _ = require("lodash");
|
5 | const calldata_1 = require("../calldata/calldata");
|
6 | const raw_calldata_1 = require("../calldata/raw_calldata");
|
7 | const constants_1 = require("../utils/constants");
|
8 | class DataType {
|
9 | constructor(dataItem, factory) {
|
10 | this._dataItem = dataItem;
|
11 | this._factory = factory;
|
12 | }
|
13 | getDataItem() {
|
14 | return this._dataItem;
|
15 | }
|
16 | getFactory() {
|
17 | return this._factory;
|
18 | }
|
19 | encode(value, rules, selector) {
|
20 | const rules_ = Object.assign(Object.assign({}, constants_1.constants.DEFAULT_ENCODING_RULES), rules);
|
21 | const calldata = new calldata_1.Calldata(rules_);
|
22 | if (selector !== undefined) {
|
23 | calldata.setSelector(selector);
|
24 | }
|
25 | const block = this.generateCalldataBlock(value);
|
26 | calldata.setRoot(block);
|
27 | const encodedCalldata = calldata.toString();
|
28 | return encodedCalldata;
|
29 | }
|
30 | decode(calldata, rules, selector) {
|
31 | if (selector !== undefined && !_.startsWith(calldata, selector)) {
|
32 | throw new Error(`Tried to decode calldata, but it was missing the function selector. Expected prefix '${selector}'. Got '${calldata}'.`);
|
33 | }
|
34 | const hasSelector = selector !== undefined;
|
35 | const rawCalldata = new raw_calldata_1.RawCalldata(calldata, hasSelector);
|
36 | const rules_ = Object.assign(Object.assign({}, constants_1.constants.DEFAULT_DECODING_RULES), rules);
|
37 | const value = rules_.isStrictMode || rawCalldata.getSizeInBytes() > 0
|
38 | ? this.generateValue(rawCalldata, rules_)
|
39 | : this.getDefaultValue(rules_);
|
40 | return value;
|
41 | }
|
42 | decodeAsArray(returndata, rules) {
|
43 | const value = this.decode(returndata, rules);
|
44 | const valuesAsArray = _.isObject(value) ? _.values(value) : [value];
|
45 | return valuesAsArray;
|
46 | }
|
47 | getSignature(isDetailed) {
|
48 | if (_.isEmpty(this._dataItem.name) || !isDetailed) {
|
49 | return this.getSignatureType();
|
50 | }
|
51 | const name = this.getDataItem().name;
|
52 | const lastIndexOfScopeDelimiter = name.lastIndexOf('.');
|
53 | const isScopedName = lastIndexOfScopeDelimiter !== undefined && lastIndexOfScopeDelimiter > 0;
|
54 | const shortName = isScopedName ? name.substr(lastIndexOfScopeDelimiter + 1) : name;
|
55 | const detailedSignature = `${shortName} ${this.getSignatureType()}`;
|
56 | return detailedSignature;
|
57 | }
|
58 | }
|
59 | exports.DataType = DataType;
|
60 |
|
\ | No newline at end of file |