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

Use strk as a default fee token #2749

Merged
merged 30 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
868699d
Init
kkawula Nov 28, 2024
2976595
Wip
kkawula Nov 28, 2024
66918c1
Add test
kkawula Dec 2, 2024
4f41255
Clean
kkawula Dec 2, 2024
8a85685
WIP
kkawula Dec 3, 2024
4e1cd17
Update logic
kkawula Dec 5, 2024
e4afecf
Update test
kkawula Dec 5, 2024
3757ed4
Remove `validate`
kkawula Dec 5, 2024
6d1ff43
Update docs
kkawula Dec 8, 2024
14ae382
Update docs
kkawula Dec 8, 2024
2444474
Update docs
kkawula Dec 8, 2024
2395caf
Merge branch 'master' into kkawula/2696-use-strk-as-a-default-fee-token
kkawula Dec 8, 2024
500ec0e
Update CHANGELOG.md
kkawula Dec 9, 2024
b04879b
Update CHANGELOG.md
kkawula Dec 10, 2024
87aa0d2
Add deprecation info
kkawula Dec 10, 2024
58cc85b
Merge branch 'master' into kkawula/2696-use-strk-as-a-default-fee-token
kkawula Dec 10, 2024
5dc8da5
Typos
kkawula Dec 10, 2024
539fd6c
Update test
kkawula Dec 10, 2024
4a184d7
Update deprecation message
kkawula Dec 10, 2024
9f59dae
Update tests
kkawula Dec 10, 2024
e82bb73
Merge branch 'master' into kkawula/2696-use-strk-as-a-default-fee-token
kkawula Dec 10, 2024
7d08eaf
Fmt + lint
kkawula Dec 10, 2024
0d3cde9
Update printing warnings
kkawula Dec 13, 2024
527579f
Update change log
kkawula Dec 13, 2024
810fe98
Update docs
kkawula Dec 13, 2024
0d30c81
Update printing warning logic
kkawula Dec 13, 2024
1141adb
Add `test_fee_token_deprecation_warning`
kkawula Dec 13, 2024
d526d73
Update README
kkawula Dec 13, 2024
10b22e0
Update CHANGELOG.md
kkawula Dec 13, 2024
d59aefe
Merge branch 'master' into kkawula/2696-use-strk-as-a-default-fee-token
kkawula Dec 13, 2024
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Cast

#### Changed

- `--fee-token` and `--version` flags are now optional, `strk` and `v3` will be used by default

### `snforge_scarb_plugin`
kkawula marked this conversation as resolved.
Show resolved Hide resolved

#### Changed
kkawula marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
51 changes: 40 additions & 11 deletions crates/sncast/src/helpers/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use conversions::TryIntoConv;
use starknet::core::types::BlockId;
use starknet::providers::Provider;
use starknet_types_core::felt::{Felt, NonZeroFelt};
use std::str::FromStr;

#[derive(Args, Debug, Clone)]
pub struct FeeArgs {
/// Token that transaction fee will be paid in
#[clap(long)]
#[clap(long, value_parser = parse_fee_token)]
pub fee_token: Option<FeeToken>,

/// Max fee for the transaction. If not provided, will be automatically estimated.
Expand Down Expand Up @@ -50,9 +51,9 @@ impl From<ScriptFeeSettings> for FeeArgs {

impl FeeArgs {
#[must_use]
pub fn fee_token(self, fee_token: Option<FeeToken>) -> Self {
pub fn fee_token(self, fee_token: FeeToken) -> Self {
Self {
fee_token: fee_token.or(self.fee_token),
fee_token: Some(fee_token),
..self
}
}
Expand Down Expand Up @@ -138,9 +139,10 @@ impl FeeArgs {
}
}

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

Expand Down Expand Up @@ -187,7 +189,7 @@ impl From<ScriptFeeSettings> for FeeSettings {

pub trait PayableTransaction {
fn error_message(&self, token: &str, version: &str) -> String;
fn validate(&self) -> Result<()>;
fn validate_and_get_token(&self) -> Result<FeeToken>;
fn token_from_version(&self) -> Option<FeeToken>;
}

Expand All @@ -199,21 +201,27 @@ macro_rules! impl_payable_transaction {
$err_func(token, version)
}

fn validate(&self) -> Result<()> {
fn validate_and_get_token(&self) -> Result<FeeToken> {
match (
&self.token_from_version(),
&self.fee_args.fee_token,
) {
(Some(token_from_version), Some(token)) if token_from_version != token => {
Err(anyhow!(self.error_message(
&format!("{token:?}").to_lowercase(),
&format!("{:?}", token).to_lowercase(),
&format!("{:?}", self.version.clone().unwrap()).to_lowercase()
)))
}
},
(None, Some(token)) => {
Ok(token.clone())
},
(Some(token_from_version), None) => {
Ok(token_from_version.clone())
},
(None, None) => {
Err(anyhow!("Either --fee-token or --version must be provided"))
}
_ => Ok(()),
Ok(FeeToken::default())
},
_ => Ok(self.fee_args.fee_token.clone().unwrap_or_else(|| self.token_from_version().unwrap_or_else(|| unreachable!())))
}
}

Expand All @@ -225,3 +233,24 @@ macro_rules! impl_payable_transaction {
}
};
}

impl FromStr for FeeToken {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"eth" => Ok(FeeToken::Eth),
"strk" => Ok(FeeToken::Strk),
_ => Err(String::from("Possible values: eth, strk")),
}
}
}
cptartur marked this conversation as resolved.
Show resolved Hide resolved

