Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

get gas from block via RPC SN #963

Merged
Merged
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
34 changes: 30 additions & 4 deletions rpc_state_reader_sn_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,25 @@ impl RpcState {
deserialize_transaction_json(result).expect("transaction should deserialize correctly")
}

/// Gets the gas price of a given block.
pub fn get_gas_price(&self, block_number: u64) -> serde_json::Result<u128> {
let chain_name = self.get_chain_name();

let response = ureq::get(&format!(
"https://{}.starknet.io/feeder_gateway/get_block",
chain_name
))
.query("blockNumber", &block_number.to_string())
.call()
.unwrap();

let res: serde_json::Value = response.into_json().expect("should be json");

let gas_price_hex = res["gas_price"].as_str().unwrap();
let gas_price = u128::from_str_radix(gas_price_hex.trim_start_matches("0x"), 16).unwrap();
Ok(gas_price)
}

pub fn get_chain_name(&self) -> ChainId {
ChainId(match self.chain {
RpcChain::MainNet => "alpha-mainnet".to_string(),
Expand Down Expand Up @@ -867,13 +886,13 @@ mod blockifier_transaction_tests {
tx_hash: &str,
network: RpcChain,
block_number: u64,
gas_price: u128,
) -> (TransactionExecutionInfo, TransactionTrace) {
let tx_hash = tx_hash.strip_prefix("0x").unwrap();

// Instantiate the RPC StateReader and the CachedState
let block = BlockValue::Number(serde_json::to_value(block_number).unwrap());
let rpc_reader = RpcStateReader(RpcState::new(network, block));
let gas_price = rpc_reader.0.get_gas_price(block_number).unwrap();

// Get values for block context before giving ownership of the reader
let chain_id = rpc_reader.0.get_chain_name();
Expand Down Expand Up @@ -951,13 +970,20 @@ mod blockifier_transaction_tests {
use super::*;

#[test]
#[ignore = "working on fixes"]
fn test_get_gas_price() {
let block = BlockValue::Number(serde_json::to_value(169928).unwrap());
let rpc_state = RpcState::new(RpcChain::MainNet, block);

let price = rpc_state.get_gas_price(169928).unwrap();
assert_eq!(price, 22804578690);
}

#[test]
fn test_recent_tx() {
let (tx_info, trace) = execute_tx(
"0x05d200ef175ba15d676a68b36f7a7b72c17c17604eda4c1efc2ed5e4973e2c91",
RpcChain::MainNet,
169928,
17110275391107,
);

let TransactionExecutionInfo {
Expand All @@ -978,7 +1004,7 @@ mod blockifier_transaction_tests {
trace.function_invocation.internal_calls.len()
);

assert_eq!(actual_fee.0, 5728510166928);
assert_eq!(actual_fee.0, 57285101669280);
}
}
}