@@ -11,6 +11,7 @@ import { ValidationError, object, string } from 'yup'
1111import { utils } from 'ethers'
1212import { AddressService } from '../service/address/AddressService'
1313import { supportedFiat } from '../coinmarketcap/support'
14+ import { Flow } from '../types/event'
1415
1516interface HttpsAPIDependencies {
1617 app : Express ,
@@ -44,20 +45,21 @@ export class HttpsAPI {
4445 }
4546
4647 init ( ) : void {
47- const chainIdSchema = object ( {
48- chainId : string ( ) . optional ( )
49- . trim ( )
50- . oneOf ( Object . keys ( this . dataSourceMapping ) , 'The current chainId is not supported' )
51- } )
5248 const addressSchema = object ( {
5349 address : string ( ) . required ( 'An address is invalid' )
5450 . trim ( )
5551 . transform ( address => utils . isAddress ( address . toLowerCase ( ) ) ? address : '' )
56- } ) . required ( )
57- const currencySchema = object ( {
52+ } )
53+ const schema = object ( {
54+ chainId : string ( ) . optional ( )
55+ . trim ( )
56+ . oneOf ( Object . keys ( this . dataSourceMapping ) , 'The current chainId is not supported' ) ,
5857 convert : string ( ) . optional ( )
5958 . trim ( )
60- . oneOf ( supportedFiat , 'The current currency is not supported' )
59+ . oneOf ( supportedFiat , 'The current currency is not supported' ) ,
60+ flow : string ( ) . optional ( )
61+ . trim ( )
62+ . oneOf ( [ Flow . ALL , Flow . FROM , Flow . TO ] , 'The transaction filter is invalid' )
6163 } )
6264
6365 const whilelist = [
@@ -79,7 +81,7 @@ export class HttpsAPI {
7981
8082 this . app . get ( '/tokens' , ( { query : { chainId = '31' } } : Request , res : Response , next : NextFunction ) => {
8183 try {
82- chainIdSchema . validateSync ( { chainId } )
84+ schema . validateSync ( { chainId } )
8385 return this
8486 . dataSourceMapping [ chainId as string ] . getTokens ( )
8587 . then ( this . responseJsonOk ( res ) )
@@ -93,8 +95,8 @@ export class HttpsAPI {
9395 '/address/:address/tokens' ,
9496 async ( { params : { address } , query : { chainId = '31' } } : Request , res : Response , next : NextFunction ) => {
9597 try {
96- chainIdSchema . validateSync ( { chainId } )
9798 addressSchema . validateSync ( { address } )
99+ schema . validateSync ( { chainId } )
98100 const balance = await this . addressService . getTokensByAddress ( {
99101 chainId : chainId as string ,
100102 address : address as string
@@ -110,8 +112,8 @@ export class HttpsAPI {
110112 '/address/:address/events' ,
111113 ( { params : { address } , query : { chainId = '31' } } : Request , res : Response , next : NextFunction ) => {
112114 try {
113- chainIdSchema . validateSync ( { chainId } )
114115 addressSchema . validateSync ( { address } )
116+ schema . validateSync ( { chainId } )
115117 return this
116118 . dataSourceMapping [ chainId as string ] . getEventsByAddress ( address )
117119 . then ( this . responseJsonOk ( res ) )
@@ -124,19 +126,23 @@ export class HttpsAPI {
124126
125127 this . app . get (
126128 '/address/:address/transactions' ,
127- async ( { params : { address } , query : { limit, prev, next, chainId = '31' , blockNumber = '0' } } : Request ,
128- res : Response , nextFunction : NextFunction ) => {
129+ async ( {
130+ params : { address } ,
131+ query : { limit, prev, next, chainId = '31' , blockNumber = '0' , flow = Flow . ALL }
132+ } : Request ,
133+ res : Response , nextFunction : NextFunction ) => {
129134 try {
130- chainIdSchema . validateSync ( { chainId } )
131135 addressSchema . validateSync ( { address } )
136+ schema . validateSync ( { chainId, flow } )
132137
133138 const transactions = await this . addressService . getTransactionsByAddress ( {
134139 address : address as string ,
135140 chainId : chainId as string ,
136141 limit : limit as string ,
137142 prev : prev as string ,
138143 next : next as string ,
139- blockNumber : blockNumber as string
144+ blockNumber : blockNumber as string ,
145+ flow : flow as Flow
140146 } ) . catch ( nextFunction )
141147 return this . responseJsonOk ( res ) ( transactions )
142148 } catch ( e ) {
@@ -149,7 +155,7 @@ export class HttpsAPI {
149155 async ( { params : { address } , query : { chainId = '31' } } : Request , res : Response ,
150156 nextFunction : NextFunction ) => {
151157 try {
152- chainIdSchema . validateSync ( { chainId } )
158+ schema . validateSync ( { chainId } )
153159 addressSchema . validateSync ( { address } )
154160 const nft = await this . addressService . getNftInfo ( { chainId : chainId as string , address } ) . catch ( nextFunction )
155161 return this . responseJsonOk ( res ) ( nft )
@@ -162,7 +168,7 @@ export class HttpsAPI {
162168 async ( { params : { nft, address } , query : { chainId = '31' } } : Request , res : Response ,
163169 nextFunction : NextFunction ) => {
164170 try {
165- chainIdSchema . validateSync ( { chainId } )
171+ schema . validateSync ( { chainId } )
166172 addressSchema . validateSync ( { address } )
167173 const nftInfo = await this . addressService
168174 . getNftOwnedByAddress ( { chainId : chainId as string , address, nftAddress : nft } )
@@ -199,7 +205,7 @@ export class HttpsAPI {
199205 async ( req : Request < { } , { } , { } , PricesQueryParams > , res : Response ) => {
200206 try {
201207 const { convert = 'USD' , addresses = '' } = req . query
202- currencySchema . validateSync ( { convert } )
208+ schema . validateSync ( { convert } )
203209 addresses . split ( ',' ) . forEach ( address => addressSchema . validateSync ( { address } ) )
204210 const prices = await this . addressService . getPrices ( {
205211 addresses,
@@ -228,17 +234,18 @@ export class HttpsAPI {
228234 '/address/:address' ,
229235 async ( req , res ) => {
230236 try {
231- const { limit, prev, next, chainId = '31' , blockNumber = '0' } = req . query
237+ const { limit, prev, next, chainId = '31' , blockNumber = '0' , flow = Flow . ALL } = req . query
232238 const { address } = req . params
233- chainIdSchema . validateSync ( { chainId } )
234239 addressSchema . validateSync ( { address } )
240+ schema . validateSync ( { chainId } )
235241 const data = await this . addressService . getAddressDetails ( {
236242 chainId : chainId as string ,
237243 address,
238244 blockNumber : blockNumber as string ,
239245 limit : limit as string ,
240246 prev : prev as string ,
241- next : next as string
247+ next : next as string ,
248+ flow : flow as Flow
242249 } )
243250 return this . responseJsonOk ( res ) ( data )
244251 } catch ( error ) {
0 commit comments