/
contract.ts
47 lines (40 loc) · 1.19 KB
/
contract.ts
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
import { ABIMethod, ABIMethodParams } from './method';
export interface ABIContractNetworkInfo {
appID: number;
}
export interface ABIContractNetworks {
[network: string]: ABIContractNetworkInfo;
}
export interface ABIContractParams {
name: string;
desc?: string;
networks?: ABIContractNetworks;
methods: ABIMethodParams[];
}
export class ABIContract {
public readonly name: string;
public readonly description?: string;
public readonly networks: ABIContractNetworks;
public readonly methods: ABIMethod[];
constructor(params: ABIContractParams) {
if (
typeof params.name !== 'string' ||
!Array.isArray(params.methods) ||
(params.networks && typeof params.networks !== 'object')
) {
throw new Error('Invalid ABIContract parameters');
}
this.name = params.name;
this.description = params.desc;
this.networks = params.networks ? { ...params.networks } : {};
this.methods = params.methods.map((method) => new ABIMethod(method));
}
toJSON(): ABIContractParams {
return {
name: this.name,
desc: this.description,
networks: this.networks,
methods: this.methods.map((method) => method.toJSON()),
};
}
}