From 285d043bf33531878b37c6024bafc3a915cfd048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dragi=C5=A1a=20Spasojevi=C4=87?= Date: Tue, 29 Oct 2024 11:57:12 +0100 Subject: [PATCH] update names and add jsdoc --- ...eateRollupEnoughCustomFeeTokenAllowance.ts | 4 ++-- ...ustomFeeTokenApprovalTransactionRequest.ts | 4 ++-- src/createTokenBridge.integration.test.ts | 6 ++--- ...okenBridgeEnoughCustomFeeTokenAllowance.ts | 4 ++-- ...ustomFeeTokenApprovalTransactionRequest.ts | 4 ++-- src/index.ts | 7 ++++++ src/utils/decimals.ts | 24 +++++++++++++++---- 7 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/createRollupEnoughCustomFeeTokenAllowance.ts b/src/createRollupEnoughCustomFeeTokenAllowance.ts index 735fd3ad..01dc88d6 100644 --- a/src/createRollupEnoughCustomFeeTokenAllowance.ts +++ b/src/createRollupEnoughCustomFeeTokenAllowance.ts @@ -6,7 +6,7 @@ import { getRollupCreatorAddress } from './utils/getRollupCreatorAddress'; import { Prettify } from './types/utils'; import { WithRollupCreatorAddressOverride } from './types/createRollupTypes'; import { createRollupGetRetryablesFeesWithDefaults } from './createRollupGetRetryablesFees'; -import { scaleToNativeTokenDecimals } from './utils/decimals'; +import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals'; export type CreateRollupEnoughCustomFeeTokenAllowanceParams = Prettify< @@ -43,5 +43,5 @@ export async function createRollupEnoughCustomFeeTokenAllowance= scaleToNativeTokenDecimals({ amount: fees, decimals }); + return allowance >= scaleFrom18DecimalsToNativeTokenDecimals({ amount: fees, decimals }); } diff --git a/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts b/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts index e3695e9f..d1015833 100644 --- a/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts +++ b/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts @@ -7,7 +7,7 @@ import { getRollupCreatorAddress } from './utils/getRollupCreatorAddress'; import { Prettify } from './types/utils'; import { WithRollupCreatorAddressOverride } from './types/createRollupTypes'; import { createRollupGetRetryablesFeesWithDefaults } from './createRollupGetRetryablesFees'; -import { scaleToNativeTokenDecimals } from './utils/decimals'; +import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals'; export type CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams< TChain extends Chain | undefined, @@ -48,7 +48,7 @@ export async function createRollupPrepareCustomFeeTokenApprovalTransactionReques address: nativeToken, owner: account, spender: rollupCreatorAddressOverride ?? getRollupCreatorAddress(publicClient), - amount: amount ?? scaleToNativeTokenDecimals({ amount: fees, decimals }), + amount: amount ?? scaleFrom18DecimalsToNativeTokenDecimals({ amount: fees, decimals }), publicClient, }); diff --git a/src/createTokenBridge.integration.test.ts b/src/createTokenBridge.integration.test.ts index 66dc038a..da2d3ba5 100644 --- a/src/createTokenBridge.integration.test.ts +++ b/src/createTokenBridge.integration.test.ts @@ -20,7 +20,7 @@ import { createTokenBridgePrepareSetWethGatewayTransactionRequest } from './crea import { createTokenBridgePrepareSetWethGatewayTransactionReceipt } from './createTokenBridgePrepareSetWethGatewayTransactionReceipt'; import { createTokenBridge } from './createTokenBridge'; import { TokenBridgeContracts } from './types/TokenBridgeContracts'; -import { scaleToNativeTokenDecimals } from './utils/decimals'; +import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals'; const testnodeAccounts = getNitroTestnodePrivateKeyAccounts(); const l2RollupOwner = testnodeAccounts.l2RollupOwner; @@ -223,7 +223,7 @@ describe('createTokenBridge utils function', () => { functionName: 'transfer', args: [ l3RollupOwner.address, - scaleToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }), + scaleFrom18DecimalsToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }), ], }), value: BigInt(0), @@ -392,7 +392,7 @@ describe('createTokenBridge', () => { functionName: 'transfer', args: [ l3RollupOwner.address, - scaleToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }), + scaleFrom18DecimalsToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }), ], }), value: BigInt(0), diff --git a/src/createTokenBridgeEnoughCustomFeeTokenAllowance.ts b/src/createTokenBridgeEnoughCustomFeeTokenAllowance.ts index 29b6b816..683aa792 100644 --- a/src/createTokenBridgeEnoughCustomFeeTokenAllowance.ts +++ b/src/createTokenBridgeEnoughCustomFeeTokenAllowance.ts @@ -6,7 +6,7 @@ import { createTokenBridgeDefaultRetryablesFees } from './constants'; import { Prettify } from './types/utils'; import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes'; import { getTokenBridgeCreatorAddress } from './utils/getTokenBridgeCreatorAddress'; -import { scaleToNativeTokenDecimals } from './utils/decimals'; +import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals'; export type CreateTokenBridgeEnoughCustomFeeTokenAllowanceParams = Prettify< @@ -39,7 +39,7 @@ export async function createTokenBridgeEnoughCustomFeeTokenAllowance< return ( allowance >= - scaleToNativeTokenDecimals({ + scaleFrom18DecimalsToNativeTokenDecimals({ amount: createTokenBridgeDefaultRetryablesFees, decimals, }) diff --git a/src/createTokenBridgePrepareCustomFeeTokenApprovalTransactionRequest.ts b/src/createTokenBridgePrepareCustomFeeTokenApprovalTransactionRequest.ts index a439bbe9..65334a62 100644 --- a/src/createTokenBridgePrepareCustomFeeTokenApprovalTransactionRequest.ts +++ b/src/createTokenBridgePrepareCustomFeeTokenApprovalTransactionRequest.ts @@ -7,7 +7,7 @@ import { validateParentChain } from './types/ParentChain'; import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes'; import { getTokenBridgeCreatorAddress } from './utils/getTokenBridgeCreatorAddress'; import { createTokenBridgeDefaultRetryablesFees } from './constants'; -import { scaleToNativeTokenDecimals } from './utils/decimals'; +import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals'; export type CreateTokenBridgePrepareCustomFeeTokenApprovalTransactionRequestParams< TChain extends Chain | undefined, @@ -42,7 +42,7 @@ export async function createTokenBridgePrepareCustomFeeTokenApprovalTransactionR spender: tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(publicClient), amount: amount ?? - scaleToNativeTokenDecimals({ + scaleFrom18DecimalsToNativeTokenDecimals({ amount: createTokenBridgeDefaultRetryablesFees, decimals, }), diff --git a/src/index.ts b/src/index.ts index ae67d37b..a95218d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -146,6 +146,10 @@ import { } from './wasmModuleRoot'; import { registerCustomParentChain } from './chains'; export * from './actions'; +import { + scaleFrom18DecimalsToNativeTokenDecimals, + scaleFromNativeTokenDecimalsTo18Decimals, +} from './utils/decimals'; export { arbOwnerPublicActions, @@ -267,4 +271,7 @@ export { GetConsensusReleaseByWasmModuleRoot, // registerCustomParentChain, + // + scaleFrom18DecimalsToNativeTokenDecimals, + scaleFromNativeTokenDecimalsTo18Decimals, }; diff --git a/src/utils/decimals.ts b/src/utils/decimals.ts index 49b9045e..c1090aaa 100644 --- a/src/utils/decimals.ts +++ b/src/utils/decimals.ts @@ -4,25 +4,41 @@ import { nativeTokenDecimalsTo18Decimals as ethers_nativeTokenDecimalsTo18Decimals, } from '@arbitrum/sdk/dist/lib/utils/lib'; -export function scaleToNativeTokenDecimals({ +/** + * Scales a value from 18 decimals to the number of decimals of the native token. + * + * @param param.amount The value to scale. + * @param param.decimals The number of decimals of the native token. + * + * @returns The scaled value. + */ +export function scaleFrom18DecimalsToNativeTokenDecimals({ amount, decimals, }: { amount: bigint; decimals: number; -}) { +}): bigint { const amountBigNumber = BigNumber.from(amount); const result = ethers_scaleToNativeTokenDecimals({ amount: amountBigNumber, decimals }); return BigInt(result.toString()); } -export function nativeTokenDecimalsTo18Decimals({ +/** + * Scales a value from the number of decimals of the native token to 18 decimals. + * + * @param param.amount The value to scale. + * @param param.decimals The number of decimals of the native token. + * + * @returns The scaled value. + */ +export function scaleFromNativeTokenDecimalsTo18Decimals({ amount, decimals, }: { amount: bigint; decimals: number; -}) { +}): bigint { const amountBigNumber = BigNumber.from(amount); const result = ethers_nativeTokenDecimalsTo18Decimals({ amount: amountBigNumber, decimals }); return BigInt(result.toString());