Skip to content

Commit edcc8bd

Browse files
authored
Merge pull request #44 from polkadot-api/vo/nominating-sub
fix(staking): `accountStatus$` not updating when starting a nomination
2 parents 350d959 + 60acb81 commit edcc8bd

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

packages/sdk-staking/CHANGELOG.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
## Unreleased
44

5-
## 0.1.0 2025-09-21
5+
### Fixed
6+
7+
- `accountStatus$` not updating when starting a nomination.
8+
- Receiving bonded rewards after calling `stopNomination`.
9+
10+
## 0.3.0 2025-10-22
11+
12+
### Added
13+
14+
- `upsertNomination` that batches a transaction with the required changes to a nomination.
15+
- `stopNomination` that batches a transaction to stop nominating.
16+
17+
### Fixed
18+
19+
- Remove console errors when an active validator has no points.
20+
21+
## 0.2.0 2025-10-21
22+
23+
### Added
24+
25+
- Expose active nominators through `getActiveNominators`
26+
27+
## 0.1.0 2025-10-21
628

729
Initial release

packages/sdk-staking/src/accountStatus.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { SS58String, TypedApi } from "polkadot-api"
22
import {
33
combineLatest,
4-
defer,
54
distinctUntilChanged,
65
map,
76
Observable,
87
share,
98
switchMap,
9+
takeWhile,
1010
} from "rxjs"
1111
import { Dot } from "../.papi/descriptors/dist"
1212
import { AccountStatus, StakingSdk } from "./sdk-types"
@@ -65,7 +65,12 @@ const getNomination$ = (
6565
addr: SS58String,
6666
balance$: Observable<AccountStatus["balance"]>,
6767
) => {
68-
const bonded$ = defer(() => api.query.Staking.Bonded.getValue(addr)).pipe(
68+
const bonded$ = api.query.Staking.Bonded.watchValue(addr).pipe(
69+
// Keep watching until we have a controller account.
70+
// We have to find a compromise on active storage subscriptions vs reactivity
71+
// For "Is nominating" we can just watch ledger, with the pre-condition that
72+
// Staking.Bonded has a controller.
73+
takeWhile((controller) => !controller, true),
6974
switchMap((controller) => {
7075
if (!controller) return [null]
7176
return api.query.Staking.Ledger.watchValue(controller).pipe(

packages/sdk-staking/src/nominationActions.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@ export const stopNominationFn = (
1515

1616
return (address) =>
1717
wrapAsyncTx(async () => {
18-
const [nominator, ledger] = await Promise.all([
18+
const [nominator, ledger, payee] = await Promise.all([
1919
api.query.Staking.Nominators.getValue(address),
2020
api.query.Staking.Bonded.getValue(address).then((controller) =>
2121
controller ? api.query.Staking.Ledger.getValue(controller) : null,
2222
),
23+
api.query.Staking.Payee.getValue(address),
2324
])
2425

2526
const txs: Transaction<any, any, any, any>[] = []
27+
// Chill only if it has targets selected
2628
if (nominator?.targets.length) {
2729
txs.push(api.tx.Staking.chill())
2830
}
31+
32+
// Unbond only if it has active bond
2933
const currentBond = ledger?.active ?? 0n
3034
if (currentBond > 0n) {
3135
txs.push(
@@ -35,6 +39,15 @@ export const stopNominationFn = (
3539
)
3640
}
3741

42+
// Move payee from staked to stash for the upcoming era payout
43+
if (payee?.type === "Staked") {
44+
txs.push(
45+
api.tx.Staking.set_payee({
46+
payee: StakingRewardDestination.Stash(),
47+
}),
48+
)
49+
}
50+
3851
if (txs.length === 0) {
3952
throw new Error(`Account ${address} is not nominating`)
4053
}

0 commit comments

Comments
 (0)