forked from poanetwork/poa-test-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepareValidatorsDapp.js
61 lines (48 loc) · 2.98 KB
/
prepareValidatorsDapp.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
const fs = require('fs');
const Constants = require("./utils/constants");
const constants = Constants.constants;
const utils = require("./utils/utils");
main()
function main() {
const pathToAddressesJSON = `${constants.pathToContractRepo}/${constants.addressesSourceFile}`;
const addresses = JSON.parse(fs.readFileSync(pathToAddressesJSON));
let moc = JSON.parse(fs.readFileSync(`${constants.mocKeysFolder}moc.key`, 'utf8'));
const addition = `
const local = {
"METADATA_ADDRESS": "${addresses.METADATA_ADDRESS}",
"KEYS_MANAGER_ADDRESS": "${addresses.KEYS_MANAGER_ADDRESS}",
"POA_ADDRESS": '${constants.poaNetworkConsensusContractAddress}',
"MOC": '${moc.address}'
};
`
let dappAddresses = `${constants.pathToValidatorsDAppRepo}/src/contracts/addresses.js`;
let addressesFromDapp = fs.readFileSync(dappAddresses, 'utf8');
let lastImport = `import messages from '../messages'`;
addressesFromDapp = addressesFromDapp.replace(lastImport, lastImport + addition)
addressesFromDapp = addressesFromDapp.replace('resolve({ addresses: json', 'resolve({ addresses: local')
fs.writeFileSync(dappAddresses, addressesFromDapp);
// Hardcode ABIs into helpers.js
const pathToKeysManagerJSON = `${constants.contractsFolder}/KeysManager.json`;
const keysManagerABI = JSON.stringify(JSON.parse(fs.readFileSync(pathToKeysManagerJSON)).abi).replace(/,/g, ', ');
const pathToPoaNetworkConsensusJSON = `${constants.contractsFolder}/PoaNetworkConsensus.json`;
const poaNetworkConsensusABI = JSON.stringify(JSON.parse(fs.readFileSync(pathToPoaNetworkConsensusJSON)).abi).replace(/,/g, ', ');
const pathToValidatorMetadataJSON = `${constants.contractsFolder}/ValidatorMetadata.json`;
const validatorMetadataABI = JSON.stringify(JSON.parse(fs.readFileSync(pathToValidatorMetadataJSON)).abi).replace(/,/g, ', ');
const dappHelpers = `${constants.pathToValidatorsDAppRepo}/src/contracts/helpers.js`;
let dappHelpersContent = fs.readFileSync(dappHelpers, 'utf8');
const abiAddition = `
if (contract === 'KeysManager') return ${keysManagerABI};
else if (contract === 'PoaNetworkConsensus') return ${poaNetworkConsensusABI};
else if (contract === 'ValidatorMetadata') return ${validatorMetadataABI};`;
const lastGetABI = `function getABI(branch, contract) {`;
dappHelpersContent = dappHelpersContent.replace(lastGetABI, lastGetABI + abiAddition);
fs.writeFileSync(dappHelpers, dappHelpersContent);
// Fix Metadata.contract.js for using new ValidatorMetadata contract
// in local Sokol Network (instead of live xDai Network)
const metadataContractPath = `${constants.pathToValidatorsDAppRepo}/src/contracts/Metadata.contract.js`;
let metadataContractText = fs.readFileSync(metadataContractPath, 'utf8');
const replacePattern = (net) => `if (this.netId === helpersGlobal.netIdByName('${net}')) {`;
metadataContractText = metadataContractText.replace(replacePattern('dai'), replacePattern('sokol'));
fs.writeFileSync(metadataContractPath, metadataContractText);
console.log("Validators Repo is prepared");
}