Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e committed Aug 23, 2023
1 parent f2e9bfe commit 51980fd
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 90 deletions.
2 changes: 1 addition & 1 deletion packages/fuels-accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ mod tests {
let tx_signature = Signature::from_bytes(bytes);

// Sign the transaction manually
let message = Message::from_bytes(*tx.id());
let message = Message::from_bytes(*tx.id(0.into()));
let signature = Signature::sign(&wallet.private_key, &message);

// Check if the signatures are the same
Expand Down
45 changes: 17 additions & 28 deletions packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,29 +440,18 @@ impl Provider {
&self,
tx_id: &TxId,
) -> ProviderResult<Option<TransactionResponse>> {
Ok(self.client.transaction(tx_id).await?.map(|fuel_tr| {
TransactionResponse::from_fuel_response(fuel_tr, self.consensus_parameters)
}))
Ok(self.client.transaction(tx_id).await?.map(Into::into))
}

// - Get transaction(s)
pub async fn get_transactions(
&self,
request: PaginationRequest<String>,
) -> ProviderResult<PaginatedResult<TransactionResponse, String>> {
let pr = self.client.transactions(request).await?;

let results = pr
.results
.into_iter()
.map(|fuel_tr| {
TransactionResponse::from_fuel_response(fuel_tr, self.consensus_parameters)
})
.collect();

Ok(PaginatedResult {
cursor: pr.cursor,
results,
results: pr.results.into_iter().map(Into::into).collect(),
has_next_page: pr.has_next_page,
has_previous_page: pr.has_previous_page,
})
Expand All @@ -479,17 +468,9 @@ impl Provider {
.transactions_by_owner(&owner.into(), request)
.await?;

let results = pr
.results
.into_iter()
.map(|fuel_tr| {
TransactionResponse::from_fuel_response(fuel_tr, self.consensus_parameters)
})
.collect();

Ok(PaginatedResult {
cursor: pr.cursor,
results,
results: pr.results.into_iter().map(Into::into).collect(),
has_next_page: pr.has_next_page,
has_previous_page: pr.has_previous_page,
})
Expand Down Expand Up @@ -546,27 +527,35 @@ impl Provider {
let tolerance = tolerance.unwrap_or(DEFAULT_GAS_ESTIMATION_TOLERANCE);

// Remove limits from an existing Transaction for accurate gas estimation
let new_tx = tx.to_dry_run_tx(0, MAX_GAS_PER_TX);
let dry_run_tx = Self::generate_dry_run_tx(tx.clone());
let gas_used = self
.get_gas_used_with_tolerance(new_tx.clone(), tolerance)
.get_gas_used_with_tolerance(dry_run_tx.clone(), tolerance)
.await?;

// Update the tx with estimated gas_used and correct gas price to calculate the total_fee
let new_tx = new_tx.to_dry_run_tx(gas_price, gas_used);
let dry_run_tx = dry_run_tx
.with_gas_price(gas_price)
.with_gas_limit(gas_used);

let transaction_fee = new_tx
.fee_checked_from_tx()
let transaction_fee = dry_run_tx
.fee_checked_from_tx(&self.consensus_parameters)
.expect("Error calculating TransactionFee");

Ok(TransactionCost {
min_gas_price,
gas_price,
gas_used,
metered_bytes_size: new_tx.metered_bytes_size() as u64,
metered_bytes_size: dry_run_tx.metered_bytes_size() as u64,
total_fee: transaction_fee.max_fee(),
})
}

// Remove limits from an existing Transaction to get an accurate gas estimation
fn generate_dry_run_tx<T: Transaction>(tx: T) -> T {
// Simulate the contract call with MAX_GAS_PER_TX to get the complete gas_used
tx.clone().with_gas_limit(MAX_GAS_PER_TX).with_gas_price(0)
}

// Increase estimated gas by the provided tolerance
async fn get_gas_used_with_tolerance<T: Transaction>(
&self,
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;

pub use fuel_tx::{Address, AssetId, ContractId, TxPointer, UtxoId};
use fuel_types::bytes::padded_len;
pub use fuel_types::{MessageId, Nonce};
pub use fuel_types::{ChainId, MessageId, Nonce};

pub use crate::types::{core::*, wrappers::*};
use crate::types::{
Expand Down
5 changes: 1 addition & 4 deletions packages/fuels-core/src/types/transaction_builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ macro_rules! impl_tx_trait {
tx.estimate_predicates(&consensus_parameters, &GasCosts::default())?;
};

Ok($tx_ty {
tx,
consensus_parameters
})
Ok($tx_ty { tx })
}

fn add_unresolved_signature(&mut self, owner: Bech32Address, secret_key: SecretKey) {
Expand Down
129 changes: 88 additions & 41 deletions packages/fuels-core/src/types/wrappers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use fuel_tx::{
Salt as FuelSalt, Script, StorageSlot, Transaction as FuelTransaction, TransactionFee,
UniqueIdentifier, Witness,
};
use fuel_types::ChainId;

use crate::{
constants::{DEFAULT_GAS_LIMIT, DEFAULT_GAS_PRICE, DEFAULT_MATURITY},
Expand Down Expand Up @@ -67,24 +68,33 @@ pub enum TransactionType {
}

pub trait Transaction: Into<FuelTransaction> + Clone {
fn fee_checked_from_tx(&self) -> Option<TransactionFee>;
fn fee_checked_from_tx(
&self,
consensus_parameters: &ConsensusParameters,
) -> Option<TransactionFee>;

fn check_without_signatures(
&self,
block_height: u32,
parameters: &ConsensusParameters,
consensus_parameters: &ConsensusParameters,
) -> Result<()>;

fn to_dry_run_tx(self, gas_price: u64, gas_limit: u64) -> Self;

fn id(&self) -> Bytes32;
fn id(&self, chain_id: ChainId) -> Bytes32;

fn maturity(&self) -> u32;

fn with_maturity(self, maturity: u32) -> Self;

fn gas_price(&self) -> u64;

fn with_gas_price(self, gas_price: u64) -> Self;

fn gas_limit(&self) -> u64;

fn with_gas_limit(self, gas_limit: u64) -> Self;

fn with_tx_params(self, tx_params: TxParameters) -> Self;

fn metered_bytes_size(&self) -> usize;

fn inputs(&self) -> &Vec<Input>;
Expand All @@ -107,39 +117,35 @@ impl From<TransactionType> for FuelTransaction {
}

impl Transaction for TransactionType {
fn fee_checked_from_tx(&self) -> Option<TransactionFee> {
fn fee_checked_from_tx(
&self,
consensus_parameters: &ConsensusParameters,
) -> Option<TransactionFee> {
match self {
TransactionType::Script(tx) => tx.fee_checked_from_tx(),
TransactionType::Create(tx) => tx.fee_checked_from_tx(),
TransactionType::Script(tx) => tx.fee_checked_from_tx(consensus_parameters),
TransactionType::Create(tx) => tx.fee_checked_from_tx(consensus_parameters),
}
}

fn check_without_signatures(
&self,
block_height: u32,
parameters: &ConsensusParameters,
consensus_parameters: &ConsensusParameters,
) -> Result<()> {
match self {
TransactionType::Script(tx) => tx.check_without_signatures(block_height, parameters),
TransactionType::Create(tx) => tx.check_without_signatures(block_height, parameters),
}
}

fn to_dry_run_tx(self, gas_price: u64, gas_limit: u64) -> Self {
match self {
TransactionType::Script(tx) => {
TransactionType::Script(tx.to_dry_run_tx(gas_price, gas_limit))
tx.check_without_signatures(block_height, consensus_parameters)
}
TransactionType::Create(tx) => {
TransactionType::Create(tx.to_dry_run_tx(gas_price, gas_limit))
tx.check_without_signatures(block_height, consensus_parameters)
}
}
}

fn id(&self) -> Bytes32 {
fn id(&self, chain_id: ChainId) -> Bytes32 {
match self {
TransactionType::Script(tx) => tx.id(),
TransactionType::Create(tx) => tx.id(),
TransactionType::Script(tx) => tx.id(chain_id),
TransactionType::Create(tx) => tx.id(chain_id),
}
}

Expand All @@ -150,20 +156,48 @@ impl Transaction for TransactionType {
}
}

fn with_maturity(self, maturity: u32) -> Self {
match self {
TransactionType::Script(tx) => TransactionType::Script(tx.with_maturity(maturity)),
TransactionType::Create(tx) => TransactionType::Create(tx.with_maturity(maturity)),
}
}

fn gas_price(&self) -> u64 {
match self {
TransactionType::Script(tx) => tx.gas_price(),
TransactionType::Create(tx) => tx.gas_price(),
}
}

fn with_gas_price(self, gas_price: u64) -> Self {
match self {
TransactionType::Script(tx) => TransactionType::Script(tx.with_gas_price(gas_price)),
TransactionType::Create(tx) => TransactionType::Create(tx.with_gas_price(gas_price)),
}
}

fn gas_limit(&self) -> u64 {
match self {
TransactionType::Script(tx) => tx.gas_limit(),
TransactionType::Create(tx) => tx.gas_limit(),
}
}

fn with_gas_limit(self, gas_limit: u64) -> Self {
match self {
TransactionType::Script(tx) => TransactionType::Script(tx.with_gas_limit(gas_limit)),
TransactionType::Create(tx) => TransactionType::Create(tx.with_gas_limit(gas_limit)),
}
}

fn with_tx_params(self, tx_params: TxParameters) -> Self {
match self {
TransactionType::Script(tx) => TransactionType::Script(tx.with_tx_params(tx_params)),
TransactionType::Create(tx) => TransactionType::Create(tx.with_tx_params(tx_params)),
}
}

fn metered_bytes_size(&self) -> usize {
match self {
TransactionType::Script(tx) => tx.metered_bytes_size(),
Expand Down Expand Up @@ -205,7 +239,6 @@ macro_rules! impl_tx_wrapper {
#[derive(Debug, Clone)]
pub struct $wrapper {
pub(crate) tx: $wrapped,
pub(crate) consensus_parameters: ConsensusParameters,
}

impl From<$wrapper> for $wrapped {
Expand All @@ -220,53 +253,67 @@ macro_rules! impl_tx_wrapper {
}
}

impl $wrapper {
pub fn from_fuel_tx(tx: $wrapped, consensus_parameters: ConsensusParameters) -> Self {
$wrapper {
tx,
consensus_parameters,
}
impl From<$wrapped> for $wrapper {
fn from(tx: $wrapped) -> Self {
$wrapper { tx }
}
}

impl Transaction for $wrapper {
fn fee_checked_from_tx(&self) -> Option<TransactionFee> {
TransactionFee::checked_from_tx(&self.consensus_parameters, &self.tx)
fn fee_checked_from_tx(
&self,
consensus_parameters: &ConsensusParameters,
) -> Option<TransactionFee> {
TransactionFee::checked_from_tx(consensus_parameters, &self.tx)
}

fn check_without_signatures(
&self,
block_height: u32,
parameters: &ConsensusParameters,
consensus_parameters: &ConsensusParameters,
) -> Result<()> {
Ok(self
.tx
.check_without_signatures(block_height.into(), parameters)?)
}

fn to_dry_run_tx(mut self, gas_price: u64, gas_limit: u64) -> Self {
*self.tx.gas_price_mut() = gas_price;
*self.tx.gas_limit_mut() = gas_limit;

self
.check_without_signatures(block_height.into(), consensus_parameters)?)
}

fn id(&self) -> Bytes32 {
self.tx.id(&self.consensus_parameters.chain_id.into())
fn id(&self, chain_id: ChainId) -> Bytes32 {
self.tx.id(&chain_id)
}

fn maturity(&self) -> u32 {
(*self.tx.maturity()).into()
}

fn with_maturity(mut self, maturity: u32) -> Self {
*self.tx.maturity_mut() = maturity.into();
self
}

fn gas_price(&self) -> u64 {
*self.tx.gas_price()
}

fn with_gas_price(mut self, gas_price: u64) -> Self {
*self.tx.gas_price_mut() = gas_price;
self
}

fn gas_limit(&self) -> u64 {
*self.tx.gas_limit()
}

fn with_gas_limit(mut self, gas_limit: u64) -> Self {
*self.tx.gas_limit_mut() = gas_limit;
self
}

fn with_tx_params(self, tx_params: TxParameters) -> Self {
self.with_gas_limit(tx_params.gas_limit)
.with_gas_price(tx_params.gas_price)
.with_maturity(tx_params.maturity)
}

fn metered_bytes_size(&self) -> usize {
self.tx.metered_bytes_size()
}
Expand Down
Loading

0 comments on commit 51980fd

Please sign in to comment.