aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_encoder/evm_data_type_factory.ts
blob: a3e1d404eedf50cae872c15af45e10296e759e64 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* tslint:disable prefer-function-over-method */
/* tslint:disable max-classes-per-file */
/* tslint:disable no-construct */
import { DataItem, MethodAbi } from 'ethereum-types';

import { DataType, DataTypeFactory } from './data_type';
import * as Impl from './evm_data_types';

export class Address extends Impl.Address {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Bool extends Impl.Bool {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Int extends Impl.Int {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class UInt extends Impl.UInt {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class StaticBytes extends Impl.StaticBytes {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class DynamicBytes extends Impl.DynamicBytes {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class String extends Impl.String {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Pointer extends Impl.Pointer {
    public constructor(destDataType: DataType, parentDataType: DataType) {
        super(destDataType, parentDataType, EvmDataTypeFactory.getInstance());
    }
}

export class Tuple extends Impl.Tuple {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Array extends Impl.Array {
    public constructor(dataItem: DataItem) {
        super(dataItem, EvmDataTypeFactory.getInstance());
    }
}

export class Method extends Impl.Method {
    public constructor(abi: MethodAbi) {
        super(abi, EvmDataTypeFactory.getInstance());
    }
}

export class EvmDataTypeFactory implements DataTypeFactory {
    private static _instance: DataTypeFactory;

    public static getInstance(): DataTypeFactory {
        if (!EvmDataTypeFactory._instance) {
            EvmDataTypeFactory._instance = new EvmDataTypeFactory();
        }
        return EvmDataTypeFactory._instance;
    }

    public mapDataItemToDataType(dataItem: DataItem): DataType {
        if (Array.matchType(dataItem.type)) {
            return new Array(dataItem);
        } else if (Address.matchType(dataItem.type)) {
            return new Address(dataItem);
        } else if (Bool.matchType(dataItem.type)) {
            return new Bool(dataItem);
        } else if (Int.matchType(dataItem.type)) {
            return new Int(dataItem);
        } else if (UInt.matchType(dataItem.type)) {
            return new UInt(dataItem);
        } else if (StaticBytes.matchType(dataItem.type)) {
            return new StaticBytes(dataItem);
        } else if (Tuple.matchType(dataItem.type)) {
            return new Tuple(dataItem);
        } else if (DynamicBytes.matchType(dataItem.type)) {
            return new DynamicBytes(dataItem);
        } else if (String.matchType(dataItem.type)) {
            return new String(dataItem);
        }
        // @TODO: Implement Fixed/UFixed types
        throw new Error(`Unrecognized data type: '${dataItem.type}'`);
    }

    public create(dataItem: DataItem, parentDataType?: DataType): DataType {
        const dataType = this.mapDataItemToDataType(dataItem);
        if (dataType.isStatic()) {
            return dataType;
        }

        if (parentDataType === undefined) {
            // @Todo -- will this work for return values?
            throw new Error(`Trying to create a pointer`);
        }
        const pointer = new Pointer(dataType, parentDataType);
        return pointer;
    }

    private constructor() {}
}