forked from ethereum/remix-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
universal-dapp.js
802 lines (700 loc) · 23.9 KB
/
universal-dapp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/* global prompt */
'use strict'
var $ = require('jquery')
var ethJSUtil = require('ethereumjs-util')
var ethJSABI = require('ethereumjs-abi')
var BN = ethJSUtil.BN
var EventManager = require('./lib/eventManager')
var crypto = require('crypto')
var async = require('async')
var TxRunner = require('./app/txRunner')
/*
trigger debugRequested
*/
function UniversalDApp (executionContext, options) {
this.event = new EventManager()
var self = this
self.options = options || {}
self.$el = $('<div class="udapp" />')
self.personalMode = self.options.personalMode || false
self.contracts
self.transactionContextAPI
var defaultRenderOutputModifier = function (name, content) { return content }
self.renderOutputModifier = defaultRenderOutputModifier
self.web3 = executionContext.web3()
self.vm = executionContext.vm()
self.executionContext = executionContext
self.executionContext.event.register('contextChanged', this, function (context) {
self.reset(self.contracts)
})
self.txRunner = new TxRunner(executionContext, {}, {
queueTxs: true,
personalMode: this.personalMode
})
}
UniversalDApp.prototype.reset = function (contracts, transactionContextAPI, renderer) {
this.$el.empty()
this.contracts = contracts
this.transactionContextAPI = transactionContextAPI
this.renderOutputModifier = renderer
this.accounts = {}
if (this.executionContext.isVM()) {
this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511')
this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c')
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a')
this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea')
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce')
}
this.txRunner = new TxRunner(this.executionContext, this.accounts, {
queueTxs: true,
personalMode: this.personalMode
})
}
UniversalDApp.prototype.newAccount = function (password, cb) {
if (!this.executionContext.isVM()) {
if (!this.personalMode) {
return cb('Not running in personal mode')
}
this.web3.personal.newAccount(password, cb)
} else {
var privateKey
do {
privateKey = crypto.randomBytes(32)
} while (!ethJSUtil.isValidPrivate(privateKey))
this._addAccount(privateKey)
cb(null, '0x' + ethJSUtil.privateToAddress(privateKey))
}
}
UniversalDApp.prototype._addAccount = function (privateKey, balance) {
var self = this
if (!self.executionContext.isVM()) {
throw new Error('_addAccount() cannot be called in non-VM mode')
}
if (self.accounts) {
privateKey = new Buffer(privateKey, 'hex')
var address = ethJSUtil.privateToAddress(privateKey)
// FIXME: we don't care about the callback, but we should still make this proper
self.vm.stateManager.putAccountBalance(address, balance || 'f00000000000000001', function cb () {})
self.accounts['0x' + address.toString('hex')] = { privateKey: privateKey, nonce: 0 }
}
}
UniversalDApp.prototype.getAccounts = function (cb) {
var self = this
if (!self.executionContext.isVM()) {
// Weirdness of web3: listAccounts() is sync, `getListAccounts()` is async
// See: https://github.com/ethereum/web3.js/issues/442
if (self.personalMode) {
self.web3.personal.getListAccounts(cb)
} else {
self.web3.eth.getAccounts(cb)
}
} else {
if (!self.accounts) {
return cb('No accounts?')
}
cb(null, Object.keys(self.accounts))
}
}
UniversalDApp.prototype.getBalance = function (address, cb) {
var self = this
address = ethJSUtil.stripHexPrefix(address)
if (!self.executionContext.isVM()) {
self.web3.eth.getBalance(address, function (err, res) {
if (err) {
cb(err)
} else {
cb(null, res.toString(10))
}
})
} else {
if (!self.accounts) {
return cb('No accounts?')
}
self.vm.stateManager.getAccountBalance(new Buffer(address, 'hex'), function (err, res) {
if (err) {
cb('Account not found')
} else {
cb(null, new BN(res).toString(10))
}
})
}
}
UniversalDApp.prototype.render = function () {
var self = this
// NOTE: don't display anything if there are no contracts to display
if (self.contracts.length === 0) {
return self.$el
}
var $legend = $('<div class="legend" />')
.append($('<div class="attach"/>').text('Attach'))
.append($('<div class="transact"/>').text('Transact'))
.append($('<div class="payable"/>').text('Transact (Payable)'))
.append($('<div class="call"/>').text('Call'))
self.$el.append($legend)
for (var c in self.contracts) {
var $contractEl = $('<div class="contract"/>')
if (self.contracts[c].address) {
self.getInstanceInterface(self.contracts[c], self.contracts[c].address, $contractEl)
} else {
var $title = $('<span class="title"/>').text(self.contracts[c].name)
if (self.contracts[c].bytecode) {
$title.append($('<div class="size"/>').text((self.contracts[c].bytecode.length / 2) + ' bytes'))
}
$contractEl.append($title).append(self.getCreateInterface($contractEl, self.contracts[c]))
}
self.$el.append(self.renderOutputModifier(self.contracts[c].name, $contractEl))
}
return self.$el
}
UniversalDApp.prototype.getContractByName = function (contractName) {
var self = this
for (var c in self.contracts) {
if (self.contracts[c].name === contractName) {
return self.contracts[c]
}
}
return null
}
UniversalDApp.prototype.getCreateInterface = function ($container, contract) {
var self = this
var $createInterface = $('<div class="create"/>')
if (self.options.removable) {
var $close = $('<div class="udapp-close" />')
$close.click(function () { self.$el.remove() })
$createInterface.append($close)
}
var $atButton = $('<button class="atAddress"/>').text('At Address').click(function () { self.clickContractAt(self, $container.find('.createContract'), contract) })
$createInterface.append($atButton)
var $newButton = self.getInstanceInterface(contract)
if (!$newButton) {
return $createInterface
}
$createInterface.append($newButton)
// Only display creation interface for non-abstract contracts.
// FIXME: maybe have a flag for this in the JSON?
// FIXME: maybe fix getInstanceInterface() below for this case
if (contract.bytecode.length === 0) {
var $createButton = $newButton.find('.constructor .call')
// NOTE: we must show the button to have CSS properly lined up
$createButton.text('Create')
$createButton.attr('disabled', 'disabled')
$createButton.attr('title', 'This contract does not implement all functions and thus cannot be created.')
}
return $createInterface
}
UniversalDApp.prototype.getInstanceInterface = function (contract, address, $target) {
var self = this
var abi = JSON.parse(contract.interface).sort(function (a, b) {
if (a.name > b.name) {
return -1
} else {
return 1
}
}).sort(function (a, b) {
if (a.constant === true) {
return -1
} else {
return 1
}
})
var funABI = self.getConstructorInterface(abi)
if (!funABI) {
return
}
var $createInterface = $('<div class="createContract"/>')
var appendFunctions = function (address, $el) {
var $instance = $('<div class="instance"/>')
if (self.options.removable_instances) {
var $close = $('<div class="udapp-close" />')
$close.click(function () { $instance.remove() })
$instance.append($close)
}
var context = self.executionContext.isVM() ? 'memory' : 'blockchain'
address = (address.slice(0, 2) === '0x' ? '' : '0x') + address.toString('hex')
var $title = $('<span class="title"/>').text(contract.name + ' at ' + address + ' (' + context + ')')
$title.click(function () {
$instance.toggleClass('hide')
})
var $events = $('<div class="events"/>')
var parseLogs = function (err, response) {
if (err) {
return
}
var $event = $('<div class="event" />')
var $close = $('<div class="udapp-close" />')
$close.click(function () { $event.remove() })
$event.append($('<span class="name"/>').text(response.event))
.append($('<span class="args" />').text(JSON.stringify(response.args, null, 2)))
.append($close)
$events.append($event)
}
if (self.executionContext.isVM()) {
// FIXME: support indexed events
var eventABI = {}
$.each(abi, function (i, funABI) {
if (funABI.type !== 'event') {
return
}
var hash = ethJSABI.eventID(funABI.name, funABI.inputs.map(function (item) { return item.type }))
eventABI[hash.toString('hex')] = { event: funABI.name, inputs: funABI.inputs }
})
self.vm.on('afterTx', function (response) {
for (var i in response.vm.logs) {
// [address, topics, mem]
var log = response.vm.logs[i]
var event
var decoded
try {
var abi = eventABI[log[1][0].toString('hex')]
event = abi.event
var types = abi.inputs.map(function (item) {
return item.type
})
decoded = ethJSABI.rawDecode(types, log[2])
decoded = ethJSABI.stringify(types, decoded)
} catch (e) {
decoded = '0x' + log[2].toString('hex')
}
parseLogs(null, { event: event, args: decoded })
}
})
} else {
var eventFilter = self.web3.eth.contract(abi).at(address).allEvents()
eventFilter.watch(parseLogs)
}
$instance.append($title)
// Add the fallback function
var fallback = self.getFallbackInterface(abi)
if (fallback) {
$instance.append(self.getCallButton({
abi: fallback,
encode: function (args) {
return ''
},
address: address
}))
}
$.each(abi, function (i, funABI) {
if (funABI.type !== 'function') {
return
}
// @todo getData cannot be used with overloaded functions
$instance.append(self.getCallButton({
abi: funABI,
encode: function (args) {
var types = []
for (var i = 0; i < funABI.inputs.length; i++) {
types.push(funABI.inputs[i].type)
}
return Buffer.concat([ ethJSABI.methodID(funABI.name, types), ethJSABI.rawEncode(types, args) ]).toString('hex')
},
address: address
}))
})
$el = $el || $createInterface
$el.append($instance.append($events))
}
if (!address || !$target) {
$createInterface.append(self.getCallButton({
abi: funABI,
encode: function (args) {
var types = []
for (var i = 0; i < funABI.inputs.length; i++) {
types.push(funABI.inputs[i].type)
}
// NOTE: the caller will concatenate the bytecode and this
// it could be done here too for consistency
return ethJSABI.rawEncode(types, args).toString('hex')
},
contractName: contract.name,
bytecode: contract.bytecode,
appendFunctions: appendFunctions
}))
} else {
appendFunctions(address, $target)
}
return $createInterface
}
UniversalDApp.prototype.getConstructorInterface = function (abi) {
for (var i = 0; i < abi.length; i++) {
if (abi[i].type === 'constructor') {
return abi[i]
}
}
return { 'type': 'constructor', 'payable': false, 'inputs': [] }
}
UniversalDApp.prototype.getFallbackInterface = function (abi) {
for (var i = 0; i < abi.length; i++) {
if (abi[i].type === 'fallback') {
return abi[i]
}
}
}
UniversalDApp.prototype.getCallButton = function (args) {
var self = this
// args.abi, args.encode, args.bytecode [constr only], args.address [fun only]
// args.contractName [constr only], args.appendFunctions [constr only]
var isConstructor = args.bytecode !== undefined
var lookupOnly = (args.abi.constant && !isConstructor)
var inputs = ''
if (args.abi.inputs) {
$.each(args.abi.inputs, function (i, inp) {
if (inputs !== '') {
inputs += ', '
}
inputs += inp.type + ' ' + inp.name
})
}
var inputField = $('<input/>').attr('placeholder', inputs).attr('title', inputs)
var $outputOverride = $('<div class="value" />')
var outputSpan = $('<div class="output"/>')
var getReturnOutput = function (result) {
var returnName = lookupOnly ? 'Value' : 'Result'
var returnCls = lookupOnly ? 'value' : 'returned'
return $('<div class="' + returnCls + '">').html('<strong>' + returnName + ':</strong> ' + JSON.stringify(result, null, 2))
}
var getDebugTransaction = function (result) {
var $debugTx = $('<div class="debugTx">')
var $button = $('<button title="Launch Debugger" class="debug"><i class="fa fa-bug"></i></button>')
$button.click(function () {
self.event.trigger('debugRequested', [result])
})
$debugTx.append($button)
return $debugTx
}
var getDebugCall = function (result) {
var $debugTx = $('<div class="debugCall">')
var $button = $('<button title="Launch Debugger" class="debug"><i class="fa fa-bug"></i></button>')
$button.click(function () {
self.event.trigger('debugRequested', [result])
})
$debugTx.append($button)
return $debugTx
}
var getGasUsedOutput = function (result, vmResult) {
var $gasUsed = $('<div class="gasUsed">')
var caveat = lookupOnly ? '<em>(<span class="caveat" title="Cost only applies when called by a contract">caveat</span>)</em>' : ''
var gas
if (result.gasUsed) {
gas = result.gasUsed.toString(10)
$gasUsed.html('<strong>Transaction cost:</strong> ' + gas + ' gas. ' + caveat)
}
if (vmResult && vmResult.gasUsed) {
var $callGasUsed = $('<div class="gasUsed">')
gas = vmResult.gasUsed.toString(10)
$callGasUsed.append('<strong>Execution cost:</strong> ' + gas + ' gas.')
$gasUsed.append($callGasUsed)
}
return $gasUsed
}
var getDecodedOutput = function (result) {
var $decoded
if (Array.isArray(result)) {
$decoded = $('<ol>')
for (var i = 0; i < result.length; i++) {
$decoded.append($('<li>').text(result[i]))
}
} else {
$decoded = result
}
return $('<div class="decoded">').html('<strong>Decoded:</strong> ').append($decoded)
}
var getOutput = function () {
var $result = $('<div class="result" />')
var $close = $('<div class="udapp-close" />')
$close.click(function () { $result.remove() })
$result.append($close)
return $result
}
var clearOutput = function ($result) {
$(':not(.udapp-close)', $result).remove()
}
var replaceOutput = function ($result, message) {
clearOutput($result)
$result.append(message)
}
var handleCallButtonClick = function (ev, $result) {
if (!$result) {
$result = getOutput()
if (lookupOnly && !inputs.length) {
$outputOverride.empty().append($result)
} else {
outputSpan.append($result)
}
}
var funArgs = ''
try {
funArgs = $.parseJSON('[' + inputField.val() + ']')
} catch (e) {
replaceOutput($result, $('<span/>').text('Error encoding arguments: ' + e))
return
}
var data = ''
if (!isConstructor || funArgs.length > 0) {
try {
data = args.encode(funArgs)
} catch (e) {
replaceOutput($result, $('<span/>').text('Error encoding arguments: ' + e))
return
}
}
if (data.slice(0, 9) === 'undefined') {
data = data.slice(9)
}
if (data.slice(0, 2) === '0x') {
data = data.slice(2)
}
replaceOutput($result, $('<span>Waiting for transaction to be mined...</span>'))
if (isConstructor) {
if (args.bytecode.indexOf('_') >= 0) {
replaceOutput($result, $('<span>Deploying and linking required libraries...</span>'))
self.linkBytecode(args.contractName, function (err, bytecode) {
if (err) {
replaceOutput($result, $('<span/>').text('Error deploying required libraries: ' + err))
} else {
args.bytecode = bytecode
handleCallButtonClick(ev, $result)
}
})
return
} else {
data = args.bytecode + data
}
}
var decodeResponse = function (response) {
// Only decode if there supposed to be fields
if (args.abi.outputs && args.abi.outputs.length > 0) {
try {
var i
var outputTypes = []
for (i = 0; i < args.abi.outputs.length; i++) {
outputTypes.push(args.abi.outputs[i].type)
}
// decode data
var decodedObj = ethJSABI.rawDecode(outputTypes, response)
// format decoded data
decodedObj = ethJSABI.stringify(outputTypes, decodedObj)
for (i = 0; i < outputTypes.length; i++) {
var name = args.abi.outputs[i].name
if (name.length > 0) {
decodedObj[i] = outputTypes[i] + ' ' + name + ': ' + decodedObj[i]
} else {
decodedObj[i] = outputTypes[i] + ': ' + decodedObj[i]
}
}
return getDecodedOutput(decodedObj)
} catch (e) {
return getDecodedOutput('Failed to decode output: ' + e)
}
}
}
var decoded
self.runTx({ to: args.address, data: data, useCall: lookupOnly }, function (err, txResult) {
if (!txResult) {
replaceOutput($result, $('<span/>').text('callback contain no result ' + err).addClass('error'))
return
}
var result = txResult.result
if (err) {
replaceOutput($result, $('<span/>').text(err).addClass('error'))
// VM only
} else if (self.executionContext.isVM() && result.vm.exception === 0 && result.vm.exceptionError) {
replaceOutput($result, $('<span/>').text('VM Exception: ' + result.vm.exceptionError).addClass('error'))
$result.append(getDebugTransaction(txResult))
// VM only
} else if (self.executionContext.isVM() && result.vm.return === undefined) {
replaceOutput($result, $('<span/>').text('Exception during execution.').addClass('error'))
$result.append(getDebugTransaction(txResult))
} else if (isConstructor) {
replaceOutput($result, getGasUsedOutput(result, result.vm))
$result.append(getDebugTransaction(txResult))
args.appendFunctions(self.executionContext.isVM() ? result.createdAddress : result.contractAddress)
} else if (self.executionContext.isVM()) {
var outputObj = '0x' + result.vm.return.toString('hex')
clearOutput($result)
$result.append(getReturnOutput(outputObj)).append(getGasUsedOutput(result, result.vm))
decoded = decodeResponse(result.vm.return)
if (decoded) {
$result.append(decoded)
}
if (lookupOnly) {
$result.append(getDebugCall(txResult))
} else {
$result.append(getDebugTransaction(txResult))
}
} else if (lookupOnly) {
clearOutput($result)
$result.append(getReturnOutput(result)).append(getGasUsedOutput({}))
decoded = decodeResponse(ethJSUtil.toBuffer(result))
if (decoded) {
$result.append(decoded)
}
} else {
clearOutput($result)
$result.append(getReturnOutput(result)).append(getGasUsedOutput(result))
$result.append(getDebugTransaction(txResult))
}
})
}
var title
if (isConstructor) {
title = 'Create'
} else if (args.abi.name) {
title = args.abi.name
} else {
title = '(fallback)'
}
var button = $('<button />')
.addClass('call')
.attr('title', title)
.text(title)
.click(handleCallButtonClick)
if (lookupOnly && !inputs.length) {
handleCallButtonClick()
}
var $contractProperty = $('<div class="contractProperty"/>')
$contractProperty
.append(button)
.append((lookupOnly && !inputs.length) ? $outputOverride : inputField)
if (isConstructor) {
$contractProperty.addClass('constructor')
}
if (lookupOnly) {
$contractProperty.addClass('constant')
}
if (args.abi.inputs && args.abi.inputs.length > 0) {
$contractProperty.addClass('hasArgs')
}
if (args.abi.payable === true) {
$contractProperty.addClass('payable')
}
return $contractProperty.append(outputSpan)
}
UniversalDApp.prototype.linkBytecode = function (contractName, cb) {
var self = this
var bytecode = self.getContractByName(contractName).bytecode
if (bytecode.indexOf('_') < 0) {
return cb(null, bytecode)
}
var m = bytecode.match(/__([^_]{1,36})__/)
if (!m) {
return cb('Invalid bytecode format.')
}
var libraryName = m[1]
if (!self.getContractByName(libraryName)) {
return cb('Library ' + libraryName + ' not found.')
}
self.deployLibrary(libraryName, function (err, address) {
if (err) {
return cb(err)
}
var libLabel = '__' + libraryName + Array(39 - libraryName.length).join('_')
var hexAddress = address.toString('hex')
if (hexAddress.slice(0, 2) === '0x') {
hexAddress = hexAddress.slice(2)
}
hexAddress = Array(40 - hexAddress.length + 1).join('0') + hexAddress
while (bytecode.indexOf(libLabel) >= 0) {
bytecode = bytecode.replace(libLabel, hexAddress)
}
self.getContractByName(contractName).bytecode = bytecode
self.linkBytecode(contractName, cb)
})
}
UniversalDApp.prototype.deployLibrary = function (contractName, cb) {
var self = this
if (self.getContractByName(contractName).address) {
return cb(null, self.getContractByName(contractName).address)
}
var bytecode = self.getContractByName(contractName).bytecode
if (bytecode.indexOf('_') >= 0) {
self.linkBytecode(contractName, function (err, bytecode) {
if (err) cb(err)
else self.deployLibrary(contractName, cb)
})
} else {
self.runTx({ data: bytecode, useCall: false }, function (err, txResult) {
if (err) {
return cb(err)
}
var address = self.executionContext.isVM() ? txResult.result.createdAddress : txResult.result.contractAddress
self.getContractByName(contractName).address = address
cb(err, address)
})
}
}
UniversalDApp.prototype.clickContractAt = function (self, $output, contract) {
var address = prompt('What Address is this contract at in the Blockchain? ie: 0xdeadbeaf...')
self.getInstanceInterface(contract, address, $output)
}
UniversalDApp.prototype.runTx = function (args, cb) {
var self = this
var tx = {
to: args.to,
data: args.data,
useCall: args.useCall
}
async.waterfall([
// query gas limit
function (callback) {
tx.gasLimit = 3000000
if (self.transactionContextAPI.getGasLimit) {
self.transactionContextAPI.getGasLimit(function (err, ret) {
if (err) {
return callback(err)
}
tx.gasLimit = ret
callback()
})
} else {
callback()
}
},
// query value
function (callback) {
tx.value = 0
if (self.transactionContextAPI.getValue) {
self.transactionContextAPI.getValue(function (err, ret) {
if (err) {
return callback(err)
}
tx.value = ret
callback()
})
} else {
callback()
}
},
// query address
function (callback) {
if (self.transactionContextAPI.getAddress) {
self.transactionContextAPI.getAddress(function (err, ret) {
if (err) {
return callback(err)
}
tx.from = ret
callback()
})
} else {
self.getAccounts(function (err, ret) {
if (err) {
return callback(err)
}
if (ret.length === 0) {
return callback('No accounts available')
}
if (self.executionContext.isVM() && !self.accounts[ret[0]]) {
return callback('Invalid account selected')
}
tx.from = ret[0]
callback()
})
}
},
// run transaction
function (callback) {
self.txRunner.rawRun(tx, function (error, result) { callback(error, result) })
}
], cb)
}
module.exports = UniversalDApp