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

Revert runes balances json #3571

Merged
merged 7 commits into from
Apr 18, 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
37 changes: 37 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl Server {
.route("/rune/:rune", get(Self::rune))
.route("/runes", get(Self::runes))
.route("/runes/:page", get(Self::runes_paginated))
.route("/runes/balances", get(Self::runes_balances))
.route("/sat/:sat", get(Self::sat))
.route("/search", get(Self::search_by_query))
.route("/search/*query", get(Self::search_by_path))
Expand Down Expand Up @@ -740,6 +741,34 @@ impl Server {
})
}

async fn runes_balances(
Extension(index): Extension<Arc<Index>>,
AcceptJson(accept_json): AcceptJson,
) -> ServerResult {
task::block_in_place(|| {
Ok(if accept_json {
Json(
index
.get_rune_balance_map()?
.into_iter()
.map(|(rune, balances)| {
(
rune,
balances
.into_iter()
.map(|(outpoint, pile)| (outpoint, pile.amount))
.collect(),
)
})
.collect::<BTreeMap<SpacedRune, BTreeMap<OutPoint, u128>>>(),
)
.into_response()
} else {
StatusCode::NOT_FOUND.into_response()
})
})
}

async fn home(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
Expand Down Expand Up @@ -2546,6 +2575,14 @@ mod tests {
);
}

#[test]
fn html_runes_balances_not_found() {
TestServer::builder()
.chain(Chain::Regtest)
.build()
.assert_response("/runes/balances", StatusCode::NOT_FOUND, "");
}

#[test]
fn fallback() {
let server = TestServer::new();
Expand Down
70 changes: 70 additions & 0 deletions tests/json_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,73 @@ fn get_runes() {
}
);
}

#[test]
fn get_runes_balances() {
let core = mockcore::builder().network(Network::Regtest).build();

let ord = TestServer::spawn_with_server_args(&core, &["--index-runes", "--regtest"], &[]);

create_wallet(&core, &ord);

core.mine_blocks(3);

let rune0 = Rune(RUNE);
let rune1 = Rune(RUNE + 1);
let rune2 = Rune(RUNE + 2);

let e0 = etch(&core, &ord, rune0);
let e1 = etch(&core, &ord, rune1);
let e2 = etch(&core, &ord, rune2);

core.mine_blocks(1);

let rune_balances: BTreeMap<Rune, BTreeMap<OutPoint, u128>> = vec![
(
rune0,
vec![(
OutPoint {
txid: e0.output.reveal,
vout: 1,
},
1000,
)]
.into_iter()
.collect(),
),
(
rune1,
vec![(
OutPoint {
txid: e1.output.reveal,
vout: 1,
},
1000,
)]
.into_iter()
.collect(),
),
(
rune2,
vec![(
OutPoint {
txid: e2.output.reveal,
vout: 1,
},
1000,
)]
.into_iter()
.collect(),
),
]
.into_iter()
.collect();

let response = ord.json_request("/runes/balances");
assert_eq!(response.status(), StatusCode::OK);

let runes_balance_json: BTreeMap<Rune, BTreeMap<OutPoint, u128>> =
serde_json::from_str(&response.text().unwrap()).unwrap();

pretty_assert_eq!(runes_balance_json, rune_balances);
}
Loading