Skip to content

Commit

Permalink
resp mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Jul 1, 2024
1 parent e41bca4 commit 2723e32
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
71 changes: 67 additions & 4 deletions lib/ain-ocean/src/api/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use ain_macros::ocean_endpoint;
use axum::{routing::get, Extension, Router};
use bitcoin::{hashes::Hash, hex::DisplayHex, Txid};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

#[derive(Deserialize)]
struct Address {
Expand Down Expand Up @@ -162,12 +163,74 @@ async fn get_aggregation(
// format!("List vaults for address {}", address)
// }

#[skip_serializing_none]
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptActivityResponse {
pub id: String,
pub hid: String,
pub r#type: String,
pub type_hex: String,
pub txid: Txid,
pub block: BlockContext,
pub script: ScriptActivityScriptResponse,
pub vin: Option<ScriptActivityVinVoutResponse>,
pub vout: Option<ScriptActivityVinVoutResponse>,
pub value: String,
pub token_id: Option<u32>,
}

impl From<ScriptActivity> for ScriptActivityResponse {
fn from(v: ScriptActivity) -> Self {
Self {
id: v.id,
hid: v.hid,
r#type: v.r#type.to_string(),
type_hex: v.type_hex.to_string(),
txid: v.txid,
block: v.block,
script: ScriptActivityScriptResponse {
r#type: v.script.r#type,
hex: v.script.hex.to_lower_hex_string(),
},
vin: v.vin.map(|vin| {
ScriptActivityVinVoutResponse {
txid: vin.txid,
n: vin.n
}
}),
vout: v.vout.map(|vout| {
ScriptActivityVinVoutResponse {
txid: vout.txid,
n: vout.n
}
}),
value: v.value,
token_id: v.token_id,
}
}
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptActivityScriptResponse {
pub r#type: String,
pub hex: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptActivityVinVoutResponse {
pub txid: Txid,
pub n: usize,
}

#[ocean_endpoint]
async fn list_transaction(
async fn list_transactions(
Path(Address { address }): Path<Address>,
Query(query): Query<PaginationQuery>,
Extension(ctx): Extension<Arc<AppContext>>,
) -> Result<ApiPagedResponse<ScriptActivity>> {
) -> Result<ApiPagedResponse<ScriptActivityResponse>> {
let hid = address_to_hid(&address, ctx.network.into())?;
let next = query
.next
Expand Down Expand Up @@ -203,7 +266,7 @@ async fn list_transaction(
})
.map(|item| {
let (_, v) = item?;
Ok(v)
Ok(v.into())
})
.collect::<Result<Vec<_>>>()?;

Expand Down Expand Up @@ -320,7 +383,7 @@ pub fn router(ctx: Arc<AppContext>) -> Router {
.route("/:address/aggregation", get(get_aggregation))
// .route("/tokens", get(list_token))
// .route("/vaults", get(list_vault))
.route("/:address/transactions", get(list_transaction))
.route("/:address/transactions", get(list_transactions))
.route(
"/:address/transactions/unspent",
get(list_transaction_unspent),
Expand Down
4 changes: 2 additions & 2 deletions lib/ain-ocean/src/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn index_script_activity(services: &Arc<Services>, block: &Block<Transaction>) -
n: vin.vout,
}),
vout: None,
value: vout.value,
value: format!("{:.8}", vout.value.parse::<f32>()?),
token_id: vout.token_id,
};
let id = (hid, block.height, ScriptActivityTypeHex::Vin, vin.txid, vin.vout);
Expand Down Expand Up @@ -239,7 +239,7 @@ fn index_script_activity(services: &Arc<Services>, block: &Block<Transaction>) -
txid: tx.txid,
n: vout.n,
}),
value: vout.value.to_string(),
value: format!("{:.8}", vout.value),
token_id: vout.token_id,
};
let id = (hid, block.height, ScriptActivityTypeHex::Vout, tx.txid, vout.n);
Expand Down
13 changes: 9 additions & 4 deletions lib/ain-ocean/src/model/script_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ pub enum ScriptActivityType {
Vout,
}

impl fmt::Display for ScriptActivityType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ScriptActivityType::Vin => write!(f, "vin"),
ScriptActivityType::Vout => write!(f, "vout"),
}
}
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ScriptActivityTypeHex {
Vin,
Expand All @@ -27,7 +36,6 @@ impl fmt::Display for ScriptActivityTypeHex {
pub type ScriptActivityId = (String, u32, ScriptActivityTypeHex, Txid, usize); // (hid, block.height, type_hex, txid, index)

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptActivity {
pub id: String, // unique id of this output: block height, type, txid(vin/vout), n(vin/vout)
pub hid: String, // hashed id, for length compatibility reasons this is the hashed id of script
Expand All @@ -43,21 +51,18 @@ pub struct ScriptActivity {
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptActivityScript {
pub r#type: String,
pub hex: Vec<u8>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptActivityVin {
pub txid: Txid,
pub n: usize,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ScriptActivityVout {
pub txid: Txid,
pub n: usize,
Expand Down

0 comments on commit 2723e32

Please sign in to comment.