Skip to content

Commit

Permalink
Calculate fee amount denominated in fee token decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
gvladika committed May 7, 2024
1 parent d9142a0 commit f22e38e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
55 changes: 22 additions & 33 deletions scripts/atomicTokenBridgeDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { ContractVerifier } from './contractVerifier'
import { OmitTyped } from '@arbitrum/sdk/dist/lib/utils/types'
import { L1ToL2MessageGasParams } from '@arbitrum/sdk/dist/lib/message/L1ToL2MessageCreator'
import { L1ContractCallTransactionReceipt } from '@arbitrum/sdk/dist/lib/message/L1Transaction'
import { _getScaledAmount } from './local-deployment/localDeploymentLib'

/**
* Dummy non-zero address which is provided to logic contracts initializers
Expand Down Expand Up @@ -112,10 +113,12 @@ export const createTokenBridge = async (
const maxSubmissionCostForContracts =
deployFactoryGasParams.maxSubmissionCost.mul(2)

let retryableFee = maxSubmissionCostForFactory
.add(maxSubmissionCostForContracts)
.add(maxGasForFactory.mul(gasPrice))
.add(maxGasForContracts.mul(gasPrice))
let retryableFeeForFactory = maxSubmissionCostForFactory.add(
maxGasForFactory.mul(gasPrice)
)
let retryableFeeForContracts = maxSubmissionCostForContracts.add(
maxGasForContracts.mul(gasPrice)
)

// get inbox from rollup contract
const inbox = await RollupAdminLogic__factory.connect(
Expand All @@ -126,20 +129,24 @@ export const createTokenBridge = async (
// if fee token is used approve the fee
const feeToken = await _getFeeToken(inbox, l1Signer.provider!)
if (feeToken != ethers.constants.AddressZero) {
// scale the retryable fee to the fee token decimals denomination
const scaledRetryableFee = await _getScaledAmount(
// scale the retryable fees to the fee token decimals denomination
let scaledRetryableFeeForFactory = await _getScaledAmount(
feeToken,
retryableFeeForFactory,
l1Signer.provider!
)
let scaledRetryableFeeForContracts = await _getScaledAmount(
feeToken,
retryableFee,
retryableFeeForContracts,
l1Signer.provider!
)

await (
await IERC20__factory.connect(feeToken, l1Signer).approve(
l1TokenBridgeCreator.address,
scaledRetryableFee
scaledRetryableFeeForFactory.add(scaledRetryableFeeForContracts)
)
).wait()
retryableFee = BigNumber.from(0)
}

/// do it - create token bridge
Expand All @@ -149,7 +156,12 @@ export const createTokenBridge = async (
rollupOwnerAddress,
maxGasForContracts,
gasPrice,
{ value: retryableFee }
{
value:
feeToken == ethers.constants.AddressZero
? retryableFeeForFactory.add(retryableFeeForContracts)
: BigNumber.from(0),
}
)
).wait()

Expand Down Expand Up @@ -708,29 +720,6 @@ const _getFeeToken = async (
return feeToken
}

/**
* Scale the amount from 18-denomination to the fee token decimals denomination
*/
async function _getScaledAmount(
feeToken: string,
amount: BigNumber,
provider: ethers.providers.Provider
): Promise<BigNumber> {
const decimals = await ERC20__factory.connect(feeToken, provider).decimals()
if (decimals == 18) {
return amount
} else if (decimals < 18) {
let scaledAmount = amount.div(BigNumber.from(10).pow(18 - decimals))
// round up if necessary
if (scaledAmount.mul(BigNumber.from(10).pow(18 - decimals)).lt(amount)) {
scaledAmount = scaledAmount.add(1)
}
return scaledAmount
} else {
return amount.mul(BigNumber.from(10).pow(decimals - 18))
}
}

export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}
26 changes: 25 additions & 1 deletion scripts/local-deployment/localDeploymentLib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ethers } from 'ethers'
import { BigNumber, ethers } from 'ethers'
import { JsonRpcProvider } from '@ethersproject/providers'
import { L1Network, L2Network, addCustomNetwork } from '@arbitrum/sdk'
import { Bridge__factory } from '@arbitrum/sdk/dist/lib/abi/factories/Bridge__factory'
Expand All @@ -11,6 +11,7 @@ import {
getEstimateForDeployingFactory,
} from '../atomicTokenBridgeDeployer'
import { l2Networks } from '@arbitrum/sdk/dist/lib/dataEntities/networks'
import { ERC20__factory } from '../../build/types'

const LOCALHOST_L2_RPC = 'http://localhost:8547'
const LOCALHOST_L3_RPC = 'http://localhost:3347'
Expand Down Expand Up @@ -258,3 +259,26 @@ export const getLocalNetworks = async (
l2Network,
}
}

/**
* Scale the amount from 18-denomination to the fee token decimals denomination
*/
export async function _getScaledAmount(
feeToken: string,
amount: BigNumber,
provider: ethers.providers.Provider
): Promise<BigNumber> {
const decimals = await ERC20__factory.connect(feeToken, provider).decimals()
if (decimals == 18) {
return amount
} else if (decimals < 18) {
let scaledAmount = amount.div(BigNumber.from(10).pow(18 - decimals))
// round up if necessary
if (scaledAmount.mul(BigNumber.from(10).pow(18 - decimals)).lt(amount)) {
scaledAmount = scaledAmount.add(1)
}
return scaledAmount
} else {
return amount.mul(BigNumber.from(10).pow(decimals - 18))
}
}

0 comments on commit f22e38e

Please sign in to comment.