-
Notifications
You must be signed in to change notification settings - Fork 0
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: read fee params from diamond proxy storage #44
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import { | |
encodeFunctionData, | ||
erc20Abi, | ||
formatUnits, | ||
Hex, | ||
parseEther, | ||
parseUnits, | ||
zeroAddress, | ||
|
@@ -18,7 +19,7 @@ import { | |
L1MetricsServiceException, | ||
} from "@zkchainhub/metrics/exceptions"; | ||
import { bridgeHubAbi, diamondProxyAbi, sharedBridgeAbi } from "@zkchainhub/metrics/l1/abis"; | ||
import { AssetTvl, GasInfo } from "@zkchainhub/metrics/types"; | ||
import { AssetTvl, FeeParams, feeParamsFieldLengths, GasInfo } from "@zkchainhub/metrics/types"; | ||
import { IPricingService, PRICING_PROVIDER } from "@zkchainhub/pricing"; | ||
import { EvmProviderService } from "@zkchainhub/providers"; | ||
import { | ||
|
@@ -39,6 +40,7 @@ import { | |
} from "@zkchainhub/shared/tokens/tokens"; | ||
|
||
const ONE_ETHER = parseEther("1"); | ||
const FEE_PARAMS_SLOT: Hex = `0x26`; | ||
|
||
/** | ||
* Acts as a wrapper around Viem library to provide methods to interact with an EVM-based blockchain. | ||
|
@@ -324,20 +326,53 @@ export class L1MetricsService { | |
} | ||
} | ||
|
||
//TODO: Implement feeParams. | ||
async feeParams(_chainId: ChainId): Promise<{ | ||
batchOverheadL1Gas: number; | ||
maxPubdataPerBatch: number; | ||
maxL2GasPerBatch: number; | ||
priorityTxMaxPubdata: number; | ||
minimalL2GasPrice: number; | ||
}> { | ||
/** | ||
* Retrieves the fee parameters for a specific chain. | ||
* | ||
* @param chainId - The ID of the chain. | ||
* @returns A Promise that resolves to a FeeParams object containing the fee parameters. | ||
* @throws {L1MetricsServiceException} If the fee parameters cannot be retrieved from L1. | ||
*/ | ||
async feeParams(chainId: ChainId): Promise<FeeParams> { | ||
const diamondProxyAddress = await this.fetchDiamondProxyAddress(chainId); | ||
|
||
// Read the storage at the target slot; | ||
const feeParamsData = await this.evmProviderService.getStorageAt( | ||
diamondProxyAddress, | ||
FEE_PARAMS_SLOT, | ||
); | ||
if (!feeParamsData) { | ||
throw new L1MetricsServiceException("Failed to get fee params from L1."); | ||
} | ||
|
||
const strippedParamsData = feeParamsData.slice(2); // Remove the 0x prefix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is nitpicking so feel free to ignore it but, what do you think about handling this with a regex replacing Assuming that this is not a performance intensive service, it might be better to specify that those two first characters we expect to slice are As an extra, you could remove the comment as the code would be self-explanatory. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 i like the suggestion |
||
let cursor = strippedParamsData.length; | ||
const values: string[] = []; | ||
|
||
for (const value of Object.values(feeParamsFieldLengths)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is a little bit similar to one of the comments in a previous PR about data structures + their ordering and it's totally fine to keep it as it is, although I'd strongly suggest leveraging an array for this part of the code, as the order —if I understand this loop correctly— is extremely important (ie unordered Arrays are structures that inherently have a defined order (universally, in other programming languages too); and although newest standards force There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're totally right, i wanted to reflect the object "field"->size but it's better an array with sizes in the correct order and just clarify with a comment the reason behind |
||
const hexValue = strippedParamsData.slice(cursor - value, cursor); | ||
assert(hexValue, "Error parsing fee params"); | ||
values.push(hexValue); | ||
cursor -= value; | ||
} | ||
|
||
const [ | ||
pubdataPricingMode, | ||
batchOverheadL1Gas, | ||
maxPubdataPerBatch, | ||
maxL2GasPerBatch, | ||
priorityTxMaxPubdata, | ||
minimalL2GasPrice, | ||
] = values as [string, string, string, string, string, string]; | ||
|
||
// Convert hex to decimal | ||
return { | ||
batchOverheadL1Gas: 50000, | ||
maxPubdataPerBatch: 120000, | ||
maxL2GasPerBatch: 10000000, | ||
priorityTxMaxPubdata: 15000, | ||
minimalL2GasPrice: 10000000, | ||
pubdataPricingMode: parseInt(pubdataPricingMode, 16), | ||
batchOverheadL1Gas: parseInt(batchOverheadL1Gas, 16), | ||
maxPubdataPerBatch: parseInt(maxPubdataPerBatch, 16), | ||
maxL2GasPerBatch: parseInt(maxL2GasPerBatch, 16), | ||
priorityTxMaxPubdata: parseInt(priorityTxMaxPubdata, 16), | ||
minimalL2GasPrice: BigInt(`0x${minimalL2GasPrice}`), | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//See: https://github.com/matter-labs/era-contracts/blob/8a70bbbc48125f5bde6189b4e3c6a3ee79631678/l1-contracts/contracts/state-transition/chain-deps/ZkSyncHyperchainStorage.sol#L52 | ||
export type FeeParams = { | ||
pubdataPricingMode: number; | ||
batchOverheadL1Gas: number; | ||
maxPubdataPerBatch: number; | ||
maxL2GasPerBatch: number; | ||
priorityTxMaxPubdata: number; | ||
minimalL2GasPrice?: bigint; | ||
}; | ||
|
||
// Define the lengths for each field (in hex digits, each byte is 2 hex digits) | ||
export const feeParamsFieldLengths = { | ||
pubdataPricingMode: 2, // uint8 -> 1 byte -> 2 hex digits | ||
batchOverheadL1Gas: 8, // uint32 -> 4 bytes -> 8 hex digits | ||
maxPubdataPerBatch: 8, // uint32 -> 4 bytes -> 8 hex digits | ||
maxL2GasPerBatch: 8, // uint32 -> 4 bytes -> 8 hex digits | ||
priorityTxMaxPubdata: 8, // uint32 -> 4 bytes -> 8 hex digits | ||
minimalL2GasPrice: 16, // uint64 -> 8 bytes -> 16 hex digits | ||
} as const; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./gasInfo.type"; | ||
export * from "./tvl.type"; | ||
export * from "./feeParams.type"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh didn't know
viem
had this type. A little bit more semantically correct for some values thanAddress