diff --git a/src/commands/bridge/deposit.ts b/src/commands/bridge/deposit.ts index 4ea4d27..60f3b46 100644 --- a/src/commands/bridge/deposit.ts +++ b/src/commands/bridge/deposit.ts @@ -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`); } diff --git a/src/commands/bridge/withdraw-finalize.ts b/src/commands/bridge/withdraw-finalize.ts index fa877bc..2bcc6cc 100644 --- a/src/commands/bridge/withdraw-finalize.ts +++ b/src/commands/bridge/withdraw-finalize.ts @@ -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} ${ diff --git a/src/commands/bridge/withdraw.ts b/src/commands/bridge/withdraw.ts index 052132a..64055a0 100644 --- a/src/commands/bridge/withdraw.ts +++ b/src/commands/bridge/withdraw.ts @@ -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`); } diff --git a/src/commands/transaction/info.ts b/src/commands/transaction/info.ts index ce4c0cc..a6b6684 100644 --- a/src/commands/transaction/info.ts +++ b/src/commands/transaction/info.ts @@ -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 = ""; diff --git a/src/commands/wallet/balance.ts b/src/commands/wallet/balance.ts index 52dc0d7..38e4d8f 100644 --- a/src/commands/wallet/balance.ts +++ b/src/commands/wallet/balance.ts @@ -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( diff --git a/src/commands/wallet/transfer.ts b/src/commands/wallet/transfer.ts index 6ecd059..83dc5f7 100644 --- a/src/commands/wallet/transfer.ts +++ b/src/commands/wallet/transfer.ts @@ -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}`); } diff --git a/src/utils/formatters.ts b/src/utils/formatters.ts index 089534d..d5660d6 100644 --- a/src/utils/formatters.ts +++ b/src/utils/formatters.ts @@ -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 { diff --git a/src/utils/validators.ts b/src/utils/validators.ts index 68f2ca8..e27b956 100644 --- a/src/utils/validators.ts +++ b/src/utils/validators.ts @@ -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; } diff --git a/tests/utils/formatters.spec.ts b/tests/utils/formatters.spec.ts index 75e0dac..24eacee 100644 --- a/tests/utils/formatters.spec.ts +++ b/tests/utils/formatters.spec.ts @@ -7,20 +7,20 @@ 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(); expect(decimalToBigNumber("1.5").toString()).toEqual("1500000000000000000"); }); test("USDC decimal value", () => { - const [decimalToBigNumber] = useDecimals(6); + const { decimalToBigNumber } = useDecimals(6); expectTypeOf(decimalToBigNumber("1.5")).toEqualTypeOf(); expect(decimalToBigNumber("1.5").toString()).toEqual("1500000"); }); @@ -28,7 +28,7 @@ describe("useDecimals", () => { 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(); @@ -43,7 +43,7 @@ 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(); @@ -51,7 +51,7 @@ describe("useDecimals", () => { }); 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();