implement contract instances

This commit is contained in:
Iuri Matias 2015-07-04 16:52:05 -04:00
parent c2416389fc
commit 4481938ccd
6 changed files with 77 additions and 14 deletions

View File

@ -14,6 +14,7 @@ ContractsConfig.prototype.init = function(files) {
this.contractFiles = files;
this.contractDependencies = {};
//TODO: have to specify environment otherwise wouldn't work with staging
if (this.blockchainConfig.config != undefined) {
this.blockchainConfig = this.blockchainConfig.config('development');
}
@ -42,6 +43,7 @@ ContractsConfig.prototype.compileContracts = function(env) {
var contractsConfig = this.config(env);
this.compiler.init(env);
// determine dependencies
if (contractsConfig != null) {
for (className in contractsConfig) {
options = contractsConfig[className];
@ -60,6 +62,8 @@ ContractsConfig.prototype.compileContracts = function(env) {
}
}
var all_compiled_contracts = {};
// compile files
for (j = 0; j < this.contractFiles.length; j++) {
contractFile = this.contractFiles[j];
source = fs.readFileSync(contractFile).toString()
@ -68,9 +72,45 @@ ContractsConfig.prototype.compileContracts = function(env) {
compiled_contracts = this.compiler.compile(source);
for (className in compiled_contracts) {
var contract = compiled_contracts[className];
all_compiled_contracts[className] = contract;
this.all_contracts.push(className);
this.contractDB[className] = {
args: [],
types: ['file'],
gasPrice: this.blockchainConfig.gas_price,
gasLimit: this.blockchainConfig.gas_limit,
compiled: contract
}
}
}
// TODO: move this
// determine full contract list
// will be a combination between compiled contracts and the ones in config
for(className in contractsConfig) {
var contractConfig = contractsConfig[className];
var contract;
contract = this.contractDB[className];
if (contract === undefined) {
contract = {};
this.contractDB[className] = contract;
}
contract.gasPrice = contract.gasPrice || contractConfig.gas_price;
contract.gasLimit = contract.gasLimit || contractConfig.gas_limit;
contract.args = contractConfig.args;
if (contractConfig.instanceOf === undefined) {
contract.types.push('instance');
contract.instanceOf = contractConfig.instanceOf;
}
if (this.all_contracts.indexOf(className) < 0) {
this.all_contracts.push(className);
}
}
this.sortContracts();

View File

@ -36,16 +36,11 @@ Deploy.prototype.deploy_contracts = function(env) {
className = all_contracts[k];
contract = this.contractDB[className];
contractGasLimit = (this.contractsConfig != null ? (ref1 = this.contractsConfig[className]) != null ? ref1.gasLimit : void 0 : void 0) || this.blockchainConfig.gasLimit;
contractGasPrice = (this.contractsConfig != null ? (ref2 = this.contractsConfig[className]) != null ? ref2.gasPrice : void 0 : void 0) || this.blockchainConfig.gasPrice;
args = (this.contractsConfig != null ? (ref3 = this.contractsConfig[className]) != null ? ref3.args : void 0 : void 0) || [];
contractObject = web3.eth.contract(contract.info.abiDefinition);
contractObject = web3.eth.contract(contract.compiled.info.abiDefinition);
realArgs = [];
for (l = 0, len3 = args.length; l < len3; l++) {
arg = args[l];
for (var l = 0; l < contract.args.length; l++) {
arg = contract.args[l];
if (arg[0] === "$") {
realArgs.push(this.deployedContracts[arg.substr(1)]);
} else {
@ -56,9 +51,9 @@ Deploy.prototype.deploy_contracts = function(env) {
contractParams = realArgs;
contractParams.push({
from: primaryAddress,
data: contract.code,
gas: contractGasLimit,
gasPrice: contractGasPrice
data: contract.compiled.code,
gas: contract.gasLimit,
gasPrice: contract.gasPrice
});
contractAddress = contractObject["new"].apply(contractObject, contractParams).address;

View File

@ -17,12 +17,12 @@ var Blockchain = require('./blockchain.js');
var Deploy = require('./deploy.js');
var Release = require('./ipfs.js');
var Config = require('./config/config.js');
var Compiler = require('./config/compiler.js');
var Compiler = require('./compiler.js');
Embark = {
init: function() {
this.blockchainConfig = (new Config.Blockchain());
this.compiler = (new Compiler(this.blockchainConfig()));
this.compiler = (new Compiler(this.blockchainConfig));
this.contractsConfig = (new Config.Contracts(this.blockchainConfig, this.compiler));
},

View File

@ -8,7 +8,7 @@ py_exec = function(cmd) {
TestContractWrapper = (function() {
function TestContractWrapper(contract, className, args) {
this.contract = contract;
this.contract = contract.compiled;
this.className = className;
this.args = args;
this.initializeContract();

View File

@ -74,6 +74,23 @@ describe('embark.config.contracts', function() {
assert.deepEqual(contractsConfig.all_contracts, [ "SimpleStorage", "AnotherStorage", "Wallet", "Wallets" ]);
});
});
context("contracts instances", function() {
before(function() {
files = [
'test/support/contracts/simple_storage.sol'
]
contractsConfig = new Config.Contracts(blockchainConfig, compiler);
contractsConfig.loadConfigFile('test/support/instances.yml');
contractsConfig.init(files);
contractsConfig.compileContracts('development');
});
it('add contracts to a list', function() {
assert.deepEqual(contractsConfig.all_contracts, [ "SimpleStorage", "BarStorage", "FooStorage" ]);
});
});
});
});

View File

@ -0,0 +1,11 @@
development:
SimpleStorage:
args:
- 100
BarStorage:
instanceOf: SimpleStorage
FooStorage:
instanceOf: SimpleStorage
args:
- 200
staging: