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.contractFiles = files;
this.contractDependencies = {}; this.contractDependencies = {};
//TODO: have to specify environment otherwise wouldn't work with staging
if (this.blockchainConfig.config != undefined) { if (this.blockchainConfig.config != undefined) {
this.blockchainConfig = this.blockchainConfig.config('development'); this.blockchainConfig = this.blockchainConfig.config('development');
} }
@ -42,6 +43,7 @@ ContractsConfig.prototype.compileContracts = function(env) {
var contractsConfig = this.config(env); var contractsConfig = this.config(env);
this.compiler.init(env); this.compiler.init(env);
// determine dependencies
if (contractsConfig != null) { if (contractsConfig != null) {
for (className in contractsConfig) { for (className in contractsConfig) {
options = contractsConfig[className]; 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++) { for (j = 0; j < this.contractFiles.length; j++) {
contractFile = this.contractFiles[j]; contractFile = this.contractFiles[j];
source = fs.readFileSync(contractFile).toString() source = fs.readFileSync(contractFile).toString()
@ -68,9 +72,45 @@ ContractsConfig.prototype.compileContracts = function(env) {
compiled_contracts = this.compiler.compile(source); compiled_contracts = this.compiler.compile(source);
for (className in compiled_contracts) { for (className in compiled_contracts) {
var contract = compiled_contracts[className]; var contract = compiled_contracts[className];
all_compiled_contracts[className] = contract;
this.all_contracts.push(className); 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; 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(); this.sortContracts();

View File

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

View File

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

View File

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

View File

@ -74,6 +74,23 @@ describe('embark.config.contracts', function() {
assert.deepEqual(contractsConfig.all_contracts, [ "SimpleStorage", "AnotherStorage", "Wallet", "Wallets" ]); 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: