@ledgerhq/coin-evm 16.4.0 → 16.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/lib/adapters/etherscan.d.ts.map +1 -1
  3. package/lib/adapters/etherscan.js +4 -1
  4. package/lib/adapters/etherscan.js.map +1 -1
  5. package/lib/config.d.ts +27 -1
  6. package/lib/config.d.ts.map +1 -1
  7. package/lib/config.js +21 -3
  8. package/lib/config.js.map +1 -1
  9. package/lib/logic/getBlock.d.ts.map +1 -1
  10. package/lib/logic/getBlock.js +66 -6
  11. package/lib/logic/getBlock.js.map +1 -1
  12. package/lib/logic/internalTransactionsFetcher.d.ts.map +1 -1
  13. package/lib/logic/internalTransactionsFetcher.js +47 -20
  14. package/lib/logic/internalTransactionsFetcher.js.map +1 -1
  15. package/lib-es/adapters/etherscan.d.ts.map +1 -1
  16. package/lib-es/adapters/etherscan.js +4 -1
  17. package/lib-es/adapters/etherscan.js.map +1 -1
  18. package/lib-es/config.d.ts +27 -1
  19. package/lib-es/config.d.ts.map +1 -1
  20. package/lib-es/config.js +21 -3
  21. package/lib-es/config.js.map +1 -1
  22. package/lib-es/logic/getBlock.d.ts.map +1 -1
  23. package/lib-es/logic/getBlock.js +66 -6
  24. package/lib-es/logic/getBlock.js.map +1 -1
  25. package/lib-es/logic/internalTransactionsFetcher.d.ts.map +1 -1
  26. package/lib-es/logic/internalTransactionsFetcher.js +47 -20
  27. package/lib-es/logic/internalTransactionsFetcher.js.map +1 -1
  28. package/package.json +3 -3
  29. package/src/adapters/etherscan.test.ts +16 -0
  30. package/src/adapters/etherscan.ts +4 -1
  31. package/src/config.ts +21 -3
  32. package/src/logic/getBlock.ts +88 -5
  33. package/src/logic/internalTransactionsFetcher.ts +55 -23
  34. package/src/logic/tests/getBlock.test.ts +203 -3
  35. package/src/logic/tests/internalTransactionsFetcher.test.ts +48 -2
@@ -13,6 +13,7 @@ import type {
13
13
  } from '../../network/node/types'
14
14
  import type { EtherscanInternalTransaction } from '../../types'
15
15
  import type { BlockOperation } from '@ledgerhq/coin-module-framework/api/index'
16
+ import { ethers } from 'ethers'
16
17
  import { CHAIN_ID, internalTxSourcesFromList } from '../../config'
17
18
  import { UnsupportedRpcMethodError } from '../../errors'
18
19
  import {
@@ -575,6 +576,129 @@ describe('getBlock', () => {
575
576
  })
576
577
  })
577
578
 
