Skip to content

Commit

Permalink
feat: adds viem package
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Dec 18, 2024
1 parent 48cecb6 commit d36d679
Show file tree
Hide file tree
Showing 11 changed files with 772 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"ts-node": "^10.2.1",
"tslint": "^6.1.3",
"typechain": "7.0.0",
"typescript": "^4.9.5",
"typescript": "^5.6.3",
"yargs": "^17.3.1"
},
"resolutions": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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 @@ -58,7 +67,7 @@ export async function fundParentCustomFeeToken(

const tx = await tokenContract.transfer(
address,
utils.parseUnits('10', decimals)
utils.parseUnits('1000', decimals)
)
await tx.wait()
}
Expand Down Expand Up @@ -109,3 +118,64 @@ 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()
// @ts-ignore
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: 44 additions & 0 deletions packages/sdk/tests/testSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
isArbitrumNetworkWithCustomFeeToken,
} from './integration/custom-fee-token/customFeeTokenTestHelpers'
import { fundParentSigner } from './integration/testHelpers'
import { Chain } from 'viem'

loadEnv()

Expand Down Expand Up @@ -85,6 +86,8 @@ 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 @@ -113,6 +116,23 @@ 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 @@ -136,6 +156,8 @@ export const testSetup = async (): Promise<{
inboxTools,
parentDeployer,
childDeployer,
localEthChain,
localArbChain,
}
}

Expand All @@ -153,3 +175,25 @@ 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
}
32 changes: 32 additions & 0 deletions packages/viem/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@arbitrum/viem-sdk",
"version": "4.0.2",
"description": "Typescript library client-side interactions with Arbitrum using viem",
"author": "Offchain Labs, Inc.",
"license": "Apache-2.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/offchainlabs/arbitrum-sdk.git"
},
"engines": {
"node": ">=v11",
"npm": "please-use-yarn",
"yarn": ">= 1.0.0"
},
"bugs": {
"url": "https://github.com/offchainlabs/arbitrum-sdk/issues"
},
"homepage": "https://offchainlabs.com",
"files": [
"dist/**/*"
],
"scripts": {
"build": "rm -rf dist && tsc -p tsconfig.json",
"test": "mocha tests"
},
"dependencies": {
"viem": "^2.21.55"
}
}
Loading

0 comments on commit d36d679

Please sign in to comment.