Skip to content

Commit

Permalink
Ocean: api address get account history (#2967)
Browse files Browse the repository at this point in the history
* script activity indexer

* refine + helper::check_if_evm_tx + error::NotFoundIndex

* fmt

* api

* fmt

* fix

* fmt

* Ocean: indexer script unspent + api.address.list_transactions_unspent (#2948)

* script unspent indexer

* list_tx_unspent api

* fmt

* Ocean: indexer script aggregation + api.address.get_balance & get_aggregation (#2949)

* script aggregation indexer

* fmt

* address api: get_balance + get_aggregation

* get_balance return string

* decimal to string as deserialize not support in decimal

* fix NotFoundIndex params

* fix script agg indexer

* ScriptAggregationResponse

* 8 decimal

* decimal -> f64

* map api resp & fix sorting

* fix paginate

* fmt

* script activity uses find_tx_vout

* fix tx getvouts sort asc

* fix indexer_script_activity, index_tx order correction, refine find_tx_vout, rm unuse

* resp mapping

* fmt

* rust-defichain-rpc main

* script activity indexer

* address list tokens

* rm dup import

* fmt

* get_acc_history

* rm log

* use anyhow::Context

---------

Co-authored-by: Jouzo <[email protected]>
  • Loading branch information
canonbrother and Jouzo authored Jul 15, 2024
1 parent 1fc3b4e commit 051b569
Showing 1 changed file with 62 additions and 21 deletions.
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

0 comments on commit 051b569

Please sign in to comment.