forked from algorand/js-algorand-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.ts
57 lines (49 loc) · 1.56 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
48
49
50
51
52
53
54
55
56
57
import { ABIMethod, ABIMethodParams, getMethodByName } from './method';
import { ARC28Event } from './event';
export interface ABIContractNetworkInfo {
appID: number;
}
export interface ABIContractNetworks {
[network: string]: ABIContractNetworkInfo;
}
export interface ABIContractParams {
name: string;
desc?: string;
networks?: ABIContractNetworks;
methods: ABIMethodParams[];
events?: ARC28Event[];
}
export class ABIContract {
public readonly name: string;
public readonly description?: string;
public readonly networks: ABIContractNetworks;
public readonly methods: ABIMethod[];
/** [ARC-28](https://arc.algorand.foundation/ARCs/arc-0028) events that MAY be emitted by this contract */
public readonly events?: ARC28Event[];
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));
this.events = params.events;
}
toJSON(): ABIContractParams {
return {
name: this.name,
desc: this.description,
networks: this.networks,
methods: this.methods.map((method) => method.toJSON()),
events: this.events,
};
}
getMethodByName(name: string): ABIMethod {
return getMethodByName(this.methods, name);
}
}