Skip to content

Commit

Permalink
refactor: rename decimal utilities
Browse files Browse the repository at this point in the history
Rename scaleToNativeTokenDecimals to scaleFrom18DecimalsToNativeTokenDecimals
Rename nativeTokenDecimalsTo18Decimals to scaleFromNativeTokenDecimalsTo18Decimals
  • Loading branch information
chrstph-dvx committed Oct 29, 2024
1 parent 91562af commit 25fb2eb
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/lib/assetBridger/erc20Bridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ import { ParentToChildMessageGasParams } from '../message/ParentToChildMessageCr
import {
getNativeTokenDecimals,
isArbitrumChain,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../utils/lib'
import { L2ERC20Gateway__factory } from '../abi/factories/L2ERC20Gateway__factory'
import { getErc20ParentAddressFromParentToChildTxRequest } from '../utils/calldata'
Expand Down Expand Up @@ -626,7 +626,7 @@ export class Erc20Bridger extends AssetBridger<
// callHookData
'0x',
// nativeTokenTotalFee
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: depositParams.gasLimit
.mul(depositParams.maxFeePerGas)
.add(depositParams.maxSubmissionCost), // will be zero
Expand Down Expand Up @@ -1134,11 +1134,11 @@ export class AdminErc20Bridger extends Erc20Bridger {
setTokenGas.gasLimit,
setGatewayGas.gasLimit,
doubleFeePerGas,
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: setTokenDeposit,
decimals: nativeTokenDecimals,
}),
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: setGatewayDeposit,
decimals: nativeTokenDecimals,
}),
Expand Down
11 changes: 6 additions & 5 deletions src/lib/assetBridger/ethBridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { ERC20__factory } from '../abi/factories/ERC20__factory'
import {
getNativeTokenDecimals,
isArbitrumChain,
nativeTokenDecimalsTo18Decimals,
scaleFromNativeTokenDecimalsTo18Decimals,
} from '../utils/lib'

export type ApproveGasTokenParams = {
Expand Down Expand Up @@ -321,10 +321,11 @@ export class EthBridger extends AssetBridger<
childNetwork: this.childNetwork,
})

const amountToBeMintedOnChildChain = nativeTokenDecimalsTo18Decimals({
amount: params.amount,
decimals,
})
const amountToBeMintedOnChildChain =
scaleFromNativeTokenDecimalsTo18Decimals({
amount: params.amount,
decimals,
})

