From 5a5485d4475f74e98d98b531a6477b8f2e384467 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 10 Oct 2018 12:09:56 -0400 Subject: [PATCH] get rid of dependency on web3 --- lib/modules/blockchain_connector/index.js | 12 +++ lib/modules/tests/test.js | 101 ++++++++++------------ 2 files changed, 58 insertions(+), 55 deletions(-) diff --git a/lib/modules/blockchain_connector/index.js b/lib/modules/blockchain_connector/index.js index 99f949b3..4e95cdec 100644 --- a/lib/modules/blockchain_connector/index.js +++ b/lib/modules/blockchain_connector/index.js @@ -218,6 +218,14 @@ class BlockchainConnector { 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) { self.getBlock(blockNumber, cb); }); @@ -247,6 +255,10 @@ class BlockchainConnector { this.web3.eth.getAccounts(cb); } + getBalance(address, cb) { + this.web3.eth.getBalance(address, cb); + } + getCode(address, cb) { this.web3.eth.getCode(address, cb); } diff --git a/lib/modules/tests/test.js b/lib/modules/tests/test.js index f82026d0..a033760a 100644 --- a/lib/modules/tests/test.js +++ b/lib/modules/tests/test.js @@ -7,8 +7,6 @@ const AccountParser = require('../../utils/accountParser'); const Provider = require('../blockchain_connector/provider.js'); const utils = require('../../utils/utils'); -const EmbarkJS = require('embarkjs'); - class Test { constructor(options) { this.options = options || {}; @@ -22,7 +20,6 @@ class Test { this.contracts = {}; this.firstDeployment = true; this.needConfig = true; - this.web3 = null; this.blockchainConnector = null; this.provider = null; this.accounts = []; @@ -40,8 +37,6 @@ class Test { this.events.request('blockchain:object', (connector) => { this.blockchainConnector = connector; - // TODO use events instead of using web3 directly? - this.web3 = this.blockchainConnector.web3; callback(); }); @@ -252,35 +247,38 @@ class Test { const {host, port, type, accounts} = options.deployment || {}; 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) { resetServices = true; } - if (accounts) { - self.simOptions.accounts = AccountParser.parseAccountsConfig(accounts, self.web3); - } else { - self.simOptions.accounts = null; - } - Object.assign(self.simOptions, { - host, - port, - type - }); - - if (!resetServices && !self.firstRunConfig) { - return callback(); - } - - self.initWeb3Provider((err) => { - if (err) { - return callback(err); + this.events.request("blockchain:get", (web3) => { + if (accounts) { + self.simOptions.accounts = AccountParser.parseAccountsConfig(accounts, web3); + } else { + self.simOptions.accounts = null; } - self.firstRunConfig = false; - // self.initDeployServices(); - callback(); + + Object.assign(self.simOptions, { + host, + port, + type + }); + + if (!resetServices && !self.firstRunConfig) { + return callback(); + } + + self.initWeb3Provider((err) => { + if (err) { + return callback(err); + } + self.firstRunConfig = false; + // self.initDeployServices(); + callback(); + }); }); } @@ -304,12 +302,6 @@ class Test { function checkDeploymentOpts(next) { self.checkDeploymentOptions(options, next); }, - function updateWeb3(next) { - self.events.request("blockchain:get", function(web3) { - self.web3 = web3; - next(); - }); - }, function compileContracts(next) { if (!self.firstDeployment) { return next(); @@ -354,23 +346,25 @@ class Test { next(); }, function getAccounts(next) { - self.web3.eth.getAccounts(function (err, accounts) { + self.events.request('blockchain:getAccounts', (err, accounts) => { if (err) { return next(err); } self.accounts = accounts; - self.web3.eth.defaultAccount = accounts[0]; - next(null, accounts); + self.events.request('blockchain:defaultAccount:set', accounts[0], () => { + next(null, accounts); + }); }); }, 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) { self.logger.warn("Warning: default account has no funds"); } next(null, accounts); - }).catch((err) => { - next(err); }); }, function deploy(accounts, next) { @@ -386,26 +380,23 @@ class Test { self.contracts[contract.className] = {}; } - let newContract = new EmbarkJS.Blockchain.Contract({ + self.events.request('blockchain:contract:create', { abi: contract.abiDefinition, - address: contract.deployedAddress, - from: self.web3.eth.defaultAccount, - gas: 6000000, - web3: self.web3 - }); - - if (newContract.options) { - newContract.options.from = self.web3.eth.defaultAccount; - newContract.options.data = contract.code; - if (!newContract.options.data.startsWith('0x')) { - newContract.options.data = '0x' + newContract.options.data; + address: contract.deployedAddress + }, (newContract) => { + if (newContract.options) { + newContract.options.data = contract.code; + newContract.options.from = accounts[0]; + if (!newContract.options.data.startsWith('0x')) { + newContract.options.data = '0x' + newContract.options.data; + } + newContract.options.gas = 6000000; } - newContract.options.gas = 6000000; - } - Object.setPrototypeOf(self.contracts[contract.className], newContract); + Object.setPrototypeOf(self.contracts[contract.className], newContract); - eachCb(); + eachCb(); + }); }, (err) => { next(err, accounts); });