11use std:: { str:: FromStr , sync:: Arc } ;
22
3+ use ain_dftx:: COIN ;
34use ain_macros:: ocean_endpoint;
45use axum:: { routing:: get, Extension , Router } ;
5- use bitcoin:: { hashes:: Hash , Txid } ;
6+ use bitcoin:: { hashes:: Hash , ScriptBuf , Txid } ;
67use defichain_rpc:: {
78 defichain_rpc_json:: {
89 loan:: { CollateralTokenDetail , LoanSchemeResult } ,
@@ -17,15 +18,16 @@ use defichain_rpc::{
1718} ;
1819use futures:: future:: try_join_all;
1920use log:: trace;
20- use serde:: { Serialize , Serializer } ;
21+ use rust_decimal:: Decimal ;
22+ use serde:: { Deserialize , Serialize , Serializer } ;
2123use serde_with:: skip_serializing_none;
2224use snafu:: OptionExt ;
2325
2426use super :: {
2527 cache:: { get_loan_scheme_cached, get_token_cached} ,
2628 common:: {
2729 from_script, parse_amount, parse_display_symbol, parse_fixed_interval_price,
28- parse_query_height_txno , Paginate ,
30+ parse_query_height_txid , Paginate ,
2931 } ,
3032 path:: Path ,
3133 query:: { PaginationQuery , Query } ,
@@ -35,7 +37,8 @@ use super::{
3537} ;
3638use crate :: {
3739 error:: { ApiError , Error , NotFoundKind , NotFoundSnafu } ,
38- model:: { OraclePriceActive , VaultAuctionBatchHistory } ,
40+ model:: { BlockContext , OraclePriceActive , VaultAuctionBatchHistory } ,
41+ network:: Network ,
3942 storage:: { RepositoryOps , SortOrder } ,
4043 Result ,
4144} ;
@@ -488,55 +491,111 @@ async fn get_vault(
488491 Ok ( Response :: new ( res) )
489492}
490493
494+ #[ derive( Serialize , Deserialize , Debug , Clone ) ]
495+ #[ serde( rename_all = "camelCase" ) ]
496+ pub struct VaultAuctionBatchHistoryResponse {
497+ pub id : String ,
498+ pub key : String ,
499+ pub sort : String ,
500+ pub vault_id : Txid ,
501+ pub index : u32 ,
502+ pub from : ScriptBuf ,
503+ pub address : String ,
504+ pub amount : String ,
505+ pub token_id : u64 ,
506+ pub block : BlockContext ,
507+ }
508+
509+ impl VaultAuctionBatchHistoryResponse {
510+ fn from (
511+ data : ( ( Txid , [ u8 ; 4 ] , [ u8 ; 4 ] , Txid ) , VaultAuctionBatchHistory ) ,
512+ address : String ,
513+ ) -> Self {
514+ let id = data. 0 ;
515+ let vault_id = id. 0 ;
516+ let batch_index = u32:: from_be_bytes ( id. 1 ) ;
517+ let block_height = id. 2 ;
518+ let txid = id. 3 ;
519+ let history = data. 1 ;
520+ let amount = Decimal :: from ( history. amount ) / Decimal :: from ( COIN ) ;
521+
522+ Self {
523+ id : format ! (
524+ "{}-{}-{}" ,
525+ vault_id. clone( ) ,
526+ batch_index. clone( ) ,
527+ txid. clone( )
528+ ) ,
529+ key : format ! ( "{}-{}" , vault_id. clone( ) , batch_index. clone( ) ) ,
530+ sort : format ! ( "{}-{}" , hex:: encode( block_height) , txid) ,
531+ vault_id,
532+ index : batch_index,
533+ from : history. from ,
534+ address,
535+ amount : amount. to_string ( ) ,
536+ token_id : history. token_id ,
537+ block : history. block ,
538+ }
539+ }
540+ }
541+
491542#[ ocean_endpoint]
492543async fn list_vault_auction_history (
493- Path ( ( vault_id, height , batch_index) ) : Path < ( Txid , u32 , u32 ) > ,
544+ Path ( ( vault_id, liquidation_height , batch_index) ) : Path < ( Txid , u32 , u32 ) > ,
494545 Query ( query) : Query < PaginationQuery > ,
495546 Extension ( ctx) : Extension < Arc < AppContext > > ,
496- ) -> Result < ApiPagedResponse < VaultAuctionBatchHistory > > {
547+ ) -> Result < ApiPagedResponse < VaultAuctionBatchHistoryResponse > > {
497548 trace ! (
498- "Auction history for vault id {}, height {}, batch index {}" ,
549+ "Auction history for vault id {}, liquidation_height {}, batch index {}" ,
499550 vault_id,
500- height ,
551+ liquidation_height ,
501552 batch_index
502553 ) ;
503554 let next = query
504555 . next
505556 . map ( |q| {
506- let ( height, txno ) = parse_query_height_txno ( & q) ?;
507- Ok :: < ( u32 , usize ) , Error > ( ( height, txno ) )
557+ let ( height, txid ) = parse_query_height_txid ( & q) ?;
558+ Ok :: < ( u32 , Txid ) , Error > ( ( height, txid ) )
508559 } )
509560 . transpose ( ) ?
510- . unwrap_or_default ( ) ;
561+ . unwrap_or ( ( liquidation_height, Txid :: from_byte_array ( [ 0xffu8 ; 32 ] ) ) ) ;
562+
563+ let size = if query. size > 0 { query. size } else { 30 } ;
511564
512- let size = if query. size > 0 { query. size } else { 20 } ;
565+ let liquidation_block_expiry = match ctx. network {
566+ Network :: Regtest => 36 ,
567+ _ => 720 ,
568+ } ;
513569
514570 let auctions = ctx
515571 . services
516572 . auction
517- . by_height
573+ . by_id
518574 . list (
519- Some ( ( vault_id, batch_index, next. 0 , next. 1 ) ) ,
575+ Some ( (
576+ vault_id,
577+ batch_index. to_be_bytes ( ) ,
578+ next. 0 . to_be_bytes ( ) ,
579+ next. 1 ,
580+ ) ) ,
520581 SortOrder :: Descending ,
521582 ) ?
522583 . take ( size)
523584 . take_while ( |item| match item {
524- Ok ( ( k, _) ) => k. 0 == vault_id && k. 1 == batch_index,
585+ Ok ( ( k, _) ) => {
586+ k. 0 == vault_id
587+ && k. 1 == batch_index. to_be_bytes ( )
588+ && u32:: from_be_bytes ( k. 2 ) > liquidation_height - liquidation_block_expiry
589+ }
525590 _ => true ,
526591 } )
527592 . map ( |item| {
528- let ( _, id) = item?;
529-
530- let auction = ctx
531- . services
532- . auction
533- . by_id
534- . get ( & id) ?
535- . context ( NotFoundSnafu {
536- kind : NotFoundKind :: Auction ,
537- } ) ?;
538-
539- Ok ( auction)
593+ let ( id, history) = item?;
594+ let address = from_script ( & history. from , ctx. network ) ?;
595+ Ok ( VaultAuctionBatchHistoryResponse :: from (
596+ ( id, history) ,
597+ address,
598+ ) )
540599 } )
541600 . collect :: < Result < Vec < _ > > > ( ) ?;
542601
@@ -611,14 +670,17 @@ async fn map_liquidation_batches(
611670 } ;
612671 let id = (
613672 Txid :: from_str ( vault_id) ?,
614- batch. index ,
673+ batch. index . to_be_bytes ( ) ,
674+ [ 0xffu8 , 0xffu8 , 0xffu8 , 0xffu8 ] ,
615675 Txid :: from_byte_array ( [ 0xffu8 ; 32 ] ) ,
616676 ) ;
617677 let bids = repo
618678 . by_id
619679 . list ( Some ( id) , SortOrder :: Descending ) ?
620680 . take_while ( |item| match item {
621- Ok ( ( ( vid, bindex, _) , _) ) => vid. to_string ( ) == vault_id && bindex == & batch. index ,
681+ Ok ( ( ( vid, bindex, _, _) , _) ) => {
682+ vid. to_string ( ) == vault_id && bindex == & batch. index . to_be_bytes ( )
683+ }
622684 _ => true ,
623685 } )
624686 . collect :: < Vec < _ > > ( ) ;
0 commit comments