const requestParams = {
...params,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/message/ParentToChildMessageGasEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
getBaseFee,
getNativeTokenDecimals,
isDefined,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../utils/lib'
import { OmitTyped } from '../utils/types'
import {
Expand Down Expand Up @@ -271,7 +271,7 @@ export class ParentToChildMessageGasEstimator {

const deposit =
options?.deposit?.base ||
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: gasLimit
.mul(maxFeePerGas)
.add(maxSubmissionFee)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/utils/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export async function getNativeTokenDecimals({
}
}

export function scaleToNativeTokenDecimals({
export function scaleFrom18DecimalsToNativeTokenDecimals({
amount,
decimals,
}: {
Expand Down Expand Up @@ -253,7 +253,7 @@ export function scaleToNativeTokenDecimals({
return amount.mul(BigNumber.from(10).pow(BigNumber.from(decimals - 18)))
}

export function nativeTokenDecimalsTo18Decimals({
export function scaleFromNativeTokenDecimalsTo18Decimals({
amount,
decimals,
}: {
Expand Down
7 changes: 5 additions & 2 deletions tests/integration/eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { itOnlyWhenEth } from './custom-fee-token/mochaExtensions'
import { ParentTransactionReceipt } from '../../src'
import {
getNativeTokenDecimals,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../../src/lib/utils/lib'
import { parseUnits } from 'ethers/lib/utils'

Expand Down Expand Up @@ -458,7 +458,10 @@ describe('Ether', async () => {
).balanceOf(randomAddress)
: await parentSigner.provider!.getBalance(randomAddress)
expect(finalRandomBalance.toString(), 'L1 final balance').to.eq(
scaleToNativeTokenDecimals({ amount: ethToWithdraw, decimals }).toString()
scaleFrom18DecimalsToNativeTokenDecimals({
amount: ethToWithdraw,
decimals,
}).toString()
)
})
})
4 changes: 2 additions & 2 deletions tests/integration/retryableData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { ERC20Inbox__factory } from '../../src/lib/abi/factories/ERC20Inbox__fac
import { isArbitrumNetworkWithCustomFeeToken } from './custom-fee-token/customFeeTokenTestHelpers'
import {
getNativeTokenDecimals,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../../src/lib/utils/lib'

describe('RevertData', () => {
Expand All @@ -57,7 +57,7 @@ describe('RevertData', () => {
l2CallValue,
data: hexlify(randomBytes(32)),
maxSubmissionCost: maxSubmissionCost,
value: scaleToNativeTokenDecimals({
value: scaleFrom18DecimalsToNativeTokenDecimals({
amount: l2CallValue
.add(maxSubmissionCost)
.add(RetryableDataTools.ErrorTriggeringParams.gasLimit)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import { ArbSdkError } from '../../src/lib/dataEntities/errors'
import { ERC20 } from '../../src/lib/abi/ERC20'
import { isArbitrumNetworkWithCustomFeeToken } from './custom-fee-token/customFeeTokenTestHelpers'
import { ERC20__factory } from '../../src/lib/abi/factories/ERC20__factory'
import { scaleToNativeTokenDecimals } from '../../src/lib/utils/lib'
import { scaleFrom18DecimalsToNativeTokenDecimals } from '../../src/lib/utils/lib'

const preFundAmount = parseEther('0.1')

Expand Down Expand Up @@ -355,7 +355,7 @@ export const depositToken = async ({

const MAX_BASE_ESTIMATED_GAS_FEE = BigNumber.from(1_000_000_000_000_000)

const maxScaledEstimatedGasFee = scaleToNativeTokenDecimals({
const maxScaledEstimatedGasFee = scaleFrom18DecimalsToNativeTokenDecimals({
amount: MAX_BASE_ESTIMATED_GAS_FEE,
decimals: feeTokenDecimals,
})
Expand Down
56 changes: 31 additions & 25 deletions tests/unit/nativeToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { expect } from 'chai'
import { BigNumber } from 'ethers'
import { parseEther } from 'ethers/lib/utils'
import {
nativeTokenDecimalsTo18Decimals,
scaleToNativeTokenDecimals,
scaleFromNativeTokenDecimalsTo18Decimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../../src/lib/utils/lib'

const AMOUNT_TO_SCALE = parseEther('1.23456789')
Expand All @@ -18,31 +18,34 @@ describe('Native token', () => {

it('scales to native token decimals', () => {
expect(
scaleToNativeTokenDecimals({ amount: AMOUNT_TO_SCALE, decimals: 18 }).eq(
BigNumber.from('1234567890000000000')
),
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 18,
}).eq(BigNumber.from('1234567890000000000')),
decimalsToError(18)
).to.be.true

// Rounds up the last digit - in this case no decimals so rounds up 1 to 2
expect(
scaleToNativeTokenDecimals({ amount: AMOUNT_TO_SCALE, decimals: 0 }).eq(
BigNumber.from('2')
),
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 0,
}).eq(BigNumber.from('2')),
decimalsToError(0)
).to.be.true

// Rounds up the last digit
expect(
scaleToNativeTokenDecimals({ amount: AMOUNT_TO_SCALE, decimals: 1 }).eq(
BigNumber.from('13')
),
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 1,
}).eq(BigNumber.from('13')),
decimalsToError(1)
).to.be.true

// Rounds up the last digit
expect(
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 6,
}).eq(BigNumber.from('1234568')),
Expand All @@ -51,31 +54,34 @@ describe('Native token', () => {

// Rounds up the last digit
expect(
scaleToNativeTokenDecimals({ amount: AMOUNT_TO_SCALE, decimals: 7 }).eq(
BigNumber.from('12345679')
),
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 7,
}).eq(BigNumber.from('12345679')),
decimalsToError(7)
).to.be.true

// Does not round up the last digit because all original decimals are included
expect(
scaleToNativeTokenDecimals({ amount: AMOUNT_TO_SCALE, decimals: 8 }).eq(
BigNumber.from('123456789')
),
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 8,
}).eq(BigNumber.from('123456789')),
decimalsToError(8)
).to.be.true

// Does not round up the last digit because all original decimals are included
expect(
scaleToNativeTokenDecimals({ amount: AMOUNT_TO_SCALE, decimals: 9 }).eq(
BigNumber.from('1234567890')
),
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 9,
}).eq(BigNumber.from('1234567890')),
decimalsToError(9)
).to.be.true

// Does not round up the last digit because all original decimals are included
expect(
scaleToNativeTokenDecimals({
scaleFrom18DecimalsToNativeTokenDecimals({
amount: AMOUNT_TO_SCALE,
decimals: 24,
}).eq(BigNumber.from('1234567890000000000000000')),
Expand All @@ -85,23 +91,23 @@ describe('Native token', () => {

it('scales native token decimals to 18 decimals', () => {
expect(
nativeTokenDecimalsTo18Decimals({
scaleFromNativeTokenDecimalsTo18Decimals({
amount: AMOUNT_TO_SCALE,
decimals: 16,
}).eq(BigNumber.from('123456789000000000000')),
decimalsToError(16)
).to.be.true

expect(
nativeTokenDecimalsTo18Decimals({
scaleFromNativeTokenDecimalsTo18Decimals({
amount: AMOUNT_TO_SCALE,
decimals: 18,
}).eq(BigNumber.from('1234567890000000000')),
decimalsToError(18)
).to.be.true

expect(
nativeTokenDecimalsTo18Decimals({
scaleFromNativeTokenDecimalsTo18Decimals({
amount: AMOUNT_TO_SCALE,
decimals: 20,
}).eq(BigNumber.from('12345678900000000')),
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2760,10 +2760,10 @@ electron-to-chromium@^1.4.535:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.556.tgz#97385917eb6ea3ac6a3378cf87bb39ee1db96e76"
integrity sha512-6RPN0hHfzDU8D56E72YkDvnLw5Cj2NMXZGg3UkgyoHxjVhG99KZpsKgBWMmTy0Ei89xwan+rbRsVB9yzATmYzQ==

[email protected], [email protected].6, elliptic@^6.5.2, elliptic@^6.5.4:
version "6.5.6"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.6.tgz#ee5f7c3a00b98a2144ac84d67d01f04d438fa53e"
integrity sha512-mpzdtpeCLuS3BmE3pO3Cpp5bbjlOPY2Q0PgoF+Od1XZrHLYI28Xe3ossCmYCQt11FQKEYd9+PF8jymTvtWJSHQ==
[email protected], [email protected].7, elliptic@^6.5.2, elliptic@^6.5.4:
version "6.5.7"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.7.tgz#8ec4da2cb2939926a1b9a73619d768207e647c8b"
integrity sha512-ESVCtTwiA+XhY3wyh24QqRGBoP3rEdDUl3EDUUo9tft074fi19IrdpH7hLCMMP3CIj7jb3W96rn8lt/BqIlt5Q==
dependencies:
bn.js "^4.11.9"
brorand "^1.1.0"
Expand Down

0 comments on commit 25fb2eb

Please sign in to comment.