Query the current active staking era on Avail DA using the staking_activeEra method.
On-chain name of method: staking_activeEra
Parameters
None
Returns
index: The index of the active era
start: The timestamp when the active era started
Minimal example (Fetch the active era)
Note
You will need to set up the dev environment required to run this example.
For instructions, check out our docs here.
If you're sending an extrinsic (i.e conducting a transaction) you will need to replace the demo seed phrase with your own seed phrase.
The rest of the code should work as is.
Inside your-file-name.ts, add the following code:
import { SDK, Pallets, BN } from "avail-js-sdk";export async function getActiveEra() { // Initialize SDK with Turing endpoint const sdk = await SDK.New("wss://turing-rpc.avail.so/ws"); // Get the current block hash const blockHash = await sdk.client.bestBlockHash(); console.log(`Current block hash: ${blockHash}`); // Get storage at the current block const storageAt = await sdk.client.storageAt(blockHash); // Fetch active era information const activeEra = await Pallets.StakingStorage.ActiveEra.fetch(storageAt); if (!activeEra) { console.log("No active era found"); process.exit(1); } // Log all properties of activeEra console.log("\nAll Active Era Properties:"); for (const [key, value] of Object.entries(activeEra)) { console.log(`${key}: ${value}`); } process.exit(0);}// Execute the functiongetActiveEra();