Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSUB-556: Add Status check to Withdraw Unbonded command #1149

Merged
merged 6 commits into from
Jun 22, 2023
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
19 changes: 18 additions & 1 deletion scripts/cc-cli/src/commands/withdrawUnbonded.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, OptionValues } from "commander";
import { newApi } from "../api";
import { getStatus, requireStatus } from "../utils/status";
import {
getControllerSeedFromEnvOrPrompt,
initKeyringPair,
Expand All @@ -8,7 +9,6 @@ import {
export function makeWithdrawUnbondedCommand() {
const cmd = new Command("withdraw-unbonded");
cmd.description("Withdraw unbonded funds from a stash account");
cmd.option("-a, --amount [amount]", "Amount to withdraw");
cmd.action(withdrawUnbondedAction);
return cmd;
}
Expand All @@ -18,6 +18,23 @@ async function withdrawUnbondedAction(options: OptionValues) {

const controllerSeed = await getControllerSeedFromEnvOrPrompt();
const controller = initKeyringPair(controllerSeed);

atodorov marked this conversation as resolved.
Show resolved Hide resolved
const controllerStatus = await getStatus(controller.address, api);

if (!controllerStatus.stash) {
console.error(
`Could not find stash account associated with the provided controller address: ${controller.address}. Please ensure the address is actually a controller.`
);
process.exit(1);
}

const status = await getStatus(controllerStatus.stash, api);
requireStatus(
status,
"canWithdraw",
"Cannot perform action, there are no unlocked funds to withdraw"
);

const slashingSpans = await api.query.staking.slashingSpans(
controller.address
);
Expand Down
43 changes: 40 additions & 3 deletions scripts/cc-cli/src/utils/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function getStatus(address: string, api: ApiPromise) {
: undefined;

const unlockingRes = res.stakingLedger.unlocking;
const currentEra = await api.query.staking.currentEra();
const currentEra = (await api.query.staking.currentEra()).unwrap();
const unlocking = unlockingRes
? unlockingRes.filter((u: any) => u.era > currentEra)
: [];
Expand All @@ -41,6 +41,18 @@ export async function getStatus(address: string, api: ApiPromise) {
? readAmountFromHex(res.redeemable.toString())
: new BN(0);

const readyForWithdraw = res.stakingLedger.unlocking
.map((u: any) => {
const chunk: UnlockChunk = {
era: u.era.toNumber(),
value: u.value.toBn(),
};
return chunk;
})
.filter((u: UnlockChunk) => u.era < currentEra.toNumber());

const canWithdraw = readyForWithdraw.length > 0;

const nextUnbondingDate =
unlocking.length > 0 ? unlocking[0].era.toNumber() : null;

Expand Down Expand Up @@ -71,6 +83,8 @@ export async function getStatus(address: string, api: ApiPromise) {
validating: validatorEntries.includes(address),
waiting: waitingValidators.includes(address),
active: activeValidators.includes(address),
canWithdraw,
readyForWithdraw,
nextUnbondingDate,
nextUnbondingAmount: nextUnbondingAmount ? nextUnbondingAmount : new BN(0),
redeemable,
Expand All @@ -87,6 +101,16 @@ export async function printValidatorStatus(status: Status, api: ApiPromise) {
console.log("Waiting: ", status.waiting);
console.log("Active: ", status.active);

console.log("Can withdraw: ", status.canWithdraw);
if (status.canWithdraw) {
console.log("Unlocked chunks: ");
status.readyForWithdraw.forEach((chunk) => {
console.log(
` ${toCTCString(chunk.value)} unlocked since era ${chunk.era}`
);
});
}

let nextUnlocking = "None";
if (status.nextUnbondingAmount && status.nextUnbondingAmount.eq(new BN(0))) {
nextUnlocking = "None";
Expand All @@ -100,10 +124,16 @@ export async function printValidatorStatus(status: Status, api: ApiPromise) {
console.log(`Next unbonding chunk: ${nextUnlocking}`);
}

export function requireStatus(status: Status, condition: keyof Status) {
export function requireStatus(
status: Status,
condition: keyof Status,
message?: string
) {
if (!status[condition]) {
console.error(
`Cannot perform action, validator is not ${condition.toString()}`
message
? message
: `Cannot perform action, validator is not ${condition.toString()}`
);
process.exit(1);
}
Expand All @@ -116,11 +146,18 @@ export interface Status {
validating: boolean;
waiting: boolean;
active: boolean;
canWithdraw: boolean;
readyForWithdraw: UnlockChunk[];
nextUnbondingDate: Option<EraNumber>;
nextUnbondingAmount: Option<Balance>;
redeemable: Balance;
}

interface UnlockChunk {
era: EraNumber;
value: Balance;
}

type Balance = BN;

type EraNumber = number;
Expand Down
Loading