embark/lib/contracts/deploy_tracker.js

66 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let fs = require('../core/fs.js');
2018-05-18 20:51:03 +00:00
let utils = require('../utils/utils.js');
2016-09-25 01:10:47 +00:00
2017-03-30 11:12:39 +00:00
class DeployTracker {
2018-01-05 20:10:47 +00:00
constructor(options, cb) {
const self = this;
2017-03-30 11:12:39 +00:00
this.logger = options.logger;
this.env = options.env;
this.chainConfig = options.chainConfig;
this.web3 = options.web3;
if (this.chainConfig === false) {
this.currentChain = {contracts: []};
2018-01-05 20:10:47 +00:00
return cb();
2017-03-30 11:12:39 +00:00
}
2018-01-05 20:10:47 +00:00
this.web3.eth.getBlock(0, function(err, block) {
let chainId = block.hash;
2017-03-30 11:12:39 +00:00
2018-01-05 20:10:47 +00:00
if (self.chainConfig[chainId] === undefined) {
self.chainConfig[chainId] = {contracts: {}};
}
self.currentChain = self.chainConfig[chainId];
2017-03-30 11:12:39 +00:00
2018-01-05 20:10:47 +00:00
self.currentChain.name = self.env;
cb();
});
2017-03-30 11:12:39 +00:00
// TODO: add other params
//this.currentChain.networkId = "";
//this.currentChain.networkType = "custom"
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-18 20:51:03 +00:00
this.currentChain.contracts[utils.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-18 20:51:03 +00:00
let contract = this.currentChain.contracts[utils.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;
}
2017-04-04 10:37:50 +00:00
fs.writeJSONSync("./chains.json", this.chainConfig, {spaces: 2});
2017-03-30 11:12:39 +00:00
}
}
2016-09-25 01:10:47 +00:00
module.exports = DeployTracker;