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

refactor send to use satpoints instead of ranges #849

Merged
merged 16 commits into from
Nov 29, 2022
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use {
serde::{Deserialize, Serialize},
std::{
cmp::Ordering,
collections::VecDeque,
collections::{BTreeMap, VecDeque},
env,
fmt::{self, Display, Formatter},
fs, io,
Expand Down
52 changes: 51 additions & 1 deletion src/sat_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Copy, Clone)]
pub(crate) struct SatPoint {
pub(crate) outpoint: OutPoint,
pub(crate) offset: u64,
Expand Down Expand Up @@ -29,3 +29,53 @@ impl Decodable for SatPoint {
})
}
}

impl FromStr for SatPoint {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let (outpoint, offset) = s
.rsplit_once(':')
.ok_or_else(|| anyhow!("invalid satpoint: {s}"))?;

Ok(SatPoint {
outpoint: outpoint.parse()?,
offset: offset.parse()?,
})
}
}

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

#[test]
fn from_str_ok() {
assert_eq!(
"1111111111111111111111111111111111111111111111111111111111111111:1:1"
.parse::<SatPoint>()
.unwrap(),
SatPoint {
outpoint: "1111111111111111111111111111111111111111111111111111111111111111:1"
.parse()
.unwrap(),
offset: 1,
}
);
}

#[test]
fn from_str_err() {
"abc".parse::<SatPoint>().unwrap_err();

"abc:xyz".parse::<SatPoint>().unwrap_err();

"1111111111111111111111111111111111111111111111111111111111111111:1"
.parse::<SatPoint>()
.unwrap_err();

"1111111111111111111111111111111111111111111111111111111111111111:1:foo"
.parse::<SatPoint>()
.unwrap_err();
}
}
37 changes: 37 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,43 @@ fn list_unspent(options: &Options, index: &Index) -> Result<Vec<(OutPoint, Vec<(
.collect()
}

#[allow(dead_code)]
casey marked this conversation as resolved.
Show resolved Hide resolved
fn list_utxos(options: &Options) -> Result<BTreeMap<OutPoint, Amount>> {
let client = options.bitcoin_rpc_client()?;

Ok(
client
.list_unspent(None, None, None, None, None)?
.iter()
.map(|utxo| {
let outpoint = OutPoint::new(utxo.txid, utxo.vout);
let amount = utxo.amount;

(outpoint, amount)
})
.collect(),
)
}

fn ordinal_to_satpoint(
ordinal: Ordinal,
utxos: BTreeMap<OutPoint, Vec<(u64, u64)>>,
) -> Option<SatPoint> {
for (outpoint, ranges) in utxos {
let mut offset = 0;
for (start, end) in ranges {
if ordinal.0 >= start && ordinal.0 < end {
return Some(SatPoint {
outpoint,
offset: offset + (ordinal.0 - start),
});
}
offset += end - start;
}
}
None
}

fn get_change_addresses(options: &Options, n: usize) -> Result<Vec<Address>> {
let client = options.bitcoin_rpc_client()?;

Expand Down
14 changes: 12 additions & 2 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,19 @@ impl Inscribe {

let commit_tx_address = Address::p2tr_tweaked(taproot_spend_info.output_key(), network);

let satpoint = ordinal_to_satpoint(ordinal, utxos.clone().into_iter().collect()).unwrap();
casey marked this conversation as resolved.
Show resolved Hide resolved

let unsigned_commit_tx = TransactionBuilder::build_transaction(
utxos.into_iter().collect(),
ordinal,
satpoint,
utxos
.into_iter()
.map(|(outpoint, ranges)| {
(
outpoint,
Amount::from_sat(ranges.iter().map(|(start, end)| end - start).sum()),
)
})
.collect(),
commit_tx_address.clone(),
change,
)?;
Expand Down
9 changes: 3 additions & 6 deletions src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

#[derive(Debug, Parser)]
pub(crate) struct Send {
ordinal: Ordinal,
satpoint: SatPoint,
address: Address,
}

Expand All @@ -18,15 +18,12 @@ impl Send {
);
}

let index = Index::open(&options)?;
index.update()?;

let utxos = list_unspent(&options, &index)?.into_iter().collect();
let utxos = list_utxos(&options)?;

let change = get_change_addresses(&options, 2)?;

let unsigned_transaction =
TransactionBuilder::build_transaction(utxos, self.ordinal, self.address, change)?;
TransactionBuilder::build_transaction(self.satpoint, utxos, self.address, change)?;

let signed_tx = client
.sign_raw_transaction_with_wallet(&unsigned_transaction, None, None)?
Expand Down
Loading