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 setting the sat to inscribe #2765

Merged
merged 6 commits into from
Nov 28, 2023
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
19 changes: 11 additions & 8 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,8 @@ impl Index {
)
}

pub(crate) fn find(&self, sat: u64) -> Result<Option<SatPoint>> {
pub(crate) fn find(&self, sat: Sat) -> Result<Option<SatPoint>> {
let sat = sat.0;
let rtx = self.begin_read()?;

if rtx.block_count()? <= Sat(sat).height().n() {
Expand Down Expand Up @@ -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<Option<Vec<FindRangeOutput>>> {
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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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]
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/subcommand/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
},
Expand Down
2 changes: 2 additions & 0 deletions src/subcommand/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl Preview {
postage: Some(TransactionBuilder::TARGET_POSTAGE),
reinscribe: false,
satpoint: None,
sat: None,
},
)),
}
Expand Down Expand Up @@ -144,6 +145,7 @@ impl Preview {
postage: Some(TransactionBuilder::TARGET_POSTAGE),
reinscribe: false,
satpoint: None,
sat: None,
},
)),
}
Expand Down
39 changes: 38 additions & 1 deletion src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ pub(crate) struct Inscribe {
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>,
}

impl Inscribe {
Expand All @@ -115,6 +117,20 @@ impl Inscribe {
let index = Index::open(&options)?;
index.update()?;

let satpoint = if let Some(sat) = self.sat {
if !index.has_sat_index() {
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))),
}
} else {
self.satpoint
};

let utxos = index.get_unspent_outputs(Wallet::load(&options)?)?;

let locked_utxos = index.get_locked_outputs(Wallet::load(&options)?)?;
Expand Down Expand Up @@ -188,7 +204,7 @@ impl Inscribe {
postage,
reinscribe: self.reinscribe,
reveal_fee_rate: self.fee_rate,
satpoint: self.satpoint,
satpoint,
}
.inscribe(chain, &index, &client, &locked_utxos, &utxos)
}
Expand Down Expand Up @@ -1322,4 +1338,25 @@ inscriptions:
.contains("error: the following required arguments were not provided:\n <--file <FILE>|--batch <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.*"
);
}
}
46 changes: 46 additions & 0 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1577,3 +1577,49 @@ fn batch_same_sat_with_parent() {
format!(r".*<a href=/inscription/{}>.*</a>.*<a href=/inscription/{}>.*</a>.*<a href=/inscription/{}>.*</a>.*", 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);

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!(".*<a href=/inscription/{inscription}>.*"),
);

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();
}