get rid of dependency on web3

This commit is contained in:
Jonathan Rainville 2018-10-10 12:09:56 -04:00 committed by Pascal Precht
parent ddc8b36329
commit 5a5485d447
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 58 additions and 55 deletions

View File

@ -218,6 +218,14 @@ class BlockchainConnector {
cb(); cb();
}); });
this.events.setCommandHandler("blockchain:getAccounts", function(cb) {
self.getAccounts(cb);
});
this.events.setCommandHandler("blockchain:getBalance", function(address, cb) {
self.getBalance(address, cb);
});
this.events.setCommandHandler("blockchain:block:byNumber", function(blockNumber, cb) { this.events.setCommandHandler("blockchain:block:byNumber", function(blockNumber, cb) {
self.getBlock(blockNumber, cb); self.getBlock(blockNumber, cb);
}); });
@ -247,6 +255,10 @@ class BlockchainConnector {
this.web3.eth.getAccounts(cb); this.web3.eth.getAccounts(cb);
} }
getBalance(address, cb) {
this.web3.eth.getBalance(address, cb);
}
getCode(address, cb) { getCode(address, cb) {
this.web3.eth.getCode(address, cb); this.web3.eth.getCode(address, cb);
} }

View File

@ -7,8 +7,6 @@ const AccountParser = require('../../utils/accountParser');
const Provider = require('../blockchain_connector/provider.js'); const Provider = require('../blockchain_connector/provider.js');
const utils = require('../../utils/utils'); const utils = require('../../utils/utils');
const EmbarkJS = require('embarkjs');
class Test { class Test {
constructor(options) { constructor(options) {
this.options = options || {}; this.options = options || {};
@ -22,7 +20,6 @@ class Test {
this.contracts = {}; this.contracts = {};
this.firstDeployment = true; this.firstDeployment = true;
this.needConfig = true; this.needConfig = true;
this.web3 = null;
this.blockchainConnector = null; this.blockchainConnector = null;
this.provider = null; this.provider = null;
this.accounts = []; this.accounts = [];
@ -40,8 +37,6 @@ class Test {
this.events.request('blockchain:object', (connector) => { this.events.request('blockchain:object', (connector) => {
this.blockchainConnector = connector; this.blockchainConnector = connector;
// TODO use events instead of using web3 directly?
this.web3 = this.blockchainConnector.web3;
callback(); callback();
}); });
@ -252,14 +247,16 @@ class Test {
const {host, port, type, accounts} = options.deployment || {}; const {host, port, type, accounts} = options.deployment || {};
if (host && port && !['rpc', 'ws'].includes(type)) { if (host && port && !['rpc', 'ws'].includes(type)) {
callback(__("contracts config error: unknown deployment type %s", type)); return callback(__("contracts config error: unknown deployment type %s", type));
} }
if(accounts || port !== this.simOptions.port || type !== this.simOptions.type || host !== this.simOptions.host) { if(accounts || port !== this.simOptions.port || type !== this.simOptions.type || host !== this.simOptions.host) {
resetServices = true; resetServices = true;
} }
this.events.request("blockchain:get", (web3) => {
if (accounts) { if (accounts) {
self.simOptions.accounts = AccountParser.parseAccountsConfig(accounts, self.web3); self.simOptions.accounts = AccountParser.parseAccountsConfig(accounts, web3);
} else { } else {
self.simOptions.accounts = null; self.simOptions.accounts = null;
} }
@ -282,6 +279,7 @@ class Test {
// self.initDeployServices(); // self.initDeployServices();
callback(); callback();
}); });
});
} }
config(options, callback) { config(options, callback) {
@ -304,12 +302,6 @@ class Test {
function checkDeploymentOpts(next) { function checkDeploymentOpts(next) {
self.checkDeploymentOptions(options, next); self.checkDeploymentOptions(options, next);
}, },
function updateWeb3(next) {
self.events.request("blockchain:get", function(web3) {
self.web3 = web3;
next();
});
},
function compileContracts(next) { function compileContracts(next) {
if (!self.firstDeployment) { if (!self.firstDeployment) {
return next(); return next();
@ -354,23 +346,25 @@ class Test {
next(); next();
}, },
function getAccounts(next) { function getAccounts(next) {
self.web3.eth.getAccounts(function (err, accounts) { self.events.request('blockchain:getAccounts', (err, accounts) => {
if (err) { if (err) {
return next(err); return next(err);
} }
self.accounts = accounts; self.accounts = accounts;
self.web3.eth.defaultAccount = accounts[0]; self.events.request('blockchain:defaultAccount:set', accounts[0], () => {
next(null, accounts); next(null, accounts);
}); });
});
}, },
function getBalance(accounts, next) { function getBalance(accounts, next) {
self.web3.eth.getBalance(self.web3.eth.defaultAccount).then((balance) => { self.events.request('blockchain:getBalance', self.accounts[0], (err, balance) => {
if (err) {
return next(err);
}
if (parseInt(balance, 10) === 0) { if (parseInt(balance, 10) === 0) {
self.logger.warn("Warning: default account has no funds"); self.logger.warn("Warning: default account has no funds");
} }
next(null, accounts); next(null, accounts);
}).catch((err) => {
next(err);
}); });
}, },
function deploy(accounts, next) { function deploy(accounts, next) {
@ -386,17 +380,13 @@ class Test {
self.contracts[contract.className] = {}; self.contracts[contract.className] = {};
} }
let newContract = new EmbarkJS.Blockchain.Contract({ self.events.request('blockchain:contract:create', {
abi: contract.abiDefinition, abi: contract.abiDefinition,
address: contract.deployedAddress, address: contract.deployedAddress
from: self.web3.eth.defaultAccount, }, (newContract) => {
gas: 6000000,
web3: self.web3
});
if (newContract.options) { if (newContract.options) {
newContract.options.from = self.web3.eth.defaultAccount;
newContract.options.data = contract.code; newContract.options.data = contract.code;
newContract.options.from = accounts[0];
if (!newContract.options.data.startsWith('0x')) { if (!newContract.options.data.startsWith('0x')) {
newContract.options.data = '0x' + newContract.options.data; newContract.options.data = '0x' + newContract.options.data;
} }
@ -406,6 +396,7 @@ class Test {
Object.setPrototypeOf(self.contracts[contract.className], newContract); Object.setPrototypeOf(self.contracts[contract.className], newContract);
eachCb(); eachCb();
});
}, (err) => { }, (err) => {
next(err, accounts); next(err, accounts);
}); });