-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSUB-638: Add chain/era info to the Status command (#1201)
* add: add --chain & --validator flag to status * rename status util to validatorStatus * rename getStatus to getValidatorStatus * add: implement cli-table for validatorStatus
- Loading branch information
Showing
6 changed files
with
107 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,45 @@ | ||
import { Command, OptionValues } from "commander"; | ||
import { newApi } from "../api"; | ||
import { getStatus, printValidatorStatus } from "../utils/status"; | ||
import { parseAddressOrExit, requiredInput } from "../utils/parsing"; | ||
import { | ||
getValidatorStatus, | ||
printValidatorStatus, | ||
} from "../utils/validatorStatus"; | ||
import { parseAddressOrExit, parseBoolean } from "../utils/parsing"; | ||
import { getChainStatus, printChainStatus } from "../utils/chainStatus"; | ||
|
||
export function makeStatusCommand() { | ||
const cmd = new Command("status"); | ||
cmd.description("Get staking status for an address"); | ||
cmd.option("-a, --address [address]", "Address to get status for"); | ||
cmd.option( | ||
"--validator [address]", | ||
"Validator stash address to get status for" | ||
); | ||
cmd.option("--chain", "Show chain status"); | ||
cmd.action(statusAction); | ||
return cmd; | ||
} | ||
|
||
async function statusAction(options: OptionValues) { | ||
const { api } = await newApi(options.url); | ||
|
||
// Check options | ||
const address = parseAddressOrExit( | ||
requiredInput( | ||
options.address, | ||
"Failed to show validator status: Must specify an address" | ||
) | ||
); | ||
const showValidatorStatus = parseBoolean(options.validator); | ||
let showChainStatus = parseBoolean(options.chain); | ||
|
||
if (!showValidatorStatus && !showChainStatus) { | ||
showChainStatus = true; | ||
} | ||
|
||
const validatorStatus = await getStatus(address, api); | ||
if (showChainStatus) { | ||
const chainStatus = await getChainStatus(api); | ||
printChainStatus(chainStatus); | ||
} | ||
|
||
await printValidatorStatus(validatorStatus, api); | ||
if (showValidatorStatus) { | ||
const validator = parseAddressOrExit(options.validator); | ||
const validatorStatus = await getValidatorStatus(validator, api); | ||
console.log(`Validator ${validator}:`); | ||
await printValidatorStatus(validatorStatus, api); | ||
} | ||
|
||
process.exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { ApiPromise } from "creditcoin-js"; | ||
import Table from "cli-table3"; | ||
|
||
interface ChainStatus { | ||
name: string; | ||
bestNumber: number; | ||
bestFinalizedNumber: number; | ||
eraInfo: EraInfo; | ||
} | ||
|
||
export async function getChainStatus(api: ApiPromise): Promise<ChainStatus> { | ||
const bestNumber = await api.derive.chain.bestNumber(); | ||
const bestFinalizedNumber = await api.derive.chain.bestNumberFinalized(); | ||
const eraInfo = await getEraInfo(api); | ||
return { | ||
name: api.runtimeVersion.specName.toString(), | ||
bestNumber: bestNumber.toNumber(), | ||
bestFinalizedNumber: bestFinalizedNumber.toNumber(), | ||
eraInfo, | ||
}; | ||
} | ||
|
||
interface EraInfo { | ||
era: number; | ||
currentSession: number; | ||
sessionsPerEra: number; | ||
} | ||
|
||
async function getEraInfo(api: ApiPromise): Promise<EraInfo> { | ||
const session = await api.derive.session.info(); | ||
return { | ||
era: session.activeEra.toNumber(), | ||
currentSession: | ||
(session.currentIndex.toNumber() % session.sessionsPerEra.toNumber()) + 1, | ||
sessionsPerEra: session.sessionsPerEra.toNumber(), | ||
}; | ||
} | ||
|
||
export function printChainStatus(status: ChainStatus) { | ||
const { eraInfo } = status; | ||
const table = new Table({ | ||
head: [status.name], | ||
}); | ||
|
||
table.push( | ||
["Best Block", status.bestNumber], | ||
["Best Finalized Block", status.bestFinalizedNumber], | ||
["Era", eraInfo.era], | ||
["Session", `${eraInfo.currentSession}/${eraInfo.sessionsPerEra}`] | ||
); | ||
console.log("Chain status:"); | ||
console.log(table.toString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters