Skip to content

Commit

Permalink
feat: adds initial viem package
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Dec 19, 2024
1 parent d64f0f2 commit 7dbc8ce
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 531 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { StaticJsonRpcProvider } from '@ethersproject/providers'
import { Signer, Wallet, ethers, utils } from 'ethers'
import {
Account,
parseEther,
parseUnits,
type Hex,
type WalletClient,
type Chain,
formatUnits,
} from 'viem'

import {
testSetup as _testSetup,
Expand Down Expand Up @@ -67,7 +58,7 @@ export async function fundParentCustomFeeToken(

const tx = await tokenContract.transfer(
address,
utils.parseUnits('1000', decimals)
utils.parseUnits('10', decimals)
)
await tx.wait()
}
Expand Down Expand Up @@ -118,63 +109,3 @@ 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 {
// Convert to 18 decimals (ETH standard) for comparison
if (tokenDecimals === 18) return balanceDiff

// Convert to decimal string with proper precision
const formattedDiff = formatUnits(balanceDiff, 18)
// Parse back with target decimals
return parseUnits(formattedDiff, 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,
})
}
}
44 changes: 0 additions & 44 deletions packages/sdk/tests/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
isArbitrumNetworkWithCustomFeeToken,
} from './integration/custom-fee-token/customFeeTokenTestHelpers'
import { fundParentSigner } from './integration/testHelpers'
import { Chain } from 'viem'

loadEnv()

Expand Down Expand Up @@ -86,8 +85,6 @@ export const testSetup = async (): Promise<{
inboxTools: InboxTools
parentDeployer: Signer
childDeployer: Signer
localEthChain: Chain
localArbChain: Chain
}> => {
const ethProvider = new JsonRpcProvider(config.ethUrl)
const arbProvider = new JsonRpcProvider(config.arbUrl)
Expand Down Expand Up @@ -116,23 +113,6 @@ export const testSetup = async (): Promise<{

assertArbitrumNetworkHasTokenBridge(setChildChain)

// Generate Viem chains using the network data we already have
const localEthChain = generateViemChain(
{
chainId: setChildChain.parentChainId,
name: 'EthLocal',
},
config.ethUrl
)

const localArbChain = generateViemChain(
{
chainId: setChildChain.chainId,
name: setChildChain.name,
},
config.arbUrl
)

const erc20Bridger = new Erc20Bridger(setChildChain)
const adminErc20Bridger = new AdminErc20Bridger(setChildChain)
const ethBridger = new EthBridger(setChildChain)
Expand All @@ -156,8 +136,6 @@ export const testSetup = async (): Promise<{
inboxTools,
parentDeployer,
childDeployer,
localEthChain,
localArbChain,
}
}

Expand All @@ -175,25 +153,3 @@ export function getLocalNetworksFromFile(): {

return { l2Network: localL2, l3Network: localL3 }
}

function generateViemChain(
networkData: {
chainId: number
name: string
},
rpcUrl: string
): Chain {
return {
id: networkData.chainId,
name: networkData.name,
nativeCurrency: {
decimals: 18,
name: 'Ether',
symbol: 'ETH',
},
rpcUrls: {
default: { http: [rpcUrl] },
public: { http: [rpcUrl] },
},
} as const
}
224 changes: 0 additions & 224 deletions packages/viem/src/actions.ts

This file was deleted.

Loading

0 comments on commit 7dbc8ce

Please sign in to comment.