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

feat: add fee token to TransactionOption and TxnConfig #2650

Merged
merged 5 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl ExecuteArgs {
self.starknet.url(profile_config.env.as_ref())?,
);

let txn_config: TxnConfig = self.transaction.into();
let txn_config: TxnConfig = self.transaction.try_into()?;

config.tokio_handle().block_on(async {
// We could save the world diff computation extracting the account directly from the
Expand Down
2 changes: 1 addition & 1 deletion bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

let world_address = world_diff.world_info.address;

let mut txn_config: TxnConfig = self.transaction.into();
let mut txn_config: TxnConfig = self.transaction.try_into()?;

Check warning on line 64 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L64

Added line #L64 was not covered by tests
txn_config.wait = true;
glihm marked this conversation as resolved.
Show resolved Hide resolved

let migration = Migration::new(
Expand Down
208 changes: 197 additions & 11 deletions bin/sozo/src/commands/options/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
use std::fmt::{Display, Formatter};

use anyhow::{bail, Result};
use clap::Args;
use dojo_utils::{TxnAction, TxnConfig};
use clap::builder::PossibleValue;
use clap::{Args, ValueEnum};
use dojo_utils::{EthFeeConfig, FeeConfig, StrkFeeConfig, TxnAction, TxnConfig};
use starknet::core::types::Felt;

#[derive(Debug, Args, Default)]
#[command(next_help_heading = "Transaction options")]
pub struct TransactionOptions {
#[arg(long)]
#[arg(help = "Fee token to use.")]
#[arg(default_value = "strk")]
glihm marked this conversation as resolved.
Show resolved Hide resolved
pub fee_token: FeeToken,

Check warning on line 15 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L15

Added line #L15 was not covered by tests
glihm marked this conversation as resolved.
Show resolved Hide resolved

#[arg(long, value_name = "MULTIPLIER")]
#[arg(help = "The multiplier to use for the fee estimate.")]
#[arg(help = "The multiplier to use for the fee estimate (--fee-token eth).")]
glihm marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long_help = "The multiplier to use for the fee estimate. This value will be used on \
the estimated fee which will be used as the max fee for the transaction. \
(max_fee = estimated_fee * multiplier)")]
#[arg(conflicts_with = "max_fee_raw")]
#[arg(conflicts_with = "gas")]
#[arg(conflicts_with = "gas_price")]
glihm marked this conversation as resolved.
Show resolved Hide resolved
#[arg(global = true)]
pub fee_estimate_multiplier: Option<f64>,

#[arg(long)]
#[arg(help = "Maximum raw value to be used for fees, in Wei.")]
#[arg(help = "Maximum raw value to be used for fees, in Wei (--fee-token eth).")]
glihm marked this conversation as resolved.
Show resolved Hide resolved
#[arg(conflicts_with = "fee_estimate_multiplier")]
#[arg(conflicts_with = "gas")]
#[arg(conflicts_with = "gas_price")]
glihm marked this conversation as resolved.
Show resolved Hide resolved
#[arg(global = true)]
pub max_fee_raw: Option<Felt>,

#[arg(long, help = "Maximum L1 gas amount (--fee-token strk).")]
#[arg(conflicts_with = "max_fee_raw")]
#[arg(conflicts_with = "fee_estimate_multiplier")]
glihm marked this conversation as resolved.
Show resolved Hide resolved
pub gas: Option<u64>,

#[arg(long, help = "Maximum L1 gas price in STRK (--fee-token strk).")]
#[arg(conflicts_with = "max_fee_raw")]
#[arg(conflicts_with = "fee_estimate_multiplier")]
glihm marked this conversation as resolved.
Show resolved Hide resolved
pub gas_price: Option<u128>,
glihm marked this conversation as resolved.
Show resolved Hide resolved

glihm marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long)]
#[arg(help = "Wait until the transaction is accepted by the sequencer, returning the status \
and hash.")]
Expand Down Expand Up @@ -58,22 +80,186 @@
(false, false) => Ok(TxnAction::Send {
wait: self.wait || self.walnut,
receipt: self.receipt,
max_fee_raw: self.max_fee_raw,
fee_estimate_multiplier: self.fee_estimate_multiplier,
fee_config: match self.fee_token {

Check warning on line 83 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L83

Added line #L83 was not covered by tests
FeeToken::Strk => {
FeeConfig::Strk(StrkFeeConfig { gas: self.gas, gas_price: self.gas_price })

Check warning on line 85 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L85

Added line #L85 was not covered by tests
}
FeeToken::Eth => FeeConfig::Eth(EthFeeConfig {
max_fee_raw: self.max_fee_raw,
fee_estimate_multiplier: self.fee_estimate_multiplier,
}),

Check warning on line 90 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L87-L90

Added lines #L87 - L90 were not covered by tests
},
walnut: self.walnut,
}),
}
}
}

