diff --git a/lib/ain-ocean/src/api/address.rs b/lib/ain-ocean/src/api/address.rs index d6a161d32e..991ee0181c 100644 --- a/lib/ain-ocean/src/api/address.rs +++ b/lib/ain-ocean/src/api/address.rs @@ -18,9 +18,10 @@ use crate::{ Error, Result, }; use ain_macros::ocean_endpoint; +use anyhow::Context; use axum::{routing::get, Extension, Router}; -use bitcoin::{hashes::Hash, hex::DisplayHex, Txid}; -use defichain_rpc::RpcApi; +use bitcoin::{hashes::Hash, hex::DisplayHex, BlockHash, Txid}; +use defichain_rpc::{json::account::AccountHistory, AccountRPC, RpcApi}; use serde::{Deserialize, Serialize}; use serde_json::json; use serde_with::skip_serializing_none; @@ -30,25 +31,65 @@ struct Address { address: String, } -// #[derive(Deserialize)] -// struct History { -// address: String, -// height: i64, -// txno: i64, -// } +#[derive(Deserialize)] +struct History { + address: String, + height: u32, + txno: u32, +} -// async fn get_account_history( -// Path(History { -// address, -// height, -// txno, -// }): Path, -// ) -> String { -// format!( -// "Account history for address {}, height {}, txno {}", -// address, height, txno -// ) -// } +#[derive(Debug, Serialize)] +struct AddressHistory { + owner: String, + txid: Option, + txn: Option, + r#type: String, + amounts: Vec, + block: AddressHistoryBlock, +} + +impl From for AddressHistory { + fn from(history: AccountHistory) -> Self { + Self { + owner: history.owner, + txid: history.txid, + txn: history.txn, + r#type: history.r#type, + amounts: history.amounts, + block: AddressHistoryBlock { + height: history.block_height, + hash: history.block_hash, + time: history.block_time, + }, + } + } +} + +#[skip_serializing_none] +#[derive(Debug, Serialize)] +struct AddressHistoryBlock { + height: u64, + hash: Option, + time: Option, +} + +#[ocean_endpoint] +async fn get_account_history( + Path(History { + address, + height, + txno, + }): Path, + Extension(ctx): Extension>, +) -> Result> { + let res = ctx + .client + .get_account_history(&address, height, txno) + .await + .context("Record not found")?; + + Ok(Response::new(res.into())) +} // async fn list_account_history(Path(Address { address }): Path
) -> String { // format!("List account history for address {}", address) @@ -448,7 +489,7 @@ async fn list_tokens( pub fn router(ctx: Arc) -> Router { Router::new() - // .route("/history/:height/:txno", get(get_account_history)) + .route("/:address/history/:height/:txno", get(get_account_history)) // .route("/history", get(list_account_history)) .route("/:address/balance", get(get_balance)) .route("/:address/aggregation", get(get_aggregation))