fn parse_fee_token(s: &str) -> Result<FeeToken, String> {
match s.to_lowercase().as_str() {
"eth" => println!("\x1b[33mwarning:\x1b[0m Specifying '--fee-token' flag will be deprecated in the future and eth transactions will not be supported due to 'SNIP-16'. It is recommended to use '--version' instead"),
_ => println!("\x1b[33mwarning:\x1b[0m Specifying '--fee-token' flag will be deprecated in the future. It is recommended to use '--version' instead"),
kkawula marked this conversation as resolved.
Show resolved Hide resolved
}
let parsed_token: FeeToken = s.parse()?;
Ok(parsed_token)
}
20 changes: 9 additions & 11 deletions crates/sncast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ async fn run_async_command(
Commands::Declare(declare) => {
let provider = declare.rpc.get_provider(&config).await?;

declare.validate()?;
let fee_token = declare.validate_and_get_token()?;

let account = get_account(
&config.account,
Expand All @@ -262,6 +262,7 @@ async fn run_async_command(
&artifacts,
wait_config,
false,
fee_token,
)
.await
.map_err(handle_starknet_command_error)
Expand All @@ -286,9 +287,7 @@ async fn run_async_command(
}

Commands::Deploy(deploy) => {
deploy.validate()?;

let fee_token = deploy.token_from_version();
let fee_token = deploy.validate_and_get_token()?;

let Deploy {
arguments,
Expand Down Expand Up @@ -378,9 +377,7 @@ async fn run_async_command(
}

Commands::Invoke(invoke) => {
invoke.validate()?;

let fee_token = invoke.token_from_version();
let fee_token = invoke.validate_and_get_token()?;

let Invoke {
contract_address,
Expand Down Expand Up @@ -457,8 +454,6 @@ async fn run_async_command(
starknet_commands::multicall::Commands::Run(run) => {
let provider = run.rpc.get_provider(&config).await?;

run.validate()?;

let account = get_account(
&config.account,
&config.accounts_file,
Expand Down Expand Up @@ -533,10 +528,12 @@ async fn run_async_command(
}

account::Commands::Deploy(deploy) => {
deploy.validate()?;

let provider = deploy.rpc.get_provider(&config).await?;

let fee_token = deploy.validate_and_get_token()?;

let fee_args = deploy.fee_args.clone().fee_token(fee_token);

let chain_id = get_chain_id(&provider).await?;
let keystore_path = config.keystore.clone();
let result = starknet_commands::account::deploy::deploy(
Expand All @@ -547,6 +544,7 @@ async fn run_async_command(
wait_config,
&config.account,
keystore_path,
fee_args,
)
.await;

Expand Down
6 changes: 1 addition & 5 deletions crates/sncast/src/starknet_commands/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,8 @@ pub async fn deploy(
wait_config: WaitForTx,
account: &str,
keystore_path: Option<Utf8PathBuf>,
fee_args: FeeArgs,
) -> Result<InvokeResponse> {
let fee_args = deploy_args
.fee_args
.clone()
.fee_token(deploy_args.token_from_version());

if let Some(keystore_path_) = keystore_path {
deploy_from_keystore(
provider,
Expand Down
3 changes: 2 additions & 1 deletion crates/sncast/src/starknet_commands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ pub async fn declare(
artifacts: &HashMap<String, StarknetContractArtifacts>,
wait_config: WaitForTx,
skip_on_already_declared: bool,
fee_token: FeeToken,
) -> Result<DeclareResponse, StarknetCommandError> {
let fee_settings = declare
.fee_args
.clone()
.fee_token(declare.token_from_version())
.fee_token(fee_token)
.try_into_fee_settings(account.provider(), account.block_id())
.await?;

Expand Down
4 changes: 3 additions & 1 deletion crates/sncast/src/starknet_commands/multicall/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ pub async fn run(
account: &SingleOwnerAccount<&JsonRpcClient<HttpTransport>, LocalWallet>,
wait_config: WaitForTx,
) -> Result<InvokeResponse> {
let fee_args = run.fee_args.clone().fee_token(run.token_from_version());
let fee_token = run.validate_and_get_token()?;

let fee_args = run.fee_args.clone().fee_token(fee_token);

let contents = std::fs::read_to_string(&run.path)?;
let items_map: HashMap<String, Vec<toml::Value>> =
Expand Down
6 changes: 4 additions & 2 deletions crates/sncast/src/starknet_commands/script/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use shared::utils::build_readable_text;
use sncast::get_nonce;
use sncast::helpers::configuration::CastConfig;
use sncast::helpers::constants::SCRIPT_LIB_ARTIFACT_NAME;
use sncast::helpers::fee::{FeeSettings, ScriptFeeSettings};
use sncast::helpers::fee::{FeeArgs, FeeSettings, ScriptFeeSettings};
use sncast::helpers::rpc::RpcArgs;
use sncast::response::structs::ScriptRunResponse;
use sncast::state::hashing::{
Expand Down Expand Up @@ -116,7 +116,8 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
}
"declare" => {
let contract: String = input_reader.read::<ByteArray>()?.to_string();
let fee_args = input_reader.read::<ScriptFeeSettings>()?.into();
let fee_args: FeeArgs = input_reader.read::<ScriptFeeSettings>()?.into();
let fee_token = fee_args.fee_token.clone().unwrap_or_default();
let nonce = input_reader.read()?;

let declare = Declare {
Expand Down Expand Up @@ -145,6 +146,7 @@ impl<'a> ExtensionLogic for CastScriptExtension<'a> {
wait_params: self.config.wait_params,
},
true,
fee_token,
));

self.state.maybe_insert_tx_entry(
Expand Down
15 changes: 9 additions & 6 deletions crates/sncast/tests/e2e/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ async fn test_invalid_version_and_token_combination(fee_token: &str, version: &s
}

#[tokio::test]
async fn test_no_version_and_token() {
async fn test_default_fee_token() {
let tempdir = create_account(false, &OZ_CLASS_HASH.into_hex_string(), "oz").await;
let accounts_file = "accounts.json";

Expand All @@ -367,11 +367,14 @@ async fn test_no_version_and_token() {

let snapbox = runner(&args).current_dir(tempdir.path());

let output = snapbox.assert().failure();
assert_stderr_contains(
output,
"Error: Either --fee-token or --version must be provided",
);
snapbox.assert().success().stdout_matches(indoc! {r"
Transaction hash: [..]
command: account deploy
transaction_hash: [..]

To see invocation details, visit:
transaction: [..]
"});
}

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions docs/src/appendix/sncast/account/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Optional.
Maximum fee for the `deploy_account` transaction in Fri or Wei depending on fee token or transaction version. When not used, defaults to auto-estimation.

## `--fee-token <FEE_TOKEN>`
Optional. Required if `--version` is not provided.
Optional. When not used, defaults to STRK.

Token used for fee payment. Possible values: ETH, STRK.

Expand All @@ -34,6 +34,6 @@ Optional.
Maximum gas unit price for the `deploy_account` transaction paid in Fri. When not used, defaults to auto-estimation. (Only for STRK fee payment)

## `--version, -v <VERSION>`
Optional. Required if `--fee-token` is not provided.
Optional. When not used, defaults to v3.

Version of the account deployment transaction. Possible values: v1, v3.
4 changes: 2 additions & 2 deletions docs/src/appendix/sncast/declare.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Optional.
Maximum fee for the `declare` transaction in Fri or Wei depending on fee token or transaction version. When not used, defaults to auto-estimation.

## `--fee-token <FEE_TOKEN>`
Optional. Required if `--version` is not provided.
Optional. When not used, defaults to STRK.

Token used for fee payment. Possible values: ETH, STRK.

Expand All @@ -38,7 +38,7 @@ Optional.
Maximum gas unit price for the `declare` transaction paid in Fri. When not used, defaults to auto-estimation. (Only for STRK fee payment)

## `--version, -v <VERSION>`
Optional. Required if `--fee-token` is not provided.
Optional. When not used, defaults to v3.

Version of the deployment transaction. Possible values: v2, v3.

Expand Down
4 changes: 2 additions & 2 deletions docs/src/appendix/sncast/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Optional.
Maximum fee for the `deploy` transaction in Fri or Wei depending on fee token or transaction version. When not used, defaults to auto-estimation.

## `--fee-token <FEE_TOKEN>`
Optional. Required if `--version` is not provided.
Optional. When not used, defaults to STRK.

Token used for fee payment. Possible values: ETH, STRK.

Expand All @@ -53,7 +53,7 @@ Optional.
Maximum gas unit price for the `deploy` transaction paid in Fri. When not used, defaults to auto-estimation. (Only for STRK fee payment)

## `--version, -v <VERSION>`
Optional. Required if `--fee-token` is not provided.
Optional. When not used, defaults to v3.

Version of the deployment transaction. Possible values: v1, v3.

Expand Down
4 changes: 2 additions & 2 deletions docs/src/appendix/sncast/invoke.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Optional.
Maximum fee for the `invoke` transaction in Fri or Wei depending on fee token or transaction version. When not used, defaults to auto-estimation.

## `--fee-token <FEE_TOKEN>`
Optional. Required if `--version` is not provided.
Optional. When not used, defaults to STRK.

Token used for fee payment. Possible values: ETH, STRK.

Expand All @@ -49,7 +49,7 @@ Optional.
Maximum gas unit price for the `invoke` transaction paid in Fri. When not used, defaults to auto-estimation. (Only for STRK fee payment)

## `--version, -v <VERSION>`
Optional. Required if `--fee-token` is not provided.
Optional. When not used, defaults to v3.

Version of the deployment transaction. Possible values: v1, v3.

Expand Down
4 changes: 2 additions & 2 deletions docs/src/appendix/sncast/multicall/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Optional.
Maximum fee for the `invoke` transaction in Fri or Wei depending on fee token or transaction version. When not used, defaults to auto-estimation.

## `--fee-token <FEE_TOKEN>`
Optional. Required if `--version` is not provided.
Optional. When not used, defaults to STRK.

Token used for fee payment. Possible values: ETH, STRK.

Expand All @@ -39,7 +39,7 @@ Optional.
Maximum gas unit price for the `invoke` transaction paid in Fri. When not used, defaults to auto-estimation. (Only for STRK fee payment)

## `--version, -v <VERSION>`
Optional. Required if `--fee-token` is not provided.
Optional. When not used, defaults to v3.

Version of the deployment transaction. Possible values: v1, v3.

Expand Down
4 changes: 0 additions & 4 deletions docs/src/starknet/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ $ sncast \
account deploy \
--url http://127.0.0.1:5055 \
--name new_account \
--fee-token strk \
--max-fee 9999999999999
```

Expand All @@ -85,8 +84,6 @@ transaction: https://sepolia.starkscan.co/tx/[..]

For a detailed CLI description, see [account deploy command reference](../appendix/sncast/account/deploy.md).

> 💡 **Info**
> You can also choose to pay in Ether by setting `--fee-token` to `eth`.

## Managing Accounts

Expand Down Expand Up @@ -211,7 +208,6 @@ $ sncast \
declare \
--url http://127.0.0.1:5055 \
--contract-name my_contract \
--fee-token eth
```

#### Creating an Account With Starkli-Style Keystore
Expand Down
Loading
Loading