impl From<TransactionOptions> for TxnConfig {
fn from(value: TransactionOptions) -> Self {
Self {
fee_estimate_multiplier: value.fee_estimate_multiplier,
impl TryFrom<TransactionOptions> for TxnConfig {
type Error = anyhow::Error;

fn try_from(value: TransactionOptions) -> Result<Self> {
match value.fee_token {
FeeToken::Eth => {
if value.gas.is_some() || value.gas_price.is_some() {
bail!(
"Gas and gas price are not supported for ETH transactions. Use \
`--fee-token strk` instead or use `--max-fee-raw` and \
`--fee-estimate-multiplier`."
);
}
}
FeeToken::Strk => {
if value.max_fee_raw.is_some() || value.fee_estimate_multiplier.is_some() {
bail!(
"Max fee raw and fee estimate multiplier are not supported for STRK \
transactions. Use `--fee-token eth` instead or use `--gas` and \
`--gas-price`."
);
}
}
};
glihm marked this conversation as resolved.
Show resolved Hide resolved

Ok(Self {
wait: value.wait || value.walnut,
receipt: value.receipt,
max_fee_raw: value.max_fee_raw,
fee_config: match value.fee_token {
FeeToken::Strk => {
FeeConfig::Strk(StrkFeeConfig { gas: value.gas, gas_price: value.gas_price })
}
FeeToken::Eth => FeeConfig::Eth(EthFeeConfig {
max_fee_raw: value.max_fee_raw,
fee_estimate_multiplier: value.fee_estimate_multiplier,
}),
},
walnut: value.walnut,
})
}
}

#[derive(Debug, Default, Clone)]
pub enum FeeToken {
#[default]
Strk,
Eth,
}

impl ValueEnum for FeeToken {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Eth, Self::Strk]
}

Check warning on line 150 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L148-L150

Added lines #L148 - L150 were not covered by tests

fn to_possible_value(&self) -> Option<PossibleValue> {
match self {
Self::Eth => Some(PossibleValue::new("ETH").alias("eth")),
Self::Strk => Some(PossibleValue::new("STRK").alias("strk")),

Check warning on line 155 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L152-L155

Added lines #L152 - L155 were not covered by tests
}
}

Check warning on line 157 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L157

Added line #L157 was not covered by tests
}

impl Display for FeeToken {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Eth => write!(f, "ETH"),
Self::Strk => write!(f, "STRK"),
}
}
}

#[cfg(test)]
mod tests {
use anyhow::Result;

use super::*;

#[test]
fn test_strk_conversion() -> Result<()> {
let opts = TransactionOptions {
wait: true,
receipt: true,
fee_token: FeeToken::Strk,
gas: Some(1000),
gas_price: Some(100),
max_fee_raw: None,
fee_estimate_multiplier: None,
walnut: false,
};

let config: TxnConfig = opts.try_into()?;

assert!(config.wait);
assert!(config.receipt);
assert!(!config.walnut);

match config.fee_config {
FeeConfig::Strk(strk_config) => {
assert_eq!(strk_config.gas, Some(1000));
assert_eq!(strk_config.gas_price, Some(100));
}
_ => panic!("Expected STRK fee config"),

Check warning on line 199 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L199

Added line #L199 was not covered by tests
}

Ok(())
}

#[test]
fn test_eth_conversion() -> Result<()> {
let opts = TransactionOptions {
wait: false,
receipt: true,
fee_token: FeeToken::Eth,
gas: None,
gas_price: None,
max_fee_raw: Some(Felt::from(1000)),
fee_estimate_multiplier: Some(1.5),
walnut: true,
};

let config: TxnConfig = opts.try_into()?;

assert!(config.wait);
assert!(config.receipt);
assert!(config.walnut);

match config.fee_config {
FeeConfig::Eth(eth_config) => {
assert_eq!(eth_config.max_fee_raw, Some(Felt::from(1000)));
assert_eq!(eth_config.fee_estimate_multiplier, Some(1.5));
}
_ => panic!("Expected ETH fee config"),

Check warning on line 229 in bin/sozo/src/commands/options/transaction.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/options/transaction.rs#L229

Added line #L229 was not covered by tests
}

Ok(())
}

#[test]
fn test_invalid_strk_config() {
let opts = TransactionOptions {
fee_token: FeeToken::Strk,
max_fee_raw: Some(Felt::from(1000)),
fee_estimate_multiplier: Some(1.5),
..Default::default()
};

let result: Result<TxnConfig, _> = opts.try_into();
assert!(result.is_err());
}

#[test]
fn test_invalid_eth_config() {
let opts = TransactionOptions {
fee_token: FeeToken::Eth,
gas: Some(1000),
gas_price: Some(100),
..Default::default()
};
let result: Result<TxnConfig, _> = opts.try_into();
assert!(result.is_err());
}

#[test]
fn test_fee_token_display() {
assert_eq!(FeeToken::Eth.to_string(), "ETH");
assert_eq!(FeeToken::Strk.to_string(), "STRK");
}
}
5 changes: 1 addition & 4 deletions crates/dojo/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ pub use tx::deployer::*;
pub use tx::error::TransactionError;
pub use tx::invoker::*;
pub use tx::waiter::*;
pub use tx::{
get_predeployed_accounts, parse_block_id, TransactionExt, TransactionResult, TxnAction,
TxnConfig,
};
pub use tx::*;