579
+ describe('staking operation type', () => {
580
+ const seiStakingPrecompile = '0x0000000000000000000000000000000000001005'
581
+ const delegateCalldata = `${ethers.id('delegate(string)').slice(0, 10)}00000000`
582
+
583
+ it('sets ledgerOpType on a prefetched staking call', async () => {
584
+ const context = createMockEvmContext(
585
+ createMockEvmConfigInfo({ chainId: CHAIN_ID.SEI_EVM, node: mkExternalNode() })
586
+ )
587
+
588
+ mockGetBlockNodeApi(
589
+ mockNodeApi({
590
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
591
+ makeNodeBlock({
592
+ transactions: [
593
+ makeNodeBlockTx({
594
+ hash: '0xtx1',
595
+ to: seiStakingPrecompile,
596
+ input: delegateCalldata,
597
+ }),
598
+ ],
599
+ })
600
+ ),
601
+ getBlockReceipts: jest
602
+ .fn()
603
+ .mockResolvedValueOnce([makeNodeBlockReceipt({ hash: '0xtx1' })]),
604
+ getTransaction: jest.fn(),
605
+ })
606
+ )
607
+
608
+ const result = await getBlock(context, 12345)
609
+
610
+ expect(result.transactions[0].details).toMatchObject({
611
+ ledgerOpType: 'DELEGATE',
612
+ contractInteraction: 'SmartContractInteraction',
613
+ })
614
+ })
615
+
616
+ it('sets ledgerOpType on a staking call when falling back to getTransaction', async () => {
617
+ const context = createMockEvmContext(
618
+ createMockEvmConfigInfo({ chainId: CHAIN_ID.SEI_EVM, node: mkExternalNode() })
619
+ )
620
+
621
+ mockGetBlockNodeApi(
622
+ mockNodeApi({
623
+ getBlockByHeight: jest
624
+ .fn()
625
+ .mockResolvedValueOnce(makeNodeBlock({ transactionHashes: ['0xtx1'] })),
626
+ getBlockReceipts: jest.fn(),
627
+ getTransaction: jest.fn().mockResolvedValueOnce(
628
+ makeNodeTxInfo({
629
+ to: seiStakingPrecompile,
630
+ input: delegateCalldata,
631
+ })
632
+ ),
633
+ })
634
+ )
635
+
636
+ const result = await getBlock(context, 12345)
637
+
638
+ expect(result.transactions[0].details).toMatchObject({ ledgerOpType: 'DELEGATE' })
639
+ })
640
+
641
+ it('omits ledgerOpType when the staking selector targets another contract', async () => {
642
+ const context = createMockEvmContext(
643
+ createMockEvmConfigInfo({ chainId: CHAIN_ID.SEI_EVM, node: mkExternalNode() })
644
+ )
645
+
646
+ mockGetBlockNodeApi(
647
+ mockNodeApi({
648
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
649
+ makeNodeBlock({
650
+ transactions: [
651
+ makeNodeBlockTx({
652
+ hash: '0xtx1',
653
+ to: address2,
654
+ input: delegateCalldata,
655
+ }),
656
+ ],
657
+ })
658
+ ),
659
+ getBlockReceipts: jest
660
+ .fn()
661
+ .mockResolvedValueOnce([makeNodeBlockReceipt({ hash: '0xtx1' })]),
662
+ getTransaction: jest.fn(),
663
+ })
664
+ )
665
+
666
+ const result = await getBlock(context, 12345)
667
+
668
+ expect(result.transactions[0].details).not.toHaveProperty('ledgerOpType')
669
+ })
670
+
671
+ it('omits ledgerOpType on a chain without staking contracts', async () => {
672
+ const context = createMockEvmContext(
673
+ createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
674
+ )
675
+
676
+ mockGetBlockNodeApi(
677
+ mockNodeApi({
678
+ getBlockByHeight: jest.fn().mockResolvedValueOnce(
679
+ makeNodeBlock({
680
+ transactions: [
681
+ makeNodeBlockTx({
682
+ hash: '0xtx1',
683
+ to: seiStakingPrecompile,
684
+ input: delegateCalldata,
685
+ }),
686
+ ],
687
+ })
688
+ ),
689
+ getBlockReceipts: jest
690
+ .fn()
691
+ .mockResolvedValueOnce([makeNodeBlockReceipt({ hash: '0xtx1' })]),
692
+ getTransaction: jest.fn(),
693
+ })
694
+ )
695
+
696
+ const result = await getBlock(context, 12345)
697
+
698
+ expect(result.transactions[0].details).not.toHaveProperty('ledgerOpType')
699
+ })
700
+ })
701
+
578
702
  it('falls back to per-transaction calls when bulk receipts are unavailable', async () => {
579
703
  const context = createMockEvmContext(
580
704
  createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
@@ -656,6 +780,39 @@ describe('getBlock', () => {
656
780
  expect(mockGetTransaction).toHaveBeenCalledWith('0xtx1')
657
781
  })
658
782
 
783
+ it('throws both errors when the transactions and the internal transactions fetch fail', async () => {
784
+ const context = createMockEvmContext(
785
+ createMockEvmConfigInfo({
786
+ chainId: 1,
787
+ node: mkExternalNode(),
788
+ getBlockInternalTxsSources: internalTxSourcesFromList(['trace_block']),
789
+ })
790
+ )
791
+
792
+ const receiptsError = new Error('receipts timeout')
793
+ const traceError = new Error('trace down')
794
+
795
+ mockGetBlockNodeApi(
796
+ mockNodeApi({
797
+ getBlockByHeight: jest.fn().mockResolvedValue(
798
+ makeNodeBlock({
799
+ height: 12345,
800
+ transactions: [makeNodeBlockTx({ hash: '0xtx1' })],
801
+ })
802
+ ),
803
+ getBlockReceipts: jest.fn().mockRejectedValue(receiptsError),
804
+ getTransaction: jest.fn(),
805
+ traceBlockErigon: jest.fn().mockRejectedValue(traceError),
806
+ })
807
+ )
808
+
809
+ await expect(getBlock(context, 12345)).rejects.toMatchObject({
810
+ errors: [receiptsError, traceError],
811
+ message:
812
+ 'getBlock 12345 failed: transactions: receipts timeout; internal transactions: trace down',
813
+ })
814
+ })
815
+
659
816
  it('does not fallback when getBlockReceipts fails for another reason', async () => {
660
817
  const context = createMockEvmContext(
661
818
  createMockEvmConfigInfo({ chainId: 1, node: mkExternalNode() })
@@ -821,6 +978,7 @@ describe('getBlock', () => {
821
978
  (op) => op.type === 'transfer' && op.asset.type === 'native'
822
979
  )
823
980
  expect(nativeOps).toHaveLength(2)
981
+ expect(result.transactions[0].details).not.toHaveProperty('internalTransfers')
824
982
  })
825
983
 
826
984
  it("keeps a nested internal transaction that does not match the coin tx's native transfer", async () => {
@@ -893,6 +1051,16 @@ describe('getBlock', () => {
893
1051
  }),
894
1052
  ])
895
1053
  )
1054
+ expect(result.transactions[0].details).toEqual({
1055
+ gasPrice: '20000000000',
1056
+ internalTransfers: [
1057
+ {
1058
+ from: safeEncodeEIP55(address2, testLogger),
1059
+ to: safeEncodeEIP55(nestedRecipient, testLogger),
1060
+ value: nestedAmount.toString(),
1061
+ },
1062
+ ],
1063
+ })
896
1064
  })
897
1065
 
898
1066
  it('when explorer is not etherscan like, falls back to node.traceBlockErigon', async () => {
@@ -931,6 +1099,15 @@ describe('getBlock', () => {
931
1099
 
932
1100
  const encodedFrom = safeEncodeEIP55(address1, testLogger)
933
1101
  const encodedTo = safeEncodeEIP55(address2, testLogger)
1102
+ expect(result.transactions[0].details).toMatchObject({
1103
+ internalTransfers: [
1104
+ {
1105
+ from: encodedFrom,
1106
+ to: encodedTo,
1107
+ value: amount.toString(),
1108
+ },
1109
+ ],
1110
+ })
934
1111
  expect(result.transactions[0].operations).toEqual(
935
1112
  expect.arrayContaining([
936
1113
  expect.objectContaining({
@@ -1136,7 +1313,15 @@ describe('getBlock', () => {
1136
1313
  const mockGetInternalTransactionsByBlock = jest.mocked(getInternalTransactionsByBlock)
1137
1314
  mockGetInternalTransactionsByBlock.mockRejectedValueOnce(new Error('explorer error'))
1138
1315
 
1139
- await expect(getBlock(context, 12345)).rejects.toThrow(error)
1316
+ await expect(getBlock(context, 12345)).rejects.toMatchObject({
1317
+ errors: [
1318
+ expect.objectContaining({
1319
+ name: 'SourceUnavailableError',
1320
+ cause: new Error('explorer error'),
1321
+ }),
1322
+ error,
1323
+ ],
1324
+ })
1140
1325
 
1141
1326
  expect(mockGetInternalTransactionsByBlock).toHaveBeenCalledWith(expect.anything(), 12345)
1142
1327
  expect(mockTraceBlock).toHaveBeenCalledWith(12345)
@@ -1182,7 +1367,14 @@ describe('getBlock', () => {
1182
1367
  mockGetInternalTransactionsByBlock.mockRejectedValueOnce(new Error('explorer error'))
1183
1368
 
1184
1369
  await expect(getBlock(context, 12345)).rejects.toMatchObject({
1185
- errors: [erigonError, gethError],
1370
+ errors: [
1371
+ expect.objectContaining({
1372
+ name: 'SourceUnavailableError',
1373
+ cause: new Error('explorer error'),
1374
+ }),
1375
+ erigonError,
1376
+ gethError,
1377
+ ],
1186
1378
  })
1187
1379
  expect(mockTraceBlockErigon).toHaveBeenCalledWith(12345)
1188
1380
  expect(mockTraceBlockGeth).toHaveBeenCalledWith(12345)
@@ -1282,7 +1474,15 @@ describe('getBlock', () => {
1282
1474
  const mockGetInternalTransactionsByBlock = jest.mocked(getInternalTransactionsByBlock)
1283
1475
  mockGetInternalTransactionsByBlock.mockRejectedValueOnce(new Error('explorer error'))
1284
1476
 
1285
- await expect(getBlock(context, 12345)).rejects.toThrow(erigonError)
1477
+ await expect(getBlock(context, 12345)).rejects.toMatchObject({
1478
+ errors: [
1479
+ expect.objectContaining({
1480
+ name: 'SourceUnavailableError',
1481
+ cause: new Error('explorer error'),
1482
+ }),
1483
+ erigonError,
1484
+ ],
1485
+ })
1286
1486
  expect(mockTraceBlockErigon).toHaveBeenCalledWith(12345)
1287
1487
  })
1288
1488
 
@@ -121,6 +121,7 @@ describe('composeInternalTxsFetcher', () => {
121
121
 
122
122
  await expect(fetch(height)).rejects.toMatchObject({
123
123
  errors: [explorerError, traceError],
124
+ message: 'internal tx sources failed: [explorer] explorer down; [trace_block] trace down',
124
125
  })
125
126
  })
126
127
 
@@ -137,9 +138,48 @@ describe('composeInternalTxsFetcher', () => {
137
138
 
138
139
  await expect(fetch(height)).rejects.toMatchObject({
139
140
  errors: [explorerError, traceError],
141
+ message: 'internal tx sources failed: [explorer] explorer down; [trace_block] trace down',
140
142
  })
141
143
  })
142
144
 
145
+ it('reports the cause of an unavailable source next to the real errors', async () => {
146
+ const explorerUnavailable = new SourceUnavailableError('explorer internal txs failed', {
147
+ cause: new Error('rate limited'),
148
+ })
149
+ const traceError = new Error('trace down')
150
+ const fetch = composeInternalTxsFetcher(
151
+ ['explorer', 'trace_block'],
152
+ makeFetchers({
153
+ explorer: jest.fn().mockRejectedValue(explorerUnavailable),
154
+ trace_block: jest.fn().mockRejectedValue(traceError),
155
+ })
156
+ )
157
+
158
+ await expect(fetch(height)).rejects.toMatchObject({
159
+ errors: [explorerUnavailable, traceError],
160
+ message:
161
+ 'internal tx sources failed: [explorer] explorer internal txs failed (cause: rate limited); [trace_block] trace down',
162
+ })
163
+ })
164
+
165
+ it('reports the cause of unavailable sources when all sources are unavailable', async () => {
166
+ const fetch = composeInternalTxsFetcher(
167
+ ['explorer', 'trace_block'],
168
+ makeFetchers({
169
+ explorer: jest.fn().mockRejectedValue(
170
+ new SourceUnavailableError('explorer internal txs failed', {
171
+ cause: new Error('rate limited'),
172
+ })
173
+ ),
174
+ trace_block: jest.fn().mockRejectedValue(new SourceUnavailableError('trace not supported')),
175
+ })
176
+ )
177
+
178
+ await expect(fetch(height)).rejects.toThrow(
179
+ 'all internal tx sources unavailable: [explorer] explorer internal txs failed (cause: rate limited)'
180
+ )
181
+ })
182
+
143
183
  it('falls back from trace_block to debug_traceBlockByNumber', async () => {
144
184
  const gethResult = new Map<string, BlockOperation[]>([['0x3', []]])
145
185
  const fetch = composeInternalTxsFetcher(
@@ -203,7 +243,10 @@ describe('makeSourceFetchers', () => {
203
243
  mockGetInternalTransactionsByBlock.mockRejectedValueOnce(new Error('explorer error'))
204
244
 
205
245
  const fetchers = makeSourceFetchers(config, mockNodeApi(), testLogger)
206
- await expect(fetchers.explorer(99)).rejects.toBeInstanceOf(SourceUnavailableError)
246
+ await expect(fetchers.explorer(99)).rejects.toMatchObject({
247
+ name: 'SourceUnavailableError',
248
+ cause: new Error('explorer error'),
249
+ })
207
250
  })
208
251
 
209
252
  it('calls getInternalTransactionsByBlock for explorer when configured', async () => {
@@ -266,7 +309,10 @@ describe('makeSourceFetchers', () => {
266
309
  .mockRejectedValueOnce(new Error('explorer error'))
267
310
 
268
311
  const fetchers = makeSourceFetchers(config, mockNodeApi(), testLogger)
269
- await expect(fetchers.explorer(99)).rejects.toBeInstanceOf(SourceUnavailableError)
312
+ await expect(fetchers.explorer(99)).rejects.toMatchObject({
313
+ name: 'SourceUnavailableError',
314
+ cause: new Error('explorer error'),
315
+ })
270
316
  })
271
317
 
272
318
  it('empty always resolves to an empty map', async () => {