chore: set contract objects to EmbarkJS' wrapper (#1893)

This commit is contained in:
André Medeiros 2019-09-11 13:06:39 -04:00 committed by Iuri Matias
parent 8849aca264
commit 5faa07cc9f
2 changed files with 27 additions and 16 deletions

View File

@ -62,6 +62,7 @@
}, },
"dependencies": { "dependencies": {
"async": "2.6.1", "async": "2.6.1",
"embarkjs": "^4.1.1",
"mocha": "6.2.0", "mocha": "6.2.0",
"web3": "1.2.1" "web3": "1.2.1"
}, },

View File

@ -1,5 +1,6 @@
const assert = require('assert').strict; const assert = require('assert').strict;
const async = require('async'); const async = require('async');
const EmbarkJS = require('embarkjs');
const Mocha = require('mocha'); const Mocha = require('mocha');
const Web3 = require('web3'); const Web3 = require('web3');
@ -61,9 +62,27 @@ class MochaTestRunner {
}, },
(contracts, next) => { (contracts, next) => {
for(const contract of contracts) { for(const contract of contracts) {
const instance = compiledContracts[contract.className]; const instance = EmbarkJS.Blockchain.Contract(contract);
const contractObj = new web3.eth.Contract(instance.abiDefinition, contract.deployedAddress);
Object.setPrototypeOf(compiledContracts[contract.className], contractObj); // Here we switch the prototype of the instance we had lying around to the more
// complete web3 contract instance (with some methods of our own.) Despite this
// looking hacky, it's necessary. As mocha tests look something like this:
//
// const SimpleStorage = require('Embark/contracts/SimpleStorage');
//
// config({
// contracts: {
// SimpleStorage: { args: [100] }
// }
// }, (err, accounts) => {
//
// });
//
// it means that we have to return something before the address is set. So,
// due to that constraint, the only sane way to modify the object the test
// file is hanging on to is to replace the prototype instead of switching
// it around and having the test losing the reference.
Object.setPrototypeOf(compiledContracts[contract.className], instance);
} }
next(); next();
@ -87,21 +106,12 @@ class MochaTestRunner {
(next) => { // request provider (next) => { // request provider
events.request("blockchain:client:provider", "ethereum", next); events.request("blockchain:client:provider", "ethereum", next);
}, },
(provider, next) => { // set provider (provider, next) => { // set provider and fetch account list
web3 = new Web3(provider); web3 = new Web3(provider);
next(); web3.eth.getAccounts(next);
}, },
(next) => { // get accounts (accts, next) => { // reset contracts as we might have state leakage from other plugins
web3.eth.getAccounts((err, accts) => { accounts = accts;
if (err) {
return next(err);
}
accounts = accts;
next();
});
},
(next) => { // reset contracts as we might have state leakage from other plugins
events.request("contracts:reset", next); events.request("contracts:reset", next);
}, },
(next) => { // get contract files (next) => { // get contract files