Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-poems-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

**ZKsync**: Added `getGasPerPubdata` Action.
17 changes: 17 additions & 0 deletions src/zksync/actions/getGasPerPubdata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from 'vitest'
import {
mockClientPublicActionsL2,
zksyncClientLocalNode,
} from '../../../test/src/zksync.js'
import { gasPerPubdataDefault } from '../constants/number.js'
import { getGasPerPubdata } from './getGasPerPubdata.js'

const client = { ...zksyncClientLocalNode }

mockClientPublicActionsL2(client)

test('default', async () => {
const fee = await getGasPerPubdata(client)

expect(fee).toEqual(gasPerPubdataDefault)
})
26 changes: 26 additions & 0 deletions src/zksync/actions/getGasPerPubdata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Client } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import type { Account } from '../../types/account.js'
import { hexToBigInt } from '../../utils/encoding/fromHex.js'
import type { ChainEIP712 } from '../types/chain.js'
import type { PublicZksyncRpcSchema } from '../types/eip1193.js'

export type GetGasPerPubdataReturnType = bigint

export async function getGasPerPubdata<
chain extends ChainEIP712 | undefined,
account extends Account | undefined,
>(
client: Client<Transport, chain, account, PublicZksyncRpcSchema>,
): Promise<GetGasPerPubdataReturnType> {
const result = await client.request(
{
method: 'zks_gasPerPubdata',
},
{
dedupe: true,
},
)

return hexToBigInt(result)
}
6 changes: 6 additions & 0 deletions src/zksync/decorators/publicL2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type { GetAllBalancesReturnType } from '../actions/getAllBalances.js'
import { getLogProof } from '../actions/getLogProof.js'
import { getTransactionDetails } from '../actions/getTransactionDetails.js'
import { zksyncLocalNode } from '../chains.js'
import { gasPerPubdataDefault } from '../constants/number.js'
import { publicActionsL2 } from './publicL2.js'

const mockedZksyncClient = createPublicClient({
Expand Down Expand Up @@ -257,3 +258,8 @@ test.skip('getL1TokenAddress', async () => {
}),
).toBeDefined()
})

test('getGasPerPubdata', async () => {
const gasPerPubdata = await mockedZksyncClient.getGasPerPubdata()
expect(gasPerPubdata).to.equal(gasPerPubdataDefault)
})
24 changes: 24 additions & 0 deletions src/zksync/decorators/publicL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import {
type GetDefaultBridgeAddressesReturnType,
getDefaultBridgeAddresses,
} from '../actions/getDefaultBridgeAddresses.js'
import {
type GetGasPerPubdataReturnType,
getGasPerPubdata,
} from '../actions/getGasPerPubdata.js'
import {
type GetL1BatchBlockRangeParameters,
type GetL1BatchBlockRangeReturnParameters,
Expand Down Expand Up @@ -471,6 +475,25 @@ export type PublicActionsL2<
getL1TokenAddress: (
args: GetL1TokenAddressParameters,
) => Promise<GetL1TokenAddressReturnType>

/**
* Returns the scaled gas per pubdata limit for the currently open batch. Available since node version 28.7.0.
*
* @returns the scaled gas per pubdata limit for the currently open batch
*
* @example
* import { createPublicClient, http } from 'viem'
* import { zksyncLocalNode } from 'viem/chains'
* import { publicActionsL2 } from 'viem/zksync'
*
* const client = createPublicClient({
* chain: zksyncLocalNode,
* transport: http(),
* }).extend(publicActionsL2())
*
* const gasPerPubdata = await client.getGasPerPubdata();
*/
getGasPerPubdata: () => Promise<GetGasPerPubdataReturnType>
}

export function publicActionsL2() {
Expand Down Expand Up @@ -500,6 +523,7 @@ export function publicActionsL2() {
getBaseTokenL1Address: () => getBaseTokenL1Address(client),
getL2TokenAddress: (args) => getL2TokenAddress(client, args),
getL1TokenAddress: (args) => getL1TokenAddress(client, args),
getGasPerPubdata: () => getGasPerPubdata(client),
}
}
}
4 changes: 4 additions & 0 deletions src/zksync/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export {
getDefaultBridgeAddresses,
} from './actions/getDefaultBridgeAddresses.js'
export { getBridgehubContractAddress } from './actions/getBridgehubContractAddress.js'
export {
type GetGasPerPubdataReturnType,
getGasPerPubdata,
} from './actions/getGasPerPubdata.js'
export {
type GetL1AllowanceErrorType,
type GetL1AllowanceParameters,
Expand Down
5 changes: 5 additions & 0 deletions src/zksync/types/eip1193.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,9 @@ export type PublicZksyncRpcSchema = [
Parameters: undefined
ReturnType: Address
},
{
Method: 'zks_gasPerPubdata'
Parameters?: undefined
ReturnType: Hex
},
]
3 changes: 3 additions & 0 deletions test/src/zksync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const mockFeeValues = {
max_priority_fee_per_gas: '0x0',
}

export const mockGasPerPubdata = '0x42'

export const mockAccountBalances = {
'0x0000000000000000000000000000000000000000': '1000000000000000000',
'0x0000000000000000000000000000000000000001': '2000000000000000000',
Expand Down Expand Up @@ -497,6 +499,7 @@ export const mockLogProof = {
export const mockRequestReturnData = async (method: string) => {
if (method === 'zks_L1ChainId') return mockChainId
if (method === 'zks_estimateFee') return mockFeeValues
if (method === 'zks_gasPerPubdata') return mockGasPerPubdata
if (method === 'zks_getAllAccountBalances') return mockAccountBalances
if (method === 'zks_getBaseTokenL1Address') return mockBaseTokenL1Address
if (method === 'zks_getBlockDetails') return mockBlockDetails
Expand Down
Loading