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

Ocean: api address get account history #2967

Merged
Merged
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
83 changes: 62 additions & 21 deletions lib/ain-ocean/src/api/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<History>,
// ) -> String {
// format!(
// "Account history for address {}, height {}, txno {}",
// address, height, txno
// )
// }
#[derive(Debug, Serialize)]
struct AddressHistory {
owner: String,
txid: Option<Txid>,
txn: Option<u64>,
r#type: String,
amounts: Vec<String>,
block: AddressHistoryBlock,
}

impl From<AccountHistory> 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<BlockHash>,
time: Option<u64>,
}

#[ocean_endpoint]
async fn get_account_history(
Path(History {
address,
height,
txno,
}): Path<History>,
Extension(ctx): Extension<Arc<AppContext>>,
) -> Result<Response<AddressHistory>> {
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<Address>) -> String {
// format!("List account history for address {}", address)
Expand Down Expand Up @@ -448,7 +489,7 @@ async fn list_tokens(

pub fn router(ctx: Arc<AppContext>) -> 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))
Expand Down
Loading