@ledgerhq/coin-evm 5.3.2 → 5.3.3
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.
- package/.oxlintrc.json +1 -1
- package/CHANGELOG.md +7 -0
- package/package.json +2 -2
- package/src/adapters/blockOperations.test.ts +27 -29
- package/src/api/index.flare.integ.test.ts +1 -13
- package/src/api/index.integ.test.ts +86 -97
- package/src/fixtures/context.fixtures.ts +79 -1
- package/src/fixtures/ethers.fixtures.ts +26 -0
- package/src/logic/broadcast.integ.test.ts +6 -18
- package/src/logic/broadcast.test.ts +19 -31
- package/src/logic/broadcastSource.test.ts +6 -7
- package/src/logic/common.test.ts +120 -112
- package/src/logic/craftTransaction.test.ts +161 -199
- package/src/logic/craftTransactionData.test.ts +6 -6
- package/src/logic/estimateFees.test.ts +19 -15
- package/src/logic/getBalance.test.ts +15 -14
- package/src/logic/getBlock.test.ts +372 -382
- package/src/logic/getBlockInfo.test.ts +6 -6
- package/src/logic/getNextSequence.test.ts +5 -5
- package/src/logic/getStakes.test.ts +34 -34
- package/src/logic/internalTransactionsFetcher.test.ts +23 -19
- package/src/logic/lastBlock.test.ts +29 -21
- package/src/logic/listOperations.test.ts +43 -40
- package/src/logic/validateIntent.test.ts +151 -101
- package/src/logic.test.ts +13 -18
- package/src/network/explorer/etherscan.test.ts +5 -5
- package/src/network/explorer/index.test.ts +51 -21
- package/src/network/explorer/ledger.test.ts +15 -4
- package/src/network/gasTracker/index.test.ts +14 -8
- package/src/network/gasTracker/ledger.test.ts +25 -13
- package/src/network/node/index.memoization.test.ts +25 -31
- package/src/network/node/index.test.ts +14 -25
- package/src/network/node/ledger.test.ts +45 -60
- package/src/network/node/node.fixtures.ts +0 -2
- package/src/network/node/rpc.test.ts +152 -177
- package/src/staking/encoder.test.ts +13 -21
- package/src/staking/logic.test.ts +20 -14
- package/src/staking/operations/index.test.ts +28 -28
- package/src/staking/prepareIntents.test.ts +26 -20
- package/src/staking/redelegations.test.ts +27 -18
- package/src/staking/rewards.test.ts +23 -15
- package/src/staking/rewardsFromReceipt.test.ts +19 -20
- package/src/staking/transactionData.test.ts +22 -22
- package/src/staking/validators/monad.test.ts +65 -24
- package/src/staking/validators/somnia.test.ts +21 -13
- package/src/staking/validators/zero_gravity.test.ts +118 -48
- package/src/staking/validators.integ.test.ts +13 -25
|
@@ -2,9 +2,17 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import type { EvmConfigInfo } from '../../config'
|
|
5
|
+
import type { StakingContractConfig } from '../../types/staking'
|
|
6
|
+
import type { AddressLike } from 'ethers'
|
|
5
7
|
import network from '@ledgerhq/live-network'
|
|
6
8
|
import { ethers } from 'ethers'
|
|
7
9
|
import monadAbi from '../../abis/monad.abi.json'
|
|
10
|
+
import {
|
|
11
|
+
createMockEvmConfigInfo,
|
|
12
|
+
mkExternalNode,
|
|
13
|
+
mkLedgerNode,
|
|
14
|
+
} from '../../fixtures/context.fixtures'
|
|
15
|
+
import { mkJsonRpcProvider } from '../../fixtures/ethers.fixtures'
|
|
8
16
|
import { withApi } from '../../network/node/rpc.common'
|
|
9
17
|
import { fetchMonadStakes, extractMonadKnownValIds } from './monad'
|
|
10
18
|
import { getValidatorAddressById } from './monadResolver'
|
|
@@ -22,11 +30,12 @@ jest.mock('@ledgerhq/live-network', () => ({
|
|
|
22
30
|
const mockedWithApi = jest.mocked(withApi)
|
|
23
31
|
const mockedNetwork = jest.mocked(network)
|
|
24
32
|
|
|
25
|
-
const mockConfig = {
|
|
33
|
+
const mockConfig = createMockEvmConfigInfo({
|
|
34
|
+
chainId: 10143,
|
|
26
35
|
name: 'Monad',
|
|
27
|
-
node: {
|
|
28
|
-
}
|
|
29
|
-
const ledgerConfig = {
|
|
36
|
+
node: mkExternalNode({ uri: 'https://monad.rpc/' }),
|
|
37
|
+
})
|
|
38
|
+
const ledgerConfig = createMockEvmConfigInfo({ chainId: 10143, node: mkLedgerNode() })
|
|
30
39
|
|
|
31
40
|
const monadIface = new ethers.Interface(monadAbi)
|
|
32
41
|
const PRECOMPILE = '0x0000000000000000000000000000000000001000'
|
|
@@ -54,13 +63,18 @@ const encodeGetValidator = (params: {
|
|
|
54
63
|
'0x',
|
|
55
64
|
])
|
|
56
65
|
|
|
57
|
-
type CallHandler = (request: {
|
|
66
|
+
type CallHandler = (request: {
|
|
67
|
+
to?: AddressLike | null
|
|
68
|
+
data?: string | Uint8Array | null
|
|
69
|
+
}) => Promise<string>
|
|
58
70
|
|
|
59
71
|
const setupRpc = (handler: CallHandler) => {
|
|
60
72
|
const callMock = jest.fn(handler)
|
|
61
|
-
mockedWithApi.mockImplementation(async (_config, _currencyId, fn) =>
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
mockedWithApi.mockImplementation(async (_config, _currencyId, fn) => {
|
|
74
|
+
const api = mkJsonRpcProvider()
|
|
75
|
+
jest.spyOn(api, 'call').mockImplementation(callMock)
|
|
76
|
+
return fn(api)
|
|
77
|
+
})
|
|
64
78
|
return callMock
|
|
65
79
|
}
|
|
66
80
|
|
|
@@ -81,36 +95,36 @@ const routeByName = (responses: {
|
|
|
81
95
|
const desc = monadIface.parseTransaction({ data })
|
|
82
96
|
if (!desc) throw new Error('could not parse calldata')
|
|
83
97
|
if (desc.name === 'getExecutionValidatorSet') {
|
|
84
|
-
const startIndex = desc.args[0]
|
|
98
|
+
const startIndex: bigint = desc.args[0]
|
|
85
99
|
const out = responses.getExecutionValidatorSet?.(startIndex)
|
|
86
100
|
if (out instanceof Error) throw out
|
|
87
101
|
if (!out) throw new Error(`no response for getExecutionValidatorSet(${startIndex})`)
|
|
88
102
|
return out
|
|
89
103
|
}
|
|
90
104
|
if (desc.name === 'getValidator') {
|
|
91
|
-
const valId = desc.args[0]
|
|
105
|
+
const valId: bigint = desc.args[0]
|
|
92
106
|
const out = responses.getValidator?.(valId)
|
|
93
107
|
if (out instanceof Error) throw out
|
|
94
108
|
if (!out) throw new Error(`no response for getValidator(${valId})`)
|
|
95
109
|
return out
|
|
96
110
|
}
|
|
97
111
|
if (desc.name === 'getDelegations') {
|
|
98
|
-
const startValId = desc.args[1]
|
|
112
|
+
const startValId: bigint = desc.args[1]
|
|
99
113
|
const out = responses.getDelegations?.(startValId)
|
|
100
114
|
if (out instanceof Error) throw out
|
|
101
115
|
if (!out) throw new Error(`no response for getDelegations(${startValId})`)
|
|
102
116
|
return out
|
|
103
117
|
}
|
|
104
118
|
if (desc.name === 'getDelegator') {
|
|
105
|
-
const valId = desc.args[0]
|
|
119
|
+
const valId: bigint = desc.args[0]
|
|
106
120
|
const out = responses.getDelegator?.(valId)
|
|
107
121
|
if (out instanceof Error) throw out
|
|
108
122
|
if (!out) throw new Error(`no response for getDelegator(${valId})`)
|
|
109
123
|
return out
|
|
110
124
|
}
|
|
111
125
|
if (desc.name === 'getWithdrawalRequest') {
|
|
112
|
-
const valId = desc.args[0]
|
|
113
|
-
const withdrawId = Number(desc.args[2]
|
|
126
|
+
const valId: bigint = desc.args[0]
|
|
127
|
+
const withdrawId = Number(desc.args[2])
|
|
114
128
|
// Default unhandled slots to "free" (all-zero) so tests that don't care about
|
|
115
129
|
// withdrawals aren't forced to stub all 256 slots.
|
|
116
130
|
const out =
|
|
@@ -204,7 +218,8 @@ describe('staking/validators/monad', () => {
|
|
|
204
218
|
// Repo filename is the lowercase secp hex without the `0x` prefix.
|
|
205
219
|
mockedNetwork.mockResolvedValueOnce({
|
|
206
220
|
data: { name: 'GalaxyDigital' },
|
|
207
|
-
|
|
221
|
+
status: 200,
|
|
222
|
+
})
|
|
208
223
|
|
|
209
224
|
const page = await getValidators(mockConfig, 'monad')
|
|
210
225
|
|
|
@@ -441,9 +456,16 @@ describe('staking/validators/monad', () => {
|
|
|
441
456
|
describe('fetchMonadStakes', () => {
|
|
442
457
|
const DELEGATOR = '0x00000000000000000000000000000000000000aa'
|
|
443
458
|
const SECP = '0x036e44a092493800e427b2b08d3427d804348b1368ecd0a6af6510ae40ce507187'
|
|
459
|
+
const dummyStakingConfig: StakingContractConfig = {
|
|
460
|
+
contractAddress: () => '',
|
|
461
|
+
value: () => 0n,
|
|
462
|
+
functions: { delegate: '', undelegate: '', getStakedBalance: '' },
|
|
463
|
+
resolveValidatorAddress: async () => null,
|
|
464
|
+
resolveOperationAmount: async () => null,
|
|
465
|
+
}
|
|
444
466
|
|
|
445
467
|
const fetchStakes = (config: EvmConfigInfo = mockConfig) =>
|
|
446
|
-
fetchMonadStakes(config, DELEGATOR,
|
|
468
|
+
fetchMonadStakes(config, DELEGATOR, dummyStakingConfig, 'monad')
|
|
447
469
|
|
|
448
470
|
const encodeDelegator = (
|
|
449
471
|
stake: bigint,
|
|
@@ -516,7 +538,8 @@ describe('staking/validators/monad', () => {
|
|
|
516
538
|
)
|
|
517
539
|
mockedNetwork.mockResolvedValueOnce({
|
|
518
540
|
data: { name: 'GalaxyDigital' },
|
|
519
|
-
|
|
541
|
+
status: 200,
|
|
542
|
+
})
|
|
520
543
|
|
|
521
544
|
const stakes = await fetchStakes()
|
|
522
545
|
|
|
@@ -832,7 +855,9 @@ describe('staking/validators/monad', () => {
|
|
|
832
855
|
})
|
|
833
856
|
)
|
|
834
857
|
|
|
835
|
-
const stakes = await fetchMonadStakes(mockConfig, DELEGATOR,
|
|
858
|
+
const stakes = await fetchMonadStakes(mockConfig, DELEGATOR, dummyStakingConfig, 'monad', [
|
|
859
|
+
'92',
|
|
860
|
+
])
|
|
836
861
|
|
|
837
862
|
const withdrawal = stakes.find((s) => s.details?.validatorId === '92')
|
|
838
863
|
expect(withdrawal).toMatchObject({
|
|
@@ -855,7 +880,9 @@ describe('staking/validators/monad', () => {
|
|
|
855
880
|
})
|
|
856
881
|
)
|
|
857
882
|
|
|
858
|
-
const stakes = await fetchMonadStakes(mockConfig, DELEGATOR,
|
|
883
|
+
const stakes = await fetchMonadStakes(mockConfig, DELEGATOR, dummyStakingConfig, 'monad', [
|
|
884
|
+
'7',
|
|
885
|
+
])
|
|
859
886
|
|
|
860
887
|
expect(stakes).toHaveLength(1)
|
|
861
888
|
expect(delegatorCallCount).toBe(1)
|
|
@@ -863,11 +890,25 @@ describe('staking/validators/monad', () => {
|
|
|
863
890
|
})
|
|
864
891
|
|
|
865
892
|
describe('extractMonadKnownValIds', () => {
|
|
866
|
-
const makeOp = (
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
893
|
+
const makeOp = (
|
|
894
|
+
type: string,
|
|
895
|
+
contractPayload?: string
|
|
896
|
+
): Parameters<typeof extractMonadKnownValIds>[1][number] => ({
|
|
897
|
+
id: '',
|
|
898
|
+
type,
|
|
899
|
+
senders: [],
|
|
900
|
+
recipients: [],
|
|
901
|
+
value: 0n,
|
|
902
|
+
asset: { type: 'native' },
|
|
903
|
+
...(contractPayload !== undefined ? { details: { contractPayload } } : {}),
|
|
904
|
+
tx: {
|
|
905
|
+
hash: '',
|
|
906
|
+
block: { height: 0, hash: '', time: new Date(0) },
|
|
907
|
+
fees: 0n,
|
|
908
|
+
date: new Date(0),
|
|
909
|
+
failed: false,
|
|
910
|
+
},
|
|
911
|
+
})
|
|
871
912
|
|
|
872
913
|
it('extracts a validator id from a DELEGATE op', () => {
|
|
873
914
|
const payload = monadIface.encodeFunctionData('delegate', [42n])
|
|
@@ -2,10 +2,18 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import type { EvmConfigInfo } from '../../config'
|
|
5
|
+
import type { DeepPartialReturn } from '@ledgerhq/coin-module-framework/test/utils'
|
|
6
|
+
import type { TransactionRequest } from 'ethers'
|
|
5
7
|
import network from '@ledgerhq/live-network'
|
|
6
8
|
import { ethers } from 'ethers'
|
|
7
9
|
import somniaAbi from '../../abis/somnia.abi.json'
|
|
10
|
+
import {
|
|
11
|
+
createMockEvmConfigInfo,
|
|
12
|
+
mkExternalNode,
|
|
13
|
+
mkLedgerNode,
|
|
14
|
+
} from '../../fixtures/context.fixtures'
|
|
8
15
|
import { withApi } from '../../network/node/rpc.common'
|
|
16
|
+
import { STAKING_CONTRACTS } from '../contracts'
|
|
9
17
|
import { clearValidatorNamesCache, fetchSomniaStakes, fetchValidatorNames } from './somnia'
|
|
10
18
|
import { clearValidatorsCache, getValidators } from './index'
|
|
11
19
|
|
|
@@ -13,13 +21,14 @@ jest.mock('../../network/node/rpc.common', () => ({ __esModule: true, withApi: j
|
|
|
13
21
|
jest.mock('@ledgerhq/live-network', () => ({ __esModule: true, default: jest.fn() }))
|
|
14
22
|
|
|
15
23
|
const mockedWithApi = jest.mocked(withApi)
|
|
16
|
-
const mockedNetwork = jest.mocked(network)
|
|
24
|
+
const mockedNetwork = jest.mocked<DeepPartialReturn<typeof network>>(network)
|
|
17
25
|
|
|
18
|
-
const mockConfig = {
|
|
26
|
+
const mockConfig = createMockEvmConfigInfo({
|
|
27
|
+
chainId: 5031,
|
|
28
|
+
node: mkExternalNode({ uri: 'https://somnia-rpc.publicnode.com' }),
|
|
19
29
|
name: 'Somnia',
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const ledgerConfig = { node: { type: 'ledger' } } as unknown as EvmConfigInfo
|
|
30
|
+
})
|
|
31
|
+
const ledgerConfig = createMockEvmConfigInfo({ chainId: 5031, node: mkLedgerNode() })
|
|
23
32
|
|
|
24
33
|
const somniaIface = new ethers.Interface(somniaAbi)
|
|
25
34
|
const CONTRACT = '0xBe367d410D96E1cAeF68C0632251072CDf1b8250'
|
|
@@ -27,12 +36,12 @@ const VALIDATOR_A = ethers.getAddress('0x' + 'aa'.repeat(20))
|
|
|
27
36
|
const VALIDATOR_B = ethers.getAddress('0x' + 'bb'.repeat(20))
|
|
28
37
|
const DELEGATOR = ethers.getAddress('0x' + 'cc'.repeat(20))
|
|
29
38
|
|
|
30
|
-
type CallHandler = (request:
|
|
39
|
+
type CallHandler = (request: TransactionRequest) => Promise<string>
|
|
31
40
|
|
|
32
41
|
const setupRpc = (handler: CallHandler) => {
|
|
33
42
|
const callMock = jest.fn(handler)
|
|
34
43
|
mockedWithApi.mockImplementation(async (_config, _currencyId, fn) =>
|
|
35
|
-
fn({ call: callMock }
|
|
44
|
+
fn(Object.assign(Object.create(null), { call: callMock }))
|
|
36
45
|
)
|
|
37
46
|
return callMock
|
|
38
47
|
}
|
|
@@ -83,7 +92,7 @@ const routeByName = (responses: {
|
|
|
83
92
|
return out
|
|
84
93
|
}
|
|
85
94
|
if (desc.name === 'getStake') {
|
|
86
|
-
const address = desc.args[0]
|
|
95
|
+
const address = desc.args[0]
|
|
87
96
|
const out = responses.getStake?.(address)
|
|
88
97
|
if (out instanceof Error) throw out
|
|
89
98
|
if (!out) throw new Error(`no response for getStake(${address})`)
|
|
@@ -96,7 +105,7 @@ const routeByName = (responses: {
|
|
|
96
105
|
return out
|
|
97
106
|
}
|
|
98
107
|
if (desc.name === 'getDelegationInfo') {
|
|
99
|
-
const validator = desc.args[1]
|
|
108
|
+
const validator = desc.args[1]
|
|
100
109
|
const out = responses.getDelegationInfo?.(validator)
|
|
101
110
|
if (out instanceof Error) throw out
|
|
102
111
|
if (!out) throw new Error(`no response for getDelegationInfo(${validator})`)
|
|
@@ -197,8 +206,7 @@ describe('staking/validators/somnia', () => {
|
|
|
197
206
|
})
|
|
198
207
|
|
|
199
208
|
describe('fetchValidatorNames', () => {
|
|
200
|
-
const mockNamesResponse = (data: unknown) =>
|
|
201
|
-
mockedNetwork.mockResolvedValue({ data } as unknown as Awaited<ReturnType<typeof network>>)
|
|
209
|
+
const mockNamesResponse = (data: unknown) => mockedNetwork.mockResolvedValue({ data })
|
|
202
210
|
|
|
203
211
|
it('returns a lowercased address -> name map from a valid flat map', async () => {
|
|
204
212
|
mockNamesResponse({
|
|
@@ -277,7 +285,7 @@ describe('staking/validators/somnia', () => {
|
|
|
277
285
|
|
|
278
286
|
describe('fetchSomniaStakes', () => {
|
|
279
287
|
const fetchStakes = (config: EvmConfigInfo = mockConfig) =>
|
|
280
|
-
fetchSomniaStakes(config, DELEGATOR,
|
|
288
|
+
fetchSomniaStakes(config, DELEGATOR, STAKING_CONTRACTS['somnia'], 'somnia')
|
|
281
289
|
|
|
282
290
|
const defaultStakeHandlers = (
|
|
283
291
|
overrides: {
|
|
@@ -351,7 +359,7 @@ describe('staking/validators/somnia', () => {
|
|
|
351
359
|
it('attaches the validator name from the names overlay to details.validatorName', async () => {
|
|
352
360
|
mockedNetwork.mockResolvedValue({
|
|
353
361
|
data: { [VALIDATOR_A.toLowerCase()]: 'Everstake' },
|
|
354
|
-
}
|
|
362
|
+
})
|
|
355
363
|
setupRpc(defaultStakeHandlers())
|
|
356
364
|
|
|
357
365
|
const stakes = await fetchStakes()
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import type {
|
|
4
|
+
import type { DeepPartialReturn } from '@ledgerhq/coin-module-framework/test/utils'
|
|
5
|
+
import type { TransactionRequest } from 'ethers'
|
|
5
6
|
import network from '@ledgerhq/live-network'
|
|
6
7
|
import { ethers } from 'ethers'
|
|
7
8
|
import zeroGravityAbi from '../../abis/zero_gravity-validator.abi.json'
|
|
9
|
+
import { createMockEvmConfigInfo, mkExternalNode } from '../../fixtures/context.fixtures'
|
|
10
|
+
import { mkJsonRpcProvider } from '../../fixtures/ethers.fixtures'
|
|
8
11
|
import { withApi } from '../../network/node/rpc.common'
|
|
9
12
|
import { isExternalNodeConfig } from '../../network/node/types'
|
|
13
|
+
import { STAKING_CONTRACTS } from '../../staking/contracts'
|
|
10
14
|
import { fetchZeroGravityStakes } from './zero_gravity'
|
|
11
15
|
import { clearValidatorsCache, getValidators } from './index'
|
|
12
16
|
|
|
@@ -20,13 +24,14 @@ jest.mock('../../network/node/types', () => ({
|
|
|
20
24
|
isExternalNodeConfig: jest.fn(),
|
|
21
25
|
}))
|
|
22
26
|
|
|
23
|
-
const mockedNetwork = jest.mocked(network)
|
|
27
|
+
const mockedNetwork = jest.mocked<DeepPartialReturn<typeof network>>(network)
|
|
24
28
|
const mockedWithApi = jest.mocked(withApi)
|
|
25
29
|
const mockedIsExternalNodeConfig = jest.mocked(isExternalNodeConfig)
|
|
26
30
|
|
|
27
|
-
const mockConfig = {
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
const mockConfig = createMockEvmConfigInfo({
|
|
32
|
+
chainId: 16661,
|
|
33
|
+
node: mkExternalNode({ uri: 'https://zero-gravity.coin.ledger.com' }),
|
|
34
|
+
})
|
|
30
35
|
|
|
31
36
|
const makeValidator = (
|
|
32
37
|
overrides: Partial<{
|
|
@@ -51,7 +56,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
51
56
|
|
|
52
57
|
it('maps addr to validatorAddress with 0x prefix', async () => {
|
|
53
58
|
const addr = 'aabbccddee' + '00'.repeat(15)
|
|
54
|
-
mockedNetwork.mockResolvedValueOnce({ data: [makeValidator({ addr })] }
|
|
59
|
+
mockedNetwork.mockResolvedValueOnce({ data: [makeValidator({ addr })] })
|
|
55
60
|
|
|
56
61
|
const page = await getValidators(mockConfig, 'zero_gravity')
|
|
57
62
|
|
|
@@ -61,7 +66,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
61
66
|
it('uses moniker as name', async () => {
|
|
62
67
|
mockedNetwork.mockResolvedValueOnce({
|
|
63
68
|
data: [makeValidator({ moniker: 'MyNode' })],
|
|
64
|
-
}
|
|
69
|
+
})
|
|
65
70
|
|
|
66
71
|
const page = await getValidators(mockConfig, 'zero_gravity')
|
|
67
72
|
|
|
@@ -72,7 +77,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
72
77
|
const addr = 'aabbccddee' + '00'.repeat(15)
|
|
73
78
|
mockedNetwork.mockResolvedValueOnce({
|
|
74
79
|
data: [makeValidator({ addr, moniker: null })],
|
|
75
|
-
}
|
|
80
|
+
})
|
|
76
81
|
|
|
77
82
|
const page = await getValidators(mockConfig, 'zero_gravity')
|
|
78
83
|
|
|
@@ -82,7 +87,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
82
87
|
it('converts commission_pct string to a decimal fraction', async () => {
|
|
83
88
|
mockedNetwork.mockResolvedValueOnce({
|
|
84
89
|
data: [makeValidator({ commission_pct: '5.00' })],
|
|
85
|
-
}
|
|
90
|
+
})
|
|
86
91
|
|
|
87
92
|
const page = await getValidators(mockConfig, 'zero_gravity')
|
|
88
93
|
|
|
@@ -92,7 +97,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
92
97
|
it('maps voting_power_tokens to tokens', async () => {
|
|
93
98
|
mockedNetwork.mockResolvedValueOnce({
|
|
94
99
|
data: [makeValidator({ voting_power_tokens: '42000000000000000000' })],
|
|
95
|
-
}
|
|
100
|
+
})
|
|
96
101
|
|
|
97
102
|
const page = await getValidators(mockConfig, 'zero_gravity')
|
|
98
103
|
|
|
@@ -108,7 +113,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
108
113
|
})
|
|
109
114
|
|
|
110
115
|
it('always returns next: undefined (single-page API)', async () => {
|
|
111
|
-
mockedNetwork.mockResolvedValueOnce({ data: [makeValidator()] }
|
|
116
|
+
mockedNetwork.mockResolvedValueOnce({ data: [makeValidator()] })
|
|
112
117
|
|
|
113
118
|
const page = await getValidators(mockConfig, 'zero_gravity')
|
|
114
119
|
|
|
@@ -118,9 +123,12 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
118
123
|
describe('fetchZeroGravityStakes', () => {
|
|
119
124
|
const DELEGATOR = '0x' + '11'.repeat(20)
|
|
120
125
|
const VALIDATOR_ADDR = ethers.getAddress('0xaabbccddee' + '00'.repeat(15))
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
|
|
126
|
+
const zgConfig = createMockEvmConfigInfo({
|
|
127
|
+
chainId: 16661,
|
|
128
|
+
name: '0G',
|
|
129
|
+
node: mkExternalNode({ uri: 'https://zero-gravity.coin.ledger.com' }),
|
|
130
|
+
})
|
|
131
|
+
const zg0Iface = new ethers.Interface(zeroGravityAbi)
|
|
124
132
|
|
|
125
133
|
const CURRENT_BLOCK = 1_000_000n
|
|
126
134
|
|
|
@@ -133,7 +141,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
133
141
|
totalDelegatorShares = 0n,
|
|
134
142
|
commissionPpm = 0n
|
|
135
143
|
) {
|
|
136
|
-
return jest.fn(async ({ data }:
|
|
144
|
+
return jest.fn(async ({ data }: TransactionRequest) => {
|
|
137
145
|
const desc = zg0Iface.parseTransaction({ data: data ?? '0x' })
|
|
138
146
|
if (desc?.name === 'getDelegation') {
|
|
139
147
|
return zg0Iface.encodeFunctionResult('getDelegation', [
|
|
@@ -166,24 +174,29 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
166
174
|
})
|
|
167
175
|
}
|
|
168
176
|
|
|
169
|
-
function setupProvider(callHandler:
|
|
170
|
-
mockedWithApi.mockImplementation(async (_config, _currencyId, fn) =>
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
)
|
|
177
|
+
function setupProvider(callHandler: (_tx: TransactionRequest) => Promise<string>) {
|
|
178
|
+
mockedWithApi.mockImplementation(async (_config, _currencyId, fn) => {
|
|
179
|
+
const api = mkJsonRpcProvider()
|
|
180
|
+
jest.spyOn(api, 'call').mockImplementation(callHandler)
|
|
181
|
+
jest.spyOn(api, 'getBlockNumber').mockResolvedValue(Number(CURRENT_BLOCK))
|
|
182
|
+
return fn(api)
|
|
183
|
+
})
|
|
176
184
|
}
|
|
177
185
|
|
|
178
186
|
beforeEach(() => {
|
|
179
187
|
mockedIsExternalNodeConfig.mockReturnValue(true)
|
|
180
|
-
mockedNetwork.mockResolvedValueOnce({ data: [makeValidator()] }
|
|
188
|
+
mockedNetwork.mockResolvedValueOnce({ data: [makeValidator()] })
|
|
181
189
|
})
|
|
182
190
|
|
|
183
191
|
it('returns an active stake when shares and amount are non-zero', async () => {
|
|
184
192
|
setupProvider(makeCallHandler(1000n, 500n))
|
|
185
193
|
|
|
186
|
-
const stakes = await fetchZeroGravityStakes(
|
|
194
|
+
const stakes = await fetchZeroGravityStakes(
|
|
195
|
+
zgConfig,
|
|
196
|
+
DELEGATOR,
|
|
197
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
198
|
+
'zero_gravity'
|
|
199
|
+
)
|
|
187
200
|
|
|
188
201
|
expect(stakes).toHaveLength(1)
|
|
189
202
|
expect(stakes[0]).toMatchObject({
|
|
@@ -199,7 +212,12 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
199
212
|
// delegator share = 900 * 500 / 1000 = 450
|
|
200
213
|
setupProvider(makeCallHandler(500n, 500n, 0n, [], 1000n, 1000n, 100_000n))
|
|
201
214
|
|
|
202
|
-
const stakes = await fetchZeroGravityStakes(
|
|
215
|
+
const stakes = await fetchZeroGravityStakes(
|
|
216
|
+
zgConfig,
|
|
217
|
+
DELEGATOR,
|
|
218
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
219
|
+
'zero_gravity'
|
|
220
|
+
)
|
|
203
221
|
|
|
204
222
|
expect(stakes[0].amountRewarded).toEqual(450n)
|
|
205
223
|
})
|
|
@@ -207,7 +225,12 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
207
225
|
it('does not set amountRewarded when rewards are zero', async () => {
|
|
208
226
|
setupProvider(makeCallHandler(1000n, 500n, 0n, [], 0n, 1000n, 0n))
|
|
209
227
|
|
|
210
|
-
const stakes = await fetchZeroGravityStakes(
|
|
228
|
+
const stakes = await fetchZeroGravityStakes(
|
|
229
|
+
zgConfig,
|
|
230
|
+
DELEGATOR,
|
|
231
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
232
|
+
'zero_gravity'
|
|
233
|
+
)
|
|
211
234
|
|
|
212
235
|
expect(stakes[0].amountRewarded).toBeUndefined()
|
|
213
236
|
})
|
|
@@ -215,13 +238,18 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
215
238
|
it('does not set amountRewarded when totalShares is zero', async () => {
|
|
216
239
|
setupProvider(makeCallHandler(1000n, 500n, 0n, [], 1000n, 0n, 0n))
|
|
217
240
|
|
|
218
|
-
const stakes = await fetchZeroGravityStakes(
|
|
241
|
+
const stakes = await fetchZeroGravityStakes(
|
|
242
|
+
zgConfig,
|
|
243
|
+
DELEGATOR,
|
|
244
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
245
|
+
'zero_gravity'
|
|
246
|
+
)
|
|
219
247
|
|
|
220
248
|
expect(stakes[0].amountRewarded).toBeUndefined()
|
|
221
249
|
})
|
|
222
250
|
|
|
223
251
|
it('does not set amountRewarded when reward RPC calls fail', async () => {
|
|
224
|
-
const callHandler = jest.fn(async ({ data }:
|
|
252
|
+
const callHandler = jest.fn(async ({ data }: TransactionRequest) => {
|
|
225
253
|
const desc = zg0Iface.parseTransaction({ data: data ?? '0x' })
|
|
226
254
|
if (desc?.name === 'getDelegation') {
|
|
227
255
|
return zg0Iface.encodeFunctionResult('getDelegation', [
|
|
@@ -239,7 +267,12 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
239
267
|
})
|
|
240
268
|
setupProvider(callHandler)
|
|
241
269
|
|
|
242
|
-
const stakes = await fetchZeroGravityStakes(
|
|
270
|
+
const stakes = await fetchZeroGravityStakes(
|
|
271
|
+
zgConfig,
|
|
272
|
+
DELEGATOR,
|
|
273
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
274
|
+
'zero_gravity'
|
|
275
|
+
)
|
|
243
276
|
|
|
244
277
|
expect(stakes[0].amountRewarded).toBeUndefined()
|
|
245
278
|
})
|
|
@@ -247,13 +280,18 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
247
280
|
it('clamps commission to 1_000_000 when contract returns an out-of-range value', async () => {
|
|
248
281
|
setupProvider(makeCallHandler(1000n, 500n, 0n, [], 1000n, 1000n, 1_500_000n))
|
|
249
282
|
|
|
250
|
-
const stakes = await fetchZeroGravityStakes(
|
|
283
|
+
const stakes = await fetchZeroGravityStakes(
|
|
284
|
+
zgConfig,
|
|
285
|
+
DELEGATOR,
|
|
286
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
287
|
+
'zero_gravity'
|
|
288
|
+
)
|
|
251
289
|
|
|
252
290
|
expect(stakes[0].amountRewarded).toBeUndefined()
|
|
253
291
|
})
|
|
254
292
|
|
|
255
293
|
it('does not set amountRewarded when reward calls return empty data', async () => {
|
|
256
|
-
const callHandler = jest.fn(async ({ data }:
|
|
294
|
+
const callHandler = jest.fn(async ({ data }: TransactionRequest) => {
|
|
257
295
|
const desc = zg0Iface.parseTransaction({ data: data ?? '0x' })
|
|
258
296
|
if (desc?.name === 'getDelegation') {
|
|
259
297
|
return zg0Iface.encodeFunctionResult('getDelegation', [
|
|
@@ -271,7 +309,12 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
271
309
|
})
|
|
272
310
|
setupProvider(callHandler)
|
|
273
311
|
|
|
274
|
-
const stakes = await fetchZeroGravityStakes(
|
|
312
|
+
const stakes = await fetchZeroGravityStakes(
|
|
313
|
+
zgConfig,
|
|
314
|
+
DELEGATOR,
|
|
315
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
316
|
+
'zero_gravity'
|
|
317
|
+
)
|
|
275
318
|
|
|
276
319
|
expect(stakes[0].amountRewarded).toBeUndefined()
|
|
277
320
|
})
|
|
@@ -279,7 +322,12 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
279
322
|
it('filters out validators with shares = 0', async () => {
|
|
280
323
|
setupProvider(makeCallHandler(0n, 0n))
|
|
281
324
|
|
|
282
|
-
const stakes = await fetchZeroGravityStakes(
|
|
325
|
+
const stakes = await fetchZeroGravityStakes(
|
|
326
|
+
zgConfig,
|
|
327
|
+
DELEGATOR,
|
|
328
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
329
|
+
'zero_gravity'
|
|
330
|
+
)
|
|
283
331
|
|
|
284
332
|
expect(stakes).toEqual([])
|
|
285
333
|
})
|
|
@@ -287,17 +335,29 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
287
335
|
it('filters out validators when convertToTokens returns 0', async () => {
|
|
288
336
|
setupProvider(makeCallHandler(1000n, 0n))
|
|
289
337
|
|
|
290
|
-
const stakes = await fetchZeroGravityStakes(
|
|
338
|
+
const stakes = await fetchZeroGravityStakes(
|
|
339
|
+
zgConfig,
|
|
340
|
+
DELEGATOR,
|
|
341
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
342
|
+
'zero_gravity'
|
|
343
|
+
)
|
|
291
344
|
|
|
292
345
|
expect(stakes).toEqual([])
|
|
293
346
|
})
|
|
294
347
|
|
|
295
348
|
it('handles a rejected getDelegation call without throwing', async () => {
|
|
296
|
-
mockedWithApi.mockImplementation(async (_config, _currencyId, fn) =>
|
|
297
|
-
|
|
298
|
-
|
|
349
|
+
mockedWithApi.mockImplementation(async (_config, _currencyId, fn) => {
|
|
350
|
+
const api = mkJsonRpcProvider()
|
|
351
|
+
jest.spyOn(api, 'call').mockRejectedValue(new Error('RPC error'))
|
|
352
|
+
return fn(api)
|
|
353
|
+
})
|
|
299
354
|
|
|
300
|
-
const stakes = await fetchZeroGravityStakes(
|
|
355
|
+
const stakes = await fetchZeroGravityStakes(
|
|
356
|
+
zgConfig,
|
|
357
|
+
DELEGATOR,
|
|
358
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
359
|
+
'zero_gravity'
|
|
360
|
+
)
|
|
301
361
|
|
|
302
362
|
expect(stakes).toEqual([])
|
|
303
363
|
})
|
|
@@ -310,10 +370,15 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
310
370
|
voting_power_tokens: '1000',
|
|
311
371
|
}))
|
|
312
372
|
mockedNetwork.mockReset()
|
|
313
|
-
mockedNetwork.mockResolvedValueOnce({ data: validators }
|
|
373
|
+
mockedNetwork.mockResolvedValueOnce({ data: validators })
|
|
314
374
|
setupProvider(makeCallHandler(1000n, 500n))
|
|
315
375
|
|
|
316
|
-
const stakes = await fetchZeroGravityStakes(
|
|
376
|
+
const stakes = await fetchZeroGravityStakes(
|
|
377
|
+
zgConfig,
|
|
378
|
+
DELEGATOR,
|
|
379
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
380
|
+
'zero_gravity'
|
|
381
|
+
)
|
|
317
382
|
|
|
318
383
|
expect(stakes).toHaveLength(11)
|
|
319
384
|
})
|
|
@@ -321,7 +386,12 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
321
386
|
it('returns [] when node config is not external', async () => {
|
|
322
387
|
mockedIsExternalNodeConfig.mockReturnValue(false)
|
|
323
388
|
|
|
324
|
-
const stakes = await fetchZeroGravityStakes(
|
|
389
|
+
const stakes = await fetchZeroGravityStakes(
|
|
390
|
+
zgConfig,
|
|
391
|
+
DELEGATOR,
|
|
392
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
393
|
+
'zero_gravity'
|
|
394
|
+
)
|
|
325
395
|
|
|
326
396
|
expect(stakes).toEqual([])
|
|
327
397
|
})
|
|
@@ -348,7 +418,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
348
418
|
const stakes = await fetchZeroGravityStakes(
|
|
349
419
|
zgConfig,
|
|
350
420
|
DELEGATOR,
|
|
351
|
-
|
|
421
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
352
422
|
'zero_gravity'
|
|
353
423
|
)
|
|
354
424
|
|
|
@@ -374,7 +444,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
374
444
|
const stakes = await fetchZeroGravityStakes(
|
|
375
445
|
zgConfig,
|
|
376
446
|
DELEGATOR,
|
|
377
|
-
|
|
447
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
378
448
|
'zero_gravity'
|
|
379
449
|
)
|
|
380
450
|
|
|
@@ -387,7 +457,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
387
457
|
const stakes = await fetchZeroGravityStakes(
|
|
388
458
|
zgConfig,
|
|
389
459
|
DELEGATOR,
|
|
390
|
-
|
|
460
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
391
461
|
'zero_gravity'
|
|
392
462
|
)
|
|
393
463
|
|
|
@@ -400,7 +470,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
400
470
|
const stakes = await fetchZeroGravityStakes(
|
|
401
471
|
zgConfig,
|
|
402
472
|
DELEGATOR,
|
|
403
|
-
|
|
473
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
404
474
|
'zero_gravity'
|
|
405
475
|
)
|
|
406
476
|
|
|
@@ -414,7 +484,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
414
484
|
const stakes = await fetchZeroGravityStakes(
|
|
415
485
|
zgConfig,
|
|
416
486
|
DELEGATOR,
|
|
417
|
-
|
|
487
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
418
488
|
'zero_gravity'
|
|
419
489
|
)
|
|
420
490
|
|
|
@@ -444,7 +514,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
444
514
|
})
|
|
445
515
|
|
|
446
516
|
it('handles rejected getWithdraw without throwing', async () => {
|
|
447
|
-
const callHandler = jest.fn(async ({ data }:
|
|
517
|
+
const callHandler = jest.fn(async ({ data }: TransactionRequest) => {
|
|
448
518
|
const desc = zg0Iface.parseTransaction({ data: data ?? '0x' })
|
|
449
519
|
if (desc?.name === 'getDelegation') {
|
|
450
520
|
return zg0Iface.encodeFunctionResult('getDelegation', [
|
|
@@ -468,7 +538,7 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
468
538
|
const stakes = await fetchZeroGravityStakes(
|
|
469
539
|
zgConfig,
|
|
470
540
|
DELEGATOR,
|
|
471
|
-
|
|
541
|
+
STAKING_CONTRACTS['zero_gravity'],
|
|
472
542
|
'zero_gravity'
|
|
473
543
|
)
|
|
474
544
|
|