Skip to content

Commit a8e87b4

Browse files
committed
feat: populate parametrized fields
1 parent a7f4890 commit a8e87b4

File tree

11 files changed

+199
-52
lines changed

11 files changed

+199
-52
lines changed

packages/client/src/actions/markets.ts

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
import {
22
type Market,
33
MarketQuery,
4-
type MarketRequest,
54
MarketsQuery,
6-
type MarketsRequest,
5+
type ReservesRequestOrderBy,
76
} from '@aave/graphql';
8-
import type { ResultAsync } from '@aave/types';
7+
import type { ChainId, EvmAddress, ResultAsync } from '@aave/types';
98
import type { AaveClient } from '../client';
109
import type { UnexpectedError } from '../errors';
1110

11+
export type MarketsRequest = {
12+
/**
13+
* The markets you want to see based on the chain ids.
14+
*/
15+
chainIds: ChainId[];
16+
/**
17+
* The user address in case you want to include user fields in the response.
18+
*
19+
* If not provided, user fields will not be included.
20+
*/
21+
userAddress?: EvmAddress;
22+
/**
23+
* The order by clause for the borrow reserves in the market.
24+
*
25+
* @defaultValue { name: OrderDirection.Asc }
26+
*/
27+
borrowsOrderBy?: ReservesRequestOrderBy;
28+
/**
29+
* The order by clause for the supply reserves in the market.
30+
*
31+
* @defaultValue { name: OrderDirection.Asc }
32+
*/
33+
suppliesOrderBy?: ReservesRequestOrderBy;
34+
};
35+
1236
/**
1337
* Fetches all markets for the specified chain IDs.
1438
*
@@ -24,11 +48,47 @@ import type { UnexpectedError } from '../errors';
2448
*/
2549
export function markets(
2650
client: AaveClient,
27-
request: MarketsRequest,
51+
{ chainIds, borrowsOrderBy, suppliesOrderBy, userAddress }: MarketsRequest,
2852
): ResultAsync<Market[], UnexpectedError> {
29-
return client.query(MarketsQuery, { request });
53+
return client.query(MarketsQuery, {
54+
request: { chainIds },
55+
borrowsOrderBy,
56+
suppliesOrderBy,
57+
userAddress,
58+
includeUserFields: !!userAddress,
59+
});
3060
}
3161

62+
export type MarketRequest = {
63+
/**
64+
* The pool address for the market.
65+
*/
66+
address: EvmAddress;
67+
68+
/**
69+
* The chain id the market pool address is deployed on.
70+
*/
71+
chainId: ChainId;
72+
/**
73+
* The user address in case you want to include user fields in the response.
74+
*
75+
* If not provided, user fields will not be included.
76+
*/
77+
userAddress?: EvmAddress;
78+
/**
79+
* The order by clause for the borrow reserves in the market.
80+
*
81+
* @defaultValue { name: OrderDirection.Asc }
82+
*/
83+
borrowsOrderBy?: ReservesRequestOrderBy;
84+
/**
85+
* The order by clause for the supply reserves in the market.
86+
*
87+
* @defaultValue { name: OrderDirection.Asc }
88+
*/
89+
suppliesOrderBy?: ReservesRequestOrderBy;
90+
};
91+
3292
/**
3393
* Fetches a specific market by address and chain ID.
3494
*
@@ -45,7 +105,19 @@ export function markets(
45105
*/
46106
export function market(
47107
client: AaveClient,
48-
request: MarketRequest,
108+
{
109+
address,
110+
chainId,
111+
userAddress,
112+
borrowsOrderBy,
113+
suppliesOrderBy,
114+
}: MarketRequest,
49115
): ResultAsync<Market | null, UnexpectedError> {
50-
return client.query(MarketQuery, { request });
116+
return client.query(MarketQuery, {
117+
request: { address, chainId },
118+
includeUserFields: !!userAddress,
119+
userAddress,
120+
borrowsOrderBy,
121+
suppliesOrderBy,
122+
});
51123
}

packages/client/src/actions/reserve.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1-
import { type Reserve, ReserveQuery, type ReserveRequest } from '@aave/graphql';
2-
import type { ResultAsync } from '@aave/types';
1+
import { type Reserve, ReserveQuery } from '@aave/graphql';
2+
import type { ChainId, EvmAddress, ResultAsync } from '@aave/types';
33
import type { AaveClient } from '../client';
44
import type { UnexpectedError } from '../errors';
55

