-
-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zksync,actions): add
getL1TokenAddress
public action
- Loading branch information
1 parent
04d3274
commit 61c12aa
Showing
8 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
"viem": minor | ||
--- | ||
|
||
Added `getL1TokenAddress` public action to the `ZKsync` extension |
This file contains 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,58 @@ | ||
--- | ||
description: Returns the L1 token address equivalent for a L2 token address as they are not equal. | ||
--- | ||
|
||
# getL1TokenAddress | ||
|
||
Returns the L1 token address equivalent for a L2 token address as they are not equal. | ||
|
||
:::info | ||
|
||
Only works for tokens bridged on default ZKsync Era bridges. | ||
|
||
::: | ||
|
||
## Usage | ||
|
||
:::code-group | ||
|
||
```ts [example.ts] | ||
import { account, publicClient } from './config' | ||
|
||
const address = await client.getL1TokenAddress({ | ||
token: '0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b' | ||
}) | ||
``` | ||
|
||
```ts [config.ts] | ||
import { createPublicClient, http } from 'viem' | ||
import { zksync } from 'viem/chains' | ||
import { publicActionsL2 } from 'viem/zksync' | ||
|
||
export const client = createPublicClient({ | ||
chain: zksync, | ||
transport: http(), | ||
}).extend(publicActionsL2()) | ||
``` | ||
|
||
::: | ||
|
||
## Returns | ||
|
||
`Address` | ||
|
||
Returns the L1 token address equivalent for a L2 token address. | ||
|
||
## Parameters | ||
|
||
### token | ||
|
||
- **Type:** `Address` | ||
|
||
The address of the token on L2. | ||
|
||
```ts | ||
const address = await client.getL1TokenAddress({ | ||
token: '0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b' | ||
}) | ||
``` |
This file contains 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 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,44 @@ | ||
import { expect, test } from 'vitest' | ||
import { daiL1 } from '../../../test/src/zksync.js' | ||
import { http, createClient } from '../../index.js' | ||
import { | ||
zksyncLocalCustomHyperchain, | ||
zksyncLocalHyperchain, | ||
} from '../chains.js' | ||
import { legacyEthAddress } from '../constants/address.js' | ||
import { getL1TokenAddress } from './getL1TokenAddress.js' | ||
import { getL2TokenAddress } from './getL2TokenAddress.js' | ||
|
||
const client = createClient({ | ||
chain: zksyncLocalHyperchain, | ||
transport: http(), | ||
}) | ||
|
||
const customChainClient = createClient({ | ||
chain: zksyncLocalCustomHyperchain, | ||
transport: http(), | ||
}) | ||
|
||
test('ETH: provided token address is L2 ETH address', async () => { | ||
expect(await getL1TokenAddress(client, { token: legacyEthAddress })).toBe( | ||
legacyEthAddress, | ||
) | ||
}) | ||
|
||
test('ETH: provided token address is L1 DAI address', async () => { | ||
const daiL2 = await getL2TokenAddress(client, { token: daiL1 }) | ||
expect(await getL1TokenAddress(client, { token: daiL2 })).toBe(daiL1) | ||
}) | ||
|
||
test('Custom: provided token address is L2 ETH address', async () => { | ||
expect(await getL1TokenAddress(client, { token: legacyEthAddress })).toBe( | ||
legacyEthAddress, | ||
) | ||
}) | ||
|
||
test('Custom: provided token address is L1 DAI address', async () => { | ||
const daiL2 = await getL2TokenAddress(customChainClient, { token: daiL1 }) | ||
expect(await getL1TokenAddress(customChainClient, { token: daiL2 })).toBe( | ||
daiL1, | ||
) | ||
}) |
This file contains 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,37 @@ | ||
import type { Address } from '../../accounts/index.js' | ||
import { readContract } from '../../actions/public/readContract.js' | ||
import type { Client } from '../../clients/createClient.js' | ||
import type { Transport } from '../../clients/transports/createTransport.js' | ||
import type { Account } from '../../types/account.js' | ||
import type { Chain } from '../../types/chain.js' | ||
import { isAddressEqual } from '../../utils/index.js' | ||
import { l2SharedBridgeAbi } from '../constants/abis.js' | ||
import { legacyEthAddress } from '../constants/address.js' | ||
import { getDefaultBridgeAddresses } from './getDefaultBridgeAddresses.js' | ||
|
||
export type GetL1TokenAddressParameters = { | ||
/** The address of the token on L2. */ | ||
token: Address | ||
} | ||
|
||
export type GetL1TokenAddressReturnType = Address | ||
|
||
export async function getL1TokenAddress< | ||
chain extends Chain | undefined, | ||
account extends Account | undefined, | ||
>( | ||
client: Client<Transport, chain, account>, | ||
parameters: GetL1TokenAddressParameters, | ||
): Promise<Address> { | ||
const { token } = parameters | ||
if (isAddressEqual(token, legacyEthAddress)) return legacyEthAddress | ||
|
||
const bridgeAddress = (await getDefaultBridgeAddresses(client)).sharedL2 | ||
|
||
return await readContract(client, { | ||
address: bridgeAddress, | ||
abi: l2SharedBridgeAbi, | ||
functionName: 'l1TokenAddress', | ||
args: [token], | ||
}) | ||
} |
This file contains 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 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 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