-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.ts
274 lines (246 loc) · 7.35 KB
/
index.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { randomUUID } from "crypto";
import * as compose from "docker-compose";
import * as fs from "fs";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { join } from "path";
import * as constants from "../../shared/constants";
import { NodeStartType } from "../../shared/enums";
import { DockerOutput } from "../../shared/types";
const login = async (): Promise<void> => {
await compose.exec(
constants.CHAINLINK_NODE_CONTAINER,
["chainlink", "admin", "login", "-f", "/clroot/api-credentials"],
{
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
log: true,
}
);
};
const up = async (
hre: HardhatRuntimeEnvironment,
nodeStartType: NodeStartType
): Promise<DockerOutput> => {
// Read variables from Hardhat config
const { node } = hre.config.chainlink;
if (!fs.existsSync("clroot")) {
fs.mkdirSync("clroot");
}
// Read the config template TOML file
const configTemplate = fs.readFileSync(
join(__dirname, "/setup/clroot/config_template.toml"),
"utf8"
);
// Replace placeholders with values
const config = configTemplate
.replace("{{CHAIN_ID}}", node?.chain_id || constants.DEFAULT_CHAIN_ID)
.replace("{{CHAIN_NAME}}", node?.chain_name || constants.DEFAULT_CHAIN_NAME)
.replace("{{CHAIN_WSURL}}", node?.ws_url || constants.DEFAULT_WS_URL)
.replace("{{CHAIN_HTTPURL}}", node?.http_url || constants.DEFAULT_HTTP_URL);
// Write the replaced content to a new TOML file
try {
fs.writeFileSync(join("./clroot/config.toml"), config, "utf8");
} catch (e: any) {
throw new Error(e);
}
// Read the docker-compose template YAML file
const dockerComposeTemplate = fs.readFileSync(
join(__dirname, "/setup/docker-compose_template.yaml"),
"utf8"
);
// Replace placeholders with values
const dockerCompose = dockerComposeTemplate
.replace("{{PG_USER}}", node?.pg_user || constants.DEFAULT_PG_USER)
.replace("{{PG_USER}}", node?.pg_user || constants.DEFAULT_PG_USER)
.replace("{{PG_USER}}", node?.pg_user || constants.DEFAULT_PG_USER)
.replace(
"{{PG_PASSWORD}}",
node?.pg_password || constants.DEFAULT_PG_PASSWORD
)
.replace(
"{{PG_PASSWORD}}",
node?.pg_password || constants.DEFAULT_PG_PASSWORD
)
.replace("{{PG_DB}}", node?.pg_db || constants.DEFAULT_PG_DB)
.replace("{{PG_DB}}", node?.pg_db || constants.DEFAULT_PG_DB)
.replace(
"{{CL_PASSWORD_KEYSTORE}}",
node?.cl_keystore_password || constants.DEFAULT_CL_KEYSTORE_PASSWORD
);
// Write the replaced content to a new YAML file
try {
fs.writeFileSync(
join("./docker-compose-chainlink-hardhat.yaml"),
dockerCompose,
"utf8"
);
} catch (e: any) {
throw new Error(e);
}
// Combine the strings with a line break
const apiCredentials = `${
node?.cl_api_user || constants.DEFAULT_CL_API_USER
}\n${node?.cl_api_password || constants.DEFAULT_CL_API_PASSWORD}`;
// Write the content to a file
try {
fs.writeFileSync(join("./clroot/api-credentials"), apiCredentials, "utf8");
} catch (e: any) {
throw new Error(e);
}
let result;
switch (nodeStartType) {
case NodeStartType.run:
result = await compose.upAll({
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
log: true,
});
break;
case NodeStartType.restart:
await compose.down({
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
log: true,
});
result = await compose.upAll({
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
log: true,
});
break;
case NodeStartType.stop:
result = await compose.stop({
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
log: true,
});
break;
default:
throw new Error("Unknown Chainlink node start type");
}
return {
exitCode: result.exitCode,
err: result.exitCode !== 0 ? result.err : "",
};
};
export const run = async (
hre: HardhatRuntimeEnvironment
): Promise<DockerOutput> => {
return up(hre, NodeStartType.run);
};
export const restart = async (
hre: HardhatRuntimeEnvironment
): Promise<DockerOutput> => {
return up(hre, NodeStartType.restart);
};
export const stop = async (
hre: HardhatRuntimeEnvironment
): Promise<DockerOutput> => {
return up(hre, NodeStartType.stop);
};
export const getETHKeys = async (
hre: HardhatRuntimeEnvironment
): Promise<string> => {
await login();
const result = await compose.exec(
constants.CHAINLINK_NODE_CONTAINER,
["chainlink", "-j", "keys", "eth", "list"],
{
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
}
);
return result.out;
};
export const getP2PKeys = async (
hre: HardhatRuntimeEnvironment
): Promise<string> => {
await login();
const result = await compose.exec(
constants.CHAINLINK_NODE_CONTAINER,
["chainlink", "-j", "keys", "p2p", "list"],
{
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
}
);
return result.out;
};
export const getVRFKeys = async (
hre: HardhatRuntimeEnvironment
): Promise<string> => {
await login();
const result = await compose.exec(
constants.CHAINLINK_NODE_CONTAINER,
["chainlink", "-j", "keys", "vrf", "list"],
{
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
}
);
return result.out;
};
export const getOCRKeys = async (
hre: HardhatRuntimeEnvironment
): Promise<string> => {
await login();
const result = await compose.exec(
constants.CHAINLINK_NODE_CONTAINER,
["chainlink", "-j", "keys", "ocr", "list"],
{
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
}
);
return result.out;
};
export const getJobs = async (
hre: HardhatRuntimeEnvironment
): Promise<string> => {
await login();
const result = await compose.exec(
constants.CHAINLINK_NODE_CONTAINER,
["chainlink", "-j", "jobs", "list"],
{
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
}
);
return result.out;
};
export const createDirectRequestJob = async (
hre: HardhatRuntimeEnvironment,
operatorAddress: string
): Promise<void> => {
// Read the config template TOML file
const directRequestJobTemplate = fs.readFileSync(
join(__dirname, "/setup/clroot/jobs/direct-request-job_template.toml"),
"utf8"
);
// Replace placeholders with values
const directRequestJob = directRequestJobTemplate
.replace("{{OPERATOR_ADDRESS}}", operatorAddress)
.replace("{{OPERATOR_ADDRESS}}", operatorAddress)
.replace("{{OPERATOR_ADDRESS}}", operatorAddress)
.replace("{{UUID}}", randomUUID());
// Write the replaced content to a new TOML file
try {
fs.writeFileSync(
join("./clroot/jobs/direct-request-job.toml"),
directRequestJob,
"utf8"
);
} catch (e: any) {
throw new Error(e);
}
await login();
await compose.exec(
constants.CHAINLINK_NODE_CONTAINER,
["chainlink", "jobs", "create", "/clroot/jobs/direct-request-job.toml"],
{
cwd: join("./"),
composeOptions: ["-f", "docker-compose-chainlink-hardhat.yaml"],
log: true,
}
);
};