From d388c186962e0cccc3382f80ff51ec38222abb51 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Sun, 26 Nov 2023 17:06:12 -0300 Subject: [PATCH 1/5] Add ability to specify sat to inscribe --- src/subcommand/wallet/inscribe.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index b12b5e67a8..9f0b8f3786 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -106,6 +106,8 @@ pub(crate) struct Inscribe { pub(crate) reinscribe: bool, #[arg(long, help = "Inscribe .")] pub(crate) satpoint: Option, + #[arg(long, help = "Inscribe .")] + pub(crate) sat: Option, } impl Inscribe { From 0f557157e18151cfaf15c5698965240c1cf8becc Mon Sep 17 00:00:00 2001 From: raphjaph Date: Mon, 27 Nov 2023 16:43:04 -0300 Subject: [PATCH 2/5] Find sat --- src/subcommand/wallet/inscribe.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index 9f0b8f3786..59efbf92a8 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -106,7 +106,7 @@ pub(crate) struct Inscribe { pub(crate) reinscribe: bool, #[arg(long, help = "Inscribe .")] pub(crate) satpoint: Option, - #[arg(long, help = "Inscribe .")] + #[arg(long, help = "Inscribe .", conflicts_with = "satpoint")] pub(crate) sat: Option, } @@ -117,6 +117,12 @@ impl Inscribe { let index = Index::open(&options)?; index.update()?; + let satpoint = if let Some(sat) = self.sat { + index.find(sat.0)? + } else { + self.satpoint + }; + let utxos = index.get_unspent_outputs(Wallet::load(&options)?)?; let locked_utxos = index.get_locked_outputs(Wallet::load(&options)?)?; @@ -190,7 +196,7 @@ impl Inscribe { postage, reinscribe: self.reinscribe, reveal_fee_rate: self.fee_rate, - satpoint: self.satpoint, + satpoint, } .inscribe(chain, &index, &client, &locked_utxos, &utxos) } From ed74963cff1f60644a939dd28ee1c66c4e34f2ec Mon Sep 17 00:00:00 2001 From: raphjaph Date: Tue, 28 Nov 2023 17:45:58 -0300 Subject: [PATCH 3/5] make it run --- src/subcommand/preview.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/subcommand/preview.rs b/src/subcommand/preview.rs index 28f2785f74..471cf09061 100644 --- a/src/subcommand/preview.rs +++ b/src/subcommand/preview.rs @@ -113,6 +113,7 @@ impl Preview { postage: Some(TransactionBuilder::TARGET_POSTAGE), reinscribe: false, satpoint: None, + sat: None, }, )), } @@ -144,6 +145,7 @@ impl Preview { postage: Some(TransactionBuilder::TARGET_POSTAGE), reinscribe: false, satpoint: None, + sat: None, }, )), } From 64b55925888f074fa99cb95c2c1dac315526022e Mon Sep 17 00:00:00 2001 From: raphjaph Date: Tue, 28 Nov 2023 18:26:34 -0300 Subject: [PATCH 4/5] Allow sat argument when inscribing --- src/subcommand/wallet/inscribe.rs | 24 +++++++++++++++++++++++ tests/wallet/inscribe.rs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index 8f945b7caf..fdd7124f77 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -118,6 +118,9 @@ impl Inscribe { index.update()?; let satpoint = if let Some(sat) = self.sat { + if !index.has_sat_index() { + return Err(anyhow!("run with --index-sats to use the --sat argument")); + } index.find(sat.0)? } else { self.satpoint @@ -1330,4 +1333,25 @@ inscriptions: .contains("error: the following required arguments were not provided:\n <--file |--batch >") ); } + + #[test] + fn satpoint_and_sat_flags_conflict() { + assert_regex_match!( + Arguments::try_parse_from([ + "ord", + "--index-sats", + "wallet", + "inscribe", + "--sat", + "50000000000", + "--satpoint", + "038112028c55f3f77cc0b8b413df51f70675f66be443212da0642b7636f68a00:1:0", + "--file", + "baz", + ]) + .unwrap_err() + .to_string(), + ".*--sat.*cannot be used with.*--satpoint.*" + ); + } } diff --git a/tests/wallet/inscribe.rs b/tests/wallet/inscribe.rs index 639d4e2e3e..ee5dc473fb 100644 --- a/tests/wallet/inscribe.rs +++ b/tests/wallet/inscribe.rs @@ -1577,3 +1577,35 @@ fn batch_same_sat_with_parent() { format!(r".*.*.*.*.*.*.*", output.inscriptions[0].id, output.inscriptions[1].id, output.inscriptions[2].id), ); } + +#[test] +fn inscribe_with_sat_arg() { + let rpc_server = test_bitcoincore_rpc::spawn(); + create_wallet(&rpc_server); + rpc_server.mine_blocks(2); + + CommandBuilder::new("wallet inscribe --file foo.txt --sat 5010000000 --fee-rate 1") + .write("foo.txt", "FOO") + .rpc_server(&rpc_server) + .expected_exit_code(1) + .expected_stderr("error: run with --index-sats to use the --sat argument\n"); + + let Inscribe { inscriptions, .. } = CommandBuilder::new( + "--index-sats wallet inscribe --file foo.txt --sat 5010000000 --fee-rate 1", + ) + .write("foo.txt", "FOO") + .rpc_server(&rpc_server) + .run_and_deserialize_output(); + + let inscription = inscriptions[0].id; + + rpc_server.mine_blocks(1); + + TestServer::spawn_with_args(&rpc_server, &["--index-sats"]).assert_response_regex( + "/sat/5010000000", + format!(".*.*"), + ); + + TestServer::spawn_with_args(&rpc_server, &[]) + .assert_response_regex(format!("/content/{inscription}",), "FOO"); +} From 458480d0749283cf93f5630ec511419edce51609 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Tue, 28 Nov 2023 18:51:46 -0300 Subject: [PATCH 5/5] functions should take Sat instead of u64 --- src/index.rs | 19 +++++++++++-------- src/subcommand/find.rs | 4 ++-- src/subcommand/wallet/inscribe.rs | 9 +++++++-- tests/wallet/inscribe.rs | 26 ++++++++++++++++++++------ 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/index.rs b/src/index.rs index 94bc4c6ad8..2d78521b2a 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1282,7 +1282,8 @@ impl Index { ) } - pub(crate) fn find(&self, sat: u64) -> Result> { + pub(crate) fn find(&self, sat: Sat) -> Result> { + let sat = sat.0; let rtx = self.begin_read()?; if rtx.block_count()? <= Sat(sat).height().n() { @@ -1311,9 +1312,11 @@ impl Index { pub(crate) fn find_range( &self, - range_start: u64, - range_end: u64, + range_start: Sat, + range_end: Sat, ) -> Result>> { + let range_start = range_start.0; + let range_end = range_end.0; let rtx = self.begin_read()?; if rtx.block_count()? < Sat(range_end - 1).height().n() + 1 { @@ -2029,7 +2032,7 @@ mod tests { fn find_first_sat() { let context = Context::builder().arg("--index-sats").build(); assert_eq!( - context.index.find(0).unwrap().unwrap(), + context.index.find(Sat(0)).unwrap().unwrap(), SatPoint { outpoint: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0" .parse() @@ -2043,7 +2046,7 @@ mod tests { fn find_second_sat() { let context = Context::builder().arg("--index-sats").build(); assert_eq!( - context.index.find(1).unwrap().unwrap(), + context.index.find(Sat(1)).unwrap().unwrap(), SatPoint { outpoint: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0" .parse() @@ -2058,7 +2061,7 @@ mod tests { let context = Context::builder().arg("--index-sats").build(); context.mine_blocks(1); assert_eq!( - context.index.find(50 * COIN_VALUE).unwrap().unwrap(), + context.index.find(Sat(50 * COIN_VALUE)).unwrap().unwrap(), SatPoint { outpoint: "30f2f037629c6a21c1f40ed39b9bd6278df39762d68d07f49582b23bcb23386a:0" .parse() @@ -2071,7 +2074,7 @@ mod tests { #[test] fn find_unmined_sat() { let context = Context::builder().arg("--index-sats").build(); - assert_eq!(context.index.find(50 * COIN_VALUE).unwrap(), None); + assert_eq!(context.index.find(Sat(50 * COIN_VALUE)).unwrap(), None); } #[test] @@ -2085,7 +2088,7 @@ mod tests { }); context.mine_blocks(1); assert_eq!( - context.index.find(50 * COIN_VALUE).unwrap().unwrap(), + context.index.find(Sat(50 * COIN_VALUE)).unwrap().unwrap(), SatPoint { outpoint: OutPoint::new(spend_txid, 0), offset: 0, diff --git a/src/subcommand/find.rs b/src/subcommand/find.rs index deb28c44f2..68884679df 100644 --- a/src/subcommand/find.rs +++ b/src/subcommand/find.rs @@ -31,11 +31,11 @@ impl Find { index.update()?; match self.end { - Some(end) => match index.find_range(self.sat.0, end.0)? { + Some(end) => match index.find_range(self.sat, end)? { Some(result) => Ok(Box::new(result)), None => Err(anyhow!("range has not been mined as of index height")), }, - None => match index.find(self.sat.0)? { + None => match index.find(self.sat)? { Some(satpoint) => Ok(Box::new(Output { satpoint })), None => Err(anyhow!("sat has not been mined as of index height")), }, diff --git a/src/subcommand/wallet/inscribe.rs b/src/subcommand/wallet/inscribe.rs index fdd7124f77..98d24c335e 100644 --- a/src/subcommand/wallet/inscribe.rs +++ b/src/subcommand/wallet/inscribe.rs @@ -119,9 +119,14 @@ impl Inscribe { let satpoint = if let Some(sat) = self.sat { if !index.has_sat_index() { - return Err(anyhow!("run with --index-sats to use the --sat argument")); + return Err(anyhow!( + "index must be built with `--index-sats` to use `--sat`" + )); + } + match index.find(sat)? { + Some(satpoint) => Some(satpoint), + None => return Err(anyhow!(format!("could not find sat {}", sat))), } - index.find(sat.0)? } else { self.satpoint }; diff --git a/tests/wallet/inscribe.rs b/tests/wallet/inscribe.rs index ee5dc473fb..21b9f2b5ba 100644 --- a/tests/wallet/inscribe.rs +++ b/tests/wallet/inscribe.rs @@ -1584,12 +1584,6 @@ fn inscribe_with_sat_arg() { create_wallet(&rpc_server); rpc_server.mine_blocks(2); - CommandBuilder::new("wallet inscribe --file foo.txt --sat 5010000000 --fee-rate 1") - .write("foo.txt", "FOO") - .rpc_server(&rpc_server) - .expected_exit_code(1) - .expected_stderr("error: run with --index-sats to use the --sat argument\n"); - let Inscribe { inscriptions, .. } = CommandBuilder::new( "--index-sats wallet inscribe --file foo.txt --sat 5010000000 --fee-rate 1", ) @@ -1609,3 +1603,23 @@ fn inscribe_with_sat_arg() { TestServer::spawn_with_args(&rpc_server, &[]) .assert_response_regex(format!("/content/{inscription}",), "FOO"); } + +#[test] +fn inscribe_with_sat_arg_fails_if_no_index_or_not_found() { + let rpc_server = test_bitcoincore_rpc::spawn(); + create_wallet(&rpc_server); + + CommandBuilder::new("wallet inscribe --file foo.txt --sat 5010000000 --fee-rate 1") + .write("foo.txt", "FOO") + .rpc_server(&rpc_server) + .expected_exit_code(1) + .expected_stderr("error: index must be built with `--index-sats` to use `--sat`\n") + .run_and_extract_stdout(); + + CommandBuilder::new("--index-sats wallet inscribe --sat 5000000000 --file foo.txt --fee-rate 1") + .write("foo.txt", "FOO") + .rpc_server(&rpc_server) + .expected_exit_code(1) + .expected_stderr("error: could not find sat 5000000000\n") + .run_and_extract_stdout(); +}