Skip to content

Commit

Permalink
fix: reformat formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
itsacoyote committed Jul 26, 2024
1 parent 093141d commit f5bdd51
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/commands/bridge/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const handler = async (options: DepositOptions) => {
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 { decimalToBigNumber, bigNumberToDecimal } = useDecimals(token.decimals);
if (!token.l1Address) {
throw new Error(`Token ${token.symbol} doesn't exist on ${fromChainLabel} therefore it cannot be deposited`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/commands/bridge/withdraw-finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ export const handler = async (options: WithdrawFinalizeOptions) => {
Logger.info(` Finalization transaction was mined in block ${receipt.blockNumber}`);

const token = ETH_TOKEN;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, bigNumberToDecimal] = useDecimals(token.decimals);
const { bigNumberToDecimal } = useDecimals(token.decimals);
const senderBalance = await getBalance(token.l1Address, senderWallet.address, l1Provider);
Logger.info(
`\nSender L1 balance after transaction: ${bigNumberToDecimal(senderBalance)} ${token.symbol} ${
Expand Down
2 changes: 1 addition & 1 deletion src/commands/bridge/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const handler = async (options: WithdrawOptions) => {
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 { decimalToBigNumber, bigNumberToDecimal } = useDecimals(token.decimals);
if (!token.l1Address) {
throw new Error(`Token ${token.symbol} doesn't exist on ${toChainLabel} therefore it cannot be withdrawn`);
}
Expand Down
3 changes: 1 addition & 2 deletions src/commands/transaction/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ export const handler = async (options: TransactionInfoOptions) => {
return;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, bigNumberToDecimal] = useDecimals(ETH_TOKEN.decimals);
const { bigNumberToDecimal } = useDecimals(ETH_TOKEN.decimals);

Logger.info(formatSeparator("Main info").line, { noFormat: true });
let logString = "";
Expand Down
3 changes: 1 addition & 2 deletions src/commands/wallet/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ export const handler = async (options: BalanceOptions) => {
throw new Error(`Token ${token.symbol} does not exist on ${selectedChain?.name}`);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, bigNumberToDecimal] = useDecimals(token.decimals);
const { bigNumberToDecimal } = useDecimals(token.decimals);

const balance = await getBalance(token.address, options.address!, l2Provider);
Logger.info(
Expand Down
2 changes: 1 addition & 1 deletion src/commands/wallet/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const handler = async (options: TransferOptions) => {
const l2Provider = getL2Provider(options.rpc ?? selectedChain!.rpcUrl);
const senderWallet = getL2Wallet(options.privateKey, l2Provider);
const token = options.token ? await getTokenInfo(options.token!, l2Provider) : ETH_TOKEN;
const [decimalToBigNumber, bigNumberToDecimal] = useDecimals(token.decimals);
const { decimalToBigNumber, bigNumberToDecimal } = useDecimals(token.decimals);
if (!token.address) {
throw new Error(`Token ${token.symbol} does not exist on ${selectedChain?.name}`);
}
Expand Down
17 changes: 9 additions & 8 deletions src/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import type { BigNumberish } from "ethers/lib/ethers.js";
* Sets the number of decimals for a token format conversion.
*
* @param decimals - The number of decimals to use.
* @returns A tuple with two functions: one to convert a decimal string to a BigNumber and another to convert a BigNumber to a decimal string.
*
* [decimalToBigNumber, bigNumberToDecimal]
* @returns An object with two functions: one to convert a decimal string to a BigNumber and another to convert a BigNumber to a decimal string.
*/
export function useDecimals(decimals: number): [(amount: string) => BigNumberish, (amount: BigNumberish) => string] {
return [
(amount: string) => decimalToBigNumber(amount, decimals),
(amount: BigNumberish) => bigNumberToDecimal(amount, decimals),
];
export function useDecimals(decimals: number): {
decimalToBigNumber: (amount: string) => BigNumberish;
bigNumberToDecimal: (amount: BigNumberish) => string;
} {
return {
decimalToBigNumber: (amount: string) => decimalToBigNumber(amount, decimals),
bigNumberToDecimal: (amount: BigNumberish) => bigNumberToDecimal(amount, decimals),
};
}

function decimalToBigNumber(amount: string, decimals: number): BigNumberish {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useDecimals } from "./formatters.js";

export const isDecimalAmount = (amount: string, decimals = ETH_TOKEN.decimals) => {
try {
const [decimalToBigNumber] = useDecimals(decimals);
const { decimalToBigNumber } = useDecimals(decimals);
if (BigNumber.isBigNumber(decimalToBigNumber(amount))) {
return true;
}
Expand Down
16 changes: 8 additions & 8 deletions tests/utils/formatters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import type { BigNumberish } from "ethers";
describe("useDecimals", () => {
test("returns two functions in an array", () => {
const useDecimalsResult = useDecimals(1.005);
expectTypeOf(useDecimalsResult).toBeArray();
expectTypeOf(useDecimalsResult[0]).toBeFunction();
expectTypeOf(useDecimalsResult[1]).toBeFunction();
expectTypeOf(useDecimalsResult).toBeObject();
expectTypeOf(useDecimalsResult.bigNumberToDecimal).toBeFunction();
expectTypeOf(useDecimalsResult.decimalToBigNumber).toBeFunction();
});

describe("decimalToBigNumber", () => {
test("ETH decimal value", () => {
const [decimalToBigNumber] = useDecimals(18);
const { decimalToBigNumber } = useDecimals(18);
expectTypeOf(decimalToBigNumber("1.5")).toEqualTypeOf<BigNumberish>();
expect(decimalToBigNumber("1.5").toString()).toEqual("1500000000000000000");
});

test("USDC decimal value", () => {
const [decimalToBigNumber] = useDecimals(6);
const { decimalToBigNumber } = useDecimals(6);
expectTypeOf(decimalToBigNumber("1.5")).toEqualTypeOf<BigNumberish>();
expect(decimalToBigNumber("1.5").toString()).toEqual("1500000");
});
});

describe("bigNumberToDecimal", () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, bigNumberToDecimal] = useDecimals(12);
const { bigNumberToDecimal } = useDecimals(12);

test("string argument", () => {
expectTypeOf(bigNumberToDecimal("5")).toEqualTypeOf<string>();
Expand All @@ -43,15 +43,15 @@ describe("useDecimals", () => {

describe("switcheroo", () => {
test("convert from decimal to bigNumber back to decimal", () => {
const [decimalToBigNumber, bigNumberToDecimal] = useDecimals(12);
const { decimalToBigNumber, bigNumberToDecimal } = useDecimals(12);
const testValue = "4.2";
const result = bigNumberToDecimal(decimalToBigNumber(testValue));
expectTypeOf(result).toEqualTypeOf<string>();
expect(result).toEqual("4.2");
});

test("convert from bigNumber to decimal back to bigNumber", () => {
const [decimalToBigNumber, bigNumberToDecimal] = useDecimals(12);
const { decimalToBigNumber, bigNumberToDecimal } = useDecimals(12);
const testValue = "470000";
const result = decimalToBigNumber(bigNumberToDecimal(testValue));
expectTypeOf(result).toEqualTypeOf<BigNumberish>();
Expand Down

0 comments on commit f5bdd51

Please sign in to comment.