6+
export type ReserveRequest = {
7+
/**
8+
* The pool address for the market
9+
*/
10+
market: EvmAddress;
11+
/**
12+
* The asset for the reserve
13+
*/
14+
token: EvmAddress;
15+
/**
16+
* The chain id the pool is deployed on
17+
*/
18+
chainId: ChainId;
19+
/**
20+
* The user address in case you want to include user fields in the response.
21+
*
22+
* If not provided, user fields will not be included.
23+
*/
24+
userAddress?: EvmAddress;
25+
};
26+
627
/**
728
* Fetches a specific reserve by market address, token address, and chain ID.
829
*
@@ -20,7 +41,11 @@ import type { UnexpectedError } from '../errors';
2041
*/
2142
export function reserve(
2243
client: AaveClient,
23-
request: ReserveRequest,
44+
{ market, token, chainId, userAddress }: ReserveRequest,
2445
): ResultAsync<Reserve | null, UnexpectedError> {
25-
return client.query(ReserveQuery, { request });
46+
return client.query(ReserveQuery, {
47+
request: { market, token, chainId },
48+
includeUserFields: !!userAddress,
49+
userAddress,
50+
});
2651
}

packages/graphql/src/common.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
import type { graphql } from './graphql';
2+
13
/**
24
* A standardized data object.
35
*
46
* All GQL operations should alias their results to `value` to ensure interoperability
57
* with this client interface.
68
*/
79
export type StandardData<T> = { value: T };
10+
11+
/**
12+
* Criteria for ordering by user.
13+
*/
14+
export type OrderByUserCriteria = ReturnType<
15+
typeof graphql.scalar<'OrderByUserCriteria'>
16+
>;
17+
18+
/**
19+
* Criteria for ordering reserves.
20+
*/
21+
export type ReservesRequestOrderBy = ReturnType<
22+
typeof graphql.scalar<'ReservesRequestOrderBy'>
23+
>;

packages/graphql/src/fragments/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ export * from './common';
33
export * from './market';
44
export * from './positions';
55
export * from './reserve';
6-
export * from './transaction';
6+
export * from './transactions';

packages/graphql/src/fragments/market.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,7 @@ import type { FragmentOf } from 'gql.tada';
22
import { graphql } from '../graphql';
33
import { ChainFragment } from './chain';
44
import { DecimalValueFragment } from './common';
5-
6-
export const MarketFragment = graphql(
7-
`fragment Market on Market {
8-
__typename
9-
name
10-
chain {
11-
...Chain
12-
}
13-
address
14-
totalMarketSize
15-
totalAvailableLiquidity
16-
totalBorrows
17-
}`,
18-
[ChainFragment],
19-
);
20-
export type Market = FragmentOf<typeof MarketFragment>;
5+
import { ReserveFragment } from './reserve';
216

227
export const MarketInfoFragment = graphql(
238
`fragment MarketInfo on MarketInfo {
@@ -48,3 +33,31 @@ export const MarketUserStatsFragment = graphql(
4833
[DecimalValueFragment],
4934
);
5035
export type MarketUserStats = FragmentOf<typeof MarketUserStatsFragment>;
36+
37+
export const MarketFragment = graphql(
38+
`fragment Market on Market {
39+
__typename
40+
name
41+
chain {
42+
...Chain
43+
}
44+
address
45+
totalMarketSize
46+
totalAvailableLiquidity
47+
totalBorrows
48+
49+
borrows: reserves(request: { reserveType: BORROW, orderBy: $borrowsOrderBy }) {
50+
...Reserve
51+
}
52+
53+
supplies: reserves(request: { reserveType: SUPPLY, orderBy: $suppliesOrderBy }) {
54+
...Reserve
55+
}
56+
57+
userStats(address: $userAddress) @include(if: $includeUserFields) {
58+
...MarketUserStats
59+
}
60+
}`,
61+
[ChainFragment, ReserveFragment, MarketUserStatsFragment],
62+
);
63+
export type Market = FragmentOf<typeof MarketFragment>;

packages/graphql/src/fragments/positions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export const UserReserveBorrowPositionFragment = graphql(
1818
}`,
1919
[CurrencyFragment, DecimalValueFragment],
2020
);
21-
export type UserReserveBorrowPosition = FragmentOf<typeof UserReserveBorrowPositionFragment>;
21+
export type UserReserveBorrowPosition = FragmentOf<
22+
typeof UserReserveBorrowPositionFragment
23+
>;
2224

2325
export const UserReserveSupplyPositionFragment = graphql(
2426
`fragment UserReserveSupplyPosition on UserReserveSupplyPosition {
@@ -37,4 +39,6 @@ export const UserReserveSupplyPositionFragment = graphql(
3739
}`,
3840
[CurrencyFragment, DecimalValueFragment],
3941
);
40-
export type UserReserveSupplyPosition = FragmentOf<typeof UserReserveSupplyPositionFragment>;
42+
export type UserReserveSupplyPosition = FragmentOf<
43+
typeof UserReserveSupplyPositionFragment
44+
>;

