Building one block at a time
Explore advanced Polkadot.js API features including staking, governance, identity, and complex blockchain interactions.
0 of 5 lessons completed
Complete previous lesson
Complete previous lesson
Complete previous lesson
Complete previous lesson
Learn to nominate validators, unbond, and read exposure data
Staking is the process of participating in the network's consensus mechanism by nominating validators and earning rewards.
// Staking operations
console.log('=== Staking APIs ===');
// Get current era
const currentEra = await api.query.staking.currentEra();
console.log('Current era:', currentEra.toString());
// Get active validators
const validators = await api.query.session.validators();
console.log('Active validators:', validators.length);
// Get validator exposure for current era
const validatorAddress = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const exposure = await api.query.staking.erasStakers(currentEra, validatorAddress);
console.log('Validator exposure:', exposure.toHuman());
// Get nominator's nominations
const nominatorAddress = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const nominations = await api.query.staking.nominators(nominatorAddress);
console.log('Nominations:', nominations.toHuman());
// Get staking rewards
const rewards = await api.query.staking.erasRewardPoints(currentEra);
console.log('Reward points:', rewards.toHuman());
// Nominate validators (example transaction)
const validatorsToNominate = [
'5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
'5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty'
];
const nominateTx = api.tx.staking.nominate(validatorsToNominate);
console.log('Nomination transaction created');
// Bond additional funds
const bondAmount = 1000000000000; // 1 WND
const bondTx = api.tx.staking.bondExtra(bondAmount);
console.log('Bond extra transaction created');
// Unbond funds
const unbondAmount = 500000000000; // 0.5 WND
const unbondTx = api.tx.staking.unbond(unbondAmount);
console.log('Unbond transaction created');
Create a function that displays staking information for a given address
Run your code to see the output here...