forked from bnb-chain/bsc-genesis-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-genesis.js
140 lines (128 loc) · 3.39 KB
/
generate-genesis.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const { spawn } = require("child_process")
const program = require("commander")
const nunjucks = require("nunjucks")
const fs = require("fs")
const web3 = require("web3")
const validators = require("./validators")
const init_holders = require("./init_holders")
require("./generate-system");
require("./generate-systemReward");
require("./generate-validatorset");
require("./generate-tokenhub");
require("./generate-tendermintlightclient");
require("./generate-relayerincentivizecontract");
require("./generate-crosschain");
require("./generate-slash");
program.version("0.0.1")
program.option("-c, --chainid <chainid>", "chain id", "714")
program.option(
"-o, --output <output-file>",
"Genesis json file",
"./genesis.json"
)
program.option(
"-t, --template <template>",
"Genesis template json",
"./genesis-template.json"
)
program.parse(process.argv)
// compile contract
function compileContract(key, contractFile, contractName) {
return new Promise((resolve, reject) => {
const ls = spawn("solc", [
"--bin-runtime",
"/=/",
"--optimize",
"--optimize-runs",
"200",
contractFile
])
const result = []
ls.stdout.on("data", data => {
result.push(data.toString())
})
ls.stderr.on("data", data => {
// console.log(`stderr: ${data}`)
})
ls.on("close", code => {
console.log(`child process exited with code ${code}`)
resolve(result.join(""))
})
}).then(compiledData => {
compiledData = compiledData.replace(
`======= ${contractFile}:${contractName} =======\nBinary of the runtime part:`,
"@@@@"
)
const matched = compiledData.match(/@@@@\n([a-f0-9]+)/)
return { key, compiledData: matched[1], contractName, contractFile }
})
}
// compile files
Promise.all([
compileContract(
"validatorContract",
"contracts/BSCValidatorSet.sol",
"BSCValidatorSet"
),
compileContract(
"systemRewardContract",
"contracts/SystemReward.sol",
"SystemReward"
),
compileContract(
"slashContract",
"contracts/SlashIndicator.sol",
"SlashIndicator"
),
compileContract(
"tendermintLightClient",
"contracts/TendermintLightClient.sol",
"TendermintLightClient"
),
compileContract(
"tokenHub",
"contracts/TokenHub.sol",
"TokenHub"
),
compileContract(
"relayerHub",
"contracts/RelayerHub.sol",
"RelayerHub"
),
compileContract(
"relayerIncentivize",
"contracts/RelayerIncentivize.sol",
"RelayerIncentivize"
),
compileContract(
"govHub",
"contracts/GovHub.sol",
"GovHub"
),
compileContract(
"tokenManager",
"contracts/TokenManager.sol",
"TokenManager"
),
compileContract(
"crossChain",
"contracts/CrossChain.sol",
"CrossChain"
)
]).then(result => {
program.option("--initLockedBNBOnTokenHub <initLockedBNBOnTokenHub>",
"initLockedBNBOnTokenHub",
"176405560900000000000000000");
const data = {
initLockedBNBOnTokenHub: program.initLockedBNBOnTokenHub,
chainId: program.chainid,
initHolders: init_holders,
extraData: web3.utils.bytesToHex(validators.extraValidatorBytes)
}
result.forEach(r => {
data[r.key] = r.compiledData
})
const templateString = fs.readFileSync(program.template).toString()
const resultString = nunjucks.renderString(templateString, data)
fs.writeFileSync(program.output, resultString)
})