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

Batch inscribe on same sat #2749

Merged
merged 17 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
5 changes: 3 additions & 2 deletions batch.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# example batch file

# there are two modes:
# - `separate-outputs`: place all inscriptions in separate postage-sized outputs
# - `shared-output`: place inscriptions in a single output separated by postage
# - `separate-outputs`: inscribe on separate postage-sized outputs
# - `shared-output`: inscribe on a single output separated by postage
# - `same-sat`: inscribe on the same sat
mode: separate-outputs

# parent inscription:
Expand Down
15 changes: 11 additions & 4 deletions src/subcommand/wallet/inscribe/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl Batch {
let index = u32::try_from(index).unwrap();

let vout = match self.mode {
Mode::SharedOutput => {
Mode::SharedOutput | Mode::SameSat => {
if self.parent_info.is_some() {
1
} else {
Expand All @@ -150,7 +150,7 @@ impl Batch {

let offset = match self.mode {
Mode::SharedOutput => u64::from(index) * self.postage.to_sat(),
Mode::SeparateOutputs => 0,
Mode::SeparateOutputs | Mode::SameSat => 0,
};

inscriptions_output.push(InscriptionInfo {
Expand Down Expand Up @@ -198,6 +198,11 @@ impl Batch {
}

match self.mode {
Mode::SameSat => assert_eq!(
self.destinations.len(),
1,
"invariant: same-sat has only one destination"
),
Mode::SeparateOutputs => assert_eq!(
self.destinations.len(),
self.inscriptions.len(),
Expand Down Expand Up @@ -286,7 +291,7 @@ impl Batch {
.map(|destination| TxOut {
script_pubkey: destination.script_pubkey(),
value: match self.mode {
Mode::SeparateOutputs => self.postage.to_sat(),
Mode::SeparateOutputs | Mode::SameSat => self.postage.to_sat(),
Mode::SharedOutput => total_postage.to_sat(),
},
})
Expand Down Expand Up @@ -520,6 +525,8 @@ impl Batch {

#[derive(PartialEq, Debug, Copy, Clone, Serialize, Deserialize, Default)]
pub(crate) enum Mode {
#[serde(rename = "same-sat")]
SameSat,
#[default]
#[serde(rename = "separate-outputs")]
SeparateOutputs,
Expand Down Expand Up @@ -619,7 +626,7 @@ impl Batchfile {
}

let destinations = match self.mode {
Mode::SharedOutput => vec![get_change_address(client, chain)?],
Mode::SharedOutput | Mode::SameSat => vec![get_change_address(client, chain)?],
Mode::SeparateOutputs => self
.inscriptions
.iter()
Expand Down
1 change: 1 addition & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub(crate) fn tx_out(value: u64, address: Address) -> TxOut {
}
}

#[derive(Default, Debug)]
pub(crate) struct InscriptionTemplate {
pub(crate) parent: Option<InscriptionId>,
}
Expand Down
143 changes: 143 additions & 0 deletions tests/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,3 +1434,146 @@ fn batch_inscribe_works_with_some_destinations_set_and_others_not() {
<dd class=monospace>bc1pxwww0ct9ue7e8tdnlmug5m2tamfn7q06sahstg39ys4c9f3340qqxrdu9k</dd>.*",
);
}

#[test]
fn batch_same_sat() {
let rpc_server = test_bitcoincore_rpc::spawn();
rpc_server.mine_blocks(1);

create_wallet(&rpc_server);

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",
"mode: same-sat\ninscriptions:\n- file: inscription.txt\n- file: tulip.png\n- file: meow.wav\n"
)
.rpc_server(&rpc_server)
.run_and_deserialize_output::<Inscribe>();

assert_eq!(
output.inscriptions[0].location,
output.inscriptions[1].location
);
assert_eq!(
output.inscriptions[1].location,
output.inscriptions[2].location
);

rpc_server.mine_blocks(1);

let ord_server = TestServer::spawn_with_args(&rpc_server, &[]);

let outpoint = output.inscriptions[0].location.outpoint;

ord_server.assert_response_regex(
format!("/inscription/{}", output.inscriptions[0].id),
format!(
r".*<dt>location</dt>.*<dd class=monospace>{}:0</dd>.*",
outpoint
),
);

ord_server.assert_response_regex(
format!("/inscription/{}", output.inscriptions[1].id),
format!(
r".*<dt>location</dt>.*<dd class=monospace>{}:0</dd>.*",
outpoint
),
);

ord_server.assert_response_regex(
format!("/inscription/{}", output.inscriptions[2].id),
format!(
r".*<dt>location</dt>.*<dd class=monospace>{}:0</dd>.*",
outpoint
),
);

ord_server.assert_response_regex(
format!("/output/{}", output.inscriptions[0].location.outpoint),
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 batch_same_sat_with_parent() {
let rpc_server = test_bitcoincore_rpc::spawn();
rpc_server.mine_blocks(1);

create_wallet(&rpc_server);

let parent_output = CommandBuilder::new("wallet inscribe --fee-rate 5.0 --file parent.png")
.write("parent.png", [1; 520])
.rpc_server(&rpc_server)
.run_and_deserialize_output::<Inscribe>();

rpc_server.mine_blocks(1);

let parent_id = parent_output.inscriptions[0].id;

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\nparent: {parent_id}\ninscriptions:\n- file: inscription.txt\n- file: tulip.png\n- file: meow.wav\n")
)
.rpc_server(&rpc_server)
.run_and_deserialize_output::<Inscribe>();

assert_eq!(
output.inscriptions[0].location,
output.inscriptions[1].location
);
assert_eq!(
output.inscriptions[1].location,
output.inscriptions[2].location
);

rpc_server.mine_blocks(1);

let ord_server = TestServer::spawn_with_args(&rpc_server, &[]);

let txid = output.inscriptions[0].location.outpoint.txid;

ord_server.assert_response_regex(
format!("/inscription/{}", parent_id),
format!(
r".*<dt>location</dt>.*<dd class=monospace>{}:0:0</dd>.*",
txid
),
);

ord_server.assert_response_regex(
format!("/inscription/{}", output.inscriptions[0].id),
format!(
r".*<dt>location</dt>.*<dd class=monospace>{}:1:0</dd>.*",
txid
),
);

ord_server.assert_response_regex(
format!("/inscription/{}", output.inscriptions[1].id),
format!(
r".*<dt>location</dt>.*<dd class=monospace>{}:1:0</dd>.*",
txid
),
);

ord_server.assert_response_regex(
format!("/inscription/{}", output.inscriptions[2].id),
format!(
r".*<dt>location</dt>.*<dd class=monospace>{}:1:0</dd>.*",
txid
),
);

ord_server.assert_response_regex(
format!("/output/{}", output.inscriptions[0].location.outpoint),
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),
);
}