1
1
/* global ethereum */
2
2
3
- import { reduce } from './async' ;
3
+ import { reduce } from './async' ;
4
+ import utils from './utils' ;
4
5
5
6
let Blockchain = {
6
7
Contract : Contract ,
@@ -244,7 +245,7 @@ function Contract(options) {
244
245
this . abi = options . abi || options . abiDefinition ;
245
246
this . address = options . address || options . deployedAddress ;
246
247
this . gas = options . gas ;
247
- this . code = '0x' + options . code ;
248
+ this . code = utils . hexPrefix ( options . code ) ;
248
249
249
250
this . blockchainConnector = Blockchain . blockchainConnector ;
250
251
@@ -311,40 +312,50 @@ function Contract(options) {
311
312
}
312
313
} ) ;
313
314
315
+ // Assign helpers too
316
+ for ( const method of [ "deploy" , "new" , "at" , "send" , "deployed" ] ) {
317
+ // Make sure we don't override original methods here.
318
+ if ( originalMethods . includes ( method ) ) {
319
+ console . log ( method + " is a reserved word and will not be aliased as a helper" ) ;
320
+ continue ;
321
+ }
322
+
323
+ ContractClass [ method ] = Contract . prototype [ method ] . bind ( this ) ;
324
+ }
325
+
326
+ this . contractClass = ContractClass ;
327
+
314
328
return ContractClass ;
315
329
}
316
330
317
- Contract . prototype . deploy = function ( args , _options ) {
318
- var self = this ;
319
- var contractParams ;
320
- var options = _options || { } ;
321
-
322
- contractParams = args || [ ] ;
331
+ Contract . prototype . deploy = function ( args , _options , _txOptions ) {
332
+ const self = this ;
333
+ const options = Object . assign ( {
334
+ arguments : args || [ ] ,
335
+ data : this . code
336
+ } , _options ) ;
323
337
324
- contractParams . push ( {
338
+ const txOptions = Object . assign ( {
325
339
from : this . blockchainConnector . getDefaultAccount ( ) ,
326
- data : this . code ,
327
- gas : options . gas || 800000
340
+ gas : this . gas
341
+ } , _txOptions ) ;
342
+
343
+ const contract = this . blockchainConnector . newContract ( { abi : this . abi } ) ;
344
+
345
+ this . _deployPromise = new Promise ( ( resolve , reject ) => {
346
+ contract . deploy . apply ( contract , [ options ] ) . send ( txOptions ) . then ( instance => {
347
+ resolve ( new Contract ( {
348
+ abi : self . abi ,
349
+ code : self . code ,
350
+ address : instance . options . address
351
+ } ) ) ;
352
+
353
+ // Delete the deploy promise as we don't need to track it anymore.
354
+ delete self . _deployPromise ;
355
+ } ) . catch ( reject ) ;
328
356
} ) ;
329
357
330
-
331
- const contractObject = this . blockchainConnector . newContract ( { abi : this . abi } ) ;
332
-
333
- return new Promise ( function ( resolve , reject ) {
334
- contractParams . push ( function ( err , transaction ) {
335
- if ( err ) {
336
- reject ( err ) ;
337
- } else if ( transaction . address !== undefined ) {
338
- resolve ( new Contract ( {
339
- abi : self . abi ,
340
- code : self . code ,
341
- address : transaction . address
342
- } ) ) ;
343
- }
344
- } ) ;
345
-
346
- contractObject [ "new" ] . apply ( contractObject , contractParams ) ;
347
- } ) ;
358
+ return this . _deployPromise ;
348
359
} ;
349
360
350
361
Contract . prototype . new = Contract . prototype . deploy ;
@@ -369,6 +380,15 @@ Contract.prototype.send = function(value, unit, _options) {
369
380
return this . blockchainConnector . send ( options ) ;
370
381
} ;
371
382
383
+ Contract . prototype . deployed = function ( ) {
384
+ // This API exists just for compatibility reasons, so it works with tools that
385
+ // don't have their Smart Contracts deployed initially a the time of importing
386
+ // them, like Embark does.
387
+ return Promise . resolve ( this ) ;
388
+ } ;
389
+
390
+ Blockchain . Contract = Contract ;
391
+
372
392
class BlockchainConnectionError extends Error {
373
393
constructor ( connectionErrors ) {
374
394
super ( "Could not establish a connection to a node." ) ;
0 commit comments