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();
});
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);
}

View File

@ -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);
});