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

Recursive Address /r/address/:address #3892

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ pub struct ChildInscriptions {
pub page: usize,
}

#[derive(Serialize)]
pub struct AddressRecursive {
pub outputs: Vec<OutPoint>,
pub inscriptions: Vec<InscriptionId>,
pub sat_balance: u64,
pub runes_balances: Vec<(SpacedRune, Decimal, Option<char>)>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Inscription {
pub address: Option<String>,
Expand Down
39 changes: 39 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ impl Server {
get(Self::parents_paginated),
)
.route("/preview/:inscription_id", get(Self::preview))
.route("/r/address/:address", get(Self::address_recursive))
.route("/r/blockhash", get(Self::block_hash_json))
.route(
"/r/blockhash/:height",
Expand Down Expand Up @@ -862,6 +863,44 @@ impl Server {
})
}

async fn address_recursive(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
Path(address): Path<Address<NetworkUnchecked>>,
) -> ServerResult {
task::block_in_place(|| {
if !index.has_address_index() {
return Err(ServerError::NotFound(
"this server has no address index".to_string(),
));
}

let address = address
.require_network(server_config.chain.network())
.map_err(|err| ServerError::BadRequest(err.to_string()))?;

let mut outputs = index.get_address_info(&address)?;

outputs.sort();

let sat_balance = index.get_sat_balances_for_outputs(&outputs)?;

let inscriptions = index.get_inscriptions_for_outputs(&outputs)?;

let runes_balances = index.get_aggregated_rune_balances_for_outputs(&outputs)?;

Ok(
Json(api::AddressRecursive {
outputs,
inscriptions,
sat_balance,
runes_balances,
})
.into_response(),
)
})
}

async fn block(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
Expand Down
181 changes: 181 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,44 @@ fn address_page_shows_outputs_and_sat_balance() {
);
}

#[test]
fn address_recursive_shows_outputs_and_sat_balance() {
let core = mockcore::spawn();
let ord = TestServer::spawn_with_args(&core, &["--index-addresses"]);

create_wallet(&core, &ord);
core.mine_blocks(1);

let address = "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4";

let send = CommandBuilder::new(format!("wallet send --fee-rate 8.8 {address} 2btc"))
.core(&core)
.ord(&ord)
.run_and_deserialize_output::<Send>();

core.mine_blocks(1);
use serde_json::json;

let response = ord.request(format!("/r/address/{address}"));
println!("Response: {:?}", response.text().unwrap());

let expected_json = json!({
"outputs": [ OutPoint {
txid: send.txid,
vout: 0
}],
"inscriptions": [],
"sat_balance": 200000000,
"runes_balances": []
})
.to_string();

ord.assert_response_regex(
format!("/r/address/{address}"),
regex::escape(&expected_json),
);
}

#[test]
fn address_page_shows_single_rune() {
let core = mockcore::builder().network(Network::Regtest).build();
Expand Down Expand Up @@ -91,6 +129,33 @@ fn address_page_shows_single_rune() {
format!(".*<dd>.*{}.*: 1000¢</dd>.*", Rune(RUNE)),
);
}
#[test]
fn address_recursive_shows_single_rune() {
let core = mockcore::builder().network(Network::Regtest).build();
let ord =
TestServer::spawn_with_args(&core, &["--index-runes", "--index-addresses", "--regtest"]);

create_wallet(&core, &ord);

etch(&core, &ord, Rune(RUNE));

let address = "bcrt1qs758ursh4q9z627kt3pp5yysm78ddny6txaqgw";

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 8.8 {address} 1000:{}",
Rune(RUNE)
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(6);

let expected_regex = r#"(?s)\{"outputs":\["[^"]*"\],"inscriptions":\[\],"sat_balance":\d+,"runes_balances":\[\[\"AAAAAAAAAAAAA\",\"1000\",\"¢\"\]\]\}"#;

ord.assert_response_regex(format!("/r/address/{address}"), expected_regex);
}

#[test]
fn address_page_shows_multiple_runes() {
Expand Down Expand Up @@ -137,6 +202,45 @@ fn address_page_shows_multiple_runes() {
);
}

#[test]
fn address_recursive_shows_multiple_runes() {
let core = mockcore::builder().network(Network::Regtest).build();
let ord =
TestServer::spawn_with_args(&core, &["--index-runes", "--index-addresses", "--regtest"]);

create_wallet(&core, &ord);

etch(&core, &ord, Rune(RUNE));
etch(&core, &ord, Rune(RUNE + 1));

let address = "bcrt1qs758ursh4q9z627kt3pp5yysm78ddny6txaqgw";

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 8.8 {address} 1000:{}",
Rune(RUNE)
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(6);

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 8.8 {address} 1000:{}",
Rune(RUNE + 1)
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(6);

let expected_regex = r#"(?s)\{"outputs":\["[^"]+:0","[^"]+:0"\],"inscriptions":\[\],"sat_balance":\d+,"runes_balances":\[\["AAAAAAAAAAAAA","1000","¢"\],\["AAAAAAAAAAAAB","1000","¢"\]\]\}"#;
ord.assert_response_regex(format!("/r/address/{address}"), expected_regex);
}

#[test]
fn address_page_shows_aggregated_runes_balance() {
let core = mockcore::builder().network(Network::Regtest).build();
Expand Down Expand Up @@ -177,6 +281,45 @@ fn address_page_shows_aggregated_runes_balance() {
);
}

#[test]
fn address_recusive_shows_aggregated_runes_balance() {
let core = mockcore::builder().network(Network::Regtest).build();
let ord =
TestServer::spawn_with_args(&core, &["--index-runes", "--index-addresses", "--regtest"]);

create_wallet(&core, &ord);

etch(&core, &ord, Rune(RUNE));

let address = "bcrt1qs758ursh4q9z627kt3pp5yysm78ddny6txaqgw";

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 8.8 {address} 250:{}",
Rune(RUNE)
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(6);

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 8.8 {address} 250:{}",
Rune(RUNE)
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(6);

let expected_regex = r#"(?s)\{"outputs":\["[^"]+:2","[^"]+:2"\],"inscriptions":\[\],"sat_balance":\d+,"runes_balances":\[\["AAAAAAAAAAAAA","500","¢"\]\]\}"#;

ord.assert_response_regex(format!("/r/address/{address}"), expected_regex);
}

#[test]
fn address_page_shows_aggregated_inscriptions() {
let core = mockcore::builder().network(Network::Regtest).build();
Expand Down Expand Up @@ -224,6 +367,44 @@ fn address_page_shows_aggregated_inscriptions() {
);
}

#[test]
fn address_recursive_shows_aggregated_inscriptions() {
let core = mockcore::builder().network(Network::Regtest).build();
let ord =
TestServer::spawn_with_args(&core, &["--index-runes", "--index-addresses", "--regtest"]);

create_wallet(&core, &ord);

let (inscription_id_1, _reveal) = inscribe(&core, &ord);

let address = "bcrt1qs758ursh4q9z627kt3pp5yysm78ddny6txaqgw";

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 8.8 {address} {inscription_id_1}",
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(1);

let (inscription_id_2, _reveal) = inscribe(&core, &ord);

CommandBuilder::new(format!(
"--chain regtest --index-runes wallet send --fee-rate 8.8 {address} {inscription_id_2}",
))
.core(&core)
.ord(&ord)
.stdout_regex(".*")
.run_and_deserialize_output::<Output>();

core.mine_blocks(1);

let expected_regex = r#"(?s)\{"outputs":\["[^"]+:0","[^"]+:0"\],"inscriptions":\["[a-f0-9]{64}i\d","[a-f0-9]{64}i\d"\],"sat_balance":\d+,"runes_balances":\[\]\}"#;
ord.assert_response_regex(format!("/r/address/{address}"), expected_regex);
}

#[test]
fn inscription_page() {
let core = mockcore::spawn();
Expand Down
Loading