embark/lib/contracts/deploy_tracker.js

63 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let fs = require('../core/fs.js');
2016-09-25 01:10:47 +00:00
2017-03-30 11:12:39 +00:00
class DeployTracker {
constructor(options) {
this.logger = options.logger;
this.env = options.env;
this.chainConfig = options.chainConfig;
this.web3 = options.web3;
if (this.chainConfig === false) {
this.currentChain = {contracts: []};
return;
}
// TODO: need to make this async
let block = this.web3.eth.getBlock(0);
let chainId = block.hash;
if (this.chainConfig[chainId] === undefined) {
this.chainConfig[chainId] = {contracts: {}};
}
this.currentChain = this.chainConfig[chainId];
this.currentChain.name = this.env;
// 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) {
this.currentChain.contracts[this.web3.sha3(code + contractName + args.join(','))] = {
name: contractName,
address: address
};
}
2016-09-25 01:10:47 +00:00
2017-03-30 11:12:39 +00:00
getContract(contractName, code, args) {
let 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
}
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;