add contract to chain

This commit is contained in:
Iuri Matias 2015-08-03 08:44:16 -04:00
parent ea54f7524d
commit fab0df5c8b
3 changed files with 31 additions and 4 deletions

View File

@ -1,7 +1,10 @@
var fs = require('fs');
var web3 = require('web3');
var sha3_256 = require('js-sha3').sha3_256;
ChainManager = function() { }
ChainManager = function() {
this.currentChain = {};
}
ChainManager.prototype.loadConfigFile = function(filename) {
try {
@ -26,7 +29,16 @@ ChainManager.prototype.init = function(env, blockchainConfig) {
var chainId = web3.eth.getBlock(0).hash;
if (this.chainManagerConfig[chainId] === undefined) {
this.chainManagerConfig[chainId] = {contracts: []};
this.chainManagerConfig[chainId] = {contracts: {}};
}
this.currentChain = this.chainManagerConfig[chainId];
}
ChainManager.prototype.addContract = function(contractName, code, address) {
this.currentChain.contracts[sha3_256(code)] = {
name: contractName,
address: address
}
}

View File

@ -17,6 +17,7 @@
"grunt": "^0.4.5",
"hashmerge": "^1.0.2",
"jasmine": "^2.3.1",
"js-sha3": "^0.3.1",
"meteor-build-client": "^0.1.6",
"methodmissing": "^0.0.3",
"mkdirp": "^0.5.1",

View File

@ -11,9 +11,23 @@ describe('embark.chain_manager', function() {
chainManager.init('development', blockchainConfig);
it('should initialize chain', function() {
assert.equal(chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'] != undefined, true);
assert.equal(chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f'].contracts.length, 0);
var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f']
assert.equal(chain != undefined, true);
});
});
describe('#addContract', function() {
it('should register a contract in the chain', function() {
chainManager.addContract("Foo", "123456", "0x123");
var chain = chainManager.chainManagerConfig['0x629e768beb87dc8c54a475d310a7196e86c97d0006e5a6d34a8217726c90223f']
var contract = chain.contracts["d7190eb194ff9494625514b6d178c87f99c5973e28c398969d2233f2960a573e"]
assert.equal(contract.name, "Foo");
assert.equal(contract.address, "0x123");
});
});
});