forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsend.rs
97 lines (78 loc) Β· 2.48 KB
/
send.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use {super::*, crate::wallet::Wallet};
#[derive(Debug, Parser)]
pub(crate) struct Send {
address: Address,
outgoing: Outgoing,
#[clap(
long,
default_value = "1.0",
help = "Use fee rate of <FEE_RATE> sats/vB"
)]
fee_rate: FeeRate,
}
#[derive(Serialize, Deserialize)]
pub struct Output {
pub transaction: Txid,
}
impl Send {
pub(crate) fn run(self, options: Options) -> Result {
let client = options.bitcoin_rpc_client_for_wallet_command(false)?;
if !self.address.is_valid_for_network(options.chain().network()) {
bail!(
"Address `{}` is not valid for {}",
self.address,
options.chain()
);
}
let index = Index::open(&options)?;
index.update()?;
let unspent_outputs = index.get_unspent_outputs(Wallet::load(&options)?)?;
let inscriptions = index.get_inscriptions(None)?;
let satpoint = match self.outgoing {
Outgoing::SatPoint(satpoint) => {
for inscription_satpoint in inscriptions.keys() {
if satpoint == *inscription_satpoint {
bail!("inscriptions must be sent by inscription ID");
}
}
satpoint
}
Outgoing::InscriptionId(id) => index
.get_inscription_satpoint_by_id(id)?
.ok_or_else(|| anyhow!("Inscription {id} not found"))?,
Outgoing::Amount(amount) => {
let all_inscription_outputs = inscriptions
.keys()
.map(|satpoint| satpoint.outpoint)
.collect::<HashSet<OutPoint>>();
let wallet_inscription_outputs = unspent_outputs
.keys()
.filter(|utxo| all_inscription_outputs.contains(utxo))
.cloned()
.collect::<Vec<OutPoint>>();
if !client.lock_unspent(&wallet_inscription_outputs)? {
bail!("failed to lock ordinal UTXOs");
}
let txid =
client.send_to_address(&self.address, amount, None, None, None, None, None, None)?;
print_json(Output { transaction: txid })?;
return Ok(());
}
};
let change = [get_change_address(&client)?, get_change_address(&client)?];
let unsigned_transaction = TransactionBuilder::build_transaction_with_postage(
satpoint,
inscriptions,
unspent_outputs,
self.address,
change,
self.fee_rate,
)?;
let signed_tx = client
.sign_raw_transaction_with_wallet(&unsigned_transaction, None, None)?
.hex;
let txid = client.send_raw_transaction(&signed_tx)?;
println!("{txid}");
Ok(())
}
}