@@ -3,13 +3,16 @@ use std::sync::Arc;
33use ain_macros:: ocean_endpoint;
44use axum:: { extract:: Query , routing:: get, Extension , Router } ;
55use bitcoin:: Txid ;
6- use serde:: Deserialize ;
6+ use serde:: { Deserialize , Serialize } ;
77
88use super :: { path:: Path , query:: PaginationQuery , response:: ApiPagedResponse , AppContext } ;
99use crate :: {
1010 api:: { common:: Paginate , response:: Response } ,
1111 error:: ApiError ,
12- model:: { Transaction , TransactionVin , TransactionVout } ,
12+ model:: {
13+ Transaction , TransactionVin , TransactionVinType , TransactionVinVout , TransactionVout ,
14+ TransactionVoutScript ,
15+ } ,
1316 storage:: {
1417 InitialKeyProvider , RepositoryOps , SortOrder , TransactionVin as TransactionVinStorage ,
1518 } ,
@@ -30,12 +33,43 @@ async fn get_transaction(
3033 Ok ( Response :: new ( transactions) )
3134}
3235
36+ #[ derive( Debug , Serialize ) ]
37+ struct TransactionVinResponse {
38+ pub id : String ,
39+ pub txid : Txid ,
40+ pub coinbase : Option < String > ,
41+ pub vout : Option < TransactionVinVout > ,
42+ pub script : Option < String > ,
43+ pub tx_in_witness : Option < Vec < String > > ,
44+ pub sequence : i64 ,
45+ }
46+
47+ impl From < TransactionVin > for TransactionVinResponse {
48+ fn from ( v : TransactionVin ) -> Self {
49+ let ( id, coinbase) = match v. r#type {
50+ TransactionVinType :: Coinbase ( coinbase) => ( format ! ( "{}00" , v. txid) , Some ( coinbase) ) ,
51+ TransactionVinType :: Standard ( ( txid, vout) ) => {
52+ ( format ! ( "{}{}{:x}" , v. txid, txid, vout) , None )
53+ }
54+ } ;
55+ Self {
56+ id,
57+ txid : v. txid ,
58+ coinbase,
59+ vout : v. vout ,
60+ script : v. script ,
61+ tx_in_witness : v. tx_in_witness ,
62+ sequence : v. sequence ,
63+ }
64+ }
65+ }
66+
3367#[ ocean_endpoint]
3468async fn get_vins (
3569 Path ( TransactionId { id } ) : Path < TransactionId > ,
3670 Query ( query) : Query < PaginationQuery > ,
3771 Extension ( ctx) : Extension < Arc < AppContext > > ,
38- ) -> Result < ApiPagedResponse < TransactionVin > > {
72+ ) -> Result < ApiPagedResponse < TransactionVinResponse > > {
3973 let next = query
4074 . next
4175 . clone ( )
@@ -53,7 +87,7 @@ async fn get_vins(
5387 } )
5488 . map ( |item| {
5589 let ( _, v) = item?;
56- Ok ( v )
90+ Ok ( TransactionVinResponse :: from ( v ) )
5791 } )
5892 . collect :: < Result < Vec < _ > > > ( ) ?;
5993
@@ -62,13 +96,37 @@ async fn get_vins(
6296 } ) )
6397}
6498
99+ #[ derive( Debug , Serialize ) ]
100+ struct TransactionVoutResponse {
101+ pub id : String ,
102+ // pub vout: usize,
103+ pub txid : Txid ,
104+ pub n : usize ,
105+ pub value : String ,
106+ pub token_id : Option < u32 > ,
107+ pub script : TransactionVoutScript ,
108+ }
109+
110+ impl From < TransactionVout > for TransactionVoutResponse {
111+ fn from ( v : TransactionVout ) -> Self {
112+ Self {
113+ id : format ! ( "{}{:x}" , v. txid, v. vout) ,
114+ txid : v. txid ,
115+ n : v. n ,
116+ value : v. value ,
117+ token_id : v. token_id ,
118+ script : v. script ,
119+ }
120+ }
121+ }
122+
65123//get list of vout transaction, by passing id which contains txhash + vout_idx
66124#[ ocean_endpoint]
67125async fn get_vouts (
68126 Path ( TransactionId { id } ) : Path < TransactionId > ,
69127 Query ( query) : Query < PaginationQuery > ,
70128 Extension ( ctx) : Extension < Arc < AppContext > > ,
71- ) -> Result < ApiPagedResponse < TransactionVout > > {
129+ ) -> Result < ApiPagedResponse < TransactionVoutResponse > > {
72130 let next = query. next . as_deref ( ) . unwrap_or ( "0" ) . parse :: < usize > ( ) ?;
73131
74132 let list = ctx
@@ -83,7 +141,7 @@ async fn get_vouts(
83141 } )
84142 . map ( |item| {
85143 let ( _, v) = item?;
86- Ok ( v )
144+ Ok ( TransactionVoutResponse :: from ( v ) )
87145 } )
88146 . collect :: < Result < Vec < _ > > > ( ) ?;
89147
0 commit comments