Fetch information on the active era
On-chain name of method: staking_activeEra
Parameters
avail-js
- None
Returns
index
: The index of the active erastart
: The timestamp when the active era started
Minimal example (Fetch the active era)
-
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.
avail-js
- Inside
your-file-name.ts
, add the following code:
avail-js
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 function
getActiveEra();
- Run the code using:
terminal
ts-node your-file-name.ts
Last updated on