make require and test work
This commit is contained in:
parent
74d76585db
commit
89e0d39742
|
@ -19,11 +19,7 @@ class DeployManager {
|
||||||
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false;
|
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
deployContracts(compileOnlyIfNeeded, done) {
|
deployContracts(done) {
|
||||||
if (typeof compileOnlyIfNeeded === 'function') {
|
|
||||||
done = compileOnlyIfNeeded;
|
|
||||||
compileOnlyIfNeeded = false;
|
|
||||||
}
|
|
||||||
let self = this;
|
let self = this;
|
||||||
|
|
||||||
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
|
||||||
|
@ -34,13 +30,6 @@ class DeployManager {
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function buildContracts(callback) {
|
function buildContracts(callback) {
|
||||||
if (compileOnlyIfNeeded) {
|
|
||||||
if (self.contractsManager.compiledContracts && Object.keys(self.contractsManager.compiledContracts).length) {
|
|
||||||
// Contracts have already been compiled
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log('compiling');
|
|
||||||
self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor
|
self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor
|
||||||
self.contractsManager.build(() => {
|
self.contractsManager.build(() => {
|
||||||
callback();
|
callback();
|
||||||
|
|
|
@ -56,7 +56,8 @@ module.exports = {
|
||||||
global.contract = function(describeName, callback) {
|
global.contract = function(describeName, callback) {
|
||||||
return Mocha.describe(describeName, callback);
|
return Mocha.describe(describeName, callback);
|
||||||
};
|
};
|
||||||
next();
|
|
||||||
|
test.init(next);
|
||||||
}
|
}
|
||||||
], (err) => {
|
], (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|
|
@ -27,6 +27,8 @@ class Test {
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.options = options || {};
|
this.options = options || {};
|
||||||
this.simOptions = this.options.simulatorOptions || {};
|
this.simOptions = this.options.simulatorOptions || {};
|
||||||
|
this.contracts = {};
|
||||||
|
|
||||||
this.web3 = new Web3();
|
this.web3 = new Web3();
|
||||||
if (this.simOptions.node) {
|
if (this.simOptions.node) {
|
||||||
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node));
|
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node));
|
||||||
|
@ -57,6 +59,12 @@ class Test {
|
||||||
this.engine.startService("codeGenerator");
|
this.engine.startService("codeGenerator");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init(callback) {
|
||||||
|
this.engine.contractsManager.build(() => {
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
config(options, callback) {
|
config(options, callback) {
|
||||||
this.options = utils.recursiveMerge(this.options, options);
|
this.options = utils.recursiveMerge(this.options, options);
|
||||||
this.simOptions = this.options.simulatorOptions || {};
|
this.simOptions = this.options.simulatorOptions || {};
|
||||||
|
@ -70,46 +78,77 @@ class Test {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
_deploy(config, cb) {
|
_deploy(config, callback) {
|
||||||
const self = this;
|
const self = this;
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function getConfig(callback) {
|
function getConfig(next) {
|
||||||
let _versions_default = self.engine.config.contractsConfig.versions;
|
let _versions_default = self.engine.config.contractsConfig.versions;
|
||||||
self.engine.config.contractsConfig = {contracts: config.contracts, versions: _versions_default};
|
self.engine.config.contractsConfig = {contracts: config.contracts, versions: _versions_default};
|
||||||
callback();
|
|
||||||
},
|
|
||||||
function reloadConfig(callback) {
|
|
||||||
self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig);
|
self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig);
|
||||||
callback();
|
next();
|
||||||
},
|
},
|
||||||
function deploy(callback) {
|
function deploy(next) {
|
||||||
|
// self.engine.events.on('code-generator-ready', function () {
|
||||||
|
// self.engine.events.request('code-generator:contract:vanilla', function(vanillaABI) {
|
||||||
|
// console.log(vanillaABI);
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
self.engine.deployManager.gasLimit = 6000000;
|
self.engine.deployManager.gasLimit = 6000000;
|
||||||
self.engine.contractsManager.gasLimit = 6000000;
|
self.engine.contractsManager.gasLimit = 6000000;
|
||||||
self.engine.deployManager.fatalErrors = true;
|
self.engine.deployManager.fatalErrors = true;
|
||||||
self.engine.deployManager.deployOnlyOnConfig = true;
|
self.engine.deployManager.deployOnlyOnConfig = true;
|
||||||
self.engine.deployManager.deployContracts(true, function(err, _result) {
|
self.engine.deployManager.deployContracts(function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
callback();
|
next();
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
function getAccounts(next) {
|
||||||
|
self.web3.eth.getAccounts(function(err, accounts) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
self.accounts = accounts;
|
||||||
|
self.web3.eth.defaultAccount = accounts[0];
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
function createContractObject(next) {
|
||||||
|
async.each(Object.keys(self.contracts), (contractName, eachCb) => {
|
||||||
|
const contract = self.engine.contractsManager.contracts[contractName];
|
||||||
|
self.contracts[contractName].contract = new self.web3.eth.Contract(contract.abiDefinition, contract.address,
|
||||||
|
{from: self.web3.defaultAccount, gas: 6000000});
|
||||||
|
eachCb();
|
||||||
|
}, next);
|
||||||
}
|
}
|
||||||
], function(err) {
|
], function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(__('terminating due to error'));
|
console.log(__('terminating due to error'));
|
||||||
cb(err);
|
throw new Error(err);
|
||||||
}
|
}
|
||||||
// this should be part of the waterfall and not just something done at the
|
callback();
|
||||||
// end
|
|
||||||
self.web3.eth.getAccounts(function(err, accounts) {
|
|
||||||
if (err) {
|
|
||||||
throw new Error(err);
|
|
||||||
}
|
|
||||||
self.web3.eth.defaultAccount = accounts[0];
|
|
||||||
cb(null, accounts);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require(module) {
|
||||||
|
if (module.startsWith('contracts/')) {
|
||||||
|
const contractName = module.substr(10);
|
||||||
|
if (!this.engine.contractsManager.contracts[contractName]) {
|
||||||
|
throw new Error(__('No contract with the name %s', contractName));
|
||||||
|
}
|
||||||
|
if (this.contracts[contractName]) {
|
||||||
|
return this.contracts[contractName];
|
||||||
|
}
|
||||||
|
const contract = this.engine.contractsManager.contracts[contractName];
|
||||||
|
this.contracts[contractName] = {};
|
||||||
|
// TODO find better way to update contract
|
||||||
|
this.contracts[contractName].contract = new this.web3.eth.Contract(contract.abiDefinition, contract.address);
|
||||||
|
return this.contracts[contractName];
|
||||||
|
}
|
||||||
|
throw new Error(__('Unknown module %s', module));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Test;
|
module.exports = Test;
|
||||||
|
|
|
@ -1,31 +1,33 @@
|
||||||
|
/*global contract, before, it, embark, web3*/
|
||||||
|
const assert = require('assert');
|
||||||
|
const SimpleStorage = embark.require('contracts/SimpleStorage');
|
||||||
|
|
||||||
contract("SimpleStorage", function() {
|
contract("SimpleStorage", function () {
|
||||||
|
|
||||||
this.timeout(0);
|
this.timeout(0);
|
||||||
before(function(done) {
|
|
||||||
this.timeout(0);
|
|
||||||
|
|
||||||
//config({
|
before(function (done) {
|
||||||
// node: "http://localhost:8545"
|
const contractsConfig = {
|
||||||
//});
|
contracts: {
|
||||||
|
"SimpleStorage": {
|
||||||
var contractsConfig = {
|
args: [100]
|
||||||
"SimpleStorage": {
|
}
|
||||||
args: [100]
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
EmbarkSpec.deployAll(contractsConfig, () => { done() });
|
embark.config(contractsConfig, () => {
|
||||||
|
done();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should set constructor value", async function() {
|
it("should set constructor value", async function () {
|
||||||
let result = await SimpleStorage.methods.storedData().call();
|
let result = await SimpleStorage.contract.methods.storedData().call();
|
||||||
assert.equal(result, 100);
|
assert.strictEqual(parseInt(result, 10), 100);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("set storage value", async function() {
|
it("set storage value", async function () {
|
||||||
await SimpleStorage.methods.set(150).send();
|
// TODO Solve from
|
||||||
let result = await SimpleStorage.methods.get().call();
|
await SimpleStorage.contract.methods.set(150).send({from: web3.eth.defaultAccount});
|
||||||
assert.equal(result, 499650);
|
let result = await SimpleStorage.contract.methods.get().call();
|
||||||
|
assert.strictEqual(parseInt(result, 10), 499650);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue