-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import inquirer from "inquirer"; | ||
|
||
import Program from "./command.js"; | ||
import { | ||
accountOption, | ||
chainOption, | ||
zeekOption, | ||
} from "../../common/options.js"; | ||
import { l2Chains } from "../../data/chains.js"; | ||
import { bigNumberToDecimal } from "../../utils/formatters.js"; | ||
import { | ||
getL2Provider, | ||
optionNameToParam, | ||
} from "../../utils/helpers.js"; | ||
import Logger from "../../utils/logger.js"; | ||
import { isAddress } from "../../utils/validators.js"; | ||
import zeek from "../../utils/zeek.js"; | ||
|
||
import type { BalanceOptions } from "../../common/options.js"; | ||
|
||
export const handler = async (options: BalanceOptions) => { | ||
try { | ||
const answers: BalanceOptions = await inquirer.prompt( | ||
[ | ||
{ | ||
message: chainOption.description, | ||
name: optionNameToParam(chainOption.long!), | ||
type: "list", | ||
choices: l2Chains.filter((e) => e.l1Chain).map((e) => ({ name: e.name, value: e.network })), | ||
required: true, | ||
when(answers: BalanceOptions) { | ||
if (answers.l1RpcUrl && answers.l2RpcUrl) { | ||
return false; | ||
} | ||
return true; | ||
}, | ||
}, | ||
{ | ||
message: accountOption.description, | ||
name: optionNameToParam(accountOption.long!), | ||
type: "input", | ||
required: true, | ||
validate: (input: string) => isAddress(input), | ||
}, | ||
], | ||
options | ||
); | ||
|
||
options = { | ||
...options, | ||
...answers, | ||
}; | ||
|
||
const toChain = l2Chains.find((e) => e.network === options.chain); | ||
const Provider = getL2Provider(options.l2RpcUrl ?? toChain!.rpcUrl); | ||
const balance = await Provider.getBalance(options.account ?? "Unknown account"); | ||
|
||
Logger.info(`\n${bigNumberToDecimal(balance)} ETH`); | ||
|
||
if (options.zeek) { | ||
zeek(); | ||
} | ||
} catch (error) { | ||
Logger.error("There was an error while fetching balance for the account:"); | ||
Logger.error(error); | ||
} | ||
}; | ||
|
||
Program.command("balance") | ||
.description("Get balance of an L2 or L1 account") | ||
.addOption(chainOption) | ||
.addOption(accountOption) | ||
.addOption(zeekOption) | ||
.action(handler); |
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,3 @@ | ||
import Program from "../../program.js"; | ||
|
||
export default Program.command("wallet").description("Manage wallet related features for L2 and L1"); |
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,3 @@ | ||
import "./balance.js"; | ||
|
||
import "./command.js"; // registers all the commands above |
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