From ea2a162d4b04af7d4dd083ee89c1d07ea133fc8b Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 4 Jul 2015 18:59:57 -0400 Subject: [PATCH] fix contract creation defaults; add test for contract instances --- lib/config/contracts.js | 25 +++++----- test/deploy.js | 106 +++++++++++++++++++++++++++------------- 2 files changed, 86 insertions(+), 45 deletions(-) diff --git a/lib/config/contracts.js b/lib/config/contracts.js index 0ba03beaa..4e757bd03 100644 --- a/lib/config/contracts.js +++ b/lib/config/contracts.js @@ -77,35 +77,38 @@ ContractsConfig.prototype.compileContracts = function(env) { this.contractDB[className] = { args: [], types: ['file'], - gasPrice: this.blockchainConfig.gas_price, - gasLimit: this.blockchainConfig.gas_limit, + gasPrice: this.blockchainConfig.gasPrice, + gasLimit: this.blockchainConfig.gasLimit, 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 = {}; + contract = { + args: [], + types: ['file'], + gasPrice: this.blockchainConfig.gasPrice, + gasLimit: this.blockchainConfig.gasLimit, + compiled: contract + } this.contractDB[className] = contract; } - contract.gasPrice = contract.gasPrice || contractConfig.gas_price; - contract.gasLimit = contract.gasLimit || contractConfig.gas_limit; - contract.args = contractConfig.args; + contract.gasPrice = contractConfig.gas_price || contract.gasPrice; + contract.gasLimit = contractConfig.gas_limit || contract.gasLimit; + contract.args = contractConfig.args || []; - if (contractConfig.instanceOf === undefined) { + if (contractConfig.instanceOf !== undefined) { contract.types.push('instance'); contract.instanceOf = contractConfig.instanceOf; + contract.compiled = all_compiled_contracts[contractConfig.instanceOf]; } if (this.all_contracts.indexOf(className) < 0) { diff --git a/test/deploy.js b/test/deploy.js index c87c15ca9..5c2879dec 100644 --- a/test/deploy.js +++ b/test/deploy.js @@ -3,52 +3,90 @@ var Deploy = require('../lib/deploy.js'); var Compiler = require('../lib/compiler.js'); var assert = require('assert'); -describe('embark.deploy', function() { - var files = [ - 'test/support/contracts/wallet.sol', - 'test/support/contracts/simple_storage.sol', - 'test/support/contracts/another_storage.sol', - 'test/support/contracts/wallets.sol' - ]; - var _blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml'); +setDeployConfig = function(config) { + var _blockchainConfig = (new Config.Blockchain()).loadConfigFile(config.blockchain); var blockchainConfig = _blockchainConfig.config("development"); var compiler = new Compiler(_blockchainConfig); var contractsConfig = new Config.Contracts(blockchainConfig, compiler); - contractsConfig.loadConfigFile('test/support/arguments.yml'); - contractsConfig.init(files); - var deploy = new Deploy('development', files, blockchainConfig, contractsConfig); + contractsConfig.loadConfigFile(config.contracts); + contractsConfig.init(config.files); + compiler.init('development'); + return new Deploy('development', config.files, blockchainConfig, contractsConfig); +} - describe('#deploy_contracts', function() { - compiler.init('development'); - deploy.deploy_contracts("development"); +describe('embark.deploy', function() { - it("should deploy contracts", function() { - var all_contracts = ['Wallet', 'SimpleStorage', 'AnotherStorage', 'Wallets']; - for(var i=0; i < all_contracts; i++) { - var className = all_contracts[i]; + describe('contracts as arguments to other contracts', function() { + var files = [ + 'test/support/contracts/wallet.sol', + 'test/support/contracts/simple_storage.sol', + 'test/support/contracts/another_storage.sol', + 'test/support/contracts/wallets.sol' + ]; - assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); - } + var deploy = setDeployConfig({ + files: files, + blockchain: 'test/support/blockchain.yml', + contracts: 'test/support/arguments.yml' }); + describe('#deploy_contracts', function() { + deploy.deploy_contracts("development"); + + it("should deploy contracts", function() { + var all_contracts = ['Wallet', 'SimpleStorage', 'AnotherStorage', 'Wallets']; + for(var i=0; i < all_contracts; i++) { + var className = all_contracts[i]; + + assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); + } + }); + + }); + + describe('#generate_abi_file', function() { + deploy.deployedContracts = { + "SimpleStorage": "0x123", + "AnotherStorage": "0x234" + } + deploy.contractDB = { + "SimpleStorage": {compiled: {info: {abiDefinition: 123}}}, + "AnotherStorage": {compiled: {info: {abiDefinition: 234}}} + } + + it("should deploy contracts", function() { + var result = deploy.generate_abi_file(); + + assert.strictEqual(result, "web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));web3.eth.defaultAccount = web3.eth.accounts[0];var SimpleStorageAbi = 123;var SimpleStorageContract = web3.eth.contract(SimpleStorageAbi);var SimpleStorage = SimpleStorageContract.at('0x123');var AnotherStorageAbi = 234;var AnotherStorageContract = web3.eth.contract(AnotherStorageAbi);var AnotherStorage = AnotherStorageContract.at('0x234');"); + }); + }); }); - describe('#generate_abi_file', function() { - deploy.deployedContracts = { - "SimpleStorage": "0x123", - "AnotherStorage": "0x234" - } - deploy.contractDB = { - "SimpleStorage": {compiled: {info: {abiDefinition: 123}}}, - "AnotherStorage": {compiled: {info: {abiDefinition: 234}}} - } + describe('contracts instances', function() { + var files = [ + 'test/support/contracts/simple_storage.sol' + ]; - it("should deploy contracts", function() { - compiler.init('development'); - var result = deploy.generate_abi_file(); - - assert.strictEqual(result, "web3.setProvider(new web3.providers.HttpProvider('http://localhost:8101'));web3.eth.defaultAccount = web3.eth.accounts[0];var SimpleStorageAbi = 123;var SimpleStorageContract = web3.eth.contract(SimpleStorageAbi);var SimpleStorage = SimpleStorageContract.at('0x123');var AnotherStorageAbi = 234;var AnotherStorageContract = web3.eth.contract(AnotherStorageAbi);var AnotherStorage = AnotherStorageContract.at('0x234');"); + var deploy = setDeployConfig({ + files: files, + blockchain: 'test/support/blockchain.yml', + contracts: 'test/support/instances.yml' }); + + describe('#deploy_contracts', function() { + deploy.deploy_contracts("development"); + + it("should deploy contracts", function() { + var all_contracts = ['Wallet', 'SimpleStorage', 'AnotherStorage', 'Wallets']; + for(var i=0; i < all_contracts; i++) { + var className = all_contracts[i]; + + assert.equal(deploy.deployedContracts.hasOwnProperty(className), true); + } + }); + + }); + }); });