Skip to content

Commit

Permalink
fix custom fee handling
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Nov 26, 2024
1 parent b78ae37 commit 350f5f7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
24 changes: 21 additions & 3 deletions tests/integration/arbitrumDepositActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import {
approveParentCustomFeeToken,
fundParentCustomFeeToken,
isArbitrumNetworkWithCustomFeeToken,
getAmountInEnvironmentDecimals,
normalizeBalanceDiffByDecimals as normalizeBalanceDiffFByDecimals,
approveCustomFeeTokenWithViem,
} from './custom-fee-token/customFeeTokenTestHelpers'

describe('arbitrumDepositActions', function () {
describe.only('arbitrumDepositActions', function () {
let localEthChain: Chain
let localArbChain: Chain
let setup: Awaited<ReturnType<typeof testSetup>>
Expand All @@ -31,7 +34,10 @@ describe('arbitrumDepositActions', function () {

it('deposits ETH from parent to child using deposit action', async function () {
const parentAccount = privateKeyToAccount(`0x${config.ethKey}`)
const depositAmount = parseEther('0.01')

const [depositAmount, tokenDecimals] = await getAmountInEnvironmentDecimals(
'0.01'
)

const baseParentWalletClient = createWalletClient({
account: parentAccount,
Expand All @@ -56,6 +62,12 @@ describe('arbitrumDepositActions', function () {
address: parentAccount.address,
})

await approveCustomFeeTokenWithViem({
parentAccount,
parentWalletClient,
chain: localEthChain,
})

const result = await parentWalletClient.depositEth({
amount: depositAmount,
account: parentAccount,
Expand All @@ -68,7 +80,13 @@ describe('arbitrumDepositActions', function () {
})

const balanceDiff = finalBalance - initialBalance
expect(balanceDiff).to.equal(depositAmount)

const normalizedBalanceDiff = normalizeBalanceDiffFByDecimals(
balanceDiff,
tokenDecimals
)

expect(normalizedBalanceDiff.toString()).to.equal(depositAmount.toString())
})

it('handles deposit failure gracefully', async function () {
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/custom-fee-token/customFeeTokenTestHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { StaticJsonRpcProvider } from '@ethersproject/providers'
import { Signer, Wallet, ethers, utils } from 'ethers'
import {
Account,
parseEther,
parseUnits,
type Hex,
type WalletClient,
type Chain,
} from 'viem'

import {
testSetup as _testSetup,
Expand Down Expand Up @@ -109,3 +117,57 @@ export async function fundChildCustomFeeToken(childSigner: Signer) {
})
await tx.wait()
}

export async function getAmountInEnvironmentDecimals(
amount: string
): Promise<[bigint, number]> {
if (isArbitrumNetworkWithCustomFeeToken()) {
const tokenDecimals = await getNativeTokenDecimals({
parentProvider: ethProvider(),
childNetwork: localNetworks().l3Network!,
})
return [parseUnits(amount, tokenDecimals), tokenDecimals]
}
return [parseEther(amount), 18] // ETH decimals
}

export function normalizeBalanceDiffByDecimals(
balanceDiff: bigint,
tokenDecimals: number
): bigint {
return balanceDiff / BigInt(10 ** (18 - tokenDecimals))
}

export async function approveCustomFeeTokenWithViem({
parentAccount,
parentWalletClient,
chain,
}: {
parentAccount: { address: string }
parentWalletClient: WalletClient
chain: Chain
}) {
if (!isArbitrumNetworkWithCustomFeeToken()) return

const networks = localNetworks()
const inbox = networks.l3Network!.ethBridge.inbox

const currentAllowance = await getParentCustomFeeTokenAllowance(
parentAccount.address,
inbox
)

// Only approve if allowance is insufficient
if (currentAllowance.lt(ethers.constants.MaxUint256)) {
const ethBridger = await EthBridger.fromProvider(arbProvider())
const approveRequest = ethBridger.getApproveGasTokenRequest()
await parentWalletClient.sendTransaction({
to: approveRequest.to as Hex,
data: approveRequest.data as Hex,
account: parentAccount as Account,
chain,
value: BigInt(0),
kzg: undefined,
})
}
}

0 comments on commit 350f5f7

Please sign in to comment.