2019-05-07 19:49:22 +00:00
|
|
|
import { __ } from 'embark-i18n';
|
2019-05-16 10:03:19 +00:00
|
|
|
import { dappPath, sha3 } from 'embark-utils';
|
2019-05-15 08:09:50 +00:00
|
|
|
import * as fs from 'fs-extra';
|
2016-09-25 01:10:47 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class DeployTracker {
|
2018-05-19 02:40:47 +00:00
|
|
|
|
|
|
|
constructor(embark, options) {
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
2018-08-20 21:25:30 +00:00
|
|
|
this.embark = embark;
|
2018-08-24 13:25:47 +00:00
|
|
|
this.trackContracts = (options.trackContracts !== false);
|
2018-05-19 02:40:47 +00:00
|
|
|
|
|
|
|
// TODO: unclear where it comes from
|
2017-03-30 11:12:39 +00:00
|
|
|
this.env = options.env;
|
2019-01-25 22:16:52 +00:00
|
|
|
this.chainConfig = {};
|
|
|
|
this.chainFile = embark.config.contractsConfig.tracking;
|
|
|
|
this.loadChainTrackerFile();
|
2018-05-19 02:40:47 +00:00
|
|
|
this.registerEvents();
|
|
|
|
}
|
|
|
|
|
2019-01-25 22:16:52 +00:00
|
|
|
loadChainTrackerFile() {
|
|
|
|
if (this.chainFile === false) return;
|
|
|
|
if (this.chainFile === undefined) this.chainFile = ".embark/chains.json";
|
2019-05-15 08:09:50 +00:00
|
|
|
this.chainFile = dappPath(this.chainFile);
|
|
|
|
if (!fs.existsSync(this.chainFile)) {
|
2019-01-25 22:16:52 +00:00
|
|
|
this.logger.info(this.chainFile + ' ' + __('file not found, creating it...'));
|
2019-05-15 08:09:50 +00:00
|
|
|
fs.outputJSONSync(this.chainFile, {});
|
2019-01-25 22:16:52 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 08:09:50 +00:00
|
|
|
this.chainConfig = fs.readJSONSync(this.chainFile);
|
2019-01-25 22:16:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 02:40:47 +00:00
|
|
|
registerEvents() {
|
2019-01-25 22:16:52 +00:00
|
|
|
if (this.chainFile === false) return;
|
2018-05-19 02:40:47 +00:00
|
|
|
const self = this;
|
|
|
|
|
2018-09-22 14:08:50 +00:00
|
|
|
this.embark.registerActionForEvent("deploy:beforeAll", this.setCurrentChain.bind(this));
|
2017-03-30 11:12:39 +00:00
|
|
|
|
2018-05-19 02:40:47 +00:00
|
|
|
this.events.on("deploy:contract:deployed", (contract) => {
|
2018-06-18 14:22:04 +00:00
|
|
|
self.trackContract(contract.className, contract.realRuntimeBytecode, contract.realArgs, contract.deployedAddress);
|
2018-05-19 02:40:47 +00:00
|
|
|
self.save();
|
|
|
|
});
|
|
|
|
|
2018-08-20 21:25:30 +00:00
|
|
|
self.embark.registerActionForEvent("deploy:contract:shouldDeploy", (params, cb) => {
|
2018-08-24 13:25:47 +00:00
|
|
|
if (!self.trackContracts) {
|
2019-03-01 04:50:58 +00:00
|
|
|
return cb(null, params);
|
2018-08-24 12:59:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 21:25:30 +00:00
|
|
|
let contract = params.contract;
|
2018-05-19 02:40:47 +00:00
|
|
|
let trackedContract = self.getContract(contract.className, contract.realRuntimeBytecode, contract.realArgs);
|
2018-08-20 21:25:30 +00:00
|
|
|
if (trackedContract) {
|
|
|
|
params.contract.address = trackedContract.address;
|
|
|
|
}
|
|
|
|
if (params.shouldDeploy && trackedContract) {
|
|
|
|
params.shouldDeploy = true;
|
|
|
|
}
|
2019-03-01 04:50:58 +00:00
|
|
|
cb(null, params);
|
2018-05-19 02:40:47 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-27 17:28:18 +00:00
|
|
|
setCurrentChain(callback) {
|
2018-05-19 02:40:47 +00:00
|
|
|
const self = this;
|
2017-03-30 11:12:39 +00:00
|
|
|
if (this.chainConfig === false) {
|
|
|
|
this.currentChain = {contracts: []};
|
2019-06-27 17:28:18 +00:00
|
|
|
return callback();
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 17:28:18 +00:00
|
|
|
function getBlock(blockNum, cb) {
|
|
|
|
self.events.request("blockchain:block:byNumber", blockNum, (err, block) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
let chainId = block.hash;
|
|
|
|
|
|
|
|
if (self.chainConfig[chainId] === undefined) {
|
|
|
|
self.chainConfig[chainId] = {contracts: {}};
|
|
|
|
}
|
2018-01-05 20:10:47 +00:00
|
|
|
|
2019-06-27 17:28:18 +00:00
|
|
|
self.currentChain = self.chainConfig[chainId];
|
2017-03-30 11:12:39 +00:00
|
|
|
|
2019-06-27 17:28:18 +00:00
|
|
|
self.currentChain.name = self.env;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlock(0, (err) => {
|
|
|
|
if (err) {
|
|
|
|
// Retry with block 1 (Block 0 fails with Ganache-cli using the --fork option)
|
2019-08-19 20:11:16 +00:00
|
|
|
return getBlock(1, (err) => {
|
|
|
|
if (err) {
|
|
|
|
self.logger.error(__('Error getting block data. The deploy-tracker will not work'), err);
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
2019-06-27 17:28:18 +00:00
|
|
|
}
|
|
|
|
callback();
|
2018-01-05 20:10:47 +00:00
|
|
|
});
|
2016-10-02 22:47:26 +00:00
|
|
|
}
|
2016-10-02 20:57:13 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
loadConfig(config) {
|
|
|
|
this.chainConfig = config;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
trackContract(contractName, code, args, address) {
|
2018-05-19 02:52:14 +00:00
|
|
|
if (!this.currentChain) return false;
|
2019-04-30 13:48:49 +00:00
|
|
|
this.currentChain.contracts[sha3(code + contractName + args.join(','))] = {
|
2017-03-30 11:12:39 +00:00
|
|
|
name: contractName,
|
|
|
|
address: address
|
|
|
|
};
|
|
|
|
}
|
2016-09-25 01:10:47 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
getContract(contractName, code, args) {
|
2018-05-19 02:44:11 +00:00
|
|
|
if (!this.currentChain) return false;
|
2019-04-30 13:48:49 +00:00
|
|
|
let contract = this.currentChain.contracts[sha3(code + contractName + args.join(','))];
|
2017-03-30 11:12:39 +00:00
|
|
|
if (contract && contract.address === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return contract;
|
2016-09-25 01:10:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
// TODO: abstract this
|
|
|
|
// chainConfig can be an abstract PersistentObject
|
|
|
|
save() {
|
|
|
|
if (this.chainConfig === false) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-15 08:09:50 +00:00
|
|
|
fs.writeJSONSync(this.chainFile, this.chainConfig, {spaces: 2});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2018-05-19 02:40:47 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-09-25 01:10:47 +00:00
|
|
|
|
|
|
|
module.exports = DeployTracker;
|