Skip to content

Commit

Permalink
feat: Amount type (#1758)
Browse files Browse the repository at this point in the history
When entering amounts users will want to use `_`s to better read what they are typing. Also with this approach we could add more complex parsing for example `1 XLM`.
  • Loading branch information
willemneal authored Nov 29, 2024
1 parent a11a924 commit 390c220
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
4 changes: 2 additions & 2 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ Creates and funds a new account with the specified starting balance
* `--destination <DESTINATION>` — Account Id to create, e.g. `GBX...`
* `--starting-balance <STARTING_BALANCE>` — Initial balance in stroops of the account, default 1 XLM

Default value: `10000000`
Default value: `10_000_000`



Expand Down Expand Up @@ -1722,7 +1722,7 @@ Sends an amount in a specific asset to a destination account
* `--asset <ASSET>` — Asset to send, default native, e.i. XLM

Default value: `native`
* `--amount <AMOUNT>` — Amount of the aforementioned asset to send
* `--amount <AMOUNT>` — Amount of the aforementioned asset to send. e.g. `10_000_000` (1 XLM)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn payment() {
"--destination",
test1.as_str(),
"--amount",
ONE_XLM.to_string().as_str(),
"10_000_000",
])
.assert()
.success();
Expand Down
8 changes: 4 additions & 4 deletions cmd/soroban-cli/src/commands/tx/new/create_account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{command, Parser};

use crate::{commands::tx, xdr};
use crate::{commands::tx, tx::builder, xdr};

#[derive(Parser, Debug, Clone)]
#[group(skip)]
Expand All @@ -11,15 +11,15 @@ pub struct Cmd {
#[arg(long)]
pub destination: xdr::AccountId,
/// Initial balance in stroops of the account, default 1 XLM
#[arg(long, default_value = "10000000")]
pub starting_balance: i64,
#[arg(long, default_value = "10_000_000")]
pub starting_balance: builder::Amount,
}

impl From<&Cmd> for xdr::OperationBody {
fn from(cmd: &Cmd) -> Self {
xdr::OperationBody::CreateAccount(xdr::CreateAccountOp {
destination: cmd.destination.clone(),
starting_balance: cmd.starting_balance,
starting_balance: cmd.starting_balance.into(),
})
}
}
6 changes: 3 additions & 3 deletions cmd/soroban-cli/src/commands/tx/new/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ pub struct Cmd {
/// Asset to send, default native, e.i. XLM
#[arg(long, default_value = "native")]
pub asset: builder::Asset,
/// Amount of the aforementioned asset to send.
/// Amount of the aforementioned asset to send. e.g. `10_000_000` (1 XLM)
#[arg(long)]
pub amount: i64,
pub amount: builder::Amount,
}

impl From<&Cmd> for xdr::OperationBody {
fn from(cmd: &Cmd) -> Self {
xdr::OperationBody::Payment(xdr::PaymentOp {
destination: cmd.destination.clone(),
asset: cmd.asset.clone().into(),
amount: cmd.amount,
amount: cmd.amount.into(),
})
}
}
2 changes: 2 additions & 0 deletions cmd/soroban-cli/src/tx/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod amount;
pub mod asset;
pub mod transaction;

pub use amount::Amount;
pub use asset::Asset;
pub use transaction::TxExt;

Expand Down
35 changes: 35 additions & 0 deletions cmd/soroban-cli/src/tx/builder/amount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::str::FromStr;

#[derive(Clone, Debug, Copy)]
pub struct Amount(i64);

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("cannot start or end with `_`: {0}")]
CannotStartOrEndWithUnderscore(String),
#[error(transparent)]
IntParse(#[from] std::num::ParseIntError),
}

impl FromStr for Amount {
type Err = Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
if value.starts_with('_') || value.ends_with('_') {
return Err(Error::CannotStartOrEndWithUnderscore(value.to_string()));
}
Ok(Self(value.replace('_', "").parse::<i64>()?))
}
}

impl From<Amount> for i64 {
fn from(builder: Amount) -> Self {
builder.0
}
}

impl From<&Amount> for i64 {
fn from(builder: &Amount) -> Self {
(*builder).into()
}
}

0 comments on commit 390c220

Please sign in to comment.