@ledgerhq/coin-evm 5.2.0 → 5.3.1

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 (75) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/lib/logic/getBalance.d.ts.map +1 -1
  3. package/lib/logic/getBalance.js +19 -13
  4. package/lib/logic/getBalance.js.map +1 -1
  5. package/lib/logic/getStakes.d.ts +2 -2
  6. package/lib/logic/getStakes.d.ts.map +1 -1
  7. package/lib/logic/getStakes.js +6 -3
  8. package/lib/logic/getStakes.js.map +1 -1
  9. package/lib/logic/listOperations.d.ts.map +1 -1
  10. package/lib/logic/listOperations.js +3 -0
  11. package/lib/logic/listOperations.js.map +1 -1
  12. package/lib/network/explorer/etherscan.d.ts.map +1 -1
  13. package/lib/network/explorer/etherscan.js +8 -4
  14. package/lib/network/explorer/etherscan.js.map +1 -1
  15. package/lib/network/node/rpc.common.d.ts.map +1 -1
  16. package/lib/network/node/rpc.common.js +42 -12
  17. package/lib/network/node/rpc.common.js.map +1 -1
  18. package/lib/network/node/rpc.errors.d.ts +1 -0
  19. package/lib/network/node/rpc.errors.d.ts.map +1 -1
  20. package/lib/network/node/rpc.errors.js +22 -0
  21. package/lib/network/node/rpc.errors.js.map +1 -1
  22. package/lib/staking/fetchers.d.ts.map +1 -1
  23. package/lib/staking/fetchers.js +1 -0
  24. package/lib/staking/fetchers.js.map +1 -1
  25. package/lib/staking/validators/monad.d.ts +3 -3
  26. package/lib/staking/validators/monad.d.ts.map +1 -1
  27. package/lib/staking/validators/monad.js +42 -3
  28. package/lib/staking/validators/monad.js.map +1 -1
  29. package/lib/types/staking.d.ts +3 -2
  30. package/lib/types/staking.d.ts.map +1 -1
  31. package/lib-es/logic/getBalance.d.ts.map +1 -1
  32. package/lib-es/logic/getBalance.js +19 -13
  33. package/lib-es/logic/getBalance.js.map +1 -1
  34. package/lib-es/logic/getStakes.d.ts +2 -2
  35. package/lib-es/logic/getStakes.d.ts.map +1 -1
  36. package/lib-es/logic/getStakes.js +6 -3
  37. package/lib-es/logic/getStakes.js.map +1 -1
  38. package/lib-es/logic/listOperations.d.ts.map +1 -1
  39. package/lib-es/logic/listOperations.js +3 -0
  40. package/lib-es/logic/listOperations.js.map +1 -1
  41. package/lib-es/network/explorer/etherscan.d.ts.map +1 -1
  42. package/lib-es/network/explorer/etherscan.js +8 -4
  43. package/lib-es/network/explorer/etherscan.js.map +1 -1
  44. package/lib-es/network/node/rpc.common.d.ts.map +1 -1
  45. package/lib-es/network/node/rpc.common.js +43 -13
  46. package/lib-es/network/node/rpc.common.js.map +1 -1
  47. package/lib-es/network/node/rpc.errors.d.ts +1 -0
  48. package/lib-es/network/node/rpc.errors.d.ts.map +1 -1
  49. package/lib-es/network/node/rpc.errors.js +21 -0
  50. package/lib-es/network/node/rpc.errors.js.map +1 -1
  51. package/lib-es/staking/fetchers.d.ts.map +1 -1
  52. package/lib-es/staking/fetchers.js +2 -1
  53. package/lib-es/staking/fetchers.js.map +1 -1
  54. package/lib-es/staking/validators/monad.d.ts +3 -3
  55. package/lib-es/staking/validators/monad.d.ts.map +1 -1
  56. package/lib-es/staking/validators/monad.js +40 -2
  57. package/lib-es/staking/validators/monad.js.map +1 -1
  58. package/lib-es/types/staking.d.ts +3 -2
  59. package/lib-es/types/staking.d.ts.map +1 -1
  60. package/package.json +2 -2
  61. package/src/api/index.integ.test.ts +17 -0
  62. package/src/logic/getBalance.ts +26 -21
  63. package/src/logic/getStakes.ts +15 -6
  64. package/src/logic/listOperations.test.ts +72 -0
  65. package/src/logic/listOperations.ts +3 -0
  66. package/src/network/explorer/etherscan.test.ts +35 -0
  67. package/src/network/explorer/etherscan.ts +8 -4
  68. package/src/network/node/rpc.common.ts +42 -15
  69. package/src/network/node/rpc.errors.test.ts +30 -0
  70. package/src/network/node/rpc.errors.ts +26 -0
  71. package/src/network/node/rpc.test.ts +61 -0
  72. package/src/staking/fetchers.ts +2 -1
  73. package/src/staking/validators/monad.test.ts +102 -1
  74. package/src/staking/validators/monad.ts +56 -4
  75. package/src/types/staking.ts +4 -2
