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

Allow sending sats by name only #3344

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 13 additions & 19 deletions src/outgoing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ use super::*;
pub enum Outgoing {
Amount(Amount),
InscriptionId(InscriptionId),
SatPoint(SatPoint),
Sat(Sat),
Rune { decimal: Decimal, rune: SpacedRune },
Sat(Sat),
SatPoint(SatPoint),
}

impl Display for Outgoing {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Amount(amount) => write!(f, "{}", amount.to_string().to_lowercase()),
Self::InscriptionId(inscription_id) => inscription_id.fmt(f),
Self::Sat(sat) => sat.fmt(f),
Self::SatPoint(satpoint) => satpoint.fmt(f),
Self::Rune { decimal, rune } => write!(f, "{decimal} {rune}"),
Self::Sat(sat) => write!(f, "{}", sat.name()),
Self::SatPoint(satpoint) => satpoint.fmt(f),
}
}
}
Expand Down Expand Up @@ -63,7 +63,7 @@ impl FromStr for Outgoing {
.unwrap();
}

Ok(if s.parse::<Sat>().is_ok() {
Ok(if re::SAT_NAME.is_match(s) {
Self::Sat(s.parse()?)
} else if re::SATPOINT.is_match(s) {
Self::SatPoint(s.parse()?)
Expand Down Expand Up @@ -93,11 +93,8 @@ mod tests {
assert_eq!(s.parse::<Outgoing>().unwrap(), outgoing);
}

case("0", Outgoing::Sat("0".parse().unwrap()));
case(
"2099999997689999",
Outgoing::Sat("2099999997689999".parse().unwrap()),
);
case("nvtdijuwxlp", Outgoing::Sat("nvtdijuwxlp".parse().unwrap()));
case("a", Outgoing::Sat("a".parse().unwrap()));

case(
"0000000000000000000000000000000000000000000000000000000000000000i0",
Expand Down Expand Up @@ -179,11 +176,8 @@ mod tests {
assert_eq!(s, outgoing.to_string());
}

case("0", Outgoing::Sat("0".parse().unwrap()));
case(
"2099999997689999",
Outgoing::Sat("2099999997689999".parse().unwrap()),
);
case("nvtdijuwxlp", Outgoing::Sat("nvtdijuwxlp".parse().unwrap()));
case("a", Outgoing::Sat("a".parse().unwrap()));

case(
"0000000000000000000000000000000000000000000000000000000000000000i0",
Expand Down Expand Up @@ -232,12 +226,12 @@ mod tests {
assert_eq!(serde_json::from_str::<Outgoing>(j).unwrap(), o);
}

case("0", "\"0\"", Outgoing::Sat("0".parse().unwrap()));
case(
"2099999997689999",
"\"2099999997689999\"",
Outgoing::Sat("2099999997689999".parse().unwrap()),
"nvtdijuwxlp",
"\"nvtdijuwxlp\"",
Outgoing::Sat("nvtdijuwxlp".parse().unwrap()),
);
case("a", "\"a\"", Outgoing::Sat("a".parse().unwrap()));

case(
"0000000000000000000000000000000000000000000000000000000000000000i0",
Expand Down
13 changes: 12 additions & 1 deletion src/re.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ lazy_static! {
pub(crate) static ref OUTPOINT: Regex = re(r"[[:xdigit:]]{64}:\d+");
pub(crate) static ref RUNE_ID: Regex = re(r"[0-9]+:[0-9]+");
pub(crate) static ref SATPOINT: Regex = re(r"[[:xdigit:]]{64}:\d+:\d+");
pub(crate) static ref SAT_NAME: Regex = re(r"[a-z]+");
pub(crate) static ref SAT_NAME: Regex = re(r"[a-z]{1,11}");
pub(crate) static ref SPACED_RUNE: Regex = re(r"[A-Z•.]+");
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn sat_name() {
assert!(SAT_NAME.is_match(&Sat(0).name()));
assert!(SAT_NAME.is_match(&Sat::LAST.name()));
}
}
7 changes: 4 additions & 3 deletions tests/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ fn send_uninscribed_sat() {

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

let sat = 1;
let sat = Sat(1);

CommandBuilder::new(format!(
"wallet send --fee-rate 1 bc1qcqgs2pps4u4yedfyl5pysdjjncs8et5utseepv {sat}"
"wallet send --fee-rate 1 bc1qcqgs2pps4u4yedfyl5pysdjjncs8et5utseepv {}",
sat.name(),
))
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
Expand Down Expand Up @@ -144,7 +145,7 @@ fn send_inscription_by_sat() {

let address = "bc1qcqgs2pps4u4yedfyl5pysdjjncs8et5utseepv";

let output = CommandBuilder::new(format!("wallet send --fee-rate 1 {address} {sat}"))
let output = CommandBuilder::new(format!("wallet send --fee-rate 1 {address} {}", sat.name()))
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<Send>();
Expand Down
Loading