forked from bnb-chain/bsc-genesis-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-genesis.js
175 lines (157 loc) · 4.37 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
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
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")
program.option("--bscChainId <bscChainId>",
"bscChainId",
"0060");
program.option("-c, --chainid <chainid>", "chain id", "714")
program.option(
"--initValidatorSetBytes <initValidatorSetBytes>",
"initValidatorSetBytes",
""
)
program.option("--network <network>",
"network",
"mainnet");
program.option(
"--initBurnRatio <initBurnRatio>",
"initBurnRatio",
"0"
)
program.option("--initConsensusStateBytes <initConsensusStateBytes>",
"init consensusState bytes, hex encoding, no prefix with 0x",
"42696e616e63652d436861696e2d4e696c650000000000000000000000000000000000000000000229eca254b3859bffefaf85f4c95da9fbd26527766b784272789c30ec56b380b6eb96442aaab207bc59978ba3dd477690f5c5872334fc39e627723daa97e441e88ba4515150ec3182bc82593df36f8abb25a619187fcfab7e552b94e64ed2deed000000e8d4a51000");
require("./generate-system");
require("./generate-systemReward");
require("./generate-validatorset");
require("./generate-tokenhub");
require("./generate-tendermintlightclient");
require("./generate-relayerincentivizecontract");
require("./generate-crosschain");
require("./generate-slash");
require("./generate-relayerhub");
require("./generate-staking");
program.version("0.0.1")
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]+)/)
if (!matched) {
throw new Error(`Can not compile: ${contractName}`);
}
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"
),
compileContract(
"staking",
"contracts/Staking.sol",
"Staking"
)
]).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)
})