-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.ts
165 lines (130 loc) · 3.49 KB
/
configuration.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import deepmerge from "deepmerge";
import { readFile, writeFile } from "node:fs/promises";
import debug from "debug";
import {
HttpProvider,
Wallet,
WebsocketProvider,
type Hex,
type Provider,
} from "js-moi-sdk";
export type AccountConfig = {
mnemonic: string;
path?: string;
};
type ProviderConfiguration = {
type: "http" | "ws";
url: string;
};
type AssetConfiguration = {
asset_id?: Hex;
};
interface ParticipantConfiguration {
identifier?: Hex;
}
interface LogicConfiguration {
logic_id?: Hex;
deployment: {
manifest: string;
callsite: string;
calldata: any[];
};
}
export interface ConfigurationSetting {
logs?: boolean;
account: AccountConfig;
provider: ProviderConfiguration;
asset?: AssetConfiguration;
participant?: ParticipantConfiguration;
logic: LogicConfiguration;
}
export type ResettableKeys = Exclude<
keyof ConfigurationSetting,
"account" | "provider"
>;
const cfgLog = debug("configuration::");
const providerLog = debug("provider::");
class Configuration {
private configPath: string;
private configuration: ConfigurationSetting;
private constructor(path: string, configuration: ConfigurationSetting) {
this.configPath = path;
this.configuration = configuration;
cfgLog("Configuration module loaded");
}
public getAccountConfig(): AccountConfig {
return this.configuration.account;
}
public getProviderConfig(): ProviderConfiguration {
return this.configuration.provider;
}
public getConfiguration() {
return this.configuration;
}
public async getWallet() {
const account = this.getAccountConfig();
return await Wallet.fromMnemonic(account.mnemonic, account.path, {
provider: this.getProvider(),
});
}
private async save() {
await writeFile(
this.configPath,
JSON.stringify(this.configuration, null, 2)
);
cfgLog("Configuration saved to %s", this.configPath);
}
public async setAssetConfig(config: Partial<AssetConfiguration>) {
this.configuration = deepmerge.all([
this.configuration,
{ asset: config },
]) as ConfigurationSetting;
await this.save();
}
public async setParticipantConfig(config: Partial<ParticipantConfiguration>) {
this.configuration = deepmerge.all([
this.configuration,
{ participant: config },
]) as ConfigurationSetting;
await this.save();
}
public async setLogicConfig(config: Partial<LogicConfiguration>) {
this.configuration = deepmerge.all([
this.configuration,
{ logic: config },
]) as ConfigurationSetting;
await this.save();
}
public getProvider(): Provider {
const config = this.getProviderConfig();
let provider: Provider;
switch (config.type) {
case "http": {
provider = new HttpProvider(config.url);
break;
}
case "ws": {
provider = new WebsocketProvider(config.url);
break;
}
}
if (this.configuration.logs) {
provider.on("debug", (data) => providerLog(JSON.stringify(data.payload)));
}
return provider;
}
public async reset(...keys: ResettableKeys[]) {
for (const key of keys) {
this.configuration[key] = {} as any;
}
await this.save();
}
public getConfigurationPath() {
return this.configPath;
}
public static async init(path: string) {
const content = await readFile(path, "utf8");
return new Configuration(path, JSON.parse(content));
}
}
export const config = await Configuration.init("./config/config.json");