-
Notifications
You must be signed in to change notification settings - Fork 60
/
eth.ts
1171 lines (1038 loc) · 40.9 KB
/
eth.ts
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
998
999
1000
/*-
*
* Hedera JSON RPC Relay
*
* Copyright (C) 2022 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import { Eth } from '../index';
import { ContractId, Status, Hbar, EthereumTransaction } from '@hashgraph/sdk';
import { BigNumber } from '@hashgraph/sdk/lib/Transfer';
import { Logger } from 'pino';
import { Block, CachedBlock, Transaction, Log } from './model';
import { MirrorNode } from './mirrorNode';
import { MirrorNodeClient, SDKClient } from './clients';
import { JsonRpcError, predefined } from './errors';
import constants from './constants';
import { Precheck } from './precheck';
const _ = require('lodash');
const cache = require('js-cache');
const createHash = require('keccak');
/**
* Implementation of the "eth_" methods from the Ethereum JSON-RPC API.
* Methods are implemented by delegating to the mirror node or to a
* consensus node in the main network.
*
* FIXME: This class is a work in progress because everything we need is
* not currently supported by the mirror nodes. As such, we have a lot
* of fake stuff in this class for now for the purpose of demos and POC.
*/
export class EthImpl implements Eth {
static emptyHex = '0x';
static zeroHex = '0x0';
static zeroHex8Byte = '0x0000000000000000';
static zeroHex32Byte = '0x0000000000000000000000000000000000000000000000000000000000000000';
static emptyArrayHex = '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347';
static zeroAddressHex = '0x0000000000000000000000000000000000000000';
static emptyBloom = "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
static defaultGas = EthImpl.numberTo0x(constants.TX_DEFAULT_GAS);
static gasTxBaseCost = EthImpl.numberTo0x(constants.TX_BASE_COST);
static ethTxType = 'EthereumTransaction';
static ethEmptyTrie = '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421';
static defaultGasUsedRatio = EthImpl.numberTo0x(0.5);
static feeHistoryZeroBlockCountResponse = { gasUsedRatio: null, oldestBlock: EthImpl.zeroHex };
static feeHistoryEmptyResponse = { baseFeePerGas: [], gasUsedRatio: [], reward: [], oldestBlock: EthImpl.zeroHex };
// endpoint metric callerNames
static ethCall = 'eth_call';
static ethGasPrice = 'eth_gasPrice';
static ethGetBalance = 'eth_getBalance';
static ethGetCode = 'eth_getCode';
static ethFeeHistory = 'eth_feeHistory';
static ethGetTransactionCount = 'eth_getTransactionCount';
static ethSendRawTransaction = 'eth_sendRawTransaction';
/**
* The sdk client use for connecting to both the consensus nodes and mirror node. The account
* associated with this client will pay for all operations on the main network.
*
* @private
*/
private readonly sdkClient: SDKClient;
/**
* The mirror node mock
* @private
*/
private readonly mirrorNode: MirrorNode;
/**
* The interface through which we interact with the mirror node
* @private
*/
private readonly mirrorNodeClient: MirrorNodeClient;
/**
* The logger used for logging all output from this class.
* @private
*/
private readonly logger: Logger;
/**
* The precheck class used for checking the fields like nonce before the tx execution.
* @private
*/
private readonly precheck: Precheck;
/**
* The ID of the chain, as a hex string, as it would be returned in a JSON-RPC call.
* @private
*/
private readonly chain: string;
/**
* Create a new Eth implementation.
* @param nodeClient
* @param mirrorNode
* @param mirrorNodeClient
* @param logger
* @param chain
*/
constructor(
nodeClient: SDKClient,
mirrorNode: MirrorNode,
mirrorNodeClient: MirrorNodeClient,
logger: Logger,
chain: string
) {
this.sdkClient = nodeClient;
this.mirrorNode = mirrorNode;
this.mirrorNodeClient = mirrorNodeClient;
this.logger = logger;
this.chain = chain;
this.precheck = new Precheck(mirrorNodeClient, nodeClient, logger, chain);
}
/**
* This method is implemented to always return an empty array. This is in alignment
* with the behavior of Infura.
*/
accounts() {
this.logger.trace('accounts()');
return [];
}
/**
* Gets the fee history.
*/
async feeHistory(blockCount: number, newestBlock: string, rewardPercentiles: Array<number> | null) {
this.logger.trace(`feeHistory(blockCount=${blockCount}, newestBlock=${newestBlock}, rewardPercentiles=${rewardPercentiles})`);
try {
const latestBlockNumber = await this.translateBlockTag('latest');
const newestBlockNumber = await this.translateBlockTag(newestBlock);
if (newestBlockNumber > latestBlockNumber) {
return predefined.REQUEST_BEYOND_HEAD_BLOCK(newestBlockNumber, latestBlockNumber);
}
blockCount = blockCount > constants.FEE_HISTORY_MAX_RESULTS ? constants.FEE_HISTORY_MAX_RESULTS : blockCount;
if (blockCount <= 0) {
return EthImpl.feeHistoryZeroBlockCountResponse;
}
let feeHistory: object | undefined = cache.get(constants.CACHE_KEY.FEE_HISTORY);
if (!feeHistory) {
feeHistory = await this.getFeeHistory(blockCount, newestBlockNumber, latestBlockNumber, rewardPercentiles);
this.logger.trace(`caching ${constants.CACHE_KEY.FEE_HISTORY} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
cache.set(constants.CACHE_KEY.FEE_HISTORY, feeHistory, constants.CACHE_TTL.ONE_HOUR);
}
return feeHistory;
} catch (e) {
this.logger.error(e, 'Error constructing default feeHistory');
return EthImpl.feeHistoryEmptyResponse;
}
}
private async getFeeByBlockNumber(blockNumber: number): Promise<string> {
let fee = 0;
try {
const block = await this.mirrorNodeClient.getBlock(blockNumber);
fee = await this.getFeeWeibars(EthImpl.ethFeeHistory, `lte:${block.timestamp.to}`);
} catch (error) {
this.logger.warn(error, `Fee history cannot retrieve block or fee. Returning ${fee} fee for block ${blockNumber}`);
}
return EthImpl.numberTo0x(fee);
}
private async getFeeHistory(blockCount: number, newestBlockNumber: number, latestBlockNumber: number, rewardPercentiles: Array<number> | null) {
// include newest block number in the total block count
const oldestBlockNumber = Math.max(0, newestBlockNumber - blockCount + 1);
const shouldIncludeRewards = Array.isArray(rewardPercentiles) && rewardPercentiles.length > 0;
const feeHistory = {
baseFeePerGas: [] as string[],
gasUsedRatio: [] as string[],
oldestBlock: EthImpl.numberTo0x(oldestBlockNumber),
};
// get fees from oldest to newest blocks
for (let blockNumber = oldestBlockNumber; blockNumber <= newestBlockNumber; blockNumber++) {
const fee = await this.getFeeByBlockNumber(blockNumber);
feeHistory.baseFeePerGas.push(fee);
feeHistory.gasUsedRatio.push(EthImpl.defaultGasUsedRatio);
}
// get latest block fee
let nextBaseFeePerGas = _.last(feeHistory.baseFeePerGas);
if (latestBlockNumber > newestBlockNumber) {
// get next block fee if the newest block is not the latest
nextBaseFeePerGas = await this.getFeeByBlockNumber(newestBlockNumber + 1);
}
if (nextBaseFeePerGas) {
feeHistory.baseFeePerGas.push(nextBaseFeePerGas);
}
if (shouldIncludeRewards) {
feeHistory['reward'] = Array(blockCount).fill(Array(rewardPercentiles.length).fill(EthImpl.zeroHex));
}
return feeHistory;
}
private async getFeeWeibars(callerName: string, timestamp?: string) {
let networkFees;
try {
networkFees = await this.mirrorNodeClient.getNetworkFees(timestamp);
if (_.isNil(networkFees)) {
this.logger.debug(`Mirror Node returned no fees. Fallback to network`);
}
} catch (e: any) {
this.logger.warn(e, `Mirror Node threw an error retrieving fees. Fallback to network`);
}
if (_.isNil(networkFees)) {
networkFees = {
fees: [
{
gas: await this.sdkClient.getTinyBarGasFee(callerName),
'transaction_type': EthImpl.ethTxType
}
]
};
}
if (networkFees && Array.isArray(networkFees.fees)) {
const txFee = networkFees.fees.find(({ transaction_type }) => transaction_type === EthImpl.ethTxType);
if (txFee && txFee.gas) {
// convert tinyBars into weiBars
const weibars = Hbar
.fromTinybars(txFee.gas)
.toTinybars()
.multiply(constants.TINYBAR_TO_WEIBAR_COEF);
return weibars.toNumber();
}
}
throw new Error('Error encountered estimating the gas price');
}
/**
* Gets the most recent block number.
*/
async blockNumber(): Promise<string> {
this.logger.trace('blockNumber()');
const blocksResponse = await this.mirrorNodeClient.getLatestBlock();
const blocks = blocksResponse !== null ? blocksResponse.blocks : null;
if (Array.isArray(blocks) && blocks.length > 0) {
return EthImpl.numberTo0x(blocks[0].number);
}
throw new Error('Error encountered retrieving latest block');
}
/**
* Gets the chain ID. This is a static value, in that it always returns
* the same value. This can be specified via an environment variable
* `CHAIN_ID`.
*/
chainId(): string {
this.logger.trace('chainId()');
return this.chain;
}
/**
* Estimates the amount of gas to execute a call.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async estimateGas(transaction: any, _blockParam: string | null) {
this.logger.trace('estimateGas()');
if (!transaction || !transaction.data || transaction.data === '0x') {
return EthImpl.gasTxBaseCost;
} else {
return EthImpl.defaultGas;
}
}
/**
* Gets the current gas price of the network.
*/
async gasPrice() {
this.logger.trace('gasPrice()');
try {
let gasPrice: number | undefined = cache.get(constants.CACHE_KEY.GAS_PRICE);
if (!gasPrice) {
gasPrice = await this.getFeeWeibars(EthImpl.ethGasPrice);
this.logger.trace(`caching ${constants.CACHE_KEY.GAS_PRICE} for ${constants.CACHE_TTL.ONE_HOUR} ms`);
cache.set(constants.CACHE_KEY.GAS_PRICE, gasPrice, constants.CACHE_TTL.ONE_HOUR);
}
return EthImpl.numberTo0x(gasPrice);
} catch (error) {
this.logger.trace(error);
throw error;
}
}
/**
* Gets whether this "Ethereum client" is a miner. We don't mine, so this always returns false.
*/
async mining() {
this.logger.trace('mining()');
return false;
}
/**
* TODO Needs docs, or be removed?
*/
async submitWork() {
this.logger.trace('submitWork()');
return false;
}
/**
* TODO Needs docs, or be removed?
*/
async syncing() {
this.logger.trace('syncing()');
return false;
}
/**
* Always returns null. There are no uncles in Hedera.
*/
async getUncleByBlockHashAndIndex() {
this.logger.trace('getUncleByBlockHashAndIndex()');
return null;
}
/**
* Always returns null. There are no uncles in Hedera.
*/
async getUncleByBlockNumberAndIndex() {
this.logger.trace('getUncleByBlockNumberAndIndex()');
return null;
}
/**
* Always returns '0x0'. There are no uncles in Hedera.
*/
async getUncleCountByBlockHash() {
this.logger.trace('getUncleCountByBlockHash()');
return EthImpl.zeroHex;
}
/**
* Always returns '0x0'. There are no uncles in Hedera.
*/
async getUncleCountByBlockNumber() {
this.logger.trace('getUncleCountByBlockNumber()');
return EthImpl.zeroHex;
}
/**
* TODO Needs docs, or be removed?
*/
async hashrate() {
this.logger.trace('hashrate()');
return EthImpl.zeroHex;
}
/**
* Always returns UNSUPPORTED_METHOD error.
*/
getWork(): JsonRpcError {
this.logger.trace('getWork()');
return predefined.UNSUPPORTED_METHOD;
}
/**
* Unsupported methods always return UNSUPPORTED_METHOD error.
*/
submitHashrate(): JsonRpcError {
this.logger.trace('submitHashrate()');
return predefined.UNSUPPORTED_METHOD;
}
signTransaction(): JsonRpcError {
this.logger.trace('signTransaction()');
return predefined.UNSUPPORTED_METHOD;
}
sign(): JsonRpcError {
this.logger.trace('sign()');
return predefined.UNSUPPORTED_METHOD;
}
sendTransaction(): JsonRpcError {
this.logger.trace('sendTransaction()');
return predefined.UNSUPPORTED_METHOD;
}
protocolVersion(): JsonRpcError {
this.logger.trace('protocolVersion()');
return predefined.UNSUPPORTED_METHOD;
}
coinbase(): JsonRpcError {
this.logger.trace('coinbase()');
return predefined.UNSUPPORTED_METHOD;
}
/**
* Gets the balance of an account as of the given block.
*
* @param account
* @param blockNumberOrTag
*/
async getBalance(account: string, blockNumberOrTag: string | null) {
// FIXME: This implementation should be replaced so that instead of going to the
// consensus nodes we go to the mirror nodes instead. The problem is that
// the mirror nodes need to have the ability to give me the **CURRENT**
// account balance *and* the account balance for any given block.
this.logger.trace('getBalance(account=%s, blockNumberOrTag=%s)', account, blockNumberOrTag);
const blockNumber = await this.translateBlockTag(blockNumberOrTag);
const cachedLabel = `getBalance.${account}.${blockNumberOrTag}`;
const cachedResponse: string | undefined = cache.get(cachedLabel);
if (cachedResponse != undefined) {
return cachedResponse;
}
try {
let weibars: BigNumber | number = 0;
const result = await this.mirrorNodeClient.resolveEntityType(account);
if (result?.type === constants.TYPE_ACCOUNT) {
weibars = await this.sdkClient.getAccountBalanceInWeiBar(result.entity.account, EthImpl.ethGetBalance);
}
else if (result?.type === constants.TYPE_CONTRACT) {
weibars = await this.sdkClient.getContractBalanceInWeiBar(result.entity.contract_id, EthImpl.ethGetBalance);
}
return EthImpl.numberTo0x(weibars);
} catch (e: any) {
// handle INVALID_ACCOUNT_ID
if (e?.status?._code === Status.InvalidAccountId._code) {
this.logger.debug(`Unable to find account ${account} in block ${JSON.stringify(blockNumber)}(${blockNumberOrTag}), returning 0x0 balance`);
cache.set(cachedLabel, EthImpl.zeroHex, constants.CACHE_TTL.ONE_HOUR);
return EthImpl.zeroHex;
}
this.logger.error(e, 'Error raised during getBalance for account %s', account);
throw e;
}
}
/**
* Gets the smart contract code for the contract at the given Ethereum address.
*
* @param address
* @param blockNumber
*/
async getCode(address: string, blockNumber: string | null) {
// FIXME: This has to be reimplemented to get the data from the mirror node.
this.logger.trace('getCode(address=%s, blockNumber=%s)', address, blockNumber);
const cachedLabel = `getCode.${address}.${blockNumber}`;
const cachedResponse: string | undefined = cache.get(cachedLabel);
if (cachedResponse != undefined) {
return cachedResponse;
}
try {
const bytecode = await this.sdkClient.getContractByteCode(0, 0, address, EthImpl.ethGetCode);
return EthImpl.prepend0x(Buffer.from(bytecode).toString('hex'));
} catch (e: any) {
// handle INVALID_CONTRACT_ID
if (e?.status?._code === Status.InvalidContractId._code || e?.message?.includes(Status.InvalidContractId.toString())) {
this.logger.debug('Unable to find code for contract %s in block "%s", returning 0x0, err code: %s', address, blockNumber, e?.status?._code);
cache.set(cachedLabel, '0x0', constants.CACHE_TTL.ONE_HOUR);
return '0x0';
}
this.logger.error(e, 'Error raised during getCode for address %s, err code: %s', address, e?.status?._code);
throw e;
}
}
/**
* Gets the block with the given hash.
*
* @param hash
* @param showDetails
*/
async getBlockByHash(hash: string, showDetails: boolean): Promise<Block | null> {
this.logger.trace('getBlockByHash(hash=%s, showDetails=%o)', hash, showDetails);
return this.getBlock(hash, showDetails).catch((e: any) => {
this.logger.error(e, 'Failed to retrieve block for hash %s', hash);
return null;
});
}
/**
* Gets the block by its block number.
* @param blockNumOrTag
* @param showDetails
*/
async getBlockByNumber(blockNumOrTag: string, showDetails: boolean): Promise<Block | null> {
this.logger.trace('getBlockByNumber(blockNum=%d, showDetails=%o)', blockNumOrTag);
return this.getBlock(blockNumOrTag, showDetails).catch((e: any) => {
this.logger.error(e, 'Failed to retrieve block for blockNum %s', blockNumOrTag);
return null;
});
}
/**
* Gets the number of transaction in a block by its block hash.
*
* @param hash
*/
async getBlockTransactionCountByHash(hash: string): Promise<number | null> {
this.logger.trace('getBlockTransactionCountByHash(hash=%s, showDetails=%o)', hash);
return this.mirrorNodeClient
.getBlock(hash)
.then((block) => EthImpl.getTransactionCountFromBlockResponse(block))
.catch((e: any) => {
this.logger.error(e, 'Failed to retrieve block for hash %s', hash);
return null;
});
}
/**
* Gets the number of transaction in a block by its block number.
* @param blockNumOrTag
*/
async getBlockTransactionCountByNumber(blockNumOrTag: string): Promise<number | null> {
this.logger.trace('getBlockTransactionCountByNumber(blockNum=%s, showDetails=%o)', blockNumOrTag);
const blockNum = await this.translateBlockTag(blockNumOrTag);
return this.mirrorNodeClient
.getBlock(blockNum)
.then((block) => EthImpl.getTransactionCountFromBlockResponse(block))
.catch((e: any) => {
this.logger.error(e, 'Failed to retrieve block for blockNum %s', blockNum);
return null;
});
}
/**
* Gets the transaction in a block by its block hash and transactions index.
*
* @param blockHash
* @param transactionIndex
*/
async getTransactionByBlockHashAndIndex(blockHash: string, transactionIndex: number): Promise<Transaction | null> {
this.logger.trace('getTransactionByBlockHashAndIndex(hash=%s, index=%d)', blockHash, transactionIndex);
return this.mirrorNodeClient
.getContractResults({ blockHash: blockHash, transactionIndex: transactionIndex })
.then((contractResults) => this.getTransactionFromContractResults(contractResults))
.catch((e: any) => {
this.logger.error(
e,
'Failed to retrieve contract result for hash %s and index=%d',
blockHash,
transactionIndex
);
return null;
});
}
/**
* Gets the transaction in a block by its block hash and transactions index.
*
* @param blockNumOrTag
* @param transactionIndex
*/
async getTransactionByBlockNumberAndIndex(
blockNumOrTag: string,
transactionIndex: number
): Promise<Transaction | null> {
this.logger.trace('getTransactionByBlockNumberAndIndex(blockNum=%s, index=%d)', blockNumOrTag, transactionIndex);
const blockNum = await this.translateBlockTag(blockNumOrTag);
return this.mirrorNodeClient
.getContractResults({ blockNumber: blockNum, transactionIndex: transactionIndex })
.then((contractResults) => this.getTransactionFromContractResults(contractResults))
.catch((e: any) => {
this.logger.error(
e,
'Failed to retrieve contract result for blockNum %s and index=%d',
blockNum,
transactionIndex
);
return null;
});
}
/**
* Gets the number of transactions that have been executed for the given address.
* This goes to the consensus nodes to determine the ethereumNonce.
*
* TODO Should it go against the mirror node instead? Less load on the network vs. latency...
*
* @param address
* @param blockNumOrTag
*/
async getTransactionCount(address: string, blockNumOrTag: string): Promise<string> {
this.logger.trace('getTransactionCount(address=%s, blockNumOrTag=%s)', address, blockNumOrTag);
const blockNumber = await this.translateBlockTag(blockNumOrTag);
if (blockNumber === 0) {
return '0x0';
} else {
const result = await this.mirrorNodeClient.resolveEntityType(address);
if (result?.type === constants.TYPE_ACCOUNT) {
const accountInfo = await this.sdkClient.getAccountInfo(result?.entity.account, EthImpl.ethGetTransactionCount);
return EthImpl.numberTo0x(Number(accountInfo.ethereumNonce));
}
else if (result?.type === constants.TYPE_CONTRACT) {
return EthImpl.numberTo0x(1);
}
return EthImpl.numberTo0x(0);
}
}
/**
* Submits a transaction to the network for execution.
*
* @param transaction
*/
async sendRawTransaction(transaction: string): Promise<string | JsonRpcError> {
this.logger.trace('sendRawTransaction(transaction=%s)', transaction);
try {
await this.precheck.gasLimit(transaction);
await this.precheck.nonce(transaction);
this.precheck.chainId(transaction);
}
catch(e: any) {
return e;
}
const gasPrice = await this.getFeeWeibars(EthImpl.ethSendRawTransaction);
const gasPrecheck = this.precheck.gasPrice(transaction, gasPrice);
if (!gasPrecheck.passes) {
return gasPrecheck.error;
}
const balancePrecheck = await this.precheck.balance(transaction, EthImpl.ethSendRawTransaction);
if (!balancePrecheck.passes) {
return balancePrecheck.error;
}
const transactionBuffer = Buffer.from(EthImpl.prune0x(transaction), 'hex');
try {
const contractExecuteResponse = await this.sdkClient.submitEthereumTransaction(transactionBuffer);
try {
// Wait for the record from the execution.
const record = await this.sdkClient.executeGetTransactionRecord(contractExecuteResponse, EthereumTransaction.name, EthImpl.ethSendRawTransaction);
if (record.ethereumHash == null) {
throw new Error('The ethereumHash can never be null for an ethereum transaction, and yet it was!!');
}
const txHash = EthImpl.prepend0x(Buffer.from(record.ethereumHash).toString('hex'));
// If the transaction succeeded, create a new block for the transaction.
const mostRecentBlock = await this.mirrorNode.getMostRecentBlock();
this.logger.debug('mostRecentBlock=%o', mostRecentBlock);
let block = mostRecentBlock;
if (record.receipt.status == Status.Success) {
block = new CachedBlock(mostRecentBlock, txHash);
this.mirrorNode.storeBlock(block);
}
// Create a receipt. Register the receipt in the cache and return the tx hash
if (block == null) {
this.logger.error('Failed to get a block for transaction');
return '';
}
return txHash;
} catch (e) {
this.logger.error(e,
'Failed sendRawTransaction during record retrieval for transaction %s, returning computed hash', transaction);
//Return computed hash if unable to retrieve EthereumHash from record due to error
return EthImpl.prepend0x(createHash('keccak256').update(transactionBuffer).digest('hex'));
}
} catch (e: any) {
this.logger.error(e,
'Failed to successfully submit sendRawTransaction for transaction %s', transaction);
return predefined.INTERNAL_ERROR;
}
}
/**
* Execute a free contract call query.
*
* @param call
* @param blockParam
*/
async call(call: any, blockParam: string | null): Promise<string | JsonRpcError> {
// FIXME: In the future this will be implemented by making calls to the mirror node. For the
// time being we'll eat the cost and ask the main consensus nodes instead.
this.logger.trace('call(hash=%o, blockParam=%s)', call, blockParam);
// The "to" address must always be 42 chars.
if (call.to.length != 42) {
throw new Error(
"Invalid Contract Address: '" + call.to + "'. Expected length of 42 chars but was" + call.to.length
);
}
try {
// Get a reasonable value for "gas" if it is not specified.
let gas: number;
if (typeof call.gas === 'string') {
gas = Number(call.gas);
} else {
if (call.gas == null) {
gas = 400_000;
} else {
gas = call.gas;
}
}
// Execute the call and get the response
this.logger.debug('Making eth_call on contract %o with gas %d and call data "%s"', call.to, gas, call.data);
const contractCallResponse = await this.sdkClient.submitContractCallQuery(call.to, call.data, gas, EthImpl.ethCall);
// FIXME Is this right? Maybe so?
return EthImpl.prepend0x(Buffer.from(contractCallResponse.asBytes()).toString('hex'));
} catch (e: any) {
// handle client error
let resolvedError = e;
if (e.status && e.status._code) {
resolvedError = new Error(e.message);
}
this.logger.error(resolvedError, 'Failed to successfully submit contractCallQuery');
return predefined.INTERNAL_ERROR;
}
}
/**
* Gets a transaction by the provided hash
*
* @param hash
*/
async getTransactionByHash(hash: string) {
this.logger.trace('getTransactionByHash(hash=%s)', hash);
const contractResult = await this.mirrorNodeClient.getContractResult(hash);
if (contractResult === null || contractResult.hash === undefined) {
return null;
}
const maxPriorityFee = contractResult.max_priority_fee_per_gas === EthImpl.emptyHex ? undefined : contractResult.max_priority_fee_per_gas;
const maxFee = contractResult.max_fee_per_gas === EthImpl.emptyHex ? undefined : contractResult.max_fee_per_gas;
const rSig = contractResult.r === null ? null : contractResult.r.substring(0, 66);
const sSig = contractResult.s === null ? null : contractResult.s.substring(0, 66);
return new Transaction({
accessList: undefined, // we don't support access lists, so punt for now
blockHash: contractResult.block_hash.substring(0, 66),
blockNumber: EthImpl.numberTo0x(contractResult.block_number),
chainId: contractResult.chain_id,
from: contractResult.from.substring(0, 42),
gas: contractResult.gas_used,
gasPrice: EthImpl.toNullIfEmptyHex(contractResult.gas_price),
hash: contractResult.hash.substring(0, 66),
input: contractResult.function_parameters,
maxPriorityFeePerGas: maxPriorityFee,
maxFeePerGas: maxFee,
nonce: contractResult.nonce,
r: rSig,
s: sSig,
to: contractResult.to?.substring(0, 42),
transactionIndex: contractResult.transaction_index,
type: contractResult.type,
v: contractResult.v,
value: contractResult.amount,
});
}
/**
* Gets a receipt for a transaction that has already executed.
*
* @param hash
*/
async getTransactionReceipt(hash: string) {
this.logger.trace(`getTransactionReceipt(${hash})`);
const receiptResponse = await this.mirrorNodeClient.getContractResult(hash);
if (receiptResponse === null || receiptResponse.hash === undefined) {
this.logger.trace(`no receipt for ${hash}`);
// block not found
return null;
} else {
const effectiveGas =
receiptResponse.max_fee_per_gas === undefined || receiptResponse.max_fee_per_gas == '0x'
? receiptResponse.gas_price
: receiptResponse.max_fee_per_gas;
const createdContract =
receiptResponse.created_contract_ids.length > 0
? EthImpl.prepend0x(ContractId.fromString(receiptResponse.created_contract_ids[0]).toSolidityAddress())
: undefined;
// support stricter go-eth client which requires the transaction hash property on logs
const logs = receiptResponse.logs.map(log => {
return new Log({
address: log.address,
blockHash: EthImpl.toHash32(receiptResponse.block_hash),
blockNumber: receiptResponse.block_number,
data: log.data,
logIndex: log.index,
removed: false,
topics: log.topics,
transactionHash: EthImpl.toHash32(receiptResponse.hash),
transactionIndex: receiptResponse.transaction_index
});
});
const receipt = {
blockHash: EthImpl.toHash32(receiptResponse.block_hash),
blockNumber: EthImpl.numberTo0x(receiptResponse.block_number),
from: receiptResponse.from,
to: receiptResponse.to,
cumulativeGasUsed: EthImpl.numberTo0x(receiptResponse.block_gas_used),
gasUsed: EthImpl.numberTo0x(receiptResponse.gas_used),
contractAddress: createdContract,
logs: logs,
logsBloom: receiptResponse.bloom,
transactionHash: EthImpl.toHash32(receiptResponse.hash),
transactionIndex: EthImpl.numberTo0x(receiptResponse.transaction_index),
effectiveGasPrice: EthImpl.numberTo0x(Number.parseInt(effectiveGas) * 10_000_000_000),
root: receiptResponse.root,
status: receiptResponse.status,
};
this.logger.trace(`receipt for ${hash} found in block ${receipt.blockNumber}`);
return receipt;
}
}
/**
* Internal helper method that prepends a leading 0x if there isn't one.
* @param input
* @private
*/
static prepend0x(input: string): string {
return input.startsWith(EthImpl.emptyHex) ? input : EthImpl.emptyHex + input;
}
static numberTo0x(input: number | BigNumber): string {
return EthImpl.emptyHex + input.toString(16);
}
static toHash32(value: string): string {
return value.substring(0, 66);
}
private static toNullIfEmptyHex(value: string): string | null {
return value === EthImpl.emptyHex ? null : value;
}
/**
* Internal helper method that removes the leading 0x if there is one.
* @param input
* @private
*/
private static prune0x(input: string): string {
return input.startsWith(EthImpl.emptyHex) ? input.substring(2) : input;
}
/**
* Translates a block tag into a number. 'latest', 'pending', and null are the
* most recent block, 'earliest' is 0, numbers become numbers.
*
* @param tag null, a number, or 'latest', 'pending', or 'earliest'
* @private
*/
private async translateBlockTag(tag: string | null): Promise<number> {
if (tag === null || tag === 'latest' || tag === 'pending') {
return Number(await this.blockNumber());
} else if (tag === 'earliest') {
return 0;
} else {
return Number(tag);
}
}
/**
* Gets the block with the given hash.
* Given an ethereum transaction hash, call the mirror node to get the block info.
* Then using the block timerange get all contract results to get transaction details.
* If showDetails is set to true subsequently call mirror node for additional transaction details
*
* TODO What do we return if we cannot find the block with that hash?
* @param blockHashOrNumber
* @param showDetails
*/
private async getBlock(blockHashOrNumber: string, showDetails: boolean): Promise<Block | null> {
let blockResponse: any;
if (blockHashOrNumber == null || blockHashOrNumber == 'latest' || blockHashOrNumber == 'pending') {
const blockPromise = this.mirrorNodeClient.getLatestBlock();
const blockAnswer = await blockPromise;
blockResponse = blockAnswer.blocks[0];
} else if (blockHashOrNumber == 'earliest') {
blockResponse = await this.mirrorNodeClient.getBlock(0);
} else if (blockHashOrNumber.length < 32) {
// anything less than 32 characters is treated as a number
blockResponse = await this.mirrorNodeClient.getBlock(Number(blockHashOrNumber));
} else {
blockResponse = await this.mirrorNodeClient.getBlock(blockHashOrNumber);
}
if (_.isNil(blockResponse) || blockResponse.hash === undefined) {
// block not found
return null;
}
const timestampRange = blockResponse.timestamp;
const timestampRangeParams = [`gte:${timestampRange.from}`, `lte:${timestampRange.to}`];
const contractResults = await this.mirrorNodeClient.getContractResults({ timestamp: timestampRangeParams });
if (contractResults === null || contractResults.results === undefined) {
// contract result not found
return null;
}
// loop over contract function results to calculated aggregated datapoints
let gasUsed = 0;
let maxGasLimit = 0;
let timestamp = 0;
const transactionObjects: Transaction[] = [];
const transactionHashes: string[] = [];
for (const result of contractResults.results) {
maxGasLimit = result.gas_limit > maxGasLimit ? result.gas_limit : maxGasLimit;
gasUsed += result.gas_used;
if (timestamp === 0) {
// The consensus timestamp of the first transaction in the block, with the nanoseconds part omitted.
timestamp = result.timestamp.substring(0, result.timestamp.indexOf('.')); // mirrorNode response assures format of ssssssssss.nnnnnnnnn
}
// depending on stage of contract execution revert the result.to value may be null
if (!_.isNil(result.to)) {
const transaction = await this.getTransactionFromContractResult(result.to, result.timestamp);
if (transaction !== null) {
if (showDetails) {
transactionObjects.push(transaction);
} else {
transactionHashes.push(transaction.hash);
}
}
}
}
const blockHash = blockResponse.hash.substring(0, 66);
const transactionArray = showDetails ? transactionObjects : transactionHashes;
return new Block({
baseFeePerGas: await this.gasPrice(),
difficulty: EthImpl.zeroHex,
extraData: EthImpl.emptyHex,
gasLimit: EthImpl.numberTo0x(maxGasLimit),
gasUsed: EthImpl.numberTo0x(gasUsed),
hash: blockHash,
logsBloom: EthImpl.emptyBloom, //TODO calculate full block boom in mirror node
miner: EthImpl.zeroAddressHex,
mixHash: EthImpl.zeroHex32Byte,
nonce: EthImpl.zeroHex8Byte,
number: EthImpl.numberTo0x(blockResponse.number),
parentHash: blockResponse.previous_hash.substring(0, 66),
receiptsRoot: EthImpl.zeroHex32Byte,
timestamp: EthImpl.numberTo0x(Number(timestamp)),
sha3Uncles: EthImpl.emptyArrayHex,
size: EthImpl.numberTo0x(blockResponse.size | 0),
stateRoot: EthImpl.zeroHex32Byte,
totalDifficulty: EthImpl.zeroHex,
transactions: transactionArray,
transactionsRoot: transactionArray.length == 0 ? EthImpl.ethEmptyTrie : blockHash,
uncles: [],
});
}