forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON API endpoint
/sat/<SAT>
(ordinals#2250)
Co-authored-by: raphjaph <[email protected]>
- Loading branch information
1 parent
fb0b1e8
commit 13d5c2f
Showing
14 changed files
with
205 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use super::*; | ||
|
||
pub(crate) struct AcceptJson(pub(crate) bool); | ||
|
||
#[async_trait::async_trait] | ||
impl<S> axum::extract::FromRequestParts<S> for AcceptJson | ||
where | ||
S: Send + Sync, | ||
{ | ||
type Rejection = (); | ||
|
||
async fn from_request_parts( | ||
parts: &mut http::request::Parts, | ||
_state: &S, | ||
) -> Result<Self, Self::Rejection> { | ||
Ok(Self( | ||
parts | ||
.headers | ||
.get("accept") | ||
.map(|value| value == "application/json") | ||
.unwrap_or_default(), | ||
)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use { | ||
super::*, ord::inscription_id::InscriptionId, ord::rarity::Rarity, ord::templates::sat::SatJson, | ||
ord::SatPoint, | ||
}; | ||
|
||
#[test] | ||
fn get_sat_without_sat_index() { | ||
let rpc_server = test_bitcoincore_rpc::spawn(); | ||
|
||
let response = TestServer::spawn_with_args(&rpc_server, &["--enable-json-api"]) | ||
.json_request("/sat/2099999997689999"); | ||
|
||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
let mut sat_json: SatJson = serde_json::from_str(&response.text().unwrap()).unwrap(); | ||
|
||
// this is a hack to ignore the timestamp, since it changes for every request | ||
sat_json.timestamp = "".into(); | ||
|
||
pretty_assert_eq!( | ||
sat_json, | ||
SatJson { | ||
number: 2099999997689999, | ||
decimal: "6929999.0".into(), | ||
degree: "5°209999′1007″0‴".into(), | ||
name: "a".into(), | ||
block: 6929999, | ||
cycle: 5, | ||
epoch: 32, | ||
period: 3437, | ||
offset: 0, | ||
rarity: Rarity::Uncommon, | ||
percentile: "100%".into(), | ||
satpoint: None, | ||
timestamp: "".into(), | ||
inscriptions: vec![], | ||
} | ||
) | ||
} | ||
|
||
#[test] | ||
fn get_sat_with_inscription_and_sat_index() { | ||
let rpc_server = test_bitcoincore_rpc::spawn(); | ||
|
||
create_wallet(&rpc_server); | ||
|
||
let Inscribe { reveal, .. } = inscribe(&rpc_server); | ||
let inscription_id = InscriptionId::from(reveal); | ||
|
||
let response = TestServer::spawn_with_args(&rpc_server, &["--index-sats", "--enable-json-api"]) | ||
.json_request(format!("/sat/{}", 50 * COIN_VALUE)); | ||
|
||
assert_eq!(response.status(), StatusCode::OK); | ||
|
||
let sat_json: SatJson = serde_json::from_str(&response.text().unwrap()).unwrap(); | ||
|
||
pretty_assert_eq!( | ||
sat_json, | ||
SatJson { | ||
number: 50 * COIN_VALUE, | ||
decimal: "1.0".into(), | ||
degree: "0°1′1″0‴".into(), | ||
name: "nvtcsezkbth".into(), | ||
block: 1, | ||
cycle: 0, | ||
epoch: 0, | ||
period: 0, | ||
offset: 0, | ||
rarity: Rarity::Uncommon, | ||
percentile: "0.00023809523835714296%".into(), | ||
satpoint: Some(SatPoint::from_str(&format!("{}:{}:{}", reveal, 0, 0)).unwrap()), | ||
timestamp: "1970-01-01 00:00:01 UTC".into(), | ||
inscriptions: vec![inscription_id], | ||
} | ||
) | ||
} | ||
|
||
#[test] | ||
fn json_request_fails_when_not_enabled() { | ||
let rpc_server = test_bitcoincore_rpc::spawn(); | ||
|
||
let response = | ||
TestServer::spawn_with_args(&rpc_server, &[]).json_request("/sat/2099999997689999"); | ||
|
||
assert_eq!(response.status(), StatusCode::NOT_ACCEPTABLE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.