@ledgerhq/coin-evm 5.3.9 → 5.4.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.
- package/CHANGELOG.md +6 -0
- package/lib/logic/getStakes.d.ts.map +1 -1
- package/lib/logic/getStakes.js +6 -2
- package/lib/logic/getStakes.js.map +1 -1
- package/lib/staking/contracts.d.ts.map +1 -1
- package/lib/staking/contracts.js +4 -0
- package/lib/staking/contracts.js.map +1 -1
- package/lib/staking/index.d.ts +1 -0
- package/lib/staking/index.d.ts.map +1 -1
- package/lib/staking/index.js +3 -1
- package/lib/staking/index.js.map +1 -1
- package/lib/staking/unbondings.d.ts +8 -0
- package/lib/staking/unbondings.d.ts.map +1 -0
- package/lib/staking/unbondings.js +114 -0
- package/lib/staking/unbondings.js.map +1 -0
- package/lib/types/staking.d.ts +17 -0
- package/lib/types/staking.d.ts.map +1 -1
- package/lib-es/logic/getStakes.d.ts.map +1 -1
- package/lib-es/logic/getStakes.js +6 -2
- package/lib-es/logic/getStakes.js.map +1 -1
- package/lib-es/staking/contracts.d.ts.map +1 -1
- package/lib-es/staking/contracts.js +4 -0
- package/lib-es/staking/contracts.js.map +1 -1
- package/lib-es/staking/index.d.ts +1 -0
- package/lib-es/staking/index.d.ts.map +1 -1
- package/lib-es/staking/index.js +1 -0
- package/lib-es/staking/index.js.map +1 -1
- package/lib-es/staking/unbondings.d.ts +8 -0
- package/lib-es/staking/unbondings.d.ts.map +1 -0
- package/lib-es/staking/unbondings.js +108 -0
- package/lib-es/staking/unbondings.js.map +1 -0
- package/lib-es/types/staking.d.ts +17 -0
- package/lib-es/types/staking.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/logic/getStakes.test.ts +84 -0
- package/src/logic/getStakes.ts +12 -8
- package/src/staking/contracts.ts +4 -0
- package/src/staking/index.ts +1 -0
- package/src/staking/unbondings.test.ts +245 -0
- package/src/staking/unbondings.ts +137 -0
- package/src/types/staking.ts +18 -0
|
@@ -24,6 +24,21 @@ export type RedelegationStrategy = {
|
|
|
24
24
|
} | {
|
|
25
25
|
type: 'none';
|
|
26
26
|
};
|
|
27
|
+
/**
|
|
28
|
+
* Per-chain strategy for fetching pending unbondings from an off-chain source.
|
|
29
|
+
*
|
|
30
|
+
* - `cosmos-rest`: Cosmos REST API, EVM address resolved via
|
|
31
|
+
* `apiConfig.precompileAddress`. Needed because the staking precompile
|
|
32
|
+
* exposes only active delegations.
|
|
33
|
+
* - `none`: pending unbondings are not surfaced.
|
|
34
|
+
*/
|
|
35
|
+
export type UnbondingStrategy = {
|
|
36
|
+
type: 'cosmos-rest';
|
|
37
|
+
/** REST endpoint template; `{address}` is replaced with the resolved Cosmos address. */
|
|
38
|
+
endpoint: string;
|
|
39
|
+
} | {
|
|
40
|
+
type: 'none';
|
|
41
|
+
};
|
|
27
42
|
/**
|
|
28
43
|
* Per-chain strategy for fetching pending delegation rewards.
|
|
29
44
|
*
|
|
@@ -118,6 +133,8 @@ export type StakingContractConfig = {
|
|
|
118
133
|
};
|
|
119
134
|
/** How to fetch active redelegations from an off-chain source. Defaults to `"none"` when absent. */
|
|
120
135
|
redelegationStrategy?: RedelegationStrategy;
|
|
136
|
+
/** How to fetch pending unbondings from an off-chain source. Defaults to `"none"` when absent. */
|
|
137
|
+
unbondingStrategy?: UnbondingStrategy;
|
|
121
138
|
/** How to fetch pending delegation rewards from an off-chain source. Defaults to `"none"` when absent. */
|
|
122
139
|
rewardsStrategy?: RewardsStrategy;
|
|
123
140
|
/** How to recover a REWARD op's amount from its tx receipt logs. Absent → REWARD ops keep their native value. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"staking.d.ts","sourceRoot":"","sources":["../../src/types/staking.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AAC9C,OAAO,KAAK,EAAE,gBAAgB,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,2CAA2C,CAAA;AACnG,OAAO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AC5B,OAAO,EAAE,cAAc,CAAA;IACvB,kBAAkB,CAAC,EAAE,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,CAAC,SAAS,CAAC,gBAAgB,CAAC,CAAC,KAAK,MAAM,EAAE,CAAA;CAC/F,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,MAAM,CAAA;AAE3D,MAAM,WAAW,uBAAuB;IACtC,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,gBAAgB,CAAA;IAC3B,MAAM,EAAE,qBAAqB,CAAA;IAC7B,MAAM,EAAE,OAAO,EAAE,CAAA;CAClB"}
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ import { createMockEvmContext } from '../fixtures/context.fixtures'
|
|
|
5
5
|
import { mkJsonRpcProvider, mkResult } from '../fixtures/ethers.fixtures'
|
|
6
6
|
import { withApi } from '../network/node/rpc.common'
|
|
7
7
|
import { encodeStakingData, decodeStakingResult } from '../staking/encoder'
|
|
8
|
+
import { fetchUnbondings } from '../staking/unbondings'
|
|
8
9
|
import { getValidators } from '../staking/validators'
|
|
9
10
|
import { getStakes } from './getStakes'
|
|
10
11
|
|
|
@@ -22,10 +23,15 @@ jest.mock('../staking/validators', () => ({
|
|
|
22
23
|
getValidators: jest.fn(),
|
|
23
24
|
}))
|
|
24
25
|
|
|
26
|
+
jest.mock('../staking/unbondings', () => ({
|
|
27
|
+
fetchUnbondings: jest.fn(),
|
|
28
|
+
}))
|
|
29
|
+
|
|
25
30
|
const mockWithApi = jest.mocked(withApi)
|
|
26
31
|
const mockEncodeStakingData = jest.mocked(encodeStakingData)
|
|
27
32
|
const mockDecodeStakingResult = jest.mocked(decodeStakingResult)
|
|
28
33
|
const mockGetValidators = jest.mocked(getValidators)
|
|
34
|
+
const mockFetchUnbondings = jest.mocked(fetchUnbondings)
|
|
29
35
|
|
|
30
36
|
const externalNodeConfig = { type: 'external' as const, uri: 'https://test' }
|
|
31
37
|
const context = createMockEvmContext({
|
|
@@ -44,6 +50,7 @@ describe('EVM Staking - getStakes', () => {
|
|
|
44
50
|
beforeEach(() => {
|
|
45
51
|
jest.clearAllMocks()
|
|
46
52
|
jest.spyOn(console, 'error').mockImplementation(() => undefined)
|
|
53
|
+
mockFetchUnbondings.mockResolvedValue([])
|
|
47
54
|
})
|
|
48
55
|
|
|
49
56
|
it('should return stake objects with positive amounts for supported currencies', async () => {
|
|
@@ -230,4 +237,81 @@ describe('EVM Staking - getStakes', () => {
|
|
|
230
237
|
items: [],
|
|
231
238
|
})
|
|
232
239
|
})
|
|
240
|
+
|
|
241
|
+
describe('pending unbondings', () => {
|
|
242
|
+
const unbondingStake = {
|
|
243
|
+
uid: 'unbonding-uid',
|
|
244
|
+
address,
|
|
245
|
+
delegate: 'seivaloper1abc',
|
|
246
|
+
state: 'deactivating' as const,
|
|
247
|
+
stateUpdatedAt: new Date('2026-10-07T12:25:32.274Z'),
|
|
248
|
+
asset: {
|
|
249
|
+
type: 'native' as const,
|
|
250
|
+
name: 'Sei',
|
|
251
|
+
unit: { name: 'SEI', code: 'SEI', magnitude: 18 },
|
|
252
|
+
},
|
|
253
|
+
amount: 1569020n * 10n ** 12n,
|
|
254
|
+
actions: [],
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const mockOneDelegation = () => {
|
|
258
|
+
mockGetValidators.mockResolvedValue({
|
|
259
|
+
items: [makeValidator('seivaloper1abc')],
|
|
260
|
+
next: undefined,
|
|
261
|
+
})
|
|
262
|
+
mockWithApi.mockImplementation(async (_cur, _currencyId, fn) => {
|
|
263
|
+
const api = mkJsonRpcProvider()
|
|
264
|
+
jest.spyOn(api, 'call').mockResolvedValue('0x')
|
|
265
|
+
return fn(api)
|
|
266
|
+
})
|
|
267
|
+
mockEncodeStakingData.mockReturnValue('0xdeadbeef')
|
|
268
|
+
mockDecodeStakingResult.mockReturnValue(
|
|
269
|
+
mkResult({
|
|
270
|
+
balance: { amount: '42', denom: 'usei' },
|
|
271
|
+
delegation: { delegator_address: address, validator_address: 'seivaloper1abc' },
|
|
272
|
+
})
|
|
273
|
+
)
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
it('should append them to the active delegations', async () => {
|
|
277
|
+
mockOneDelegation()
|
|
278
|
+
mockFetchUnbondings.mockResolvedValue([unbondingStake])
|
|
279
|
+
|
|
280
|
+
const result = await getStakes(context, 'sei_evm', address)
|
|
281
|
+
|
|
282
|
+
expect(result.items.map((i) => i.state)).toEqual(['active', 'deactivating'])
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
it('should return them even when no delegation is left', async () => {
|
|
286
|
+
mockGetValidators.mockResolvedValue({ items: [], next: undefined })
|
|
287
|
+
mockFetchUnbondings.mockResolvedValue([unbondingStake])
|
|
288
|
+
|
|
289
|
+
const result = await getStakes(context, 'sei_evm', address)
|
|
290
|
+
|
|
291
|
+
expect(result.items).toEqual([unbondingStake])
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
it('should keep the delegations when the unbonding fetch fails', async () => {
|
|
295
|
+
mockOneDelegation()
|
|
296
|
+
mockFetchUnbondings.mockRejectedValue(new Error('REST down'))
|
|
297
|
+
|
|
298
|
+
const result = await getStakes(context, 'sei_evm', address)
|
|
299
|
+
|
|
300
|
+
expect(result.items.map((i) => i.state)).toEqual(['active'])
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
it('should not be attempted for a chain without an unbonding strategy', async () => {
|
|
304
|
+
mockWithApi.mockImplementation(async (_cur, _currencyId, fn) => {
|
|
305
|
+
const api = mkJsonRpcProvider()
|
|
306
|
+
jest.spyOn(api, 'call').mockResolvedValue('0x')
|
|
307
|
+
return fn(api)
|
|
308
|
+
})
|
|
309
|
+
mockEncodeStakingData.mockReturnValue('0xdeadbeef')
|
|
310
|
+
mockDecodeStakingResult.mockReturnValue(mkResult({ toString: (): string => '1000000' }))
|
|
311
|
+
|
|
312
|
+
const result = await getStakes(context, 'celo', address)
|
|
313
|
+
|
|
314
|
+
expect(result.items.map((i) => i.state)).toEqual(['active'])
|
|
315
|
+
})
|
|
316
|
+
})
|
|
233
317
|
})
|
package/src/logic/getStakes.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import type { EvmContext } from '../config'
|
|
5
5
|
import type { Page, Stake, StakesOptions } from '@ledgerhq/coin-module-framework/api/types'
|
|
6
6
|
import { STAKING_CONTRACTS, STAKING_CONFIG } from '../staking'
|
|
7
|
+
import { fetchUnbondings } from '../staking/unbondings'
|
|
7
8
|
|
|
8
9
|
export const getStakes = async (
|
|
9
10
|
context: EvmContext,
|
|
@@ -23,14 +24,17 @@ export const getStakes = async (
|
|
|
23
24
|
|
|
24
25
|
try {
|
|
25
26
|
const config = await context.config(currencyId)
|
|
26
|
-
const stakes = await
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
const [stakes, unbondings] = await Promise.all([
|
|
28
|
+
stakingStrategy.fetcher(
|
|
29
|
+
config,
|
|
30
|
+
address,
|
|
31
|
+
contractConfig,
|
|
32
|
+
currencyId,
|
|
33
|
+
options?.knownValidatorIds
|
|
34
|
+
),
|
|
35
|
+
fetchUnbondings(config, currencyId, address).catch(() => []),
|
|
36
|
+
])
|
|
37
|
+
return { items: [...stakes, ...unbondings] }
|
|
34
38
|
} catch {
|
|
35
39
|
return { items: [] }
|
|
36
40
|
}
|
package/src/staking/contracts.ts
CHANGED
|
@@ -58,6 +58,10 @@ export const STAKING_CONTRACTS: Record<string, StakingContractConfig> = {
|
|
|
58
58
|
hrp: 'sei',
|
|
59
59
|
endpoint: '/cosmos/staking/v1beta1/delegators/{address}/redelegations',
|
|
60
60
|
},
|
|
61
|
+
unbondingStrategy: {
|
|
62
|
+
type: 'cosmos-rest',
|
|
63
|
+
endpoint: '/cosmos/staking/v1beta1/delegators/{address}/unbonding_delegations',
|
|
64
|
+
},
|
|
61
65
|
rewardsStrategy: {
|
|
62
66
|
type: 'cosmos-rest',
|
|
63
67
|
endpoint: '/cosmos/distribution/v1beta1/delegators/{address}/rewards',
|
package/src/staking/index.ts
CHANGED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { EvmConfigInfo } from '../config'
|
|
5
|
+
import type { DeepPartialCtor } from '@ledgerhq/coin-module-framework/test/utils'
|
|
6
|
+
import { ethers } from 'ethers'
|
|
7
|
+
import { USEI_TO_EVM_SCALE } from '../utils'
|
|
8
|
+
import { STAKING_CONTRACTS } from './contracts'
|
|
9
|
+
import { fetchUnbondings } from './unbondings'
|
|
10
|
+
|
|
11
|
+
jest.mock('ethers', () => {
|
|
12
|
+
const actual = jest.requireActual('ethers')
|
|
13
|
+
const JsonRpcProvider = jest.fn()
|
|
14
|
+
const Contract = jest.fn()
|
|
15
|
+
return {
|
|
16
|
+
...actual,
|
|
17
|
+
ethers: { ...actual.ethers, JsonRpcProvider, Contract },
|
|
18
|
+
JsonRpcProvider,
|
|
19
|
+
Contract,
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
jest.mock('@ledgerhq/live-network', () => jest.fn())
|
|
23
|
+
jest.mock('../network/node/types')
|
|
24
|
+
|
|
25
|
+
const mockNetwork = jest.mocked(require('@ledgerhq/live-network'))
|
|
26
|
+
const mockIsExternalNodeConfig = jest.mocked(require('../network/node/types').isExternalNodeConfig)
|
|
27
|
+
|
|
28
|
+
const mockNode: EvmConfigInfo['node'] = { type: 'external', uri: 'https://sei-evm.coin.ledger.com' }
|
|
29
|
+
const mockConfig: EvmConfigInfo = {
|
|
30
|
+
status: { type: 'active' },
|
|
31
|
+
chainId: 1329,
|
|
32
|
+
name: 'Sei',
|
|
33
|
+
node: mockNode,
|
|
34
|
+
explorer: { type: 'none' },
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const SEI_CURRENCY = 'sei_evm'
|
|
38
|
+
const EVM_ADDRESS = '0x1111111111111111111111111111111111111111'
|
|
39
|
+
const VALIDATOR = 'seivaloper1qe8uuf5x69c526h4nzxwv4ltftr73v7qnkypla'
|
|
40
|
+
const OTHER_VALIDATOR = 'seivaloper1y82m5y3wevjneamzg0pmx87dzanyxzht0kepvn'
|
|
41
|
+
|
|
42
|
+
/** Far enough ahead that the entry is always still unbonding when the test runs. */
|
|
43
|
+
const futureDate = (daysFromNow: number): string =>
|
|
44
|
+
new Date(Date.now() + daysFromNow * 24 * 60 * 60 * 1000).toISOString()
|
|
45
|
+
|
|
46
|
+
const pastDate = (): string => new Date(Date.now() - 60 * 1000).toISOString()
|
|
47
|
+
|
|
48
|
+
function unbondingResponse(
|
|
49
|
+
validatorAddress: string,
|
|
50
|
+
entries: Array<{ balance: string; completion_time: string; creation_height?: string }>
|
|
51
|
+
) {
|
|
52
|
+
return {
|
|
53
|
+
delegator_address: 'sei1delegator',
|
|
54
|
+
validator_address: validatorAddress,
|
|
55
|
+
entries: entries.map((e) => ({
|
|
56
|
+
creation_height: e.creation_height ?? '231825631',
|
|
57
|
+
completion_time: e.completion_time,
|
|
58
|
+
initial_balance: e.balance,
|
|
59
|
+
balance: e.balance,
|
|
60
|
+
})),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
describe('staking/unbondings', () => {
|
|
65
|
+
describe('fetchUnbondings', () => {
|
|
66
|
+
let mockGetSeiAddr: jest.Mock
|
|
67
|
+
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
jest.clearAllMocks()
|
|
70
|
+
mockGetSeiAddr = jest.fn().mockResolvedValue('sei1defaultcosmosaddressfortest')
|
|
71
|
+
jest
|
|
72
|
+
.mocked<DeepPartialCtor<typeof ethers.JsonRpcProvider>>(ethers.JsonRpcProvider)
|
|
73
|
+
.mockImplementation(() => ({}))
|
|
74
|
+
jest
|
|
75
|
+
.mocked<DeepPartialCtor<typeof ethers.Contract>>(ethers.Contract)
|
|
76
|
+
.mockImplementation(() => ({ getSeiAddr: mockGetSeiAddr }))
|
|
77
|
+
mockIsExternalNodeConfig.mockImplementation((node: unknown) => node === mockNode)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('should return [] for an unknown currencyId', async () => {
|
|
81
|
+
expect(await fetchUnbondings(mockConfig, 'unknown_chain', EVM_ADDRESS)).toEqual([])
|
|
82
|
+
expect(mockNetwork).not.toHaveBeenCalled()
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it('should return [] for a currency without an unbonding strategy', async () => {
|
|
86
|
+
expect(await fetchUnbondings(mockConfig, 'celo', EVM_ADDRESS)).toEqual([])
|
|
87
|
+
expect(mockNetwork).not.toHaveBeenCalled()
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('should return [] when the address is not associated on-chain yet', async () => {
|
|
91
|
+
mockGetSeiAddr.mockResolvedValue(null)
|
|
92
|
+
expect(await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)).toEqual([])
|
|
93
|
+
expect(mockNetwork).not.toHaveBeenCalled()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
it('should return [] when the REST call fails', async () => {
|
|
97
|
+
mockNetwork.mockRejectedValue(new Error('network error'))
|
|
98
|
+
expect(await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)).toEqual([])
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should return [] when the response has no unbonding_responses array', async () => {
|
|
102
|
+
mockNetwork.mockResolvedValue({ data: {} })
|
|
103
|
+
expect(await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)).toEqual([])
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('should query the delegator endpoint with the resolved Cosmos address', async () => {
|
|
107
|
+
mockNetwork.mockResolvedValue({ data: { unbonding_responses: [] } })
|
|
108
|
+
|
|
109
|
+
await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)
|
|
110
|
+
|
|
111
|
+
expect(mockNetwork).toHaveBeenCalledWith({
|
|
112
|
+
url: 'https://rest.sei-apis.com/cosmos/staking/v1beta1/delegators/sei1defaultcosmosaddressfortest/unbonding_delegations',
|
|
113
|
+
method: 'GET',
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('should map a pending entry to a deactivating stake scaled to wei', async () => {
|
|
118
|
+
const completionTime = futureDate(21)
|
|
119
|
+
mockNetwork.mockResolvedValue({
|
|
120
|
+
data: {
|
|
121
|
+
unbonding_responses: [
|
|
122
|
+
unbondingResponse(VALIDATOR, [
|
|
123
|
+
{ balance: '101000000', completion_time: completionTime },
|
|
124
|
+
]),
|
|
125
|
+
],
|
|
126
|
+
},
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
const stakes = await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)
|
|
130
|
+
|
|
131
|
+
expect(stakes).toHaveLength(1)
|
|
132
|
+
expect(stakes[0]).toMatchObject({
|
|
133
|
+
address: EVM_ADDRESS,
|
|
134
|
+
delegate: VALIDATOR,
|
|
135
|
+
state: 'deactivating',
|
|
136
|
+
stateUpdatedAt: new Date(completionTime),
|
|
137
|
+
amount: 101000000n * USEI_TO_EVM_SCALE,
|
|
138
|
+
actions: [],
|
|
139
|
+
})
|
|
140
|
+
expect(stakes[0].asset).toMatchObject({ type: 'native', name: 'Sei' })
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('should not expose a withdraw action nor a withdrawId', async () => {
|
|
144
|
+
mockNetwork.mockResolvedValue({
|
|
145
|
+
data: {
|
|
146
|
+
unbonding_responses: [
|
|
147
|
+
unbondingResponse(VALIDATOR, [{ balance: '1000000', completion_time: futureDate(21) }]),
|
|
148
|
+
],
|
|
149
|
+
},
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
const [stake] = await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)
|
|
153
|
+
|
|
154
|
+
// Sei credits the funds back automatically: no withdraw transaction exists.
|
|
155
|
+
expect(stake.actions).toEqual([])
|
|
156
|
+
expect(stake.details).not.toHaveProperty('withdrawId')
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
it('should keep concurrent entries of the same validator distinct', async () => {
|
|
160
|
+
mockNetwork.mockResolvedValue({
|
|
161
|
+
data: {
|
|
162
|
+
unbonding_responses: [
|
|
163
|
+
unbondingResponse(VALIDATOR, [
|
|
164
|
+
{ balance: '1000000', completion_time: futureDate(10), creation_height: '1' },
|
|
165
|
+
{ balance: '2000000', completion_time: futureDate(20), creation_height: '2' },
|
|
166
|
+
]),
|
|
167
|
+
],
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
const stakes = await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)
|
|
172
|
+
|
|
173
|
+
expect(stakes).toHaveLength(2)
|
|
174
|
+
expect(new Set(stakes.map((s) => s.uid)).size).toBe(2)
|
|
175
|
+
expect(stakes.map((s) => s.amount)).toEqual([
|
|
176
|
+
1000000n * USEI_TO_EVM_SCALE,
|
|
177
|
+
2000000n * USEI_TO_EVM_SCALE,
|
|
178
|
+
])
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('should map entries across several validators', async () => {
|
|
182
|
+
mockNetwork.mockResolvedValue({
|
|
183
|
+
data: {
|
|
184
|
+
unbonding_responses: [
|
|
185
|
+
unbondingResponse(VALIDATOR, [{ balance: '1000000', completion_time: futureDate(21) }]),
|
|
186
|
+
unbondingResponse(OTHER_VALIDATOR, [
|
|
187
|
+
{ balance: '20000000', completion_time: futureDate(21) },
|
|
188
|
+
]),
|
|
189
|
+
],
|
|
190
|
+
},
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
const stakes = await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)
|
|
194
|
+
|
|
195
|
+
expect(stakes.map((s) => s.delegate)).toEqual([VALIDATOR, OTHER_VALIDATOR])
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('should skip entries whose unbonding period already completed', async () => {
|
|
199
|
+
mockNetwork.mockResolvedValue({
|
|
200
|
+
data: {
|
|
201
|
+
unbonding_responses: [
|
|
202
|
+
unbondingResponse(VALIDATOR, [
|
|
203
|
+
{ balance: '1000000', completion_time: pastDate(), creation_height: '1' },
|
|
204
|
+
{ balance: '2000000', completion_time: futureDate(5), creation_height: '2' },
|
|
205
|
+
]),
|
|
206
|
+
],
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
const stakes = await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)
|
|
211
|
+
|
|
212
|
+
expect(stakes).toHaveLength(1)
|
|
213
|
+
expect(stakes[0].amount).toBe(2000000n * USEI_TO_EVM_SCALE)
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('should skip malformed entries without failing the whole fetch', async () => {
|
|
217
|
+
mockNetwork.mockResolvedValue({
|
|
218
|
+
data: {
|
|
219
|
+
unbonding_responses: [
|
|
220
|
+
unbondingResponse(VALIDATOR, [
|
|
221
|
+
{ balance: 'not-a-number', completion_time: futureDate(21), creation_height: '1' },
|
|
222
|
+
{ balance: '1000000', completion_time: 'not-a-date', creation_height: '2' },
|
|
223
|
+
{ balance: '0', completion_time: futureDate(21), creation_height: '3' },
|
|
224
|
+
{ balance: '3000000', completion_time: futureDate(21), creation_height: '4' },
|
|
225
|
+
]),
|
|
226
|
+
],
|
|
227
|
+
},
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
const stakes = await fetchUnbondings(mockConfig, SEI_CURRENCY, EVM_ADDRESS)
|
|
231
|
+
|
|
232
|
+
expect(stakes).toHaveLength(1)
|
|
233
|
+
expect(stakes[0].amount).toBe(3000000n * USEI_TO_EVM_SCALE)
|
|
234
|
+
})
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
describe('sei_evm unbonding strategy config', () => {
|
|
238
|
+
it('should be configured to read the Cosmos unbonding_delegations endpoint', () => {
|
|
239
|
+
expect(STAKING_CONTRACTS[SEI_CURRENCY]?.unbondingStrategy).toEqual({
|
|
240
|
+
type: 'cosmos-rest',
|
|
241
|
+
endpoint: '/cosmos/staking/v1beta1/delegators/{address}/unbonding_delegations',
|
|
242
|
+
})
|
|
243
|
+
})
|
|
244
|
+
})
|
|
245
|
+
})
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: © 2026 LEDGER SAS
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { EvmConfigInfo } from '../config'
|
|
5
|
+
import type { StakingContractConfig, UnbondingStrategy } from '../types/staking'
|
|
6
|
+
import type { Stake } from '@ledgerhq/coin-module-framework/api/types'
|
|
7
|
+
import network from '@ledgerhq/live-network'
|
|
8
|
+
import { log } from '@ledgerhq/logs'
|
|
9
|
+
import { evmUnit } from '../logic/evmUnit'
|
|
10
|
+
import { isExternalNodeConfig } from '../network/node/types'
|
|
11
|
+
import { STAKING_CONTRACTS } from './contracts'
|
|
12
|
+
import { getCosmosAddr } from './redelegations'
|
|
13
|
+
|
|
14
|
+
type UnbondingEntry = {
|
|
15
|
+
creation_height: string
|
|
16
|
+
completion_time: string
|
|
17
|
+
balance: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type ValidatorUnbonding = {
|
|
21
|
+
validator_address: string
|
|
22
|
+
entries: UnbondingEntry[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type UnbondingsApiResponse = { unbonding_responses: ValidatorUnbonding[] }
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The staking precompile only reports active delegations, so an undelegation
|
|
29
|
+
* would otherwise be invisible for the whole unbonding period.
|
|
30
|
+
*
|
|
31
|
+
* Returns `[]` on any failure so a sync is never blocked by this call.
|
|
32
|
+
*/
|
|
33
|
+
async function fetchCosmosRestUnbondings(
|
|
34
|
+
evmConfig: EvmConfigInfo,
|
|
35
|
+
config: StakingContractConfig,
|
|
36
|
+
strategy: Extract<UnbondingStrategy, { type: 'cosmos-rest' }>,
|
|
37
|
+
currencyId: string,
|
|
38
|
+
evmAddress: string
|
|
39
|
+
): Promise<Stake[]> {
|
|
40
|
+
const baseUrl = config.apiConfig?.baseUrl
|
|
41
|
+
const precompileAddress = config.apiConfig?.precompileAddress
|
|
42
|
+
if (!baseUrl || !precompileAddress) return []
|
|
43
|
+
|
|
44
|
+
const node = evmConfig.node
|
|
45
|
+
const evmRpcUrl = isExternalNodeConfig(node) ? node.uri : undefined
|
|
46
|
+
if (!evmRpcUrl) return []
|
|
47
|
+
|
|
48
|
+
const cosmosAddress = await getCosmosAddr(evmRpcUrl, precompileAddress, evmAddress)
|
|
49
|
+
if (!cosmosAddress) return []
|
|
50
|
+
|
|
51
|
+
const url = `${baseUrl.replace(/\/$/, '')}${strategy.endpoint.replace('{address}', cosmosAddress)}`
|
|
52
|
+
|
|
53
|
+
let data: UnbondingsApiResponse | undefined
|
|
54
|
+
try {
|
|
55
|
+
const res = await network<UnbondingsApiResponse>({ url, method: 'GET' })
|
|
56
|
+
data = res.data
|
|
57
|
+
} catch (e) {
|
|
58
|
+
log('coin-evm/staking', 'fetchCosmosRestUnbondings: REST request failed', {
|
|
59
|
+
url,
|
|
60
|
+
error: e instanceof Error ? e.message : String(e),
|
|
61
|
+
})
|
|
62
|
+
return []
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!Array.isArray(data?.unbonding_responses)) return []
|
|
66
|
+
|
|
67
|
+
// The REST balance is in the same base unit as the staking calldata (usei).
|
|
68
|
+
const scale = config.calldataAmountScale ?? 1n
|
|
69
|
+
const contractAddress = config.contractAddress({ mode: 'undelegate' })
|
|
70
|
+
const now = new Date()
|
|
71
|
+
const stakes: Stake[] = []
|
|
72
|
+
|
|
73
|
+
for (const response of data.unbonding_responses) {
|
|
74
|
+
const validatorAddress = response.validator_address
|
|
75
|
+
if (typeof validatorAddress !== 'string' || !validatorAddress) continue
|
|
76
|
+
|
|
77
|
+
for (const entry of response.entries ?? []) {
|
|
78
|
+
const completionDate = new Date(entry.completion_time)
|
|
79
|
+
if (Number.isNaN(completionDate.valueOf())) continue
|
|
80
|
+
// A matured entry is already back in the available balance.
|
|
81
|
+
if (completionDate <= now) continue
|
|
82
|
+
|
|
83
|
+
let amount: bigint
|
|
84
|
+
try {
|
|
85
|
+
amount = BigInt(entry.balance) * scale
|
|
86
|
+
} catch {
|
|
87
|
+
continue
|
|
88
|
+
}
|
|
89
|
+
if (amount === 0n) continue
|
|
90
|
+
|
|
91
|
+
stakes.push({
|
|
92
|
+
// Cosmos keeps concurrent unbondings from one validator as separate entries.
|
|
93
|
+
uid: `${contractAddress}-${validatorAddress}-${evmAddress}-unbonding-${entry.creation_height}`,
|
|
94
|
+
address: evmAddress,
|
|
95
|
+
delegate: validatorAddress,
|
|
96
|
+
// Never `withdrawable`: Sei credits the funds back automatically, and the
|
|
97
|
+
// empty `actions` keeps `canWithdraw` false, so no withdraw action shows up.
|
|
98
|
+
state: 'deactivating',
|
|
99
|
+
stateUpdatedAt: completionDate,
|
|
100
|
+
asset: {
|
|
101
|
+
type: 'native',
|
|
102
|
+
name: evmConfig.name,
|
|
103
|
+
unit: evmUnit[currencyId],
|
|
104
|
+
},
|
|
105
|
+
amount,
|
|
106
|
+
actions: [],
|
|
107
|
+
details: {
|
|
108
|
+
contractAddress,
|
|
109
|
+
validator: validatorAddress,
|
|
110
|
+
},
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return stakes
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* To support a new chain, add a `case` for its `UnbondingStrategy.type` and the
|
|
120
|
+
* matching entry in `STAKING_CONTRACTS`.
|
|
121
|
+
*/
|
|
122
|
+
export async function fetchUnbondings(
|
|
123
|
+
evmConfig: EvmConfigInfo,
|
|
124
|
+
currencyId: string,
|
|
125
|
+
evmAddress: string
|
|
126
|
+
): Promise<Stake[]> {
|
|
127
|
+
const config = STAKING_CONTRACTS[currencyId]
|
|
128
|
+
const strategy = config?.unbondingStrategy
|
|
129
|
+
if (!config || !strategy || strategy.type === 'none') return []
|
|
130
|
+
|
|
131
|
+
switch (strategy.type) {
|
|
132
|
+
case 'cosmos-rest':
|
|
133
|
+
return fetchCosmosRestUnbondings(evmConfig, config, strategy, currencyId, evmAddress)
|
|
134
|
+
default:
|
|
135
|
+
return []
|
|
136
|
+
}
|
|
137
|
+
}
|
package/src/types/staking.ts
CHANGED
|
@@ -44,6 +44,22 @@ export type RedelegationStrategy =
|
|
|
44
44
|
}
|
|
45
45
|
| { type: 'none' }
|
|
46
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Per-chain strategy for fetching pending unbondings from an off-chain source.
|
|
49
|
+
*
|
|
50
|
+
* - `cosmos-rest`: Cosmos REST API, EVM address resolved via
|
|
51
|
+
* `apiConfig.precompileAddress`. Needed because the staking precompile
|
|
52
|
+
* exposes only active delegations.
|
|
53
|
+
* - `none`: pending unbondings are not surfaced.
|
|
54
|
+
*/
|
|
55
|
+
export type UnbondingStrategy =
|
|
56
|
+
| {
|
|
57
|
+
type: 'cosmos-rest'
|
|
58
|
+
/** REST endpoint template; `{address}` is replaced with the resolved Cosmos address. */
|
|
59
|
+
endpoint: string
|
|
60
|
+
}
|
|
61
|
+
| { type: 'none' }
|
|
62
|
+
|
|
47
63
|
/**
|
|
48
64
|
* Per-chain strategy for fetching pending delegation rewards.
|
|
49
65
|
*
|
|
@@ -134,6 +150,8 @@ export type StakingContractConfig = {
|
|
|
134
150
|
}
|
|
135
151
|
/** How to fetch active redelegations from an off-chain source. Defaults to `"none"` when absent. */
|
|
136
152
|
redelegationStrategy?: RedelegationStrategy
|
|
153
|
+
/** How to fetch pending unbondings from an off-chain source. Defaults to `"none"` when absent. */
|
|
154
|
+
unbondingStrategy?: UnbondingStrategy
|
|
137
155
|
/** How to fetch pending delegation rewards from an off-chain source. Defaults to `"none"` when absent. */
|
|
138
156
|
rewardsStrategy?: RewardsStrategy
|
|
139
157
|
/** How to recover a REWARD op's amount from its tx receipt logs. Absent → REWARD ops keep their native value. */
|