Skip to content

Commit

Permalink
feat: automate issue fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Dec 5, 2022
1 parent ec4bb7e commit 037b61f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 94 deletions.
23 changes: 0 additions & 23 deletions actions/index.ts

This file was deleted.

12 changes: 10 additions & 2 deletions actions/transferAll/ui.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import ora from "ora";
import prompts from "prompts";
import { ValidationError } from "yup";
import { detectAccountIssues, fixAccountIssues } from "../../issues";
import { addressSchema } from "../../schema";
import { Account } from "../../ui/pickAccounts";
import { transferAll } from "./core";

export async function showTransferAll(accounts: Account[]) {
export async function showTransferAll(
accounts: Account[],
networkId: "mainnet-alpha" | "goerli-alpha"
) {
const { toAddress } = await prompts(
{
type: "text",
Expand All @@ -28,7 +32,11 @@ export async function showTransferAll(accounts: Account[]) {
}
);

const spinner = ora("Transferring all tokens").start();
const spinner = ora("Detecting potential issues").start();

const issues = await detectAccountIssues(accounts);

await fixAccountIssues(accounts, networkId, issues);

const transferResults = await Promise.allSettled(
accounts.map(async (acc) => transferAll(acc, toAddress, spinner))
Expand Down
37 changes: 1 addition & 36 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import ora from "ora";
import { Account, pickAccounts } from "./ui/pickAccounts";
import { getAccountInfos } from "./getAccountsInfo";
import { getAccountsAndNetwork } from "./ui/getAccounts";
import {
detectAccountIssues,
fixAccountIssues,
selectAccountIssuesToFix,
} from "./issues";
import { display } from "./displayAccounts";
import { selectAction } from "./actions";
import { unionWith } from "lodash";
import { oraLog } from "./oraLog";
import { showTransferAll } from "./actions/transferAll/ui";
import { askForExtraAccounts, extraAccount } from "./ui/extraAccount";
import { equalSigner, getDefaultSigners } from "./genSigners";
Expand Down Expand Up @@ -93,37 +86,9 @@ program.parse();
network
);

const allAccounts = unionWith(
accountWithSigner,
filteredAccountWithSigner,
(a, b) => a.address === b.address
);

display(filteredAccountWithSigner);

const issues = await detectAccountIssues(filteredAccountWithSigner);

const issuesDetectedCount = Object.values(issues).reduce(
(acc, curr) => acc + curr.length,
0
);

const selectedAction = await selectAction(issuesDetectedCount);

switch (selectedAction) {
case "fixIssues": {
if (issuesDetectedCount) {
const fixIssues = await selectAccountIssuesToFix(issues);

return await fixAccountIssues(allAccounts, network, fixIssues);
}
}
case "transferAll": {
return await showTransferAll(filteredAccountWithSigner);
}
default:
process.exit(0);
}
await showTransferAll(filteredAccountWithSigner, network);
})().catch((e) => {
console.error("An error occured", e);
process.exit(1);
Expand Down
33 changes: 0 additions & 33 deletions issues/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,6 @@ export async function detectAccountIssues(
return { oldHashAlgo };
}

export async function selectAccountIssuesToFix(
issues: IssuesMap
): Promise<IssuesMap> {
const allIssues = uniqBy(
(Object.entries(issues) as [string, string[]][]).flatMap(([key, value]) =>
value.map((x) => [key, x] as const)
),
"1"
);
const { issuesToFix }: { issuesToFix: [keyof IssuesMap, string][] } =
await prompts(
{
type: "multiselect",
name: "issuesToFix",
message: "Choose issues to fix",
choices: allIssues.map(([issue, address]) => ({
title: `${issue}: ${truncateAddress(address)}`,
value: [issue, address],
selected: true,
})),
},
{ onCancel: () => process.exit(1) }
);

return issuesToFix.reduce(
(acc, [issue, address]) => ({
...acc,
[issue]: [...(acc[issue] ?? []), address],
}),
{} as IssuesMap
);
}

export async function fixAccountIssues(
accounts: Account[],
network: "mainnet-alpha" | "goerli-alpha",
Expand Down

0 comments on commit 037b61f

Please sign in to comment.