@ledgerhq/coin-evm 16.6.0 → 16.8.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 +16 -0
- package/lib/staking/contracts.d.ts.map +1 -1
- package/lib/staking/contracts.js +7 -0
- package/lib/staking/contracts.js.map +1 -1
- package/lib/staking/validators/index.d.ts.map +1 -1
- package/lib/staking/validators/index.js +36 -1
- package/lib/staking/validators/index.js.map +1 -1
- package/lib/staking/validators/monad.d.ts +1 -0
- package/lib/staking/validators/monad.d.ts.map +1 -1
- package/lib/staking/validators/monad.js +103 -6
- package/lib/staking/validators/monad.js.map +1 -1
- package/lib/staking/validators/sei.d.ts.map +1 -1
- package/lib/staking/validators/sei.js +46 -14
- package/lib/staking/validators/sei.js.map +1 -1
- package/lib/staking/validators/somnia.d.ts.map +1 -1
- package/lib/staking/validators/somnia.js +31 -2
- package/lib/staking/validators/somnia.js.map +1 -1
- package/lib/staking/validators/zero_gravity.d.ts.map +1 -1
- package/lib/staking/validators/zero_gravity.js +14 -2
- package/lib/staking/validators/zero_gravity.js.map +1 -1
- package/lib/types/staking.d.ts +10 -0
- package/lib/types/staking.d.ts.map +1 -1
- package/lib-es/staking/contracts.d.ts.map +1 -1
- package/lib-es/staking/contracts.js +7 -0
- package/lib-es/staking/contracts.js.map +1 -1
- package/lib-es/staking/validators/index.d.ts.map +1 -1
- package/lib-es/staking/validators/index.js +3 -1
- package/lib-es/staking/validators/index.js.map +1 -1
- package/lib-es/staking/validators/monad.d.ts +1 -0
- package/lib-es/staking/validators/monad.d.ts.map +1 -1
- package/lib-es/staking/validators/monad.js +101 -5
- package/lib-es/staking/validators/monad.js.map +1 -1
- package/lib-es/staking/validators/sei.d.ts.map +1 -1
- package/lib-es/staking/validators/sei.js +46 -14
- package/lib-es/staking/validators/sei.js.map +1 -1
- package/lib-es/staking/validators/somnia.d.ts.map +1 -1
- package/lib-es/staking/validators/somnia.js +31 -2
- package/lib-es/staking/validators/somnia.js.map +1 -1
- package/lib-es/staking/validators/zero_gravity.d.ts.map +1 -1
- package/lib-es/staking/validators/zero_gravity.js +14 -2
- package/lib-es/staking/validators/zero_gravity.js.map +1 -1
- package/lib-es/types/staking.d.ts +10 -0
- package/lib-es/types/staking.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/staking/contracts.ts +7 -0
- package/src/staking/validators/index.ts +4 -1
- package/src/staking/validators/monad.test.ts +175 -44
- package/src/staking/validators/monad.ts +156 -4
- package/src/staking/validators/sei.test.ts +93 -0
- package/src/staking/validators/sei.ts +56 -14
- package/src/staking/validators/somnia.test.ts +44 -2
- package/src/staking/validators/somnia.ts +32 -2
- package/src/staking/validators/zero_gravity.test.ts +25 -0
- package/src/staking/validators/zero_gravity.ts +17 -2
- package/src/staking/validators.integ.test.ts +35 -4
- package/src/staking/validators.test.ts +34 -22
- package/src/types/staking.ts +10 -0
|
@@ -27,32 +27,74 @@ type CosmosValidator = {
|
|
|
27
27
|
|
|
28
28
|
type CosmosValidatorsResponse = { validators: CosmosValidator[] }
|
|
29
29
|
|
|
30
|
+
type SeiMinter = { total_mint_amount: string }
|
|
31
|
+
type SeiPool = { pool: { bonded_tokens: string } }
|
|
32
|
+
|
|
33
|
+
const isIntegerString = (value: unknown): value is string =>
|
|
34
|
+
typeof value === 'string' && /^\d+$/.test(value)
|
|
35
|
+
|
|
36
|
+
// Sei mints `total_mint_amount` over the current year (15 Aug to 14 Aug) and sends it to the
|
|
37
|
+
// fee collector, with no community tax: it is shared between the bonded tokens, before commission.
|
|
38
|
+
// Source: https://github.com/sei-protocol/sei-chain/blob/main/x/mint/keeper/hooks.go
|
|
39
|
+
async function fetchGrossRewardRate(
|
|
40
|
+
baseUrl: string,
|
|
41
|
+
mintEndpoint: string,
|
|
42
|
+
poolEndpoint: string
|
|
43
|
+
): Promise<number | undefined> {
|
|
44
|
+
const [{ data: minter }, { data: pool }] = await Promise.all([
|
|
45
|
+
network<SeiMinter>({ url: `${baseUrl}${mintEndpoint}`, method: 'GET' }),
|
|
46
|
+
network<SeiPool>({ url: `${baseUrl}${poolEndpoint}`, method: 'GET' }),
|
|
47
|
+
])
|
|
48
|
+
const minted = minter?.total_mint_amount
|
|
49
|
+
const bonded = pool?.pool?.bonded_tokens
|
|
50
|
+
if (!isIntegerString(minted) || !isIntegerString(bonded) || Number(bonded) === 0) return undefined
|
|
51
|
+
return Number(minted) / Number(bonded)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function toApy(grossRate: number | undefined, commissionRate: string): number | undefined {
|
|
55
|
+
const apy = grossRate === undefined ? Number.NaN : grossRate * (1 - Number(commissionRate))
|
|
56
|
+
return Number.isFinite(apy) ? apy : undefined
|
|
57
|
+
}
|
|
58
|
+
|
|
30
59
|
// Sei's REST endpoint returns the whole validator set in one response, so it
|
|
31
60
|
// ignores the cursor and always reports `next: undefined` (single page).
|
|
32
61
|
const seiValidatorApi: ValidatorApi = {
|
|
33
|
-
fetchValidators: async (config): Promise<Page<Validator>> => {
|
|
62
|
+
fetchValidators: async (config, logger): Promise<Page<Validator>> => {
|
|
34
63
|
const apiConfig = STAKING_CONTRACTS[config.chainId]?.apiConfig
|
|
35
64
|
if (!apiConfig?.baseUrl) return { items: [], next: undefined }
|
|
36
65
|
|
|
37
|
-
const { baseUrl, validatorsEndpoint } = apiConfig
|
|
66
|
+
const { baseUrl, validatorsEndpoint, mintEndpoint, poolEndpoint } = apiConfig
|
|
38
67
|
|
|
39
68
|
try {
|
|
40
|
-
const { data } = await
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
69
|
+
const [{ data }, grossRate] = await Promise.all([
|
|
70
|
+
network<CosmosValidatorsResponse>({
|
|
71
|
+
url: `${baseUrl}${validatorsEndpoint}`,
|
|
72
|
+
method: 'GET',
|
|
73
|
+
}),
|
|
74
|
+
mintEndpoint && poolEndpoint
|
|
75
|
+
? fetchGrossRewardRate(baseUrl, mintEndpoint, poolEndpoint).catch((error) => {
|
|
76
|
+
logger('coin-evm/staking', 'fetchValidators: Sei APY fetch failed', {
|
|
77
|
+
error: error instanceof Error ? error.message : String(error),
|
|
78
|
+
})
|
|
79
|
+
return undefined
|
|
80
|
+
})
|
|
81
|
+
: undefined,
|
|
82
|
+
])
|
|
44
83
|
|
|
45
84
|
const items: Validator[] = Array.isArray(data?.validators)
|
|
46
85
|
? data.validators
|
|
47
86
|
.filter((v): v is CosmosValidator => typeof v?.operator_address === 'string')
|
|
48
|
-
.map((v) =>
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
87
|
+
.map((v) => {
|
|
88
|
+
const commissionRate = v.commission?.commission_rates?.rate ?? '0'
|
|
89
|
+
return {
|
|
90
|
+
id: v.operator_address,
|
|
91
|
+
address: v.operator_address,
|
|
92
|
+
name: v.description?.moniker ?? v.operator_address,
|
|
93
|
+
commissionRate,
|
|
94
|
+
balance: seiBalanceAmountToNativeMinUnit(v.tokens, 'usei'),
|
|
95
|
+
apy: toApy(grossRate, commissionRate),
|
|
96
|
+
}
|
|
97
|
+
})
|
|
56
98
|
: []
|
|
57
99
|
|
|
58
100
|
return { items, next: undefined }
|
|
@@ -51,6 +51,15 @@ const setupRpc = (handler: CallHandler) => {
|
|
|
51
51
|
return callMock
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
// Serves `response` for the staking rate endpoint (rejects when it is an Error) and an empty
|
|
55
|
+
// body for every other request, such as the validator names.
|
|
56
|
+
const mockStakingRate = (response: unknown) =>
|
|
57
|
+
mockedNetwork.mockImplementation(async ({ url }) => {
|
|
58
|
+
if (!url?.includes('apr-calculation')) return { data: {} }
|
|
59
|
+
if (response instanceof Error) throw response
|
|
60
|
+
return { data: response }
|
|
61
|
+
})
|
|
62
|
+
|
|
54
63
|
const encodeCommitteeValidators = (addresses: string[]): string =>
|
|
55
64
|
somniaIface.encodeFunctionResult('getCommitteeValidators', [addresses])
|
|
56
65
|
|
|
@@ -129,6 +138,7 @@ describe('staking/validators/somnia', () => {
|
|
|
129
138
|
|
|
130
139
|
describe('fetchValidators', () => {
|
|
131
140
|
it('returns committee validators with address as name and computed commission', async () => {
|
|
141
|
+
mockStakingRate({ data: { stakingApr: 0.5 } })
|
|
132
142
|
setupRpc(
|
|
133
143
|
routeByName({
|
|
134
144
|
getCommitteeValidators: () => encodeCommitteeValidators([VALIDATOR_A, VALIDATOR_B]),
|
|
@@ -137,7 +147,8 @@ describe('staking/validators/somnia', () => {
|
|
|
137
147
|
address,
|
|
138
148
|
stakedAmount: 1000n,
|
|
139
149
|
delegatedStake: 500n,
|
|
140
|
-
|
|
150
|
+
// validator B keeps the whole reward: a 100% commission
|
|
151
|
+
delegateStakeRate: address === VALIDATOR_A ? 9n * 10n ** 17n : 0n,
|
|
141
152
|
}),
|
|
142
153
|
})
|
|
143
154
|
)
|
|
@@ -151,14 +162,45 @@ describe('staking/validators/somnia', () => {
|
|
|
151
162
|
name: VALIDATOR_A,
|
|
152
163
|
commissionRate: '0.1',
|
|
153
164
|
balance: 1500n,
|
|
154
|
-
apy: 0,
|
|
165
|
+
apy: 0.45,
|
|
155
166
|
})
|
|
156
167
|
expect(page.items[1]).toMatchObject({
|
|
157
168
|
address: VALIDATOR_B,
|
|
169
|
+
commissionRate: '1.0',
|
|
158
170
|
balance: 1500n,
|
|
171
|
+
apy: 0,
|
|
159
172
|
})
|
|
160
173
|
})
|
|
161
174
|
|
|
175
|
+
it.each([
|
|
176
|
+
['the request fails', new Error('boom')],
|
|
177
|
+
['the rate is missing', { data: {} }],
|
|
178
|
+
['the rate is not a number', { data: { stakingApr: '0.5' } }],
|
|
179
|
+
['the rate is negative', { data: { stakingApr: -0.1 } }],
|
|
180
|
+
['the rate is not finite', { data: { stakingApr: null } }],
|
|
181
|
+
// 2 * (1 - 0.1) = 1.8, outside the [0, 1] contract of the apy
|
|
182
|
+
['the apy would be above 1', { data: { stakingApr: 2 } }],
|
|
183
|
+
])('keeps the validators with an undefined apy when %s', async (_name, response) => {
|
|
184
|
+
mockStakingRate(response)
|
|
185
|
+
setupRpc(
|
|
186
|
+
routeByName({
|
|
187
|
+
getCommitteeValidators: () => encodeCommitteeValidators([VALIDATOR_A]),
|
|
188
|
+
getStake: (address) =>
|
|
189
|
+
encodeGetStake({
|
|
190
|
+
address,
|
|
191
|
+
stakedAmount: 1000n,
|
|
192
|
+
delegatedStake: 500n,
|
|
193
|
+
delegateStakeRate: 9n * 10n ** 17n,
|
|
194
|
+
}),
|
|
195
|
+
})
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
const page = await getValidators(mockConfig, testLogger)
|
|
199
|
+
|
|
200
|
+
expect(page.items).toHaveLength(1)
|
|
201
|
+
expect(page.items[0].apy).toBeUndefined()
|
|
202
|
+
})
|
|
203
|
+
|
|
162
204
|
it('skips a validator whose getStake call fails and keeps the rest', async () => {
|
|
163
205
|
setupRpc(
|
|
164
206
|
routeByName({
|
|
@@ -135,6 +135,33 @@ const validatorNamesCache = makeLRUCache(
|
|
|
135
135
|
export const clearValidatorNamesCache = (chainId: ChainId): void =>
|
|
136
136
|
validatorNamesCache.clear(String(chainId))
|
|
137
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Network-wide staking rate (fraction, before commission) from the official dashboard.
|
|
140
|
+
* Returns `undefined` when unconfigured, unreachable or malformed: the APY is then unknown.
|
|
141
|
+
*/
|
|
142
|
+
const fetchStakingRate = async (chainId: ChainId, logger: Logger): Promise<number | undefined> => {
|
|
143
|
+
const baseUrl = STAKING_CONTRACTS[chainId]?.stakingRateSource?.baseUrl
|
|
144
|
+
if (!baseUrl) return undefined
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const res = await network<{ data?: { stakingApr?: unknown } }>({ url: baseUrl, method: 'GET' })
|
|
148
|
+
const rate = res?.data?.data?.stakingApr
|
|
149
|
+
return typeof rate === 'number' && Number.isFinite(rate) && rate >= 0 ? rate : undefined
|
|
150
|
+
} catch (error) {
|
|
151
|
+
logger('coin-evm/staking', 'fetchStakingRate: staking rate lookup failed', {
|
|
152
|
+
chainId,
|
|
153
|
+
error: error instanceof Error ? error.message : String(error),
|
|
154
|
+
})
|
|
155
|
+
return undefined
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// The apy is a fraction between 0 and 1: anything else means the rate cannot be trusted.
|
|
160
|
+
const toApy = (stakingRate: number | undefined, commissionRate: string): number | undefined => {
|
|
161
|
+
const apy = stakingRate === undefined ? Number.NaN : stakingRate * (1 - Number(commissionRate))
|
|
162
|
+
return apy >= 0 && apy <= 1 ? apy : undefined
|
|
163
|
+
}
|
|
164
|
+
|
|
138
165
|
const fetchValidatorDetails = async (
|
|
139
166
|
chainId: ChainId,
|
|
140
167
|
provider: JsonRpcProvider,
|
|
@@ -143,7 +170,10 @@ const fetchValidatorDetails = async (
|
|
|
143
170
|
addresses: string[],
|
|
144
171
|
logger: Logger
|
|
145
172
|
): Promise<Validator[]> => {
|
|
146
|
-
const names = await
|
|
173
|
+
const [names, stakingRate] = await Promise.all([
|
|
174
|
+
validatorNamesCache(chainId, logger).catch((): ValidatorNamesMap => ({})),
|
|
175
|
+
fetchStakingRate(chainId, logger),
|
|
176
|
+
])
|
|
147
177
|
|
|
148
178
|
const items: Validator[] = []
|
|
149
179
|
|
|
@@ -169,7 +199,7 @@ const fetchValidatorDetails = async (
|
|
|
169
199
|
name: name ?? validatorAddress,
|
|
170
200
|
commissionRate,
|
|
171
201
|
balance: stakedAmount + delegatedStake,
|
|
172
|
-
apy:
|
|
202
|
+
apy: toApy(stakingRate, commissionRate),
|
|
173
203
|
}
|
|
174
204
|
})
|
|
175
205
|
)
|
|
@@ -46,6 +46,8 @@ const makeValidator = (
|
|
|
46
46
|
moniker: string | null
|
|
47
47
|
commission_pct: string
|
|
48
48
|
voting_power_tokens: string
|
|
49
|
+
apy_basis_points: string | null
|
|
50
|
+
bond_status_raw: string
|
|
49
51
|
}> = {}
|
|
50
52
|
) => ({
|
|
51
53
|
addr: 'aabbccddee' + '00'.repeat(15),
|
|
@@ -111,6 +113,29 @@ describe('staking/validators/zero_gravity', () => {
|
|
|
111
113
|
expect(page.items[0].balance).toEqual(42000000000000000000n)
|
|
112
114
|
})
|
|
113
115
|
|
|
116
|
+
it('converts apy_basis_points to a decimal fraction for a bonded validator', async () => {
|
|
117
|
+
mockedNetwork.mockResolvedValueOnce({
|
|
118
|
+
data: [makeValidator({ apy_basis_points: '1400', bond_status_raw: '3' })],
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const page = await getValidators(mockConfig, testLogger)
|
|
122
|
+
|
|
123
|
+
expect(page.items[0].apy).toBe(0.14)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it.each([
|
|
127
|
+
['the validator is not bonded', { apy_basis_points: '8304', bond_status_raw: '2' }],
|
|
128
|
+
['the bond status is missing', { apy_basis_points: '1400' }],
|
|
129
|
+
['apy_basis_points is null', { apy_basis_points: null, bond_status_raw: '3' }],
|
|
130
|
+
])('leaves the APY undefined but keeps the validator when %s', async (_label, overrides) => {
|
|
131
|
+
mockedNetwork.mockResolvedValueOnce({ data: [makeValidator(overrides)] })
|
|
132
|
+
|
|
133
|
+
const page = await getValidators(mockConfig, testLogger)
|
|
134
|
+
|
|
135
|
+
expect(page.items).toHaveLength(1)
|
|
136
|
+
expect(page.items[0].apy).toBeUndefined()
|
|
137
|
+
})
|
|
138
|
+
|
|
114
139
|
it('returns empty page on network error', async () => {
|
|
115
140
|
mockedNetwork.mockRejectedValueOnce(new Error('network failure'))
|
|
116
141
|
|
|
@@ -21,6 +21,17 @@ type ExploreMe0gValidator = {
|
|
|
21
21
|
moniker: string | null
|
|
22
22
|
commission_pct: string
|
|
23
23
|
voting_power_tokens: string
|
|
24
|
+
apy_basis_points?: string | null
|
|
25
|
+
bond_status_raw?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The APY is only computed for bonded validators
|
|
29
|
+
const BONDED = '3'
|
|
30
|
+
|
|
31
|
+
function toApy(validator: ExploreMe0gValidator): number | undefined {
|
|
32
|
+
if (validator.bond_status_raw !== BONDED) return undefined
|
|
33
|
+
const apy = Number(validator.apy_basis_points ?? Number.NaN) / 10_000
|
|
34
|
+
return Number.isFinite(apy) ? apy : undefined
|
|
24
35
|
}
|
|
25
36
|
|
|
26
37
|
function isExploreMe0gValidator(value: unknown): value is ExploreMe0gValidator {
|
|
@@ -35,7 +46,11 @@ function isExploreMe0gValidator(value: unknown): value is ExploreMe0gValidator {
|
|
|
35
46
|
'commission_pct' in value &&
|
|
36
47
|
typeof value.commission_pct === 'string' &&
|
|
37
48
|
'voting_power_tokens' in value &&
|
|
38
|
-
typeof value.voting_power_tokens === 'string'
|
|
49
|
+
typeof value.voting_power_tokens === 'string' &&
|
|
50
|
+
(!('apy_basis_points' in value) ||
|
|
51
|
+
value.apy_basis_points === null ||
|
|
52
|
+
typeof value.apy_basis_points === 'string') &&
|
|
53
|
+
(!('bond_status_raw' in value) || typeof value.bond_status_raw === 'string')
|
|
39
54
|
)
|
|
40
55
|
}
|
|
41
56
|
|
|
@@ -61,7 +76,7 @@ const zeroGravityValidatorApi: ValidatorApi = {
|
|
|
61
76
|
name: v.moniker ?? address,
|
|
62
77
|
commissionRate: (parseFloat(v.commission_pct) / 100).toString(),
|
|
63
78
|
balance: BigInt(v.voting_power_tokens),
|
|
64
|
-
apy:
|
|
79
|
+
apy: toApy(v),
|
|
65
80
|
}
|
|
66
81
|
})
|
|
67
82
|
: []
|
|
@@ -6,11 +6,27 @@ import { testLogger } from '../fixtures/context.fixtures'
|
|
|
6
6
|
import { getValidators } from './validators'
|
|
7
7
|
|
|
8
8
|
describe('getValidators', () => {
|
|
9
|
+
it("reports the APY of the validators on 'sei_evm'", async () => {
|
|
10
|
+
const { items } = await getValidators(evmDefaults.sei_evm, testLogger)
|
|
11
|
+
|
|
12
|
+
expect(items.length).toBeGreaterThan(0)
|
|
13
|
+
for (const validator of items) {
|
|
14
|
+
expect(validator.apy).toBeGreaterThanOrEqual(0)
|
|
15
|
+
expect(validator.apy).toBeLessThan(1)
|
|
16
|
+
}
|
|
17
|
+
// A validator with 100% commission legitimately gets 0, the others must get a positive APY
|
|
18
|
+
const withCommission = items.filter((validator) => Number(validator.commissionRate) < 1)
|
|
19
|
+
expect(withCommission.length).toBeGreaterThan(0)
|
|
20
|
+
for (const validator of withCommission) {
|
|
21
|
+
expect(validator.apy).toBeGreaterThan(0)
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
|
|
9
25
|
it.each([
|
|
10
|
-
['monad', { info: evmDefaults.monad }],
|
|
11
|
-
['zero_gravity', { info: evmDefaults.zero_gravity }],
|
|
12
|
-
['somnia', { info: evmDefaults.somnia }],
|
|
13
|
-
])("fetches validators on '%s'", async (_chainName, config) => {
|
|
26
|
+
['monad', { info: evmDefaults.monad }, true],
|
|
27
|
+
['zero_gravity', { info: evmDefaults.zero_gravity }, false],
|
|
28
|
+
['somnia', { info: evmDefaults.somnia }, true],
|
|
29
|
+
])("fetches validators on '%s'", async (_chainName, config, hasApy) => {
|
|
14
30
|
const firstPage = await getValidators(config.info, testLogger)
|
|
15
31
|
expect(firstPage.items.length).toBeGreaterThan(0)
|
|
16
32
|
|
|
@@ -21,5 +37,20 @@ describe('getValidators', () => {
|
|
|
21
37
|
expect(validator.balance).toBeGreaterThan(0n)
|
|
22
38
|
expect(parseFloat(validator.commissionRate ?? '0')).toBeGreaterThanOrEqual(0)
|
|
23
39
|
expect(parseFloat(validator.commissionRate ?? '0')).toBeLessThanOrEqual(1)
|
|
40
|
+
if (hasApy) {
|
|
41
|
+
expect(validator.apy).toBeGreaterThanOrEqual(0)
|
|
42
|
+
expect(validator.apy).toBeLessThanOrEqual(1)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("reports the APY of the validators on 'zero_gravity'", async () => {
|
|
47
|
+
const { items } = await getValidators(evmDefaults.zero_gravity, testLogger)
|
|
48
|
+
|
|
49
|
+
const withApy = items.filter((validator) => validator.apy !== undefined)
|
|
50
|
+
expect(withApy.length).toBeGreaterThan(0)
|
|
51
|
+
for (const validator of withApy) {
|
|
52
|
+
expect(validator.apy).toBeGreaterThanOrEqual(0)
|
|
53
|
+
expect(validator.apy).toBeLessThanOrEqual(1)
|
|
54
|
+
}
|
|
24
55
|
})
|
|
25
56
|
})
|
|
@@ -41,7 +41,7 @@ const cosmosValidatorsPayload = {
|
|
|
41
41
|
{
|
|
42
42
|
operator_address: 'seivaloper1abc',
|
|
43
43
|
description: { moniker: 'John' },
|
|
44
|
-
commission: { commission_rates: { rate: '0.
|
|
44
|
+
commission: { commission_rates: { rate: '0.25' } },
|
|
45
45
|
tokens: '100',
|
|
46
46
|
},
|
|
47
47
|
{
|
|
@@ -54,6 +54,17 @@ const cosmosValidatorsPayload = {
|
|
|
54
54
|
},
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
// 1000 usei minted over 2000 bonded: a gross APY of 0.5
|
|
58
|
+
const mockSeiRest = (validatorsPayload: typeof cosmosValidatorsPayload) =>
|
|
59
|
+
mockedNetwork.mockImplementation(async ({ url }) => {
|
|
60
|
+
if (url?.includes('/minter')) return { status: 200, data: { total_mint_amount: '1000' } }
|
|
61
|
+
if (url?.includes('/pool')) return { status: 200, data: { pool: { bonded_tokens: '2000' } } }
|
|
62
|
+
return validatorsPayload
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const validatorsRequests = () =>
|
|
66
|
+
mockedNetwork.mock.calls.filter(([request]) => request.url?.includes('/validators'))
|
|
67
|
+
|
|
57
68
|
describe('staking/validators', () => {
|
|
58
69
|
beforeEach(() => {
|
|
59
70
|
jest.clearAllMocks()
|
|
@@ -62,7 +73,7 @@ describe('staking/validators', () => {
|
|
|
62
73
|
|
|
63
74
|
describe('getValidators + cache', () => {
|
|
64
75
|
it('fetches Sei validators from REST and caches non-empty results', async () => {
|
|
65
|
-
|
|
76
|
+
mockSeiRest(cosmosValidatorsPayload)
|
|
66
77
|
|
|
67
78
|
const first = await getValidators(mockConfig, testLogger)
|
|
68
79
|
|
|
@@ -78,10 +89,11 @@ describe('staking/validators', () => {
|
|
|
78
89
|
id: 'seivaloper1abc',
|
|
79
90
|
address: 'seivaloper1abc',
|
|
80
91
|
name: 'John',
|
|
81
|
-
commissionRate: '0.
|
|
92
|
+
commissionRate: '0.25',
|
|
82
93
|
// REST returns usei (6 decimals); scaled to sei_evm's 18-decimal unit (×10^12).
|
|
83
94
|
balance: 100000000000000n,
|
|
84
|
-
|
|
95
|
+
// 1000 minted over 2000 bonded, minus 25% commission
|
|
96
|
+
apy: 0.375,
|
|
85
97
|
}),
|
|
86
98
|
expect.objectContaining({
|
|
87
99
|
id: 'seivaloper1def',
|
|
@@ -96,7 +108,7 @@ describe('staking/validators', () => {
|
|
|
96
108
|
})
|
|
97
109
|
|
|
98
110
|
expect(await getValidators(mockConfig, testLogger)).toEqual(first)
|
|
99
|
-
expect(
|
|
111
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
100
112
|
})
|
|
101
113
|
|
|
102
114
|
it('caches each cursor page under a separate key', async () => {
|
|
@@ -104,11 +116,11 @@ describe('staking/validators', () => {
|
|
|
104
116
|
|
|
105
117
|
await getValidators(mockConfig, testLogger)
|
|
106
118
|
await getValidators(mockConfig, testLogger, '5')
|
|
107
|
-
expect(
|
|
119
|
+
expect(validatorsRequests()).toHaveLength(2)
|
|
108
120
|
|
|
109
121
|
await getValidators(mockConfig, testLogger)
|
|
110
122
|
await getValidators(mockConfig, testLogger, '5')
|
|
111
|
-
expect(
|
|
123
|
+
expect(validatorsRequests()).toHaveLength(2)
|
|
112
124
|
})
|
|
113
125
|
|
|
114
126
|
it('clearValidatorsCache(chainId) evicts every cached page of that chain', async () => {
|
|
@@ -116,13 +128,13 @@ describe('staking/validators', () => {
|
|
|
116
128
|
|
|
117
129
|
await getValidators(mockConfig, testLogger)
|
|
118
130
|
await getValidators(mockConfig, testLogger, '5')
|
|
119
|
-
expect(
|
|
131
|
+
expect(validatorsRequests()).toHaveLength(2)
|
|
120
132
|
|
|
121
133
|
clearValidatorsCache(CHAIN_ID.SEI_EVM)
|
|
122
134
|
|
|
123
135
|
await getValidators(mockConfig, testLogger)
|
|
124
136
|
await getValidators(mockConfig, testLogger, '5')
|
|
125
|
-
expect(
|
|
137
|
+
expect(validatorsRequests()).toHaveLength(4)
|
|
126
138
|
})
|
|
127
139
|
|
|
128
140
|
it('returns cached data without a second network call while fresh', async () => {
|
|
@@ -131,7 +143,7 @@ describe('staking/validators', () => {
|
|
|
131
143
|
await getValidators(mockConfig, testLogger)
|
|
132
144
|
await getValidators(mockConfig, testLogger)
|
|
133
145
|
|
|
134
|
-
expect(
|
|
146
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
135
147
|
})
|
|
136
148
|
|
|
137
149
|
it('deduplicates concurrent in-flight fetches', async () => {
|
|
@@ -142,7 +154,7 @@ describe('staking/validators', () => {
|
|
|
142
154
|
getValidators(mockConfig, testLogger),
|
|
143
155
|
])
|
|
144
156
|
|
|
145
|
-
expect(
|
|
157
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
146
158
|
})
|
|
147
159
|
|
|
148
160
|
it('does not cache an empty validator list', async () => {
|
|
@@ -152,7 +164,7 @@ describe('staking/validators', () => {
|
|
|
152
164
|
await getValidators(mockConfig, testLogger)
|
|
153
165
|
|
|
154
166
|
// empty results are not cached, so each call hits the network again
|
|
155
|
-
expect(
|
|
167
|
+
expect(validatorsRequests()).toHaveLength(2)
|
|
156
168
|
})
|
|
157
169
|
|
|
158
170
|
it('returns an empty page for currencies without a validator API', async () => {
|
|
@@ -187,12 +199,12 @@ describe('staking/validators', () => {
|
|
|
187
199
|
],
|
|
188
200
|
next: undefined,
|
|
189
201
|
})
|
|
190
|
-
expect(
|
|
202
|
+
expect(validatorsRequests()).toHaveLength(2)
|
|
191
203
|
|
|
192
204
|
releaseFirst(cosmosValidatorsPayload)
|
|
193
205
|
await firstCall
|
|
194
206
|
|
|
195
|
-
expect(
|
|
207
|
+
expect(validatorsRequests()).toHaveLength(2)
|
|
196
208
|
})
|
|
197
209
|
})
|
|
198
210
|
|
|
@@ -218,7 +230,7 @@ describe('staking/validators', () => {
|
|
|
218
230
|
// non-zero base: lru-cache treats a start time of 0 as "no TTL"
|
|
219
231
|
setNow(1000)
|
|
220
232
|
await getValidators(mockConfig, testLogger)
|
|
221
|
-
expect(
|
|
233
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
222
234
|
|
|
223
235
|
// lru-cache debounces its clock read for `ttlResolution` (1ms) via a real
|
|
224
236
|
// setTimeout; wait it out so the next read picks up the advanced time.
|
|
@@ -227,7 +239,7 @@ describe('staking/validators', () => {
|
|
|
227
239
|
setNow(32000) // 31s later, past the 30s TTL
|
|
228
240
|
await getValidators(mockConfig, testLogger)
|
|
229
241
|
|
|
230
|
-
expect(
|
|
242
|
+
expect(validatorsRequests()).toHaveLength(2)
|
|
231
243
|
})
|
|
232
244
|
})
|
|
233
245
|
|
|
@@ -238,23 +250,23 @@ describe('staking/validators', () => {
|
|
|
238
250
|
prefetchValidators(mockConfig, testLogger)
|
|
239
251
|
await Promise.resolve()
|
|
240
252
|
await Promise.resolve()
|
|
241
|
-
expect(
|
|
253
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
242
254
|
|
|
243
255
|
// the warmed entry serves the subsequent read without another network call
|
|
244
256
|
await getValidators(mockConfig, testLogger)
|
|
245
|
-
expect(
|
|
257
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
246
258
|
})
|
|
247
259
|
|
|
248
260
|
it('is a no-op when cache is already fresh', async () => {
|
|
249
261
|
mockedNetwork.mockResolvedValue(cosmosValidatorsPayload)
|
|
250
262
|
|
|
251
263
|
await getValidators(mockConfig, testLogger)
|
|
252
|
-
expect(
|
|
264
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
253
265
|
|
|
254
266
|
prefetchValidators(mockConfig, testLogger)
|
|
255
267
|
await Promise.resolve()
|
|
256
268
|
|
|
257
|
-
expect(
|
|
269
|
+
expect(validatorsRequests()).toHaveLength(1)
|
|
258
270
|
})
|
|
259
271
|
})
|
|
260
272
|
|
|
@@ -312,7 +324,7 @@ describe('staking/validators', () => {
|
|
|
312
324
|
|
|
313
325
|
describe('getValidatorsPage', () => {
|
|
314
326
|
it('maps staking items to the generic Validator page shape', async () => {
|
|
315
|
-
|
|
327
|
+
mockSeiRest({
|
|
316
328
|
status: 200,
|
|
317
329
|
data: {
|
|
318
330
|
validators: [
|
|
@@ -338,7 +350,7 @@ describe('staking/validators', () => {
|
|
|
338
350
|
// 42 usei scaled to sei_evm's 18-decimal unit (×10^12).
|
|
339
351
|
balance: 42000000000000n,
|
|
340
352
|
commissionRate: '0.1',
|
|
341
|
-
apy: 0,
|
|
353
|
+
apy: 0.45,
|
|
342
354
|
},
|
|
343
355
|
])
|
|
344
356
|
})
|
package/src/types/staking.ts
CHANGED
|
@@ -118,6 +118,9 @@ export type StakingContractConfig = {
|
|
|
118
118
|
apiConfig?: {
|
|
119
119
|
baseUrl: string
|
|
120
120
|
validatorsEndpoint: string
|
|
121
|
+
/** Cosmos endpoints giving the yearly mint amount and the bonded tokens, used to derive the APY. */
|
|
122
|
+
mintEndpoint?: string
|
|
123
|
+
poolEndpoint?: string
|
|
121
124
|
/**
|
|
122
125
|
* Address precompile used to resolve the canonical Cosmos bech32 address
|
|
123
126
|
* for a given EVM address. Required for `cosmos-rest` redelegation
|
|
@@ -149,6 +152,13 @@ export type StakingContractConfig = {
|
|
|
149
152
|
validatorNameSource?: {
|
|
150
153
|
baseUrl: string
|
|
151
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Off-chain endpoint returning the network-wide staking rate as `{ data: { stakingApr } }`,
|
|
157
|
+
* a fraction before each validator's commission. Absence means the APY is unknown.
|
|
158
|
+
*/
|
|
159
|
+
stakingRateSource?: {
|
|
160
|
+
baseUrl: string
|
|
161
|
+
}
|
|
152
162
|
/** How to fetch active redelegations from an off-chain source. Defaults to `"none"` when absent. */
|
|
153
163
|
redelegationStrategy?: RedelegationStrategy
|
|
154
164
|
/** How to fetch pending unbondings from an off-chain source. Defaults to `"none"` when absent. */
|