-
Notifications
You must be signed in to change notification settings - Fork 204
feat: initialize viem package #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
douglance
wants to merge
12
commits into
main
Choose a base branch
from
dl/viem-package-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c88bc2e
feat: initialize viem package
douglance e74b931
Merge branch 'main' into dl/viem-package-init
douglance a10e420
update package name
douglance 757ac90
fixes issue with statefulness
douglance a7c2d6d
change name
douglance 37afa5f
add no op
douglance 5b8ea8b
update test runner
douglance 1262a9e
fix ci
douglance 44b85bd
simplify system
douglance ad08638
restore network register
douglance 3b25110
lint fix
douglance b61e1b5
Merge branch 'main' into dl/viem-package-init
fionnachan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { ethers } from 'ethers' | ||
import { | ||
Account, | ||
formatUnits, | ||
parseEther, | ||
parseUnits, | ||
type Chain, | ||
type Hex, | ||
type WalletClient, | ||
} from 'viem' | ||
|
||
import { | ||
getParentCustomFeeTokenAllowance, | ||
isArbitrumNetworkWithCustomFeeToken, | ||
} from '@arbitrum/sdk/tests/integration/custom-fee-token/customFeeTokenTestHelpers' | ||
import { EthBridger } from '@arbitrum/sdk/src' | ||
import { getNativeTokenDecimals } from '@arbitrum/sdk/src/lib/utils/lib' | ||
import { | ||
testSetup as _testSetup, | ||
config, | ||
getLocalNetworksFromFile, | ||
} from '@arbitrum/sdk/tests/testSetup' | ||
import { StaticJsonRpcProvider } from '@ethersproject/providers' | ||
|
||
const ethProvider = () => new StaticJsonRpcProvider(config.ethUrl) | ||
const arbProvider = () => new StaticJsonRpcProvider(config.arbUrl) | ||
const localNetworks = () => getLocalNetworksFromFile() | ||
|
||
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, | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
config, | ||
testSetup as sdkTestSetup, | ||
} from '@arbitrum/sdk/tests/testSetup' | ||
import { Address, Chain, createWalletClient, http } from 'viem' | ||
import { privateKeyToAccount } from 'viem/accounts' | ||
import { | ||
ArbitrumClients, | ||
createArbitrumClient, | ||
} from '../src/createArbitrumClient' | ||
|
||
export type ViemTestSetup = { | ||
localEthChain: Chain | ||
localArbChain: Chain | ||
parentAccount: ReturnType<typeof privateKeyToAccount> | ||
childPublicClient: ArbitrumClients['childPublicClient'] | ||
parentWalletClient: ArbitrumClients['parentWalletClient'] | ||
childChain: Awaited<ReturnType<typeof sdkTestSetup>>['childChain'] | ||
parentSigner: Awaited<ReturnType<typeof sdkTestSetup>>['parentSigner'] | ||
} | ||
|
||
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 | ||
} | ||
|
||
export async function testSetup(): Promise<ViemTestSetup> { | ||
const setup = await sdkTestSetup() | ||
|
||
const parentPrivateKey = setup.seed._signingKey().privateKey as Address | ||
const parentAccount = privateKeyToAccount(parentPrivateKey) | ||
|
||
// Generate Viem chains using the network data we already have | ||
const localEthChain = generateViemChain( | ||
{ | ||
chainId: setup.childChain.parentChainId, | ||
name: 'EthLocal', | ||
}, | ||
config.ethUrl | ||
) | ||
|
||
const localArbChain = generateViemChain( | ||
{ | ||
chainId: setup.childChain.chainId, | ||
name: setup.childChain.name, | ||
}, | ||
config.arbUrl | ||
) | ||
|
||
const baseParentWalletClient = createWalletClient({ | ||
account: parentAccount, | ||
chain: localEthChain, | ||
transport: http(config.ethUrl), | ||
}) | ||
|
||
const baseChildWalletClient = createWalletClient({ | ||
account: parentAccount, | ||
chain: localArbChain, | ||
transport: http(config.arbUrl), | ||
}) | ||
|
||
const { childPublicClient, parentWalletClient } = createArbitrumClient({ | ||
parentChain: localEthChain, | ||
childChain: localArbChain, | ||
parentWalletClient: baseParentWalletClient, | ||
childWalletClient: baseChildWalletClient, | ||
}) | ||
|
||
return { | ||
...setup, | ||
localEthChain, | ||
localArbChain, | ||
parentAccount, | ||
childPublicClient, | ||
parentWalletClient, | ||
} | ||
} | ||
|
||
export { config } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.