pub mod env;
pub mod keystore;
Expand Down
21 changes: 18 additions & 3 deletions crates/dojo/utils/src/tx/declarer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
};
use starknet::providers::{Provider, ProviderError};

use crate::{TransactionError, TransactionExt, TransactionResult, TransactionWaiter, TxnConfig};
use crate::{
FeeConfig, TransactionError, TransactionExt, TransactionResult, TransactionWaiter, TxnConfig,
};

/// A declarer is in charge of declaring contracts.
#[derive(Debug)]
Expand Down Expand Up @@ -92,8 +94,21 @@
Err(e) => return Err(TransactionError::Provider(e)),
}

let DeclareTransactionResult { transaction_hash, class_hash } =
account.declare_v2(Arc::new(class), casm_class_hash).send_with_cfg(txn_config).await?;
let DeclareTransactionResult { transaction_hash, class_hash } = match txn_config.fee_config
{
FeeConfig::Strk(_) => {
account
.declare_v3(Arc::new(class), casm_class_hash)
.send_with_cfg(txn_config)
.await?
}
FeeConfig::Eth(_) => {
account
.declare_v2(Arc::new(class), casm_class_hash)
.send_with_cfg(txn_config)
.await?

Check warning on line 109 in crates/dojo/utils/src/tx/declarer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/declarer.rs#L106-L109

Added lines #L106 - L109 were not covered by tests
}
};

tracing::trace!(
transaction_hash = format!("{:#066x}", transaction_hash),
Expand Down
20 changes: 12 additions & 8 deletions crates/dojo/utils/src/tx/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use starknet::providers::{Provider, ProviderError};
use tracing::trace;

use crate::{TransactionError, TransactionExt, TransactionResult, TransactionWaiter, TxnConfig};
use crate::{
FeeConfig, TransactionError, TransactionExt, TransactionResult, TransactionWaiter, TxnConfig,
};

const UDC_DEPLOY_SELECTOR: Felt = selector!("deployContract");
const UDC_ADDRESS: Felt =
Expand Down Expand Up @@ -56,14 +58,16 @@
return Ok(TransactionResult::Noop);
}

let txn = self.account.execute_v1(vec![Call {
calldata: udc_calldata,
selector: UDC_DEPLOY_SELECTOR,
to: UDC_ADDRESS,
}]);
let call = Call { calldata: udc_calldata, selector: UDC_DEPLOY_SELECTOR, to: UDC_ADDRESS };

let InvokeTransactionResult { transaction_hash } =
txn.send_with_cfg(&self.txn_config).await?;
let InvokeTransactionResult { transaction_hash } = match self.txn_config.fee_config {
FeeConfig::Strk(_) => {
self.account.execute_v3(vec![call]).send_with_cfg(&self.txn_config).await?
}
FeeConfig::Eth(_) => {
self.account.execute_v1(vec![call]).send_with_cfg(&self.txn_config).await?

Check warning on line 68 in crates/dojo/utils/src/tx/deployer.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/deployer.rs#L68

Added line #L68 was not covered by tests
}
};

trace!(
transaction_hash = format!("{:#066x}", transaction_hash),
Expand Down
12 changes: 11 additions & 1 deletion crates/dojo/utils/src/tx/invoker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use tracing::trace;

use super::TransactionResult;
use crate::tx::FeeConfig;
use crate::{TransactionError, TransactionExt, TransactionWaiter, TxnConfig};

#[derive(Debug)]
Expand Down Expand Up @@ -54,7 +55,16 @@
) -> Result<TransactionResult, TransactionError<A::SignError>> {
trace!(?call, "Invoke contract.");

let tx = self.account.execute_v1(vec![call]).send_with_cfg(&self.txn_config).await?;
let tx = match self.txn_config.fee_config {
FeeConfig::Strk(config) => {
trace!(?config, "Invoking with STRK.");
self.account.execute_v3(vec![call]).send_with_cfg(&self.txn_config).await?

Check warning on line 61 in crates/dojo/utils/src/tx/invoker.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/invoker.rs#L58-L61

Added lines #L58 - L61 were not covered by tests
}
FeeConfig::Eth(config) => {
trace!(?config, "Invoking with ETH.");
self.account.execute_v1(vec![call]).send_with_cfg(&self.txn_config).await?

Check warning on line 65 in crates/dojo/utils/src/tx/invoker.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/utils/src/tx/invoker.rs#L63-L65

Added lines #L63 - L65 were not covered by tests
}
};

trace!(transaction_hash = format!("{:#066x}", tx.transaction_hash), "Invoke contract.");

Expand Down
Loading
Loading