-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen.js
122 lines (112 loc) · 3 KB
/
gen.js
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
const fs = require("fs").promises;
const cla = require("command-line-args");
const {config, network, out, abi: abiPath} = cla([
{name: "network", alias: "n", type: String, defaultValue: "development"},
{name: "config", alias: "c", type: String, defaultValue: "gen.config.json"},
{name: "out", alias: "o", type: String, defaultValue: "gen"},
{name: "abi", alias: "a", type: String, defaultValue: "abi"},
]);
async function loadJson(path) {
return JSON.parse(await fs.readFile(path));
}
async function writeJs(path, data) {
return fs.writeFile(
path,
"module.exports = " + JSON.stringify(data, null, 4)
);
}
async function writeJson(path, data) {
return fs.writeFile(path, JSON.stringify(data, null, 4));
}
(async (configPath, network, out) => {
const config = JSON.parse(await fs.readFile(configPath));
const [assets, contracts, abiContracts] = await Promise.all([
(config.assets || []).reduce(
async (assets, {path, key, name, symbol, decimals, investing}) => {
const res = await assets;
try {
const {address} = await loadJson(path.replace("%network%", network));
return [
...res,
{
key,
name,
address,
symbol,
decimals,
investing,
},
];
} catch (e) {
return res; // Use preset data if asset not deployed (see USDN token)
}
},
Promise.resolve([])
),
(config.contracts || []).reduce(
async (contracts, {path, key, name, voting}) => {
const res = await contracts;
try {
const {address, abi} = await loadJson(
path.replace("%network%", network)
);
return [
...res,
{
key,
address,
name,
abi,
voting,
},
];
} catch (e) {
return res; // use preset data if contract not deployed
}
},
Promise.resolve([])
),
Promise.all(
(config.abi || []).map(async ({path}) => {
const {contractName, abi} = await loadJson(path);
return {contractName, abi};
})
),
]);
await Promise.all([
writeJs(
`${out}/contracts.js`,
contracts.reduce(
(result, {address, key, name, abi, voting}) => ({
...result,
[key || name]: {
address,
name,
voting,
abi,
},
}),
{}
)
),
...abiContracts.map(({contractName: name, abi}) =>
writeJson(`${abiPath}/${name}.json`, {abi})
),
writeJs(
`${out}/assets.js`,
assets.reduce(
(result, {address, key, name, symbol, decimals, investing}) => ({
...result,
[key || symbol]: {
address,
name,
symbol,
decimals,
investing,
},
}),
{}
)
),
]);
})(config, network, out);