2017-03-04 02:06:44 +00:00
var getSimulator = function ( ) {
try {
2017-03-12 03:23:30 +00:00
return require ( 'ethereumjs-testrpc' ) ;
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-03-12 15:21:19 +00:00
var Test ;
Test = ( function ( options ) {
var async = require ( 'async' ) ;
2017-03-12 03:23:30 +00:00
var opts = options === undefined ? { } : options ;
opts . logLevel = opts . hasOwnProperty ( 'logLevel' ) ? opts . logLevel : 'debug' ;
opts . simulatorOptions = opts . hasOwnProperty ( 'simulatorOptions' ) ? opts . simulatorOptions : { } ;
2017-03-12 15:21:19 +00:00
var sim = getSimulator ( ) ;
2017-03-04 02:06:44 +00:00
2017-03-12 15:21:19 +00:00
function newWebThree ( ) {
2017-03-12 03:23:30 +00:00
try {
var Web3 = require ( 'web3' ) ;
var web3 = new Web3 ( ) ;
2017-03-12 15:21:19 +00:00
web3 . setProvider ( sim . provider ( opts . simulatorOptions ) ) ;
return web3 ;
2017-03-12 03:23:30 +00:00
} catch ( e ) {
throw new Error ( e ) ;
}
}
2017-03-03 06:22:12 +00:00
2017-03-12 15:21:19 +00:00
function deployAll ( contractsConfig , cb ) {
2017-03-12 03:23:30 +00:00
var RunCode = require ( './runCode.js' ) ;
var self = this ;
2017-03-03 06:22:12 +00:00
2017-03-12 03:23:30 +00:00
function newEngine ( ) {
var Engine = require ( './engine.js' ) ;
return new Engine ( {
env : opts . env || 'test' ,
// TODO: confi will need to detect if this is a obj
embarkConfig : opts . embarkConfig || 'embark.json' ,
interceptLogs : false
2017-03-12 15:21:19 +00:00
} ) ;
2017-03-12 03:23:30 +00:00
}
2016-10-02 20:57:13 +00:00
2017-03-12 15:21:19 +00:00
self . web3 = newWebThree ( ) ;
self . engine = newEngine ( ) ;
self . engine . init ( ) ;
2017-03-12 03:23:30 +00:00
async . waterfall ( [
2017-02-03 11:30:08 +00:00
function getConfig ( callback ) {
2017-03-12 15:21:19 +00:00
self . engine . config . contractsConfig = { contracts : contractsConfig } ;
2017-03-03 06:22:12 +00:00
callback ( ) ;
2017-02-03 11:30:08 +00:00
} ,
2017-03-03 06:22:12 +00:00
function startServices ( callback ) {
//{abiType: 'contracts', embarkJS: false}
self . engine . startService ( "abi" ) ;
self . engine . startService ( "deployment" , {
web3 : self . web3 ,
trackContracts : false
2017-02-06 11:42:58 +00:00
} ) ;
2017-03-03 06:22:12 +00:00
callback ( ) ;
2016-10-02 20:57:13 +00:00
} ,
2017-03-03 06:22:12 +00:00
function deploy ( callback ) {
2017-03-12 15:21:19 +00:00
self . engine . events . on ( 'abi-contracts-vanila' , function ( vanillaABI ) {
2017-03-03 06:22:12 +00:00
callback ( null , vanillaABI ) ;
2016-10-02 20:57:13 +00:00
} ) ;
2017-03-12 15:21:19 +00:00
self . engine . deployManager . deployContracts ( function ( err , result ) {
2017-03-05 02:51:28 +00:00
if ( err ) {
console . log ( err ) ;
callback ( err ) ;
}
2016-10-02 20:57:13 +00:00
} ) ;
}
2017-03-12 15:21:19 +00:00
] , function ( err , result ) {
2016-10-31 01:35:08 +00:00
if ( err ) {
2017-03-12 03:23:30 +00:00
console . log ( "got error" ) ;
process . exit ( ) ;
2016-10-31 01:35:08 +00:00
}
2017-03-12 03:23:30 +00:00
// this should be part of the waterfall and not just something done at the
// end
2017-03-12 15:21:19 +00:00
self . web3 . eth . getAccounts ( function ( err , accounts ) {
2017-03-12 03:23:30 +00:00
if ( err ) {
throw new Error ( err ) ;
}
self . web3 . eth . defaultAccount = accounts [ 0 ] ;
RunCode . doEval ( result , self . web3 ) ; // jshint ignore:line
cb ( ) ;
} ) ;
2016-08-21 22:05:35 +00:00
} ) ;
2017-03-12 03:23:30 +00:00
}
return {
2017-03-12 15:21:19 +00:00
deployAll : deployAll ,
sim : sim
} ;
} ( ) ) ;
2016-08-21 22:05:35 +00:00
module . exports = Test ;