Skip to content

Commit

Permalink
feat: add getGasPerPubdataByte endpoint (#1778)
Browse files Browse the repository at this point in the history
## What ❔

Add a `getGasPerPubdataByte` endpoint in `zks` namespace, that returns
the amount of L2 gas required to pay for 1 byte of pubdata.

## Why ❔

One of our partners requires this endpoint, they want to have a better
insight into the pricing model.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `zk spellcheck`.
- [ ] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
dimazhornyk committed Apr 30, 2024
1 parent 6a30a31 commit d62dd08
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/lib/types/src/fee_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub struct L1PeggedBatchFeeModelInput {
}

/// Pubdata price may be independent from L1 gas price.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct PubdataIndependentBatchFeeModelInput {
/// Fair L2 gas price to provide
pub fair_l2_gas_price: u64,
Expand Down
5 changes: 4 additions & 1 deletion core/lib/web3_decl/src/namespaces/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use zksync_types::{
TransactionDetailedResult, TransactionDetails,
},
fee::Fee,
fee_model::FeeParams,
fee_model::{FeeParams, PubdataIndependentBatchFeeModelInput},
transaction_request::CallRequest,
Address, L1BatchNumber, L2BlockNumber, H256, U256, U64,
};
Expand Down Expand Up @@ -122,6 +122,9 @@ pub trait ZksNamespace {
l1_batch_number: L1BatchNumber,
) -> RpcResult<Option<Proof>>;

#[method(name = "getBatchFeeInput")]
async fn get_batch_fee_input(&self) -> RpcResult<PubdataIndependentBatchFeeModelInput>;

#[method(name = "sendRawTransactionWithDetailedOutput")]
async fn send_raw_transaction_with_detailed_output(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use zksync_types::{
ProtocolVersion, TransactionDetailedResult, TransactionDetails,
},
fee::Fee,
fee_model::FeeParams,
fee_model::{FeeParams, PubdataIndependentBatchFeeModelInput},
transaction_request::CallRequest,
Address, Bytes, L1BatchNumber, L2BlockNumber, StorageLogQueryType, H256, U256, U64,
};
Expand Down Expand Up @@ -141,16 +141,24 @@ impl ZksNamespaceServer for ZksNamespace {
.map_err(|err| self.current_method().map_err(err))
}

// to be removed in favor of `get_batch_fee_input`
async fn get_l1_gas_price(&self) -> RpcResult<U64> {
self.get_l1_gas_price_impl()
.await
.map_err(|err| self.current_method().map_err(err))
match self.get_batch_fee_input_impl().await {
Ok(fee_input) => Ok(fee_input.l1_gas_price.into()),
Err(err) => Err(self.current_method().map_err(err)),
}
}

async fn get_fee_params(&self) -> RpcResult<FeeParams> {
Ok(self.get_fee_params_impl())
}

async fn get_batch_fee_input(&self) -> RpcResult<PubdataIndependentBatchFeeModelInput> {
self.get_batch_fee_input_impl()
.await
.map_err(|err| self.current_method().map_err(err))
}

async fn get_protocol_version(
&self,
version_id: Option<u16>,
Expand Down
23 changes: 16 additions & 7 deletions core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use zksync_types::{
ProtocolVersion, StorageProof, TransactionDetails,
},
fee::Fee,
fee_model::FeeParams,
fee_model::{FeeParams, PubdataIndependentBatchFeeModelInput},
l1::L1Tx,
l2::L2Tx,
l2_to_l1_log::{l2_to_l1_logs_tree_size, L2ToL1Log},
Expand Down Expand Up @@ -442,12 +442,7 @@ impl ZksNamespace {
.map_err(DalError::generalize)?)
}

pub async fn get_l1_gas_price_impl(&self) -> Result<U64, Web3Error> {
let fee_input_provider = &self.state.tx_sender.0.batch_fee_input_provider;
let fee_input = fee_input_provider.get_batch_fee_input().await?;
Ok(fee_input.l1_gas_price().into())
}

#[tracing::instrument(skip(self))]
pub fn get_fee_params_impl(&self) -> FeeParams {
self.state
.tx_sender
Expand Down Expand Up @@ -539,6 +534,20 @@ impl ZksNamespace {
.ok_or(Web3Error::NotImplemented)
}

#[tracing::instrument(skip(self))]
pub async fn get_batch_fee_input_impl(
&self,
) -> Result<PubdataIndependentBatchFeeModelInput, Web3Error> {
Ok(self
.state
.tx_sender
.0
.batch_fee_input_provider
.get_batch_fee_input()
.await?
.into_pubdata_independent())
}

#[tracing::instrument(skip(self, tx_bytes))]
pub async fn send_raw_transaction_with_detailed_output_impl(
&self,
Expand Down

0 comments on commit d62dd08

Please sign in to comment.