2017-06-26 13:01:54 +00:00
var async = require ( 'async' ) ;
2018-01-05 20:10:47 +00:00
//require("../utils/debug_util.js")(__filename, async);
2017-06-26 13:01:54 +00:00
var Web3 = require ( 'web3' ) ;
2018-01-20 02:13:20 +00:00
var Engine = require ( '../core/engine.js' ) ;
var RunCode = require ( '../core/runCode.js' ) ;
2017-06-26 13:01:54 +00:00
var TestLogger = require ( './test_logger.js' ) ;
var getSimulator = function ( ) {
2017-03-04 02:06:44 +00:00
try {
2017-06-26 13:01:54 +00:00
var sim = require ( 'ethereumjs-testrpc' ) ;
return sim ;
2017-03-04 02:06:44 +00:00
} catch ( e ) {
if ( e . code === 'MODULE_NOT_FOUND' ) {
console . log ( 'Simulator not found; Please install it with "npm install ethereumjs-testrpc --save"' ) ;
console . log ( 'IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save"' ) ;
console . log ( 'For more information see https://github.com/ethereumjs/testrpc' ) ;
// TODO: should throw exception instead
return process . exit ( ) ;
}
console . log ( "==============" ) ;
console . log ( "Tried to load testrpc but an error occurred. This is a problem with testrpc" ) ;
console . log ( 'IMPORTANT: if you using a NodeJS version older than 6.9.1 then you need to install an older version of testrpc "npm install ethereumjs-testrpc@2.0 --save". Alternatively install node 6.9.1 and the testrpc 3.0' ) ;
console . log ( "==============" ) ;
throw e ;
}
} ;
2017-06-26 13:01:54 +00:00
var Test = function ( options ) {
this . options = options || { } ;
2018-01-15 14:51:45 +00:00
this . simOptions = this . options . simulatorOptions || { } ;
2017-03-04 02:06:44 +00:00
2017-06-26 13:01:54 +00:00
this . engine = new Engine ( {
env : this . options . env || 'test' ,
// TODO: confi will need to detect if this is a obj
embarkConfig : this . options . embarkConfig || 'embark.json' ,
interceptLogs : false
} ) ;
2017-03-30 11:12:39 +00:00
2017-06-26 13:01:54 +00:00
this . engine . init ( {
logger : new TestLogger ( { logLevel : 'debug' } )
} ) ;
this . web3 = new Web3 ( ) ;
} ;
Test . prototype . deployAll = function ( contractsConfig , cb ) {
var self = this ;
2018-01-15 14:51:45 +00:00
if ( this . simOptions . node ) {
this . web3 . setProvider ( new this . web3 . providers . HttpProvider ( this . simOptions . node ) ) ;
} else {
this . sim = getSimulator ( ) ;
this . web3 . setProvider ( this . sim . provider ( this . simOptions ) ) ;
}
2017-06-26 13:01:54 +00:00
async . waterfall ( [
function getConfig ( callback ) {
2017-12-16 22:55:27 +00:00
let _versions _default = self . engine . config . contractsConfig . versions ;
self . engine . config . contractsConfig = { contracts : contractsConfig , versions : _versions _default } ;
2017-06-26 13:01:54 +00:00
callback ( ) ;
} ,
function startServices ( callback ) {
//{abiType: 'contracts', embarkJS: false}
2017-12-30 22:07:13 +00:00
self . engine . startService ( "libraryManager" ) ;
2017-10-13 09:56:42 +00:00
self . engine . startService ( "codeGenerator" ) ;
2017-06-26 13:01:54 +00:00
self . engine . startService ( "deployment" , {
web3 : self . web3 ,
trackContracts : false
} ) ;
callback ( ) ;
} ,
function deploy ( callback ) {
2017-10-13 09:56:42 +00:00
self . engine . events . on ( 'code-generator-ready' , function ( ) {
2017-10-17 11:03:13 +00:00
self . engine . events . request ( 'code-contracts-vanila' , function ( vanillaABI ) {
2017-10-13 09:56:42 +00:00
callback ( null , vanillaABI ) ;
} ) ;
2017-06-26 13:01:54 +00:00
} ) ;
2017-10-13 09:56:42 +00:00
2018-01-13 16:38:10 +00:00
self . engine . deployManager . gasLimit = 6000000 ;
2018-02-21 23:43:34 +00:00
self . engine . contractsManager . gasLimit = 6000000 ;
2018-01-13 16:38:10 +00:00
self . engine . deployManager . fatalErrors = true ;
2018-03-11 12:28:03 +00:00
self . engine . deployManager . deployOnlyOnConfig = true ;
2017-12-14 01:15:57 +00:00
self . engine . deployManager . deployContracts ( function ( err , _result ) {
2017-06-26 13:01:54 +00:00
if ( err ) {
callback ( err ) ;
}
} ) ;
}
] , function ( err , result ) {
if ( err ) {
2018-01-13 16:38:10 +00:00
console . log ( 'terminating due to error' ) ;
2018-04-24 14:19:01 +00:00
process . exit ( 1 ) ;
2017-03-12 03:23:30 +00:00
}
2017-06-26 13:01:54 +00:00
// this should be part of the waterfall and not just something done at the
// end
2017-12-14 01:15:57 +00:00
self . web3 . eth . getAccounts ( function ( err , accounts ) {
2017-06-26 13:01:54 +00:00
if ( err ) {
throw new Error ( err ) ;
}
self . web3 . eth . defaultAccount = accounts [ 0 ] ;
2018-02-08 00:49:54 +00:00
RunCode . doEval ( result , self . web3 ) ;
2017-12-22 18:07:43 +00:00
//cb();
cb ( accounts ) ;
2017-06-26 13:01:54 +00:00
} ) ;
} ) ;
} ;
2016-08-21 22:05:35 +00:00
module . exports = Test ;