2017-02-19 03:40:42 +00:00
|
|
|
var fs = require('fs-extra');
|
2016-09-25 01:10:47 +00:00
|
|
|
|
|
|
|
var DeployTracker = function(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.env = options.env;
|
|
|
|
this.chainConfig = options.chainConfig;
|
|
|
|
this.web3 = options.web3;
|
|
|
|
|
2016-10-02 20:57:13 +00:00
|
|
|
if (this.chainConfig === false) {
|
|
|
|
this.currentChain = {contracts: []};
|
|
|
|
return;
|
2016-10-02 22:47:26 +00:00
|
|
|
}
|
2016-10-02 20:57:13 +00:00
|
|
|
|
2016-09-25 01:10:47 +00:00
|
|
|
var block = this.web3.eth.getBlock(0);
|
|
|
|
var chainId = block.hash;
|
|
|
|
|
|
|
|
if (this.chainConfig[chainId] === undefined) {
|
|
|
|
this.chainConfig[chainId] = {contracts: {}};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentChain = this.chainConfig[chainId];
|
|
|
|
|
|
|
|
this.currentChain.name = this.env;
|
2016-09-25 06:34:36 +00:00
|
|
|
// TODO: add other params
|
|
|
|
//this.currentChain.networkId = "";
|
|
|
|
//this.currentChain.networkType = "custom"
|
2016-09-25 01:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
DeployTracker.prototype.loadConfig = function(config) {
|
|
|
|
this.chainConfig = config;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
DeployTracker.prototype.trackContract = function(contractName, code, args, address) {
|
|
|
|
this.currentChain.contracts[this.web3.sha3(code + contractName + args.join(','))] = {
|
|
|
|
name: contractName,
|
|
|
|
address: address
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
DeployTracker.prototype.getContract = function(contractName, code, args) {
|
2016-10-21 03:21:32 +00:00
|
|
|
var contract = this.currentChain.contracts[this.web3.sha3(code + contractName + args.join(','))];
|
|
|
|
if (contract && contract.address === undefined) { return false; }
|
|
|
|
return contract;
|
2016-09-25 01:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: abstract this
|
|
|
|
// chainConfig can be an abstract PersistentObject
|
|
|
|
DeployTracker.prototype.save = function() {
|
2016-10-02 22:47:26 +00:00
|
|
|
if (this.chainConfig === false) { return; }
|
2017-02-19 03:40:42 +00:00
|
|
|
fs.writeJSONSync("./chains.json", this.chainConfig);
|
2016-09-25 01:10:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = DeployTracker;
|
|
|
|
|