packages/graphql/src/fragments/reserve.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { FragmentOf } from 'gql.tada';
22
import { graphql } from '../graphql';
33
import { ChainFragment } from './chain';
4-
import {
5-
DecimalValueFragment,
6-
CurrencyFragment,
7-
NativeCurrencyFragment,
8-
TokenAmountFragment
4+
import {
5+
CurrencyFragment,
6+
DecimalValueFragment,
7+
NativeCurrencyFragment,
8+
TokenAmountFragment,
99
} from './common';
1010

1111
export const EmodeInfoFragment = graphql(
@@ -82,7 +82,9 @@ export const ReserveUserAvailabilityFragment = graphql(
8282
}`,
8383
[DecimalValueFragment],
8484
);
85-
export type ReserveUserAvailability = FragmentOf<typeof ReserveUserAvailabilityFragment>;
85+
export type ReserveUserAvailability = FragmentOf<
86+
typeof ReserveUserAvailabilityFragment
87+
>;
8688

8789
export const ReserveFragment = graphql(
8890
`fragment Reserve on Reserve {
@@ -126,6 +128,9 @@ export const ReserveFragment = graphql(
126128
eModeInfo {
127129
...EmodeInfo
128130
}
131+
userAvailability(address: $userAddress) @include(if: $includeUserFields) {
132+
...ReserveUserAvailability
133+
}
129134
}`,
130135
[
131136
ChainFragment,
@@ -136,6 +141,7 @@ export const ReserveFragment = graphql(
136141
ReserveSupplyInfoFragment,
137142
ReserveBorrowInfoFragment,
138143
EmodeInfoFragment,
144+
ReserveUserAvailabilityFragment,
139145
],
140146
);
141147
export type Reserve = FragmentOf<typeof ReserveFragment>;

packages/graphql/src/fragments/transactions.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { FragmentOf } from 'gql.tada';
2-
import { graphql } from '../graphql';
2+
import { type FragmentDocumentFor, graphql } from '../graphql';
33
import { DecimalValueFragment } from './common';
44

55
export const TransactionRequestFragment = graphql(
@@ -47,9 +47,19 @@ export const InsufficientBalanceErrorFragment = graphql(
4747
}`,
4848
[DecimalValueFragment],
4949
);
50-
export type InsufficientBalanceError = FragmentOf<typeof InsufficientBalanceErrorFragment>;
50+
export type InsufficientBalanceError = FragmentOf<
51+
typeof InsufficientBalanceErrorFragment
52+
>;
5153

52-
export const TransactionFragment = graphql(
54+
export type Transaction =
55+
| TransactionRequest
56+
| ApprovalRequired
57+
| InsufficientBalanceError;
58+
59+
export const TransactionFragment: FragmentDocumentFor<
60+
Transaction,
61+
'Transaction'
62+
> = graphql(
5363
`fragment Transaction on Transaction {
5464
__typename
5565
... on TransactionRequest {
@@ -62,6 +72,9 @@ export const TransactionFragment = graphql(
6272
...InsufficientBalanceError
6373
}
6474
}`,
65-
[TransactionRequestFragment, ApprovalRequiredFragment, InsufficientBalanceErrorFragment],
75+
[
76+
TransactionRequestFragment,
77+
ApprovalRequiredFragment,
78+
InsufficientBalanceErrorFragment,
79+
],
6680
);
67-
export type Transaction = FragmentOf<typeof TransactionFragment>;

packages/graphql/src/markets.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import { MarketFragment } from './fragments';
2-
import { graphql, type RequestOf } from './graphql';
2+
import { graphql } from './graphql';
33

44
/**
55
* @internal
66
*/
77
export const MarketsQuery = graphql(
8-
`query Markets($request: MarketsRequest!) {
8+
`query Markets($request: MarketsRequest!, $includeUserFields: Boolean!, $borrowsOrderBy: ReservesRequestOrderBy, $suppliesOrderBy: ReservesRequestOrderBy, $userAddress: EvmAddress) {
99
value: markets(request: $request) {
1010
...Market
1111
}
1212
}`,
1313
[MarketFragment],
1414
);
15-
export type MarketsRequest = RequestOf<typeof MarketsQuery>;
1615

1716
/**
1817
* @internal
1918
*/
2019
export const MarketQuery = graphql(
21-
`query Market($request: MarketRequest!) {
20+
`query Market(
21+
$request: MarketRequest!, $includeUserFields: Boolean!, $borrowsOrderBy: ReservesRequestOrderBy, $suppliesOrderBy: ReservesRequestOrderBy, $userAddress: EvmAddress) {
2222
value: market(request: $request) {
2323
...Market
2424
}
2525
}`,
2626
[MarketFragment],
2727
);
28-
export type MarketRequest = RequestOf<typeof MarketQuery>;

0 commit comments

Comments
 (0)