Skip to content

Commit

Permalink
feat: rename decimal utilities (#539)
Browse files Browse the repository at this point in the history
Rename `scaleToNativeTokenDecimals` to `scaleFrom18DecimalsToNativeTokenDecimals`
Rename `nativeTokenDecimalsTo18Decimals` to `scaleFromNativeTokenDecimalsTo18Decimals`
  • Loading branch information
chrstph-dvx authored Oct 30, 2024
1 parent 647d341 commit 30b3c57
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 44 deletions.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ export {
} from './lib/dataEntities/retryableData'

export { Address } from './lib/dataEntities/address'
export {
scaleFrom18DecimalsToNativeTokenDecimals,
scaleFromNativeTokenDecimalsTo18Decimals,
} from './lib/utils/lib'
8 changes: 4 additions & 4 deletions src/lib/assetBridger/erc20Bridger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import { L1ToL2MessageGasParams } from '../message/L1ToL2MessageCreator'
import {
getNativeTokenDecimals,
isArbitrumChain,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../utils/lib'
import { L2ERC20Gateway__factory } from '../abi/factories/L2ERC20Gateway__factory'

Expand Down Expand Up @@ -596,7 +596,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 @@ -1071,11 +1071,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<
l2Network: this.l2Network,
})

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/L1ToL2MessageGasEstimator.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 @@ -262,7 +262,7 @@ export class L1ToL2MessageGasEstimator {

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 @@ -41,7 +41,7 @@ import { itOnlyWhenEth } from './custom-fee-token/mochaExtensions'
import { L1TransactionReceipt } from '../../src'
import {
getNativeTokenDecimals,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../../src/lib/utils/lib'
import { parseUnits } from 'ethers/lib/utils'

Expand Down Expand Up @@ -392,7 +392,10 @@ describe('Ether', async () => {
).balanceOf(randomAddress)
: await l1Signer.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 { isL2NetworkWithCustomFeeToken } from './custom-fee-token/customFeeTokenTestHelpers'
import {
getNativeTokenDecimals,
scaleToNativeTokenDecimals,
scaleFrom18DecimalsToNativeTokenDecimals,
} from '../../src/lib/utils/lib'

describe('RevertData', () => {
Expand All @@ -54,7 +54,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 { isL2NetworkWithCustomFeeToken } 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 @@ -350,7 +350,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

0 comments on commit 30b3c57

Please sign in to comment.