-
Notifications
You must be signed in to change notification settings - Fork 5k
/
index.js
997 lines (852 loc) · 37.8 KB
/
index.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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file index.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @author Marek Kotewicz <marek@parity.io>
* @date 2017
*/
'use strict';
var errors = require('web3-core-helpers').errors;
var formatters = require('web3-core-helpers').formatters;
var utils = require('web3-utils');
var promiEvent = require('web3-core-promievent');
var Subscriptions = require('web3-core-subscriptions').subscriptions;
var EthersTransactionUtils = require('@ethersproject/transactions');
var Method = function Method(options) {
if (!options.call || !options.name) {
throw new Error('When creating a method you need to provide at least the "name" and "call" property.');
}
this.name = options.name;
this.call = options.call;
this.params = options.params || 0;
this.inputFormatter = options.inputFormatter;
this.outputFormatter = options.outputFormatter;
this.transformPayload = options.transformPayload;
this.extraFormatters = options.extraFormatters;
this.abiCoder = options.abiCoder; // Will be used to encode the revert reason string
this.requestManager = options.requestManager;
// reference to eth.accounts
this.accounts = options.accounts;
this.defaultBlock = options.defaultBlock || 'latest';
this.defaultAccount = options.defaultAccount || null;
this.transactionBlockTimeout = options.transactionBlockTimeout || 50;
this.transactionConfirmationBlocks = options.transactionConfirmationBlocks || 24;
this.transactionPollingTimeout = options.transactionPollingTimeout || 750;
this.transactionPollingInterval = options.transactionPollingInterval || 1000;
this.blockHeaderTimeout = options.blockHeaderTimeout || 10; // 10 seconds
this.defaultCommon = options.defaultCommon;
this.defaultChain = options.defaultChain;
this.defaultHardfork = options.defaultHardfork;
this.handleRevert = options.handleRevert;
};
Method.prototype.setRequestManager = function (requestManager, accounts) {
this.requestManager = requestManager;
// reference to eth.accounts
if (accounts) {
this.accounts = accounts;
}
};
Method.prototype.createFunction = function (requestManager, accounts) {
var func = this.buildCall();
Object.defineProperty(func, 'call', { configurable: true, writable: true, value: this.call });
this.setRequestManager(requestManager || this.requestManager, accounts || this.accounts);
return func;
};
Method.prototype.attachToObject = function (obj) {
var func = this.buildCall();
Object.defineProperty(func, 'call', { configurable: true, writable: true, value: this.call });
var name = this.name.split('.');
if (name.length > 1) {
obj[name[0]] = obj[name[0]] || {};
obj[name[0]][name[1]] = func;
} else {
obj[name[0]] = func;
}
};
/**
* Should be used to determine name of the jsonrpc method based on arguments
*
* @method getCall
* @param {Array} arguments
* @return {String} name of jsonrpc method
*/
Method.prototype.getCall = function (args) {
return typeof this.call === 'function' ? this.call(args) : this.call;
};
/**
* Should be used to extract callback from array of arguments. Modifies input param
*
* @method extractCallback
* @param {Array} arguments
* @return {Function|Null} callback, if exists
*/
Method.prototype.extractCallback = function (args) {
if (typeof (args[args.length - 1]) === 'function') {
return args.pop(); // modify the args array!
}
};
/**
* Should be called to check if the number of arguments is correct
*
* @method validateArgs
* @param {Array} arguments
* @throws {Error} if it is not
*/
Method.prototype.validateArgs = function (args) {
if (args.length !== this.params) {
throw errors.InvalidNumberOfParams(args.length, this.params, this.name);
}
};
/**
* Should be called to format input args of method
*
* @method formatInput
* @param {Array}
* @return {Array}
*/
Method.prototype.formatInput = function (args) {
var _this = this;
if (!this.inputFormatter) {
return args;
}
return this.inputFormatter.map(function (formatter, index) {
// bind this for defaultBlock, and defaultAccount
return formatter ? formatter.call(_this, args[index]) : args[index];
});
};
/**
* Should be called to format output(result) of method
*
* @method formatOutput
* @param {Object}
* @return {Object}
*/
Method.prototype.formatOutput = function (result) {
var _this = this;
if (Array.isArray(result)) {
return result.map(function (res) {
return _this.outputFormatter && res ? _this.outputFormatter(res, this?.hexFormat) : res;
});
} else {
return this.outputFormatter && result ? this.outputFormatter(result, this?.hexFormat) : result;
}
};
/**
* Should create payload from given input args
*
* @method toPayload
* @param {Array} args
* @return {Object}
*/
Method.prototype.toPayload = function (args) {
var call = this.getCall(args);
var callback = this.extractCallback(args);
var params = this.formatInput(args);
this.validateArgs(params);
var payload = {
method: call,
params: params,
callback: callback
};
if (this.transformPayload) {
payload = this.transformPayload(payload);
}
return payload;
};
Method.prototype._confirmTransaction = function (defer, result, payload) {
var method = this,
promiseResolved = false,
canUnsubscribe = true,
timeoutCount = 0,
confirmationCount = 0,
intervalId = null,
blockHeaderTimeoutId = null,
lastBlock = null,
receiptJSON = '',
gasProvided = ((!!payload.params[0] && typeof payload.params[0] === 'object') && payload.params[0].gas) ? payload.params[0].gas : null,
isContractDeployment = (!!payload.params[0] && typeof payload.params[0] === 'object') &&
payload.params[0].data &&
payload.params[0].from &&
!payload.params[0].to,
hasBytecode = isContractDeployment && payload.params[0].data.length > 2;
// add custom send Methods
var _ethereumCalls = [
new Method({
name: 'getBlockByNumber',
call: 'eth_getBlockByNumber',
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, function (val) {
return !!val;
}],
outputFormatter: formatters.outputBlockFormatter
}),
new Method({
name: 'getTransactionReceipt',
call: 'eth_getTransactionReceipt',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionReceiptFormatter
}),
new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
}),
new Method({
name: 'getTransactionByHash',
call: 'eth_getTransactionByHash',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionFormatter
}),
new Subscriptions({
name: 'subscribe',
type: 'eth',
subscriptions: {
'newBlockHeaders': {
subscriptionName: 'newHeads', // replace subscription with this name
params: 0,
outputFormatter: formatters.outputBlockFormatter
}
}
})
];
// attach methods to this._ethereumCall
var _ethereumCall = {};
_ethereumCalls.forEach(mthd => {
mthd.attachToObject(_ethereumCall);
mthd.requestManager = method.requestManager; // assign rather than call setRequestManager()
});
// fire "receipt" and confirmation events and resolve after
var checkConfirmation = function (existingReceipt, isPolling, err, blockHeader, sub) {
if (!err) {
// create fake unsubscribe
if (!sub) {
sub = {
unsubscribe: function () {
clearInterval(intervalId);
clearTimeout(blockHeaderTimeoutId);
}
};
}
// if we have a valid receipt we don't need to send a request
return (existingReceipt ? promiEvent.resolve(existingReceipt) : _ethereumCall.getTransactionReceipt(result))
// catch error from requesting receipt
.catch(function (err) {
sub.unsubscribe();
promiseResolved = true;
utils._fireError(
{
message: 'Failed to check for transaction receipt:',
data: err
},
defer.eventEmitter,
defer.reject
);
})
// if CONFIRMATION listener exists check for confirmations, by setting canUnsubscribe = false
.then(async function (receipt) {
if (!receipt || !receipt.blockHash) {
throw new Error('Receipt missing or blockHash null');
}
// apply extra formatters
if (method.extraFormatters && method.extraFormatters.receiptFormatter) {
receipt = method.extraFormatters.receiptFormatter(receipt);
}
// check if confirmation listener exists
if (defer.eventEmitter.listeners('confirmation').length > 0) {
var block;
// If there was an immediately retrieved receipt, it's already
// been confirmed by the direct call to checkConfirmation needed
// for parity instant-seal
if (existingReceipt === undefined || confirmationCount !== 0) {
// Get latest block to emit with confirmation
var latestBlock = await _ethereumCall.getBlockByNumber('latest');
var latestBlockHash = latestBlock ? latestBlock.hash : null;
if (isPolling) { // Check if actually a new block is existing on polling
if (lastBlock) {
block = await _ethereumCall.getBlockByNumber(lastBlock.number + 1);
if (block) {
lastBlock = block;
defer.eventEmitter.emit('confirmation', confirmationCount, receipt, latestBlockHash);
}
} else {
block = await _ethereumCall.getBlockByNumber(receipt.blockNumber);
lastBlock = block;
defer.eventEmitter.emit('confirmation', confirmationCount, receipt, latestBlockHash);
}
} else {
defer.eventEmitter.emit('confirmation', confirmationCount, receipt, latestBlockHash);
}
}
if ((isPolling && block) || !isPolling) {
confirmationCount++;
}
canUnsubscribe = false;
if (confirmationCount === method.transactionConfirmationBlocks + 1) { // add 1 so we account for conf 0
sub.unsubscribe();
defer.eventEmitter.removeAllListeners();
}
}
return receipt;
})
// CHECK for CONTRACT DEPLOYMENT
.then(async function (receipt) {
if (isContractDeployment && !promiseResolved) {
if (!receipt.contractAddress) {
if (canUnsubscribe) {
sub.unsubscribe();
promiseResolved = true;
}
utils._fireError(
errors.NoContractAddressFoundError(receipt),
defer.eventEmitter,
defer.reject,
null,
receipt
);
return;
}
var code;
try {
code = await _ethereumCall.getCode(receipt.contractAddress);
} catch(err){
// ignore;
}
if (!code) {
return;
}
// If deployment is status.true and there was a real
// bytecode string, assume it was successful.
var deploymentSuccess = receipt.status === true && hasBytecode;
if (deploymentSuccess || code.length > 2) {
defer.eventEmitter.emit('receipt', receipt);
// if contract, return instance instead of receipt
if (method.extraFormatters && method.extraFormatters.contractDeployFormatter) {
defer.resolve(method.extraFormatters.contractDeployFormatter(receipt));
} else {
defer.resolve(receipt);
}
// need to remove listeners, as they aren't removed automatically when succesfull
if (canUnsubscribe) {
defer.eventEmitter.removeAllListeners();
}
} else {
utils._fireError(
errors.ContractCodeNotStoredError(receipt),
defer.eventEmitter,
defer.reject,
null,
receipt
);
}
if (canUnsubscribe) {
sub.unsubscribe();
}
promiseResolved = true;
}
return receipt;
})
// CHECK for normal tx check for receipt only
.then(async function (receipt) {
if (!isContractDeployment && !promiseResolved) {
if (!receipt.outOfGas &&
(!gasProvided || gasProvided !== receipt.gasUsed) &&
(receipt.status === true || receipt.status === '0x1' || typeof receipt.status === 'undefined')) {
defer.eventEmitter.emit('receipt', receipt);
defer.resolve(receipt);
// need to remove listeners, as they aren't removed automatically when succesfull
if (canUnsubscribe) {
defer.eventEmitter.removeAllListeners();
}
} else {
receiptJSON = JSON.stringify(receipt, null, 2);
if (receipt.status === false || receipt.status === '0x0') {
try {
var revertMessage = null;
if ( method.handleRevert &&
(method.call === 'eth_sendTransaction' || method.call === 'eth_sendRawTransaction'))
{
var txReplayOptions = payload.params[0];
// If send was raw, fetch the transaction and reconstitute the
// original params so they can be replayed with `eth_call`
if (method.call === 'eth_sendRawTransaction'){
var rawTransactionHex = payload.params[0];
var parsedTx = EthersTransactionUtils.parse(rawTransactionHex);
txReplayOptions = formatters.inputTransactionFormatter({
data: parsedTx.data,
to: parsedTx.to,
from: parsedTx.from,
gas: parsedTx.gasLimit.toHexString(),
gasPrice: parsedTx.gasPrice ? parsedTx.gasPrice.toHexString() : undefined,
value: parsedTx.value.toHexString()
});
}
// Get revert reason string with eth_call
revertMessage = await method.getRevertReason(
txReplayOptions,
receipt.blockNumber
);
if (revertMessage) { // Only throw a revert error if a revert reason is existing
utils._fireError(
errors.TransactionRevertInstructionError(revertMessage.reason, revertMessage.signature, receipt),
defer.eventEmitter,
defer.reject,
null,
receipt
);
} else {
throw false; // Throw false and let the try/catch statement handle the error correctly after
}
} else {
throw false; // Throw false and let the try/catch statement handle the error correctly after
}
} catch (error) {
// Throw an normal revert error if no revert reason is given or the detection of it is disabled
utils._fireError(
errors.TransactionRevertedWithoutReasonError(receipt),
defer.eventEmitter,
defer.reject,
null,
receipt
);
}
} else {
// Throw OOG if status is not existing and provided gas and used gas are equal
utils._fireError(
errors.TransactionOutOfGasError(receipt),
defer.eventEmitter,
defer.reject,
null,
receipt
);
}
}
if (canUnsubscribe) {
sub.unsubscribe();
}
promiseResolved = true;
}
})
// time out the transaction if not mined after 50 blocks
.catch(function () {
timeoutCount++;
// check to see if we are http polling
if (!!isPolling) {
// polling timeout is different than transactionBlockTimeout blocks since we are triggering every second
if (timeoutCount - 1 >= method.transactionPollingTimeout) {
sub.unsubscribe();
promiseResolved = true;
utils._fireError(
errors.TransactionError('Transaction was not mined within ' + method.transactionPollingTimeout + ' seconds, please make sure your transaction was properly sent. Be aware that it might still be mined!'),
defer.eventEmitter,
defer.reject
);
}
} else {
if (timeoutCount - 1 >= method.transactionBlockTimeout) {
sub.unsubscribe();
promiseResolved = true;
utils._fireError(
errors.TransactionError('Transaction was not mined within ' + method.transactionBlockTimeout + ' blocks, please make sure your transaction was properly sent. Be aware that it might still be mined!'),
defer.eventEmitter,
defer.reject
);
}
}
});
} else {
sub.unsubscribe();
promiseResolved = true;
utils._fireError({
message: 'Failed to subscribe to new newBlockHeaders to confirm the transaction receipts.',
data: err
}, defer.eventEmitter, defer.reject);
}
};
// start watching for confirmation depending on the support features of the provider
var startWatching = function (existingReceipt) {
let blockHeaderArrived = false;
const startInterval = () => {
intervalId = setInterval(checkConfirmation.bind(null, existingReceipt, true), method.transactionPollingInterval);
};
// If provider do not support event subscription use polling
if(!this.requestManager.provider.on) {
return startInterval();
}
// Subscribe to new block headers to look for tx receipt
_ethereumCall.subscribe('newBlockHeaders', function (err, blockHeader, sub) {
blockHeaderArrived = true;
if (err || !blockHeader) {
// fall back to polling
return startInterval();
}
checkConfirmation(existingReceipt, false, err, blockHeader, sub);
});
// Fallback to polling if tx receipt didn't arrived in "blockHeaderTimeout" [10 seconds]
blockHeaderTimeoutId = setTimeout(() => {
if(!blockHeaderArrived) {
startInterval();
}
}, this.blockHeaderTimeout * 1000);
}.bind(this);
// first check if we already have a confirmed transaction
_ethereumCall.getTransactionReceipt(result)
.then(function (receipt) {
if (receipt && receipt.blockHash) {
if (defer.eventEmitter.listeners('confirmation').length > 0) {
// We must keep on watching for new Blocks, if a confirmation listener is present
startWatching(receipt);
}
checkConfirmation(receipt, false);
} else if (!promiseResolved) {
startWatching();
}
})
.catch(function () {
if (!promiseResolved) startWatching();
});
};
var getWallet = function (from, accounts) {
var wallet = null;
// is index given
if (typeof from === 'number') {
wallet = accounts.wallet[from];
// is account given
} else if (!!from && typeof from === 'object' && from.address && from.privateKey) {
wallet = from;
// search in wallet for address
} else {
wallet = accounts.wallet[from.toLowerCase()];
}
return wallet;
};
Method.prototype.buildCall = function () {
var method = this,
isSendTx = (method.call === 'eth_sendTransaction' || method.call === 'eth_sendRawTransaction'), // || method.call === 'personal_sendTransaction'
isCall = (method.call === 'eth_call');
// actual send function
var send = function () {
let args = Array.prototype.slice.call(arguments);
var defer = promiEvent(!isSendTx),
payload = method.toPayload(args);
method.hexFormat = false;
if (method.call === 'eth_getTransactionReceipt'
|| method.call === 'eth_getTransactionByHash'
|| method.name === 'getBlock') {
method.hexFormat = (payload.params.length < args.length && args[args.length - 1] === 'hex')
}
// CALLBACK function
var sendTxCallback = function (err, result) {
if (method.handleRevert && isCall && method.abiCoder) {
var reasonData;
// Ganache / Geth <= 1.9.13 return the reason data as a successful eth_call response
// Geth >= 1.9.15 attaches the reason data to an error object.
// Geth 1.9.14 is missing revert reason (https://github.com/ethereum/web3.js/issues/3520)
if (!err && method.isRevertReasonString(result)){
reasonData = result.substring(10);
} else if (err && err.data){
// workaround embedded error details got from some providers like MetaMask
if (typeof err.data === 'object') {
// Ganache has no `originalError` sub-object unlike others
var originalError = err.data.originalError ?? err.data;
reasonData = originalError.data.substring(10);
}
else {
reasonData = err.data.substring(10);
}
}
if (reasonData){
var reason = method.abiCoder.decodeParameter('string', '0x' + reasonData);
var signature = 'Error(String)';
utils._fireError(
errors.RevertInstructionError(reason, signature),
defer.eventEmitter,
defer.reject,
payload.callback,
{
reason: reason,
signature: signature
}
);
return;
}
}
try {
result = method.formatOutput(result);
} catch (e) {
err = e;
}
if (result instanceof Error) {
err = result;
}
if (!err) {
if (payload.callback) {
payload.callback(null, result);
}
} else {
if (err.error) {
err = err.error;
}
return utils._fireError(err, defer.eventEmitter, defer.reject, payload.callback);
}
// return PROMISE
if (!isSendTx) {
if (!err) {
defer.resolve(result);
}
// return PROMIEVENT
} else {
defer.eventEmitter.emit('transactionHash', result);
method._confirmTransaction(defer, result, payload);
}
};
// SENDS the SIGNED SIGNATURE
var sendSignedTx = function (sign) {
var signedPayload = { ... payload,
method: 'eth_sendRawTransaction',
params: [sign.rawTransaction]
};
method.requestManager.send(signedPayload, sendTxCallback);
};
var sendRequest = function (payload, method) {
if (method && method.accounts && method.accounts.wallet && method.accounts.wallet.length) {
var wallet;
// ETH_SENDTRANSACTION
if (payload.method === 'eth_sendTransaction') {
var tx = payload.params[0];
wallet = getWallet((!!tx && typeof tx === 'object') ? tx.from : null, method.accounts);
// If wallet was found, sign tx, and send using sendRawTransaction
if (wallet && wallet.privateKey) {
var tx = JSON.parse(JSON.stringify(tx));
delete tx.from;
if (method.defaultChain && !tx.chain) {
tx.chain = method.defaultChain;
}
if (method.defaultHardfork && !tx.hardfork) {
tx.hardfork = method.defaultHardfork;
}
if (method.defaultCommon && !tx.common) {
tx.common = method.defaultCommon;
}
method.accounts.signTransaction(tx, wallet.privateKey)
.then(sendSignedTx)
.catch(function (err) {
if (typeof defer.eventEmitter.listeners === 'function' && defer.eventEmitter.listeners('error').length) {
try {
defer.eventEmitter.emit('error', err);
} catch (err) {
// Ignore userland error prevent it to bubble up within web3.
}
defer.eventEmitter.removeAllListeners();
defer.eventEmitter.catch(function () {
});
}
defer.reject(err);
});
return;
}
// ETH_SIGN
} else if (payload.method === 'eth_sign') {
var data = payload.params[1];
wallet = getWallet(payload.params[0], method.accounts);
// If wallet was found, sign tx, and send using sendRawTransaction
if (wallet && wallet.privateKey) {
var sign = method.accounts.sign(data, wallet.privateKey);
if (payload.callback) {
payload.callback(null, sign.signature);
}
defer.resolve(sign.signature);
return;
}
}
}
return method.requestManager.send(payload, sendTxCallback);
};
const hasSendTxObject = isSendTx
&& !!payload.params[0]
&& typeof payload.params[0] === 'object';
if (hasSendTxObject &&
payload.params[0].type === '0x1'
&& typeof payload.params[0].accessList === 'undefined'
) {
payload.params[0].accessList = [];
}
// Send the actual transaction
if (hasSendTxObject
&& (
typeof payload.params[0].gasPrice === 'undefined'
&& (
typeof payload.params[0].maxPriorityFeePerGas === 'undefined'
|| typeof payload.params[0].maxFeePerGas === 'undefined'
)
)
) {
_handleTxPricing(method, payload.params[0]).then(txPricing => {
if (txPricing.gasPrice !== undefined) {
payload.params[0].gasPrice = txPricing.gasPrice;
} else if (
txPricing.maxPriorityFeePerGas !== undefined
&& txPricing.maxFeePerGas !== undefined
) {
payload.params[0].maxPriorityFeePerGas = txPricing.maxPriorityFeePerGas;
payload.params[0].maxFeePerGas = txPricing.maxFeePerGas;
}
if (isSendTx) {
setTimeout(() => {
defer.eventEmitter.emit('sending', payload);
}, 0);
}
sendRequest(payload, method);
})
} else {
if (isSendTx) {
setTimeout(() => {
defer.eventEmitter.emit('sending', payload);
}, 0);
}
sendRequest(payload, method);
}
if (isSendTx) {
setTimeout(() => {
defer.eventEmitter.emit('sent', payload);
}, 0);
}
return defer.eventEmitter;
};
// necessary to attach things to the method
send.method = method;
// necessary for batch requests
send.request = this.request.bind(this);
return send;
};
function _handleTxPricing(method, tx) {
return new Promise((resolve, reject) => {
try {
var getBlockByNumber = (new Method({
name: 'getBlockByNumber',
call: 'eth_getBlockByNumber',
params: 2,
inputFormatter: [function(blockNumber) {
return blockNumber ? utils.toHex(blockNumber) : 'latest'
}, function() {
return false
}]
})).createFunction(method.requestManager);
var getGasPrice = (new Method({
name: 'getGasPrice',
call: 'eth_gasPrice',
params: 0
})).createFunction(method.requestManager);
Promise.all([
getBlockByNumber(),
getGasPrice()
]).then(responses => {
const [block, gasPrice] = responses;
if (
(tx.type === '0x2' || tx.type === undefined) &&
(block && block.baseFeePerGas)
) {
// The network supports EIP-1559
// Taken from https://github.com/ethers-io/ethers.js/blob/ba6854bdd5a912fe873d5da494cb5c62c190adde/packages/abstract-provider/src.ts/index.ts#L230
let maxPriorityFeePerGas, maxFeePerGas;
if (tx.gasPrice) {
// Using legacy gasPrice property on an eip-1559 network,
// so use gasPrice as both fee properties
maxPriorityFeePerGas = tx.gasPrice;
maxFeePerGas = tx.gasPrice;
delete tx.gasPrice;
} else {
maxPriorityFeePerGas = tx.maxPriorityFeePerGas || '0x9502F900'; // 2.5 Gwei
maxFeePerGas = tx.maxFeePerGas ||
utils.toHex(
utils.toBN(block.baseFeePerGas)
.mul(utils.toBN(2))
.add(utils.toBN(maxPriorityFeePerGas))
);
}
resolve({ maxFeePerGas, maxPriorityFeePerGas });
} else {
if (tx.maxPriorityFeePerGas || tx.maxFeePerGas)
throw Error("Network doesn't support eip-1559")
resolve({ gasPrice });
}
})
} catch (error) {
reject(error)
}
})
}
/**
* Returns the revert reason string if existing or otherwise false.
*
* @method getRevertReason
*
* @param {Object} txOptions
* @param {Number} blockNumber
*
* @returns {Promise<Boolean|String>}
*/
Method.prototype.getRevertReason = function (txOptions, blockNumber) {
var self = this;
return new Promise(function (resolve, reject) {
(new Method({
name: 'call',
call: 'eth_call',
params: 2,
abiCoder: self.abiCoder,
handleRevert: true
}))
.createFunction(self.requestManager)(txOptions, utils.numberToHex(blockNumber))
.then(function () {
resolve(false);
})
.catch(function (error) {
if (error.reason) {
resolve({
reason: error.reason,
signature: error.signature
});
} else {
reject(error);
}
});
});
};
/**
* Checks if the given hex string is a revert message from the EVM
*
* @method isRevertReasonString
*
* @param {String} data - Hex string prefixed with 0x
*
* @returns {Boolean}
*/
Method.prototype.isRevertReasonString = function (data) {
return typeof data === 'string' && ((data.length - 2) / 2) % 32 === 4 && data.substring(0, 10) === '0x08c379a0';
};
/**
* Should be called to create the pure JSONRPC request which can be used in a batch request
*
* @method request
* @return {Object} jsonrpc request
*/
Method.prototype.request = function () {
var payload = this.toPayload(Array.prototype.slice.call(arguments));
payload.format = this.formatOutput.bind(this);
return payload;
};
module.exports = Method;