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 satpoint in same-sat batch mode #3100

Merged
merged 8 commits into from
Feb 12, 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
33 changes: 15 additions & 18 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) struct Inscribe {
help = "Inscribe multiple inscriptions defined in a yaml <BATCH_FILE>.",
conflicts_with_all = &[
"cbor_metadata", "delegate", "destination", "file", "json_metadata", "metaprotocol",
"parent", "postage", "reinscribe", "satpoint"
"parent", "postage", "reinscribe", "sat", "satpoint"
]
)]
pub(crate) batch: Option<PathBuf>,
Expand Down Expand Up @@ -64,10 +64,10 @@ pub(crate) struct Inscribe {
pub(crate) postage: Option<Amount>,
#[clap(long, help = "Allow reinscription.")]
pub(crate) reinscribe: bool,
#[arg(long, help = "Inscribe <SATPOINT>.")]
pub(crate) satpoint: Option<SatPoint>,
#[arg(long, help = "Inscribe <SAT>.", conflicts_with = "satpoint")]
pub(crate) sat: Option<Sat>,
#[arg(long, help = "Inscribe <SATPOINT>.", conflicts_with = "sat")]
pub(crate) satpoint: Option<SatPoint>,
}

impl Inscribe {
Expand All @@ -87,9 +87,8 @@ impl Inscribe {
let inscriptions;
let mode;
let parent_info;
let sat;

match (self.file, self.batch) {
let satpoint = match (self.file, self.batch) {
(Some(file), None) => {
parent_info = wallet.get_parent_info(self.parent, &utxos)?;

Expand All @@ -115,12 +114,16 @@ impl Inscribe {

mode = Mode::SeparateOutputs;

sat = self.sat;

destinations = vec![match self.destination.clone() {
Some(destination) => destination.require_network(chain.network())?,
None => wallet.get_change_address()?,
}];

if let Some(sat) = self.sat {
Some(wallet.find_sat_in_outputs(sat, &utxos)?)
} else {
self.satpoint
}
}
(None, Some(batch)) => {
let batchfile = Batchfile::load(&batch)?;
Expand All @@ -135,26 +138,19 @@ impl Inscribe {
(inscriptions, destinations) = batchfile.inscriptions(
&wallet,
parent_info.as_ref().map(|info| info.tx_out.value),
metadata,
postage,
self.compress,
)?;

mode = batchfile.mode;

if batchfile.sat.is_some() && mode != Mode::SameSat {
return Err(anyhow!("`sat` can only be set in `same-sat` mode"));
if let Some(sat) = batchfile.sat {
Some(wallet.find_sat_in_outputs(sat, &utxos)?)
} else {
batchfile.satpoint
}

sat = batchfile.sat;
}
_ => unreachable!(),
}

let satpoint = if let Some(sat) = sat {
Some(wallet.find_sat_in_outputs(sat, &utxos)?)
} else {
self.satpoint
};

Batch {
Expand Down Expand Up @@ -1260,6 +1256,7 @@ inscriptions:
),
("--cbor-metadata", Some("foo")),
("--json-metadata", Some("foo")),
("--sat", Some("0")),
(
"--satpoint",
Some("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0:0"),
Expand Down
57 changes: 30 additions & 27 deletions src/wallet/inscribe/batch_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,41 @@ pub struct Batchfile {
pub(crate) parent: Option<InscriptionId>,
pub(crate) postage: Option<u64>,
pub(crate) sat: Option<Sat>,
pub(crate) satpoint: Option<SatPoint>,
}

impl Batchfile {
pub(crate) fn load(path: &Path) -> Result<Batchfile> {
let batchfile: Batchfile = serde_yaml::from_reader(File::open(path)?)?;

if batchfile.inscriptions.is_empty() {
bail!("batchfile must contain at least one inscription");
ensure!(
!batchfile.inscriptions.is_empty(),
"batchfile must contain at least one inscription",
);

let sat_and_satpoint = batchfile.sat.is_some() && batchfile.satpoint.is_some();

ensure!(
!sat_and_satpoint,
"batchfile cannot set both `sat` and `satpoint`",
);

let sat_or_satpoint = batchfile.sat.is_some() || batchfile.satpoint.is_some();

if sat_or_satpoint {
ensure!(
batchfile.mode == Mode::SameSat,
"neither `sat` nor `satpoint` can be set in `same-sat` mode",
);
}

if batchfile
.inscriptions
.iter()
.any(|entry| entry.destination.is_some())
&& (batchfile.mode == Mode::SharedOutput || batchfile.mode == Mode::SameSat)
{
bail!("individual inscription destinations cannot be set in shared-output or same-sat mode");
}

Ok(batchfile)
Expand All @@ -25,30 +52,9 @@ impl Batchfile {
&self,
wallet: &Wallet,
parent_value: Option<u64>,
metadata: Option<Vec<u8>>,
postage: Amount,
compress: bool,
) -> Result<(Vec<Inscription>, Vec<Address>)> {
assert!(!self.inscriptions.is_empty());

if self
.inscriptions
.iter()
.any(|entry| entry.destination.is_some())
&& (self.mode == Mode::SharedOutput || self.mode == Mode::SameSat)
{
return Err(anyhow!(
"individual inscription destinations cannot be set in shared-output or same-sat mode"
));
}

if metadata.is_some() {
assert!(self
.inscriptions
.iter()
.all(|entry| entry.metadata.is_none()));
}

let mut pointer = parent_value.unwrap_or_default();

let mut inscriptions = Vec::new();
Expand All @@ -64,10 +70,7 @@ impl Batchfile {
wallet.chain(),
compress,
entry.delegate,
match &metadata {
Some(metadata) => Some(metadata.clone()),
None => entry.metadata()?,
},
entry.metadata()?,
entry.metaprotocol.clone(),
self.parent,
&entry.file,
Expand Down
48 changes: 46 additions & 2 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ fn batch_inscribe_fails_if_invalid_network_destination_address() {
}

#[test]
fn batch_inscribe_fails_with_shared_output_and_destination_set() {
fn batch_inscribe_fails_with_shared_output_or_same_sat_and_destination_set() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();

let ord_rpc_server = TestServer::spawn_with_server_args(&bitcoin_rpc_server, &[], &[]);
Expand All @@ -1519,6 +1519,16 @@ fn batch_inscribe_fails_with_shared_output_and_destination_set() {
.expected_exit_code(1)
.stderr_regex("error: individual inscription destinations cannot be set in shared-output or same-sat mode\n")
.run_and_extract_stdout();

CommandBuilder::new("wallet inscribe --fee-rate 2.1 --batch batch.yaml")
.write("inscription.txt", "Hello World")
.write("tulip.png", "")
.write("batch.yaml", "mode: same-sat\nsat: 5000000000\ninscriptions:\n- file: inscription.txt\n destination: bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4\n- file: tulip.png")
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.expected_exit_code(1)
.stderr_regex("error: individual inscription destinations cannot be set in shared-output or same-sat mode\n")
.run_and_extract_stdout();
}

#[test]
Expand Down Expand Up @@ -1848,10 +1858,44 @@ fn batch_inscribe_with_sat_arg_fails_if_wrong_mode() {
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.expected_exit_code(1)
.expected_stderr("error: `sat` can only be set in `same-sat` mode\n")
.expected_stderr("error: neither `sat` nor `satpoint` can be set in `same-sat` mode\n")
.run_and_extract_stdout();
}

#[test]
fn batch_inscribe_with_satpoint() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();

let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--index-sats"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

let txid = bitcoin_rpc_server.mine_blocks(1)[0].txdata[0].txid();

let output = CommandBuilder::new("wallet inscribe --fee-rate 1 --batch batch.yaml")
.write("inscription.txt", "Hello World")
.write("tulip.png", [0; 555])
.write("meow.wav", [0; 2048])
.write(
"batch.yaml",
format!("mode: same-sat\nsatpoint: {txid}:0:55555\ninscriptions:\n- file: inscription.txt\n- file: tulip.png\n- file: meow.wav\n", )
)
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<Inscribe>();

bitcoin_rpc_server.mine_blocks(1);

ord_rpc_server.assert_response_regex(
"/sat/5000055555",
format!(
".*<a href=/inscription/{}>.*<a href=/inscription/{}>.*<a href=/inscription/{}>.*",
output.inscriptions[0].id, output.inscriptions[1].id, output.inscriptions[2].id
),
);
}

#[test]
fn batch_inscribe_with_fee_rate() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();
Expand Down
Loading