Skip to content

Commit

Permalink
Merge pull request #34 from Tbelleng/feat/up_blockifier
Browse files Browse the repository at this point in the history
Feat : ✨ Adding estimate_fee for v0.7.0
  • Loading branch information
antiyro committed Apr 3, 2024
2 parents 11be000 + a406040 commit dca296e
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 319 deletions.
17 changes: 9 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions crates/client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use starknet_core::types::{
BroadcastedInvokeTransaction, BroadcastedTransaction, ContractClass, DeclareTransactionResult,
DeployAccountTransactionResult, EventFilterWithPage, EventsPage, FeeEstimate, FieldElement, FunctionCall,
InvokeTransactionResult, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, MaybePendingStateUpdate,
MaybePendingTransactionReceipt, MsgFromL1, SimulatedTransaction, SimulationFlag, StateDiff, SyncStatusType,
Transaction, TransactionStatus, TransactionTraceWithHash,
MaybePendingTransactionReceipt, MsgFromL1, SimulatedTransaction, SimulationFlag, SimulationFlagForEstimateFee,
StateDiff, SyncStatusType, Transaction, TransactionStatus, TransactionTraceWithHash,
};

use crate::methods::get_block::{
Expand Down Expand Up @@ -115,6 +115,7 @@ pub trait StarknetReadRpcApi {
async fn estimate_fee(
&self,
request: Vec<BroadcastedTransaction>,
simulation_flags: Vec<SimulationFlagForEstimateFee>,
block_id: BlockId,
) -> RpcResult<Vec<FeeEstimate>>;

Expand Down
12 changes: 9 additions & 3 deletions crates/client/rpc/src/methods/read/estimate_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use deoxys_runtime::opaque::DBlockT;
use jsonrpsee::core::RpcResult;
use mc_genesis_data_provider::GenesisProvider;
use mp_hashers::HasherT;
use mp_simulations::convert_flags;
use mp_transactions::UserTransaction;
use pallet_starknet_runtime_api::{ConvertTransactionRuntimeApi, StarknetRuntimeApi};
use sc_client_api::backend::{Backend, StorageProvider};
Expand All @@ -10,7 +11,9 @@ use sc_transaction_pool::ChainApi;
use sc_transaction_pool_api::TransactionPool;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use starknet_core::types::{BlockId, BroadcastedTransaction, FeeEstimate, PriceUnit};
use starknet_core::types::{
BlockId, BroadcastedTransaction, FeeEstimate, PriceUnit, SimulationFlagForEstimateFee as EstimateFeeFlag,
};

use crate::errors::StarknetRpcApiError;
use crate::Starknet;
Expand All @@ -28,6 +31,7 @@ use crate::Starknet;
pub async fn estimate_fee<A, BE, G, C, P, H>(
starknet: &Starknet<A, BE, G, C, P, H>,
request: Vec<BroadcastedTransaction>,
simulation_flags: Vec<EstimateFeeFlag>,
block_id: BlockId,
) -> RpcResult<Vec<FeeEstimate>>
where
Expand All @@ -53,10 +57,12 @@ where

let account_transactions: Vec<UserTransaction> = transactions.into_iter().map(UserTransaction::from).collect();

let simulation_flags = convert_flags(simulation_flags);

let fee_estimates = starknet
.client
.runtime_api()
.estimate_fee(substrate_block_hash, account_transactions)
.estimate_fee(substrate_block_hash, account_transactions, simulation_flags)
.map_err(|e| {
log::error!("Request parameters error: {e}");
StarknetRpcApiError::InternalServerError
Expand All @@ -70,7 +76,7 @@ where
.into_iter()
// FIXME: https://github.com/keep-starknet-strange/madara/issues/329
// TODO: reflect right estimation
.map(|x| FeeEstimate { gas_price: 10u32.into(), gas_consumed: x.1.into(), overall_fee: x.0.into(), unit: PriceUnit::Fri})
.map(|x| FeeEstimate { gas_consumed: x.gas_consumed.0 , gas_price: x.gas_price.0, data_gas_consumed: x.data_gas_consumed.0, data_gas_price: x.data_gas_price.0, overall_fee: x.overall_fee.0, unit: x.unit.into()})
.collect();

Ok(estimates)
Expand Down
2 changes: 2 additions & 0 deletions crates/client/rpc/src/methods/read/estimate_message_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ where
// TODO: Check if fee estimation is correct (spoiler alert it is not)
let estimate = FeeEstimate {
gas_price: FieldElement::ZERO,
data_gas_consumed: FieldElement::ZERO,
data_gas_price: FieldElement::ZERO,
gas_consumed: FieldElement::ZERO,
overall_fee: FieldElement::ZERO,
unit: PriceUnit::Fri,
Expand Down
6 changes: 4 additions & 2 deletions crates/client/rpc/src/methods/read/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use sp_blockchain::HeaderBackend;
use starknet_core::types::{
BlockHashAndNumber, BlockId, BroadcastedTransaction, ContractClass, EventFilterWithPage, EventsPage, FeeEstimate,
FieldElement, FunctionCall, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs, MaybePendingStateUpdate,
MaybePendingTransactionReceipt, MsgFromL1, SyncStatusType, Transaction, TransactionStatus,
MaybePendingTransactionReceipt, MsgFromL1, SimulationFlagForEstimateFee, SyncStatusType, Transaction,
TransactionStatus,
};

use super::block_hash_and_number::*;
Expand Down Expand Up @@ -76,9 +77,10 @@ where
async fn estimate_fee(
&self,
request: Vec<BroadcastedTransaction>,
simulation_flags: Vec<SimulationFlagForEstimateFee>,
block_id: BlockId,
) -> RpcResult<Vec<FeeEstimate>> {
estimate_fee(self, request, block_id).await
estimate_fee(self, request, simulation_flags, block_id).await
}

async fn estimate_message_fee(&self, message: MsgFromL1, block_id: BlockId) -> RpcResult<FeeEstimate> {
Expand Down
12 changes: 11 additions & 1 deletion crates/client/rpc/src/methods/trace/simulate_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use sp_runtime::traits::Block as BlockT;
use starknet_core::types::{
BlockId, BroadcastedTransaction, FeeEstimate, PriceUnit, SimulatedTransaction, SimulationFlag,
};
use starknet_ff::FieldElement;

use super::lib::ConvertCallInfoToExecuteInvocationError;
use super::utils::tx_execution_infos_to_tx_trace;
Expand Down Expand Up @@ -103,10 +104,19 @@ fn tx_execution_infos_to_simulated_transactions<B: BlockT>(
let overall_fee = fee.into();

let unit: PriceUnit = PriceUnit::Wei; //TODO(Tbelleng) : Get Price Unit from Tx
let data_gas_consumed = FieldElement::default();
let data_gas_price = FieldElement::default();

results.push(SimulatedTransaction {
transaction_trace,
fee_estimation: FeeEstimate { gas_consumed, gas_price, overall_fee, unit },
fee_estimation: FeeEstimate {
gas_consumed,
data_gas_consumed,
data_gas_price,
gas_price,
overall_fee,
unit,
},
});
}
Err(_) => {
Expand Down
Loading

0 comments on commit dca296e

Please sign in to comment.