@@ -2,6 +2,7 @@ import { and, asc, eq, gte, inArray } from 'drizzle-orm';
22import { FastifyInstance } from 'fastify' ;
33import { ZodTypeProvider } from 'fastify-type-provider-zod' ;
44import gql from 'graphql-tag' ;
5+ import z from 'zod' ;
56import { chains } from '../../../configs/chains' ;
67import { client } from '../../../database/client' ;
78import { tTokens } from '../../../database/schema' ;
@@ -36,47 +37,71 @@ export default async function (fastify: FastifyInstance) {
3637 } ,
3738 ) ;
3839
39- fastify
40- . withTypeProvider < ZodTypeProvider > ( )
41- . get ( '/money-market' , async ( req , reply ) => {
40+ fastify . withTypeProvider < ZodTypeProvider > ( ) . get (
41+ '/money-market' ,
42+ {
43+ schema : {
44+ querystring : paginationSchema ,
45+ } ,
46+ } ,
47+ async ( req , reply ) => {
4248 if ( req . chain . key !== 'bob-sepolia' ) {
4349 return reply . notFound (
4450 'Money Market data is only available for BOB Sepolia' ,
4551 ) ;
4652 }
4753
4854 const chain = chains . get ( 'bob-sepolia' ) ;
55+ const { cursor, limit } = req . query ;
4956
50- const item = await queryFromSubgraph < {
57+ const data = await queryFromSubgraph < {
5158 pools : Array < {
5259 id : string ;
5360 pool : string ;
5461 } > ;
5562 } > (
5663 chain . aaveSubgraphUrl ,
5764 gql `
58- query {
59- pools {
65+ query ($first: Int!, $cursor: String) {
66+ pools(first: $first, where: { id_gt: $cursor }) {
6067 id
6168 pool
69+ addressProviderId
70+ poolCollateralManager
71+ poolImpl
72+ poolDataProviderImpl
73+ poolConfigurator
74+ proxyPriceProvider
75+ lastUpdateTimestamp
76+ bridgeProtocolFee
77+ flashloanPremiumTotal
78+ flashloanPremiumToProtocol
6279 }
6380 }
6481 ` ,
65- ) . then ( ( data ) => data . pools . flatMap ( ( pool ) => pool ) [ 0 ] ) ;
66-
67- return {
68- data : {
69- pool : item . pool ,
70- addressProvider : item . id ,
82+ {
83+ first : limit ,
84+ cursor : cursor ?? '' ,
7185 } ,
72- } ;
73- } ) ;
86+ ) ;
87+
88+ const items = data . pools . map ( ( p ) => ( {
89+ pool : p . pool ,
90+ addressProvider : p . id ,
91+ } ) ) ;
92+
93+ return paginationResponse ( items , limit , 'addressProvider' ) ;
94+ } ,
95+ ) ;
7496
7597 fastify . withTypeProvider < ZodTypeProvider > ( ) . get (
76- '/money-market/reserves ' ,
98+ '/money-market/:pool ' ,
7799 {
78100 schema : {
79101 querystring : paginationSchema ,
102+ params : z . object ( {
103+ pool : z . string ( ) ,
104+ } ) ,
80105 } ,
81106 } ,
82107 async ( req , reply ) => {
@@ -87,35 +112,56 @@ export default async function (fastify: FastifyInstance) {
87112 }
88113
89114 const chain = chains . get ( 'bob-sepolia' ) ;
115+ const { pool } = req . params ;
116+ const { cursor, limit } = req . query ;
90117
91- const items = await queryFromSubgraph < {
92- pools : Array < {
118+ const data = await queryFromSubgraph < {
119+ reserves : Array < {
93120 id : string ;
94- reserves : {
121+ totalLiquidity : string ;
122+ underlyingAsset : string ;
123+ usageAsCollateralEnabled : boolean ;
124+ borrowingEnabled : boolean ;
125+ pool : {
95126 id : string ;
96- totalLiquidity : string ;
97- underlyingAsset : string ;
98- usageAsCollateralEnabled : boolean ;
99- borrowingEnabled : boolean ;
100- } [ ] ;
127+ pool : string ;
128+ } ;
101129 } > ;
102130 } > (
103131 chain . aaveSubgraphUrl ,
104132 gql `
105- query {
106- pools {
107- id
108- reserves {
133+ query ($pool: String!, $first: Int!, $cursor: String) {
134+ reserves(
135+ first: $first
136+ where: { pool_: { id: $pool }, id_gt: $cursor }
137+ ) {
138+ underlyingAsset
139+ pool {
109140 id
110- totalLiquidity
111- underlyingAsset
112- usageAsCollateralEnabled
113- borrowingEnabled
141+ pool
114142 }
143+ symbol
144+ name
145+ decimals
146+ usageAsCollateralEnabled
147+ borrowingEnabled
148+ totalLiquidity
149+ totalATokenSupply
150+ totalLiquidityAsCollateral
151+ availableLiquidity
152+ totalSupplies
153+ liquidityRate
115154 }
116155 }
117156 ` ,
118- ) . then ( ( data ) => data . pools . flatMap ( ( pool ) => pool . reserves ) ) ;
157+ {
158+ pool,
159+ first : limit ,
160+ cursor : cursor ?? '' ,
161+ } ,
162+ ) ;
163+
164+ const items = data . reserves ;
119165
120166 const tokens = await client . query . tTokens . findMany ( {
121167 columns : tTokensSelectors . columns ,
@@ -128,16 +174,14 @@ export default async function (fastify: FastifyInstance) {
128174 ) ,
129175 } ) ;
130176
131- return {
132- data : items
133- . map ( ( item ) => ( {
134- ...item ,
135- token : tokens . find ( ( t ) => t . address === item . underlyingAsset ) ,
136- } ) )
137- . filter ( ( i ) => i . token ) ,
138- nextCursor : null ,
139- count : items . length ,
140- } ;
177+ const merged = items
178+ . map ( ( item ) => ( {
179+ ...item ,
180+ token : tokens . find ( ( t ) => t . address === item . underlyingAsset ) ,
181+ } ) )
182+ . filter ( ( i ) => i . token ) ;
183+
184+ return paginationResponse ( merged , limit , 'id' ) ;
141185 } ,
142186 ) ;
143187}
0 commit comments