@@ -6,7 +6,7 @@ import network from '@ledgerhq/live-network'
6
6
  import { ethers } from 'ethers'
7
7
  import monadAbi from '../../abis/monad.abi.json'
8
8
  import { withApi } from '../../network/node/rpc.common'
9
- import { fetchMonadStakes } from './monad'
9
+ import { fetchMonadStakes, extractMonadKnownValIds } from './monad'
10
10
  import { getValidatorAddressById } from './monadResolver'
11
11
  import { clearValidatorsCache, getValidators } from './index'
12
12
 
@@ -813,5 +813,106 @@ describe('staking/validators/monad', () => {
813
813
  expect(stakes).toHaveLength(1)
814
814
  expect(callNames(callMock).filter((name) => name === 'getDelegations')).toHaveLength(1)
815
815
  })
816
+
817
+ it('surfaces withdrawal slots for a fully-undelegated validator via knownValIds', async () => {
818
+ setupRpc(
819
+ routeByName({
820
+ getDelegations: () => monadIface.encodeFunctionResult('getDelegations', [true, 0, [1n]]),
821
+ getDelegator: (valId) =>
822
+ valId === 1n
823
+ ? encodeDelegator(100n)
824
+ : monadIface.encodeFunctionResult('getDelegator', [0n, 0n, 0n, 0n, 0n, 0n, 0n]),
825
+ getValidator: () => encodeGetValidator({ stake: 0n, commission: 0n, secpPubkey: SECP }),
826
+ getEpoch: () => monadIface.encodeFunctionResult('getEpoch', [5n, false]),
827
+ getWithdrawalRequest: (valId, withdrawId) => {
828
+ if (valId === 92n && withdrawId === 0)
829
+ return monadIface.encodeFunctionResult('getWithdrawalRequest', [500n, 0n, 10n])
830
+ return monadIface.encodeFunctionResult('getWithdrawalRequest', [0n, 0n, 0n])
831
+ },
832
+ })
833
+ )
834
+
835
+ const stakes = await fetchMonadStakes(mockConfig, DELEGATOR, {} as never, 'monad', ['92'])
836
+
837
+ const withdrawal = stakes.find((s) => s.details?.validatorId === '92')
838
+ expect(withdrawal).toMatchObject({
839
+ state: 'deactivating',
840
+ amount: 500n,
841
+ details: { validatorId: '92', withdrawId: 0 },
842
+ })
843
+ })
844
+
845
+ it('deduplicates a valId that appears in both getDelegations and knownValIds', async () => {
846
+ let delegatorCallCount = 0
847
+ setupRpc(
848
+ routeByName({
849
+ getDelegations: () => monadIface.encodeFunctionResult('getDelegations', [true, 0, [7n]]),
850
+ getDelegator: (valId) => {
851
+ if (valId === 7n) delegatorCallCount++
852
+ return encodeDelegator(100n)
853
+ },
854
+ getValidator: () => encodeGetValidator({ stake: 0n, commission: 0n, secpPubkey: SECP }),
855
+ })
856
+ )
857
+
858
+ const stakes = await fetchMonadStakes(mockConfig, DELEGATOR, {} as never, 'monad', ['7'])
859
+
860
+ expect(stakes).toHaveLength(1)
861
+ expect(delegatorCallCount).toBe(1)
862
+ })
863
+ })
864
+
865
+ describe('extractMonadKnownValIds', () => {
866
+ const makeOp = (type: string, contractPayload?: string) =>
867
+ ({
868
+ type,
869
+ ...(contractPayload !== undefined ? { details: { contractPayload } } : {}),
870
+ }) as unknown as Parameters<typeof extractMonadKnownValIds>[1][number]
871
+
872
+ it('extracts a validator id from a DELEGATE op', () => {
873
+ const payload = monadIface.encodeFunctionData('delegate', [42n])
874
+ expect(extractMonadKnownValIds('monad', [makeOp('DELEGATE', payload)])).toStrictEqual(['42'])
875
+ })
876
+
877
+ it('extracts a validator id from an UNDELEGATE op', () => {
878
+ const payload = monadIface.encodeFunctionData('undelegate', [92n, 10_000_000_000n, 0n])
879
+ expect(extractMonadKnownValIds('monad', [makeOp('UNDELEGATE', payload)])).toStrictEqual([
880
+ '92',
881
+ ])
882
+ })
883
+
884
+ it('extracts a validator id from a WITHDRAW_UNBONDED op', () => {
885
+ const payload = monadIface.encodeFunctionData('withdraw', [7n, 3n])
886
+ expect(
887
+ extractMonadKnownValIds('monad', [makeOp('WITHDRAW_UNBONDED', payload)])
888
+ ).toStrictEqual(['7'])
889
+ })
890
+
891
+ it('deduplicates repeated validator ids across multiple ops', () => {
892
+ const p1 = monadIface.encodeFunctionData('undelegate', [92n, 5_000_000_000n, 0n])
893
+ const p2 = monadIface.encodeFunctionData('undelegate', [92n, 5_000_000_000n, 1n])
894
+ const result = extractMonadKnownValIds('monad', [
895
+ makeOp('UNDELEGATE', p1),
896
+ makeOp('UNDELEGATE', p2),
897
+ ])
898
+ expect(result).toStrictEqual(['92'])
899
+ })
900
+
901
+ it('ignores ops whose type is not a staking function', () => {
902
+ const result = extractMonadKnownValIds('monad', [makeOp('OUT'), makeOp('IN')])
903
+ expect(result).toStrictEqual([])
904
+ })
905
+
906
+ it('skips ops with no contractPayload without throwing', () => {
907
+ const result = extractMonadKnownValIds('monad', [makeOp('UNDELEGATE')])
908
+ expect(result).toStrictEqual([])
909
+ })
910
+
911
+ it('returns an empty array for an unknown currencyId (no ABI available)', () => {
912
+ const payload = monadIface.encodeFunctionData('undelegate', [7n, 1n, 0n])
913
+ expect(
914
+ extractMonadKnownValIds('unknown_coin', [makeOp('UNDELEGATE', payload)])
915
+ ).toStrictEqual([])
916
+ })
816
917
  })
