Skip to content

Commit

Permalink
feat: ✅ Estimate Fee compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbelleng committed Apr 3, 2024
1 parent 7c0a9cc commit a406040
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/client/rpc/src/methods/read/estimate_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
.into_iter()
// FIXME: https://github.com/keep-starknet-strange/madara/issues/329
// TODO: reflect right estimation
.map(|x| FeeEstimate { gas_consumed: x.0, gas_price: x.1, data_gas_consumed: x.2, data_gas_price: x.3, overall_fee: x.4, unit: x.5})
.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
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
1 change: 1 addition & 0 deletions crates/pallets/starknet/runtime_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mp-transactions = { workspace = true, features = [
"parity-scale-codec",
"scale-info",
] }
pallet-starknet = { workspace = true }

# Starknet
blockifier = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/pallets/starknet/runtime_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use alloc::vec::Vec;

use mp_contract::ContractAbi;
use mp_simulations::{PlaceHolderErrorTypeForFailedStarknetExecution, SimulationFlagForEstimateFee, SimulationFlags};
use pallet_starknet::types::FeeEstimate;
use sp_runtime::DispatchError;
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector, Nonce};
use starknet_api::hash::{StarkFelt, StarkHash};
Expand Down Expand Up @@ -57,7 +58,7 @@ sp_api::decl_runtime_apis! {
/// Returns the fee token address.
fn fee_token_addresses() -> FeeTokenAddresses;
/// Returns fee estimate
fn estimate_fee(transactions: Vec<UserTransaction>, simulation_flags: Vec<SimulationFlagForEstimateFee>,) -> Result<(u128, u128, u128, u128, u128), DispatchError>;
fn estimate_fee(transactions: Vec<UserTransaction>, simulation_flags: Vec<SimulationFlagForEstimateFee>,) -> Result<Vec<FeeEstimate>, DispatchError>;
/// Returns message fee estimate
fn estimate_message_fee(message: L1HandlerTransaction) -> Result<(u128, u128, u128), DispatchError>;
/// Simulates single L1 Message and returns its trace
Expand Down
3 changes: 2 additions & 1 deletion crates/pallets/starknet/src/simulations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use mp_transactions::{user_or_l1_into_tx_vec, UserOrL1HandlerTransaction, UserTr
use sp_core::Get;
use sp_runtime::DispatchError;
use starknet_api::core::{ContractAddress, EntryPointSelector};
use starknet_core::types::{FeeEstimate, PriceUnit};

// use starknet_core::types::PriceUnit;
use crate::types::{FeeEstimate, PriceUnit};
use crate::{Config, Error, Pallet};

impl<T: Config> Pallet<T> {
Expand Down
11 changes: 10 additions & 1 deletion crates/pallets/starknet/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::collections::HashMap;

use blockifier::execution::contract_class::ContractClass;
use blockifier::transaction::account_transaction::AccountTransaction;
use blockifier::transaction::objects::{FeeType, HasRelatedFeeType};
use blockifier::transaction::transaction_execution::Transaction;
use mp_felt::Felt252Wrapper;
Expand All @@ -11,6 +10,7 @@ use sp_std::vec::Vec;
use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::state::StorageKey;
use starknet_api::transaction::{Event, Fee, MessageToL1, TransactionHash};
use starknet_core::types::PriceUnit as Price;

/// Contract Storage Key
pub type ContractStorageKey = (ContractAddress, StorageKey);
Expand Down Expand Up @@ -81,3 +81,12 @@ pub fn fee_type(transaction: &Transaction) -> FeeType {
Transaction::L1HandlerTransaction(tx) => tx.fee_type(),
}
}

impl From<PriceUnit> for Price {
fn from(unit: PriceUnit) -> Self {
match unit {
PriceUnit::Wei => Price::Wei,
PriceUnit::Fri => Price::Fri,
}
}
}
1 change: 1 addition & 0 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use pallet_grandpa::{fg_primitives, AuthorityId as GrandpaId, AuthorityList as G
/// Import the Starknet pallet.
pub use pallet_starknet;
use pallet_starknet::pallet::Error as PalletError;
use pallet_starknet::types::FeeEstimate;
use pallet_starknet_runtime_api::StarknetTransactionExecutionError;
pub use pallet_timestamp::Call as TimestampCall;
use sp_api::impl_runtime_apis;
Expand Down

0 comments on commit a406040

Please sign in to comment.