Skip to content

Commit

Permalink
chore: run formatter on project
Browse files Browse the repository at this point in the history
  • Loading branch information
itsacoyote committed Aug 16, 2024
1 parent 155e838 commit ed37a76
Show file tree
Hide file tree
Showing 63 changed files with 1,438 additions and 586 deletions.
4 changes: 3 additions & 1 deletion src/commands/bridge/command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Program from "../../program.js";

export default Program.command("bridge").description("Bridge operations (e.g. deposit, withdraw)");
export default Program.command("bridge").description(
"Bridge operations (e.g. deposit, withdraw)"
);
61 changes: 47 additions & 14 deletions src/commands/bridge/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inquirer from "inquirer";
import ora from "ora";

import Program from "./command.js";
import {
amountOptionCreate,
chainWithL1Option,
Expand All @@ -24,9 +23,14 @@ import {
} from "../../utils/helpers.js";
import Logger from "../../utils/logger.js";
import { getBalance, getTokenInfo } from "../../utils/token.js";
import { isDecimalAmount, isAddress, isPrivateKey } from "../../utils/validators.js";
import {
isAddress,
isDecimalAmount,
isPrivateKey,
} from "../../utils/validators.js";
import zeek from "../../utils/zeek.js";
import { getChains } from "../config/chains.js";
import Program from "./command.js";

import type { DefaultTransferOptions } from "../../common/options.js";

Expand All @@ -39,7 +43,10 @@ export const handler = async (options: DepositOptions) => {
try {
Logger.debug(
`Initial deposit options: ${JSON.stringify(
{ ...options, ...(options.privateKey ? { privateKey: "<hidden>" } : {}) },
{
...options,
...(options.privateKey ? { privateKey: "<hidden>" } : {}),
},
null,
2
)}`
Expand All @@ -52,7 +59,9 @@ export const handler = async (options: DepositOptions) => {
message: chainWithL1Option.description,
name: optionNameToParam(chainWithL1Option.long!),
type: "list",
choices: chains.filter((e) => e.l1Chain).map((e) => ({ name: e.name, value: e.network })),
choices: chains
.filter((e) => e.l1Chain)
.map((e) => ({ name: e.name, value: e.network })),
required: true,
when(answers: DepositOptions) {
if (answers.l1Rpc && answers.rpc) {
Expand Down Expand Up @@ -94,24 +103,42 @@ export const handler = async (options: DepositOptions) => {
...answers,
};

Logger.debug(`Final deposit options: ${JSON.stringify({ ...options, privateKey: "<hidden>" }, null, 2)}`);
Logger.debug(
`Final deposit options: ${JSON.stringify({ ...options, privateKey: "<hidden>" }, null, 2)}`
);

const fromChain = chains.find((e) => e.network === options.chain)?.l1Chain;
const fromChainLabel = fromChain && !options.l1Rpc ? fromChain.name : (options.l1Rpc ?? "Unknown chain");
const fromChainLabel =
fromChain && !options.l1Rpc
? fromChain.name
: (options.l1Rpc ?? "Unknown chain");
const toChain = chains.find((e) => e.network === options.chain);
const toChainLabel = toChain && !options.rpc ? toChain.name : (options.rpc ?? "Unknown chain");
const toChainLabel =
toChain && !options.rpc ? toChain.name : (options.rpc ?? "Unknown chain");

const l1Provider = getL1Provider(options.l1Rpc ?? fromChain!.rpcUrl);
const l2Provider = getL2Provider(options.rpc ?? toChain!.rpcUrl);
const senderWallet = getL2Wallet(options.privateKey, l2Provider, l1Provider);
const token = options.token ? await getTokenInfo(options.token!, l2Provider, l1Provider) : ETH_TOKEN;
const { decimalToBigNumber, bigNumberToDecimal } = useDecimals(token.decimals);
const senderWallet = getL2Wallet(
options.privateKey,
l2Provider,
l1Provider
);
const token = options.token
? await getTokenInfo(options.token!, l2Provider, l1Provider)
: ETH_TOKEN;
const { decimalToBigNumber, bigNumberToDecimal } = useDecimals(
token.decimals
);
if (!token.l1Address) {
throw new Error(`Token ${token.symbol} doesn't exist on ${fromChainLabel} therefore it cannot be deposited`);
throw new Error(
`Token ${token.symbol} doesn't exist on ${fromChainLabel} therefore it cannot be deposited`
);
}

Logger.info("\nDeposit:");
Logger.info(` From: ${getAddressFromPrivateKey(answers.privateKey)} (${fromChainLabel})`);
Logger.info(
` From: ${getAddressFromPrivateKey(answers.privateKey)} (${fromChainLabel})`
);
Logger.info(` To: ${options.recipient} (${toChainLabel})`);
Logger.info(
` Amount: ${bigNumberToDecimal(decimalToBigNumber(options.amount))} ${token.symbol} ${
Expand All @@ -133,10 +160,16 @@ export const handler = async (options: DepositOptions) => {
Logger.info("\nDeposit sent:");
Logger.info(` Transaction hash: ${depositHandle.hash}`);
if (fromChain?.explorerUrl) {
Logger.info(` Transaction link: ${fromChain.explorerUrl}/tx/${depositHandle.hash}`);
Logger.info(
` Transaction link: ${fromChain.explorerUrl}/tx/${depositHandle.hash}`
);
}

const senderBalance = await getBalance(token.l1Address, senderWallet.address, l1Provider);
const senderBalance = await getBalance(
token.l1Address,
senderWallet.address,
l1Provider
);
Logger.info(
`\nSender L1 balance after transaction: ${bigNumberToDecimal(senderBalance)} ${token.symbol} ${
token.name ? `(${token.name})` : ""
Expand Down
1 change: 0 additions & 1 deletion src/commands/bridge/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "./deposit.js";
import "./withdraw.js";
import "./withdraw-finalize.js";

import "./command.js"; // registers all the commands above
62 changes: 48 additions & 14 deletions src/commands/bridge/withdraw-finalize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Option } from "commander";
import inquirer from "inquirer";

import Program from "./command.js";
import {
chainWithL1Option,
l1RpcUrlOption,
Expand All @@ -24,10 +23,14 @@ import { getBalance } from "../../utils/token.js";
import { isPrivateKey, isTransactionHash } from "../../utils/validators.js";
import zeek from "../../utils/zeek.js";
import { getChains } from "../config/chains.js";
import Program from "./command.js";

import type { DefaultTransactionOptions } from "../../common/options.js";

const transactionHashOption = new Option("--hash <transaction_hash>", "L2 withdrawal transaction hash to finalize");
const transactionHashOption = new Option(
"--hash <transaction_hash>",
"L2 withdrawal transaction hash to finalize"
);

type WithdrawFinalizeOptions = DefaultTransactionOptions & {
hash: string;
Expand All @@ -37,7 +40,10 @@ export const handler = async (options: WithdrawFinalizeOptions) => {
try {
Logger.debug(
`Initial withdraw-finalize options: ${JSON.stringify(
{ ...options, ...(options.privateKey ? { privateKey: "<hidden>" } : {}) },
{
...options,
...(options.privateKey ? { privateKey: "<hidden>" } : {}),
},
null,
2
)}`
Expand All @@ -50,7 +56,9 @@ export const handler = async (options: WithdrawFinalizeOptions) => {
message: chainWithL1Option.description,
name: optionNameToParam(chainWithL1Option.long!),
type: "list",
choices: chains.filter((e) => e.l1Chain).map((e) => ({ name: e.name, value: e.network })),
choices: chains
.filter((e) => e.l1Chain)
.map((e) => ({ name: e.name, value: e.network })),
required: true,
when(answers: WithdrawFinalizeOptions) {
if (answers.l1Rpc && answers.rpc) {
Expand Down Expand Up @@ -82,22 +90,36 @@ export const handler = async (options: WithdrawFinalizeOptions) => {
...answers,
};

Logger.debug(`Final withdraw-finalize options: ${JSON.stringify({ ...options, privateKey: "<hidden>" }, null, 2)}`);
Logger.debug(
`Final withdraw-finalize options: ${JSON.stringify({ ...options, privateKey: "<hidden>" }, null, 2)}`
);

const fromChain = chains.find((e) => e.network === options.chain);
const fromChainLabel = fromChain && !options.rpc ? fromChain.name : (options.rpc ?? "Unknown chain");
const fromChainLabel =
fromChain && !options.rpc
? fromChain.name
: (options.rpc ?? "Unknown chain");
const toChain = chains.find((e) => e.network === options.chain)?.l1Chain;
const toChainLabel = toChain && !options.l1Rpc ? toChain.name : (options.l1Rpc ?? "Unknown chain");
const toChainLabel =
toChain && !options.l1Rpc
? toChain.name
: (options.l1Rpc ?? "Unknown chain");

Logger.info("\nWithdraw finalize:");
Logger.info(` From chain: ${fromChainLabel}`);
Logger.info(` To chain: ${toChainLabel}`);
Logger.info(` Withdrawal transaction (L2): ${options.hash}`);
Logger.info(` Finalizer address (L1): ${getAddressFromPrivateKey(answers.privateKey)}`);
Logger.info(
` Finalizer address (L1): ${getAddressFromPrivateKey(answers.privateKey)}`
);

const l1Provider = getL1Provider(options.l1Rpc ?? toChain!.rpcUrl);
const l2Provider = getL2Provider(options.rpc ?? fromChain!.rpcUrl);
const senderWallet = getL2Wallet(options.privateKey, l2Provider, l1Provider);
const senderWallet = getL2Wallet(
options.privateKey,
l2Provider,
l1Provider
);

Logger.info("\nChecking status of the transaction...");
const l2Details = await l2Provider.getTransactionDetails(options.hash);
Expand All @@ -109,26 +131,38 @@ export const handler = async (options: WithdrawFinalizeOptions) => {
Logger.error(
`\nTransaction is still being processed on ${fromChainLabel}, please try again when the ethExecuteTxHash has been computed`
);
Logger.info(`L2 Transaction Details: ${JSON.stringify(l2Details, null, 2)}`);
Logger.info(
`L2 Transaction Details: ${JSON.stringify(l2Details, null, 2)}`
);
return;
}
Logger.info("Transaction is ready to be finalized");

Logger.info("\nSending finalization transaction...");
const finalizationHandle = await senderWallet.finalizeWithdrawal(options.hash);
const finalizationHandle = await senderWallet.finalizeWithdrawal(
options.hash
);
Logger.info("\nWithdrawal finalized:");
Logger.info(` Finalization transaction hash: ${finalizationHandle.hash}`);
if (toChain?.explorerUrl) {
Logger.info(` Transaction link: ${toChain.explorerUrl}/tx/${finalizationHandle.hash}`);
Logger.info(
` Transaction link: ${toChain.explorerUrl}/tx/${finalizationHandle.hash}`
);
}

Logger.info("\nWaiting for finalization transaction to be mined...");
const receipt = await finalizationHandle.wait();
Logger.info(` Finalization transaction was mined in block ${receipt.blockNumber}`);
Logger.info(
` Finalization transaction was mined in block ${receipt.blockNumber}`
);

const token = ETH_TOKEN;
const { bigNumberToDecimal } = useDecimals(token.decimals);
const senderBalance = await getBalance(token.l1Address, senderWallet.address, l1Provider);
const senderBalance = await getBalance(
token.l1Address,
senderWallet.address,
l1Provider
);
Logger.info(
`\nSender L1 balance after transaction: ${bigNumberToDecimal(senderBalance)} ${token.symbol} ${
token.name ? `(${token.name})` : ""
Expand Down
72 changes: 56 additions & 16 deletions src/commands/bridge/withdraw.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import inquirer from "inquirer";
import ora from "ora";

import Program from "./command.js";
import {
amountOptionCreate,
chainWithL1Option,
Expand All @@ -24,9 +23,14 @@ import {
} from "../../utils/helpers.js";
import Logger from "../../utils/logger.js";
import { getBalance, getTokenInfo } from "../../utils/token.js";
import { isDecimalAmount, isAddress, isPrivateKey } from "../../utils/validators.js";
import {
isAddress,
isDecimalAmount,
isPrivateKey,
} from "../../utils/validators.js";
import zeek from "../../utils/zeek.js";
import { getChains } from "../config/chains.js";
import Program from "./command.js";

import type { DefaultTransferOptions } from "../../common/options.js";

Expand All @@ -39,7 +43,10 @@ export const handler = async (options: WithdrawOptions) => {
try {
Logger.debug(
`Initial withdraw options: ${JSON.stringify(
{ ...options, ...(options.privateKey ? { privateKey: "<hidden>" } : {}) },
{
...options,
...(options.privateKey ? { privateKey: "<hidden>" } : {}),
},
null,
2
)}`
Expand All @@ -52,7 +59,9 @@ export const handler = async (options: WithdrawOptions) => {
message: chainWithL1Option.description,
name: optionNameToParam(chainWithL1Option.long!),
type: "list",
choices: chains.filter((e) => e.l1Chain).map((e) => ({ name: e.name, value: e.network })),
choices: chains
.filter((e) => e.l1Chain)
.map((e) => ({ name: e.name, value: e.network })),
required: true,
when(answers: WithdrawOptions) {
if (answers.l1Rpc && answers.rpc) {
Expand Down Expand Up @@ -94,27 +103,49 @@ export const handler = async (options: WithdrawOptions) => {
...answers,
};

Logger.debug(`Final withdraw options: ${JSON.stringify({ ...options, privateKey: "<hidden>" }, null, 2)}`);
Logger.debug(
`Final withdraw options: ${JSON.stringify({ ...options, privateKey: "<hidden>" }, null, 2)}`
);

const fromChain = chains.find((e) => e.network === options.chain);
const fromChainLabel = fromChain && !options.rpc ? fromChain.name : (options.rpc ?? "Unknown chain");
const fromChainLabel =
fromChain && !options.rpc
? fromChain.name
: (options.rpc ?? "Unknown chain");
const toChain = chains.find((e) => e.network === options.chain)?.l1Chain;
const toChainLabel = toChain && !options.l1Rpc ? toChain.name : (options.l1Rpc ?? "Unknown chain");
const toChainLabel =
toChain && !options.l1Rpc
? toChain.name
: (options.l1Rpc ?? "Unknown chain");

const l1Provider = getL1Provider(options.l1Rpc ?? toChain!.rpcUrl);
const l2Provider = getL2Provider(options.rpc ?? fromChain!.rpcUrl);
const senderWallet = getL2Wallet(options.privateKey, l2Provider, l1Provider);
const token = options.token ? await getTokenInfo(options.token!, l2Provider, l1Provider) : ETH_TOKEN;
const { decimalToBigNumber, bigNumberToDecimal } = useDecimals(token.decimals);
const senderWallet = getL2Wallet(
options.privateKey,
l2Provider,
l1Provider
);
const token = options.token
? await getTokenInfo(options.token!, l2Provider, l1Provider)
: ETH_TOKEN;
const { decimalToBigNumber, bigNumberToDecimal } = useDecimals(
token.decimals
);
if (!token.l1Address) {
throw new Error(`Token ${token.symbol} doesn't exist on ${toChainLabel} therefore it cannot be withdrawn`);
throw new Error(
`Token ${token.symbol} doesn't exist on ${toChainLabel} therefore it cannot be withdrawn`
);
}
if (!token.address) {
throw new Error(`Token ${token.symbol} does not exist on ${fromChain?.name}`);
throw new Error(
`Token ${token.symbol} does not exist on ${fromChain?.name}`
);
}

Logger.info("\nWithdraw:");
Logger.info(` From: ${getAddressFromPrivateKey(answers.privateKey)} (${fromChainLabel})`);
Logger.info(
` From: ${getAddressFromPrivateKey(answers.privateKey)} (${fromChainLabel})`
);
Logger.info(` To: ${options.recipient} (${toChainLabel})`);
Logger.info(
` Amount: ${bigNumberToDecimal(decimalToBigNumber(options.amount))} ${token.symbol} ${
Expand All @@ -126,18 +157,27 @@ export const handler = async (options: WithdrawOptions) => {
try {
const withdrawHandle = await senderWallet.withdraw({
to: options.recipient,
token: token.address === ETH_TOKEN.address ? token.l1Address : token.address!,
token:
token.address === ETH_TOKEN.address
? token.l1Address
: token.address!,
amount: decimalToBigNumber(options.amount),
});
await withdrawHandle.wait();
spinner.stop();
Logger.info("\nWithdraw sent:");
Logger.info(` Transaction hash: ${withdrawHandle.hash}`);
if (fromChain?.explorerUrl) {
Logger.info(` Transaction link: ${fromChain.explorerUrl}/tx/${withdrawHandle.hash}`);
Logger.info(
` Transaction link: ${fromChain.explorerUrl}/tx/${withdrawHandle.hash}`
);
}

const senderBalance = await getBalance(token.address, senderWallet.address, l2Provider);
const senderBalance = await getBalance(
token.address,
senderWallet.address,
l2Provider
);
Logger.info(
`\nSender L2 balance after transaction: ${bigNumberToDecimal(senderBalance)} ${token.symbol} ${
token.name ? `(${token.name})` : ""
Expand Down
Loading

0 comments on commit ed37a76

Please sign in to comment.