Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/sdk-staking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

## Unreleased

## 0.1.0 2025-09-21
### Fixed

- `accountStatus$` not updating when starting a nomination.
- Receiving bonded rewards after calling `stopNomination`.

## 0.3.0 2025-10-22

### Added

- `upsertNomination` that batches a transaction with the required changes to a nomination.
- `stopNomination` that batches a transaction to stop nominating.

### Fixed

- Remove console errors when an active validator has no points.

## 0.2.0 2025-10-21

### Added

- Expose active nominators through `getActiveNominators`

## 0.1.0 2025-10-21

Initial release
9 changes: 7 additions & 2 deletions packages/sdk-staking/src/accountStatus.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { SS58String, TypedApi } from "polkadot-api"
import {
combineLatest,
defer,
distinctUntilChanged,
map,
Observable,
share,
switchMap,
takeWhile,
} from "rxjs"
import { Dot } from "../.papi/descriptors/dist"
import { AccountStatus, StakingSdk } from "./sdk-types"
Expand Down Expand Up @@ -65,7 +65,12 @@ const getNomination$ = (
addr: SS58String,
balance$: Observable<AccountStatus["balance"]>,
) => {
const bonded$ = defer(() => api.query.Staking.Bonded.getValue(addr)).pipe(
const bonded$ = api.query.Staking.Bonded.watchValue(addr).pipe(
// Keep watching until we have a controller account.
// We have to find a compromise on active storage subscriptions vs reactivity
// For "Is nominating" we can just watch ledger, with the pre-condition that
// Staking.Bonded has a controller.
takeWhile((controller) => !controller, true),
switchMap((controller) => {
if (!controller) return [null]
return api.query.Staking.Ledger.watchValue(controller).pipe(
Expand Down
15 changes: 14 additions & 1 deletion packages/sdk-staking/src/nominationActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ export const stopNominationFn = (

return (address) =>
wrapAsyncTx(async () => {
const [nominator, ledger] = await Promise.all([
const [nominator, ledger, payee] = await Promise.all([
api.query.Staking.Nominators.getValue(address),
api.query.Staking.Bonded.getValue(address).then((controller) =>
controller ? api.query.Staking.Ledger.getValue(controller) : null,
),
api.query.Staking.Payee.getValue(address),
])

const txs: Transaction<any, any, any, any>[] = []
// Chill only if it has targets selected
if (nominator?.targets.length) {
txs.push(api.tx.Staking.chill())
}

// Unbond only if it has active bond
const currentBond = ledger?.active ?? 0n
if (currentBond > 0n) {
txs.push(
Expand All @@ -35,6 +39,15 @@ export const stopNominationFn = (
)
}

// Move payee from staked to stash for the upcoming era payout
if (payee?.type === "Staked") {
txs.push(
api.tx.Staking.set_payee({
payee: StakingRewardDestination.Stash(),
}),
)
}

if (txs.length === 0) {
throw new Error(`Account ${address} is not nominating`)
}
Expand Down