Skip to content
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

feat: introduce zksync bridges utility functions #2667

Open
wants to merge 33 commits into
base: main
Choose a base branch
from

Conversation

nikola-bozin-txfusion
Copy link
Contributor

@nikola-bozin-txfusion nikola-bozin-txfusion commented Aug 29, 2024

Overview

This PR introduces helper utility functions required for Deposit & Withdraw functionality.

Summary

  • Added new chain definitions.
  • Added bridge utility functions & included corresponding tests.
  • Added mock data required for testing.

PR-Codex overview

This PR adds support for zkSync CLI local nodes, implements bridge functionalities, and enhances testing coverage.

Detailed summary

  • Added zkSync CLI local node chains L1 and L2 definitions
  • Implemented bridge functions for address aliasing and token handling
  • Enhanced testing with new test cases

The following files were skipped due to too many changes: src/zksync/utils/bridge/isEthBasedChain.ts, src/zksync/utils/bridge/sharedBridge.ts, src/zksync/utils/bridge/getL2BridgeAddress.ts, src/zksync/utils/bridge/getBaseToken.ts, src/zksync/utils/bridge/getConfirmedTokens.ts, src/zksync/utils/bridge/getErc20ContractValue.ts, src/zksync/utils/bridge/getFeeParameters.ts, src/zksync/utils/bridge/l2TokenAddress.ts, src/zksync/utils/bridge/sharedBridge.test.ts, src/zksync/utils/bridge/sendRawTransactionWithDetailedOutput.test.ts, src/zksync/types/deposit.ts, src/zksync/utils/bridge/sendRawTransactionWithDetailedOutput.ts, src/zksync/types/eip1193.ts, src/zksync/utils/bridge/getBaseToken.test.ts, src/zksync/utils/bridge/getL2TransactionBaseCost.ts, src/zksync/utils/bridge/isWithdrawalFinalized.ts, src/zksync/utils/bridge/getProtocolVersion.ts, src/zksync/utils/bridge/getL2BridgeAddress.test.ts, src/zksync/utils/bridge/getProtocolVersion.test.ts, src/zksync/utils/bridge/requestL2TransactionDirect.ts, src/zksync/utils/bridge/isAddressEqualLite.test.ts, src/zksync/utils/bridge/approveL1Erc20Token.test.ts, src/zksync/utils/bridge/getAllowanceL1.ts, src/zksync/utils/bridge/requestL2TransactionTwoBridges.ts, src/zksync/utils/bridge/getL2TransactionBaseCost.test.ts, src/zksync/utils/bridge/getBalanceOfTokenL1.ts, src/zksync/utils/bridge/approveL1Erc20Token.ts, src/zksync/utils/bridge/getBalanceL1.ts, src/zksync/utils/bridge/requestL2TransactionDirect.test.ts, src/zksync/utils/bridge/requestL2TransactionTwoBridges.test.ts, src/zksync/utils/bridge/getErc20ContractValue.test.ts, src/zksync/utils/bridge/getAllowanceL1.test.ts, src/zksync/utils/bridge/getBalanceOfTokenL1.test.ts, src/zksync/utils/bridge/getBalanceL1.test.ts, test/src/zksyncPublicActionsL2MockData.ts, src/zksync/constants/abis.ts

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

changeset-bot bot commented Aug 29, 2024

⚠️ No Changeset found

Latest commit: 4b219e1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Aug 29, 2024

@nikola-bozin-txfusion is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@jxom jxom force-pushed the main branch 3 times, most recently from b56e162 to ad2831b Compare September 7, 2024 02:46
package.json Outdated
Comment on lines 206 to 208
"dependencies": {
"nopt": "^7.2.1"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"dependencies": {
"nopt": "^7.2.1"
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these chains "official" ZKsync local chains? If so, maybe better named as zksyncLocalChain.... Also, what is the difference between zkSyncChainL1 and zkSyncLocalNodeL1?

Comment on lines 6 to 15
export const REQUIRED_L2_GAS_PRICE_PER_PUBDATA = 800

export const REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT = 800n

export const L1_FEE_ESTIMATION_COEF_NUMERATOR = 12
export const L1_FEE_ESTIMATION_COEF_DENOMINATOR = 10

export const ADDRESS_MODULO = 2n ** 160n

export const DEFAULT_POLLING_INTERVAL_MS = 500
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constants need to be camelCase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/zksync/utils instead of src/zksync/bridge-utils to conform to codebase convention.


export type ApplyL1ToL2AliasReturnType = Address

export function applyL1ToL2Alias(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have some JSDoc with a brief explanation of what this utility does (and possibly a link to more info). I am not quite sure what this utility's purpose is by looking at it. By adding JSDoc, this means we can make this utility as part of the public API if we ever need to.

import type { PublicZkSyncRpcSchema } from '../types/eip1193.js'

export type SendRawTransactionWithDetailedOutputParameters = {
signedTx: Hex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
signedTx: Hex
serializedTransaction: Hex

Keep this consistent with Viem's sendRawTransaction

Comment on lines 16 to 37
export type TransactionWithDetailedOutput = {
transactionHash: Hash
storageLogs: Array<{
address: Address
key: string
writtenValue: string
}>
events: Array<{
address: Address
topics: Hex[]
data: Hex
blockHash: Hash | null
blockNumber: bigint | null
l1BatchNumber: bigint | null
transactionHash: Hash
transactionIndex: bigint
logIndex: bigint | null
transactionLogIndex: bigint | null
logType: string | null
removed: boolean
}>
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put this in src/zksync/types/transaction.ts?

export type DepositTransactionExtended = DepositTransaction & {
bridgehubContractAddress: Address
l2ChainId: bigint
eRC20DefaultBridgeData: Hex
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it "extended"? might be worth adding a comment with a link to docs if this is a zksync standard thing.

bridgeAddresses?: GetDefaultBridgeAddressesReturnType
}

export interface Overrides
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use type

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a bit confused why we even need Overrides. Why can't we explicitly state the properties instead of putting them in an overrides property? This is what we already do for core Viem Actions.

e.g.

approveErc20L1({
  // ...
  maxFeePerGas: 69420n
})

versus.

approveErc20L1({ 
  // ...
  overrides: { maxFeePerGas: 69420n }
})


export interface Overrides
extends Omit<ZksyncTransactionRequest, 'to' | 'data'> {
gasLimit: bigint
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't use gasLimit?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there is a stray column in the name too src/chains/definitions/zkSyncChainL2.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants