100 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-05-18 22:40:47 -04:00
let utils = require('../../utils/utils.js');
let fs = require('../../core/fs.js');
2016-09-24 21:10:47 -04:00
2017-03-30 20:12:39 +09:00
class DeployTracker {
2018-05-18 22:40:47 -04:00
constructor(embark, options) {
this.logger = embark.logger;
this.events = embark.events;
this.embark = embark;
2018-08-24 09:25:47 -04:00
this.trackContracts = (options.trackContracts !== false);
2018-05-18 22:40:47 -04:00
// TODO: unclear where it comes from
2017-03-30 20:12:39 +09:00
this.env = options.env;
2018-05-18 22:40:47 -04:00
//this.chainConfig = options.chainConfig;
this.chainConfig = embark.config.chainTracker;
this.registerEvents();
}
registerEvents() {
const self = this;
this.embark.registerActionForEvent("deploy:beforeAll", this.setCurrentChain.bind(this));
2017-03-30 20:12:39 +09:00
2018-05-18 22:40:47 -04:00
this.events.on("deploy:contract:deployed", (contract) => {
2018-06-18 10:22:04 -04:00
self.trackContract(contract.className, contract.realRuntimeBytecode, contract.realArgs, contract.deployedAddress);
2018-05-18 22:40:47 -04:00
self.save();
});
self.embark.registerActionForEvent("deploy:contract:shouldDeploy", (params, cb) => {
2018-08-24 09:25:47 -04:00
if (!self.trackContracts) {
return cb(params);
}
let contract = params.contract;
2018-05-18 22:40:47 -04:00
let trackedContract = self.getContract(contract.className, contract.realRuntimeBytecode, contract.realArgs);
if (trackedContract) {
params.contract.address = trackedContract.address;
}
if (params.shouldDeploy && trackedContract) {
params.shouldDeploy = true;
}
cb(params);
2018-05-18 22:40:47 -04:00
});
}
2018-09-22 14:43:10 +01:00
setCurrentChain(cb) {
2018-05-18 22:40:47 -04:00
const self = this;
2017-03-30 20:12:39 +09:00
if (this.chainConfig === false) {
this.currentChain = {contracts: []};
2018-09-22 14:43:10 +01:00
return cb();
2017-03-30 20:12:39 +09:00
}
this.events.request("blockchain:block:byNumber", 0, function(_err, block) {
2018-01-05 15:10:47 -05:00
let chainId = block.hash;
2017-03-30 20:12:39 +09:00
2018-01-05 15:10:47 -05:00
if (self.chainConfig[chainId] === undefined) {
self.chainConfig[chainId] = {contracts: {}};
}
self.currentChain = self.chainConfig[chainId];
2017-03-30 20:12:39 +09:00
2018-01-05 15:10:47 -05:00
self.currentChain.name = self.env;
2018-09-22 14:43:10 +01:00
cb();
2018-01-05 15:10:47 -05:00
});
2016-10-02 18:47:26 -04:00
}
2016-10-02 16:57:13 -04:00
2017-03-30 20:12:39 +09:00
loadConfig(config) {
this.chainConfig = config;
return this;
}
trackContract(contractName, code, args, address) {
2018-05-18 22:52:14 -04:00
if (!this.currentChain) return false;
2018-05-18 16:51:03 -04:00
this.currentChain.contracts[utils.sha3(code + contractName + args.join(','))] = {
2017-03-30 20:12:39 +09:00
name: contractName,
address: address
};
}
2016-09-24 21:10:47 -04:00
2017-03-30 20:12:39 +09:00
getContract(contractName, code, args) {
if (!this.currentChain) return false;
2018-05-18 16:51:03 -04:00
let contract = this.currentChain.contracts[utils.sha3(code + contractName + args.join(','))];
2017-03-30 20:12:39 +09:00
if (contract && contract.address === undefined) {
return false;
}
return contract;
2016-09-24 21:10:47 -04:00
}
2017-03-30 20:12:39 +09:00
// TODO: abstract this
// chainConfig can be an abstract PersistentObject
save() {
if (this.chainConfig === false) {
return;
}
2017-04-04 06:37:50 -04:00
fs.writeJSONSync("./chains.json", this.chainConfig, {spaces: 2});
2017-03-30 20:12:39 +09:00
}
2018-05-18 22:40:47 -04:00
2017-03-30 20:12:39 +09:00
}
2016-09-24 21:10:47 -04:00
module.exports = DeployTracker;