817
918
  })
@@ -5,8 +5,14 @@ import type { EvmConfigInfo } from '../../config'
5
5
  import type { StakingContractConfig } from '../../types/staking'
6
6
  import type { ValidatorApi } from './types'
7
7
  import type { Cursor, Page } from '@ledgerhq/coin-module-framework/api/index'
8
- import type { AssetInfo, Stake, StakeState } from '@ledgerhq/coin-module-framework/api/types'
9
- import type { Validator } from '@ledgerhq/coin-module-framework/api/types'
8
+ import type {
9
+ AssetInfo,
10
+ MemoNotSupported,
11
+ Operation,
12
+ Stake,
13
+ StakeState,
14
+ Validator,
15
+ } from '@ledgerhq/coin-module-framework/api/types'
10
16
  import network from '@ledgerhq/live-network'
11
17
  import { makeLRUCache } from '@ledgerhq/live-network/cache'
12
18
  import { log } from '@ledgerhq/logs'
@@ -369,11 +375,47 @@ const fetchStakeForValId = async (
369
375
  return stakes
370
376
  }
371
377
 
378
+ export const extractMonadKnownValIds = (
379
+ currencyId: string,
380
+ ops: Array<Operation<MemoNotSupported>>
381
+ ): string[] => {
382
+ const fnNames: Record<string, string> = {
383
+ DELEGATE: 'delegate',
384
+ UNDELEGATE: 'undelegate',
385
+ WITHDRAW_UNBONDED: 'withdraw',
386
+ }
387
+
388
+ const abi = getStakingABI(currencyId)
389
+ if (!abi) return []
390
+
391
+ const iface = new ethers.Interface(abi as ethers.InterfaceAbi)
392
+ const valIds = new Set<string>()
393
+
394
+ for (const op of ops) {
395
+ const fnName = fnNames[op.type]
396
+ if (!fnName) continue
397
+ const payload = op.details?.contractPayload
398
+ if (!payload || typeof payload !== 'string') continue
399
+ try {
400
+ const d = iface.decodeFunctionData(fnName, payload)
401
+ if (typeof d[0] === 'bigint') valIds.add(d[0].toString())
402
+ } catch (err) {
403
+ log('coin-evm/staking', 'extractMonadKnownValIds: failed to decode payload', {
404
+ type: op.type,
405
+ error: err instanceof Error ? err.message : String(err),
406
+ })
407
+ }
408
+ }
409
+
410
+ return [...valIds]
411
+ }
412
+
372
413
  export const fetchMonadStakes = async (
373
414
  evmConfig: EvmConfigInfo,
374
415
  address: string,
375
416
  _config: StakingContractConfig,
376
- currencyId: string
417
+ currencyId: string,
418
+ knownValIds?: string[]
377
419
  ): Promise<Stake[]> => {
378
420
  const ctx = resolveContext(evmConfig, currencyId)
379
421
  if (!ctx) return []
@@ -384,7 +426,17 @@ export const fetchMonadStakes = async (
384
426
  currencyId,
385
427
  async (provider) => {
386
428
  const iface = new ethers.Interface(ctx.abi)
387
- const valIds = await fetchDelegatedValIds(provider, iface, ctx.contractAddress, address)
429
+ const fromDelegations = await fetchDelegatedValIds(
430
+ provider,
431
+ iface,
432
+ ctx.contractAddress,
433
+ address
434
+ )
435
+ const valIdSet = new Set(fromDelegations)
436
+ for (const id of knownValIds ?? []) {
437
+ if (/^\d+$/.test(id)) valIdSet.add(BigInt(id))
438
+ }
439
+ const valIds = [...valIdSet]
388
440
  if (valIds.length === 0) return []
389
441
 
390
442
  const currentEpoch = await callGetEpoch(provider, iface, ctx.contractAddress)
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import type { EvmConfigInfo } from '../config'
5
- import type { Stake } from '@ledgerhq/coin-module-framework/api/types'
5
+ import type { MemoNotSupported, Operation, Stake } from '@ledgerhq/coin-module-framework/api/types'
6
6
  import type BigNumber from 'bignumber.js'
7
7
 
8
8
  export type EvmStakingOperationType =
@@ -223,7 +223,8 @@ export type StakingFetcher = (
223
223
  evmConfig: EvmConfigInfo,
224
224
  address: string,
225
225
  config: StakingContractConfig,
226
- currencyId: string
226
+ currencyId: string,
227
+ knownValIds?: string[]
227
228
  ) => Promise<Stake[]>
228
229
 
229
230
  /**
@@ -231,6 +232,7 @@ export type StakingFetcher = (
231
232
  */
232
233
  export type StakingStrategy = {
233
234
  fetcher: StakingFetcher
235
+ extractKnownValIds?: (currencyId: string, ops: Array<Operation<MemoNotSupported>>) => string[]
234
236
  }
235
237
 
236
238
  /**