1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import { DataItem } from 'ethereum-types';
import * as _ from 'lodash';
import { Calldata } from '../calldata/calldata';
import { CalldataBlock } from '../calldata/calldata_block';
import { RawCalldata } from '../calldata/raw_calldata';
import { constants } from '../utils/constants';
import { DecodingRules, EncodingRules } from '../utils/rules';
import { DataTypeFactory } from './interfaces';
export abstract class DataType {
private readonly _dataItem: DataItem;
private readonly _factory: DataTypeFactory;
constructor(dataItem: DataItem, factory: DataTypeFactory) {
this._dataItem = dataItem;
this._factory = factory;
}
public getDataItem(): DataItem {
return this._dataItem;
}
public getFactory(): DataTypeFactory {
return this._factory;
}
public encode(value: any, rules?: EncodingRules, selector?: string): string {
const rules_ = _.isUndefined(rules) ? constants.DEFAULT_ENCODING_RULES : rules;
const calldata = new Calldata(rules_);
if (!_.isUndefined(selector)) {
calldata.setSelector(selector);
}
const block = this.generateCalldataBlock(value);
calldata.setRoot(block);
const encodedCalldata = calldata.toString();
return encodedCalldata;
}
public decode(calldata: string, rules?: DecodingRules, selector?: string): any {
if (!_.isUndefined(selector) && !_.startsWith(calldata, selector)) {
throw new Error(
`Tried to decode calldata, but it was missing the function selector. Expected prefix '${selector}'. Got '${calldata}'.`,
);
}
const hasSelector = !_.isUndefined(selector);
const rawCalldata = new RawCalldata(calldata, hasSelector);
const rules_ = _.isUndefined(rules) ? constants.DEFAULT_DECODING_RULES : rules;
const value = this.generateValue(rawCalldata, rules_);
return value;
}
public getSignature(detailed?: boolean): string {
if (_.isEmpty(this._dataItem.name) || !detailed) return this.getSignatureType();
const name = this.getDataItem().name;
const shortName = name.indexOf('.') > 0 ? name.substr(name.lastIndexOf('.') + 1) : name;
const detailedSignature = `${shortName} ${this.getSignatureType()}`;
return detailedSignature;
}
public abstract generateCalldataBlock(value: any, parentBlock?: CalldataBlock): CalldataBlock;
public abstract generateValue(calldata: RawCalldata, rules: DecodingRules): any;
public abstract getSignatureType(): string;
public abstract isStatic(): boolean;
}
|