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

Add conversion rate RPC endpoint #27

Merged
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
3 changes: 3 additions & 0 deletions core/lib/web3_decl/src/namespaces/zks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,7 @@ pub trait ZksNamespace {
keys: Vec<H256>,
l1_batch_number: L1BatchNumber,
) -> RpcResult<Proof>;

#[method(name = "getConversionRate")]
async fn get_conversion_rate(&self) -> RpcResult<U64>;
}
3 changes: 1 addition & 2 deletions core/lib/zksync_core/src/api_server/tx_sender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,13 +878,12 @@ impl<G: L1GasPriceProvider> TxSender<G> {

pub fn gas_price(&self) -> u64 {
let gas_price = self.0.l1_gas_price_source.estimate_effective_gas_price();
// let gas_price = self.0.l1_gas_price_source.estimate_erc_20_gas_price();
let l1_gas_price = (gas_price as f64 * self.0.sender_config.gas_price_scale_factor).round();
let (base_fee, _) = derive_base_fee_and_gas_per_pubdata(
l1_gas_price as u64,
self.0.sender_config.fair_l2_gas_price,
);
base_fee * self.0.l1_gas_price_source.estimate_erc_20_gas_price()
base_fee * self.0.l1_gas_price_source.get_erc20_conversion_rate()
}

fn ensure_tx_executable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub trait ZksNamespaceT {
keys: Vec<H256>,
l1_batch_number: L1BatchNumber,
) -> BoxFuture<Result<Proof>>;

#[rpc(name = "zks_getConversionRate")]
fn get_conversion_rate(&self) -> BoxFuture<Result<U64>>;
}

impl<G: L1GasPriceProvider + Send + Sync + 'static> ZksNamespaceT for ZksNamespace<G> {
Expand Down Expand Up @@ -331,4 +334,14 @@ impl<G: L1GasPriceProvider + Send + Sync + 'static> ZksNamespaceT for ZksNamespa
.map_err(into_jsrpc_error)
})
}

fn get_conversion_rate(&self) -> BoxFuture<Result<U64>> {
let self_ = self.clone();
Box::pin(async move {
self_
.get_conversion_rate_impl()
.await
.map_err(into_jsrpc_error)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,10 @@ impl<G: L1GasPriceProvider + Send + Sync + 'static> ZksNamespaceServer for ZksNa
.await
.map_err(into_jsrpc_error)
}

async fn get_conversion_rate(&self) -> RpcResult<U64> {
self.get_conversion_rate_impl()
.await
.map_err(into_jsrpc_error)
}
}
13 changes: 12 additions & 1 deletion core/lib/zksync_core/src/api_server/web3/namespaces/zks.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, convert::TryInto};
use std::{collections::HashMap, convert::TryInto, str::FromStr};

use bigdecimal::{BigDecimal, Zero};
use zksync_dal::StorageProcessor;
Expand Down Expand Up @@ -679,4 +679,15 @@ impl<G: L1GasPriceProvider> ZksNamespace<G> {
storage_proof,
})
}

#[tracing::instrument(skip_all)]
pub async fn get_conversion_rate_impl(&self) -> Result<U64, Web3Error> {
Ok(U64::from(
self.state
.tx_sender
.0
.l1_gas_price_source
.get_erc20_conversion_rate(),
))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<G: L1GasPriceProvider> L1GasPriceProvider for BoundedGasAdjuster<G> {
}
default_gas_price
}
fn estimate_erc_20_gas_price(&self) -> u64 {
self.default_gas_adjuster.estimate_erc_20_gas_price()
fn get_erc20_conversion_rate(&self) -> u64 {
self.default_gas_adjuster.get_erc20_conversion_rate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn to_wei(in_eth: u64, modifier: u32) -> u64 {
}

pub async fn get_erc_20_value_in_wei() -> u64 {
let erc_20_value_in_eth = fetch_it().await.unwrap();
erc20_value_from_eth_to_wei(&erc_20_value_in_eth).unwrap()
// let erc_20_value_in_eth = fetch_it().await.unwrap();
// erc20_value_from_eth_to_wei(&erc_20_value_in_eth).unwrap()
11
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<E: EthInterface> L1GasPriceProvider for GasAdjuster<E> {

/// TODO: This is for an easy refactor to test things,
/// let's discuss where this should actually be.
fn estimate_erc_20_gas_price(&self) -> u64 {
fn get_erc20_conversion_rate(&self) -> u64 {
self.erc_20_value_in_wei
.load(std::sync::atomic::Ordering::Relaxed)
}
Expand Down
14 changes: 11 additions & 3 deletions core/lib/zksync_core/src/l1_gas_price/main_node_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use zksync_web3_decl::{
namespaces::ZksNamespaceClient,
};

use super::L1GasPriceProvider;
use super::{erc_20_fetcher, L1GasPriceProvider};

const SLEEP_INTERVAL: Duration = Duration::from_secs(5);

Expand All @@ -27,13 +27,15 @@ const SLEEP_INTERVAL: Duration = Duration::from_secs(5);
pub struct MainNodeGasPriceFetcher {
client: HttpClient,
gas_price: AtomicU64,
erc20_value_in_wei: AtomicU64,
}

impl MainNodeGasPriceFetcher {
pub fn new(main_node_url: &str) -> Self {
Self {
client: Self::build_client(main_node_url),
gas_price: AtomicU64::new(1u64), // Start with 1 wei until the first update.
erc20_value_in_wei: AtomicU64::new(1u64),
}
}

Expand Down Expand Up @@ -62,6 +64,11 @@ impl MainNodeGasPriceFetcher {
self.gas_price
.store(main_node_gas_price.as_u64(), Ordering::Relaxed);
tokio::time::sleep(SLEEP_INTERVAL).await;

self.erc20_value_in_wei.store(
erc_20_fetcher::get_erc_20_value_in_wei().await,
std::sync::atomic::Ordering::Relaxed,
);
}
Ok(())
}
Expand All @@ -71,7 +78,8 @@ impl L1GasPriceProvider for MainNodeGasPriceFetcher {
fn estimate_effective_gas_price(&self) -> u64 {
self.gas_price.load(Ordering::Relaxed)
}
fn estimate_erc_20_gas_price(&self) -> u64 {
return 1;

fn get_erc20_conversion_rate(&self) -> u64 {
self.erc20_value_in_wei.load(Ordering::Relaxed)
}
}
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/l1_gas_price/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub trait L1GasPriceProvider {
/// Return value is in wei.
fn estimate_effective_gas_price(&self) -> u64;

fn estimate_erc_20_gas_price(&self) -> u64;
fn get_erc20_conversion_rate(&self) -> u64;
}

/// Extended version of `L1GasPriceProvider` that can provide parameters
Expand Down
4 changes: 2 additions & 2 deletions core/lib/zksync_core/src/state_keeper/keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ impl ZkSyncStateKeeper {
}
};
println!("Before multiplying by l2 gas price");
l1_batch_env.fair_l2_gas_price *=
crate::l1_gas_price::erc_20_fetcher::get_erc_20_value_in_wei().await;
// l1_batch_env.fair_l2_gas_price *=
// crate::l1_gas_price::erc_20_fetcher::get_erc_20_value_in_wei().await;
println!("Price of l2 gas: {}", l1_batch_env.fair_l2_gas_price);
let protocol_version = system_env.version;
let mut updates_manager = UpdatesManager::new(
Expand Down
Loading