Skip to content

Commit 60ca113

Browse files
authored
Merge pull request #219 from aave/cesare/aave-3208-sdk-support-swap-position-adapters
feat: support swap position adapters
2 parents 8478c31 + 59059be commit 60ca113

37 files changed

+1914
-950
lines changed

.changeset/fair-tables-arrive.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@aave/graphql": patch
3+
"@aave/client": patch
4+
"@aave/types": patch
5+
---
6+
7+
**feat:** `preparePositionSwap` and `supplySwapQuote` actions.

.changeset/quick-cities-listen.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@aave/graphql": patch
3+
"@aave/react": patch
4+
"@aave/types": patch
5+
---
6+
7+
**feat:** `useSupplySwap` and `useSupplySwapQuote` hooks.

.changeset/sour-plants-dig.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@aave/graphql": patch
3+
"@aave/types": patch
4+
---
5+
6+
**feat:** support latest GQL schema.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"test:client:viem": "vitest --project client packages/client/src/viem.test.ts",
3030
"test:core": "vitest --project core",
3131
"test:react": "vitest --project react",
32+
"test:react:swap": "vitest --project react packages/react/src/swap.test.ts",
3233
"test:react:viem": "vitest --project react packages/react/src/viem/*.test.ts",
3334
"test:types": "vitest --project types",
3435
"test": "pnpm test:types && pnpm test:core && pnpm test:client && pnpm test:cli && pnpm test:react"
@@ -52,6 +53,9 @@
5253
"overrides": {
5354
"react": "^19.1.0",
5455
"react-dom": "^19.1.0"
56+
},
57+
"patchedDependencies": {
58+
5559
}
5660
},
5761
"packageManager": "[email protected]"

packages/client/src/actions/swap.ts

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import type {
33
CancelSwapExecutionPlan,
44
CancelSwapRequest,
55
PaginatedUserSwapsResult,
6+
PreparePositionSwapRequest,
7+
PreparePositionSwapResult,
8+
PrepareSupplySwapRequest,
9+
PrepareSupplySwapResult,
610
PrepareSwapCancelRequest,
711
PrepareSwapCancelResult,
8-
PrepareSwapRequest,
9-
PrepareSwapResult,
12+
PrepareTokenSwapRequest,
13+
PrepareTokenSwapResult,
1014
SwapCancelled,
1115
SwapExecutionPlan,
1216
SwapExpired,
@@ -23,10 +27,12 @@ import type {
2327
} from '@aave/graphql';
2428
import {
2529
CancelSwapQuery,
30+
PreparePositionSwapQuery,
2631
PrepareSwapCancelQuery,
27-
PrepareSwapQuery,
32+
PrepareTokenSwapQuery,
33+
SupplySwapQuoteQuery,
34+
SwapMutation,
2835
SwappableTokensQuery,
29-
SwapQuery,
3036
SwapQuoteQuery,
3137
SwapStatusQuery,
3238
UserSwapsQuery,
@@ -36,7 +42,7 @@ import type { AaveClient } from '../AaveClient';
3642
import { type CurrencyQueryOptions, DEFAULT_QUERY_OPTIONS } from '../options';
3743

3844
/**
39-
* @internal
45+
* @experimental
4046
* Fetches a swap quote for the specified trade parameters.
4147
*
4248
* ```ts
@@ -64,7 +70,7 @@ export function swapQuote(
6470
}
6571

6672
/**
67-
* @internal
73+
* @experimental
6874
* Fetches the list of tokens available for swapping on a specific chain.
6975
*
7076
* ```ts
@@ -85,11 +91,11 @@ export function swappableTokens(
8591
}
8692

8793
/**
88-
* @internal
94+
* @experimental
8995
* Prepares a swap for the specified trade parameters.
9096
*
9197
* ```ts
92-
* const result = await prepareSwap(client, {
98+
* const result = await prepareTokenSwap(client, {
9399
* market: {
94100
* chainId: chainId(1),
95101
* buy: { erc20: evmAddress('0xA0b86a33E6...') },
@@ -135,16 +141,77 @@ export function swappableTokens(
135141
* @param options - The query options.
136142
* @returns The prepared swap result containing details of the swap.
137143
*/
138-
export function prepareSwap(
144+
export function prepareTokenSwap(
139145
client: AaveClient,
140-
request: PrepareSwapRequest,
146+
request: PrepareTokenSwapRequest,
141147
options: Required<CurrencyQueryOptions> = DEFAULT_QUERY_OPTIONS,
142-
): ResultAsync<PrepareSwapResult, UnexpectedError> {
143-
return client.query(PrepareSwapQuery, { request, ...options });
148+
): ResultAsync<PrepareTokenSwapResult, UnexpectedError> {
149+
return client.query(PrepareTokenSwapQuery, { request, ...options });
144150
}
145151

146152
/**
147-
* @internal
153+
* @experimental
154+
* Fetches a supply swap quote for swapping supplied collateral.
155+
*
156+
* ```ts
157+
* const result = await supplySwapQuote(client, {
158+
* market: {
159+
* sellPosition: userSupplyItemId('position_123'),
160+
* buyReserve: reserveId('reserve_456'),
161+
* amount: bigDecimal('1000'),
162+
* user: evmAddress('0x742d35cc...'),
163+
* },
164+
* });
165+
* ```
166+
*
167+
* @param client - Aave client.
168+
* @param request - The supply swap request parameters.
169+
* @param options - The query options.
170+
* @returns The supply swap result with quote, approvals, and preview.
171+
*/
172+
export function supplySwapQuote(
173+
client: AaveClient,
174+
request: PrepareSupplySwapRequest,
175+
options: Required<CurrencyQueryOptions> = DEFAULT_QUERY_OPTIONS,
176+
): ResultAsync<PrepareSupplySwapResult, UnexpectedError> {
177+
return client.query(
178+
SupplySwapQuoteQuery,
179+
{ request, currency: options.currency },
180+
{ batch: false },
181+
);
182+
}
183+
184+
/**
185+
* @experimental
186+
* Prepares a position swap by obtaining the typed data for signing.
187+
*
188+
* ```ts
189+
* const result = await preparePositionSwap(client, {
190+
* quoteId: swapQuoteId('quote_123'),
191+
* adapterContractSignature: signature('0x456...'),
192+
* positionManagerSignature: signature('0x789...'),
193+
* });
194+
* ```
195+
*
196+
* @param client - Aave client.
197+
* @param request - The position swap request with quote ID and signatures.
198+
* @param options - The query options.
199+
* @returns The position swap result with intent data for execution.
200+
*/
201+
export function preparePositionSwap(
202+
client: AaveClient,
203+
request: PreparePositionSwapRequest,
204+
options: Required<CurrencyQueryOptions> = DEFAULT_QUERY_OPTIONS,
205+
): ResultAsync<PreparePositionSwapResult, UnexpectedError> {
206+
return client.query(
207+
PreparePositionSwapQuery,
208+
{ request, currency: options.currency },
209+
{ batch: false },
210+
);
211+
}
212+
213+
/**
214+
* @experimental
148215
* Fetches the status of a specific swap.
149216
*
150217
* ```ts
@@ -169,7 +236,7 @@ export function swapStatus(
169236
export type SwapOutcome = SwapCancelled | SwapExpired | SwapFulfilled;
170237

171238
/**
172-
* @internal
239+
* @experimental
173240
* Waits for a swap to reach a final outcome (cancelled, expired, or fulfilled).
174241
*
175242
* ```ts
@@ -246,7 +313,7 @@ export function waitForSwapOutcome(
246313
}
247314

248315
/**
249-
* @internal
316+
* @experimental
250317
* Executes a swap for the specified request parameters.
251318
*
252319
* ```ts
@@ -293,11 +360,11 @@ export function swap(
293360
client: AaveClient,
294361
request: SwapRequest,
295362
): ResultAsync<SwapExecutionPlan, UnexpectedError> {
296-
return client.query(SwapQuery, { request });
363+
return client.mutation(SwapMutation, { request });
297364
}
298365

299366
/**
300-
* @internal
367+
* @experimental
301368
* Prepares a swap cancellation for the specified swap ID.
302369
*
303370
* ```ts
@@ -318,7 +385,7 @@ export function prepareSwapCancel(
318385
}
319386

320387
/**
321-
* @internal
388+
* @experimental
322389
* Executes a swap cancellation for the specified request parameters.
323390
*
324391
* ```ts
@@ -354,7 +421,7 @@ export function cancelSwap(
354421
}
355422

356423
/**
357-
* @internal
424+
* @experimental
358425
* Fetches the user's swap history for a specific chain.
359426
*
360427
* ```ts

packages/client/src/cache.ts

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
type ActivitiesQuery,
3+
type Asset,
34
type BorrowActivity,
45
type Chain,
56
type Erc20Token,
@@ -16,9 +17,6 @@ import {
1617
type ReserveInfo,
1718
type Spoke,
1819
type SupplyActivity,
19-
type SwapByIntent,
20-
type SwapByIntentWithApprovalRequired,
21-
type SwapByTransaction,
2220
type TokenInfo,
2321
type UpdatedDynamicConfigActivity,
2422
type UpdatedRiskPremiumActivity,
@@ -118,12 +116,6 @@ export const exchange = cacheExchange({
118116
AssetBorrowSample: {
119117
date: transformToDate,
120118
},
121-
AssetCategoryBorrowSample: {
122-
date: transformToDate,
123-
},
124-
AssetCategorySupplySample: {
125-
date: transformToDate,
126-
},
127119
AssetSupplySample: {
128120
date: transformToDate,
129121
},
@@ -268,30 +260,24 @@ export const exchange = cacheExchange({
268260
},
269261
},
270262
keys: {
271-
// Entitied with composite key
263+
// Entities with id field as key
264+
Asset: (data: Asset) => data.id,
265+
BorrowActivity: (data: BorrowActivity) => data.id,
272266
Hub: (data: Hub) => data.id,
273267
HubAsset: (data: HubAsset) => data.id,
268+
LiquidatedActivity: (data: LiquidatedActivity) => data.id,
269+
RepayActivity: (data: RepayActivity) => data.id,
274270
Reserve: (data: Reserve) => data.id,
275271
ReserveInfo: (data: ReserveInfo) => data.id,
276272
Spoke: (data: Spoke) => data.id,
277-
278-
// Entities with id field as key
279-
BorrowActivity: (data: BorrowActivity) => data.id,
280-
LiquidatedActivity: (data: LiquidatedActivity) => data.id,
281273
SupplyActivity: (data: SupplyActivity) => data.id,
282-
SwapByIntent: (data: SwapByIntent) => data.quote.quoteId,
283-
SwapByIntentWithApprovalRequired: (
284-
data: SwapByIntentWithApprovalRequired,
285-
) => data.quote.quoteId,
286-
SwapByTransaction: (data: SwapByTransaction) => data.quote.quoteId,
274+
TokenInfo: (data: TokenInfo) => data.id,
287275
UserPosition: (data: UserPosition) => data.id,
276+
UsingAsCollateralActivity: (data: UsingAsCollateralActivity) => data.id,
277+
WithdrawActivity: (data: WithdrawActivity) => data.id,
288278
UpdatedDynamicConfigActivity: (data: UpdatedDynamicConfigActivity) =>
289279
data.id,
290280
UpdatedRiskPremiumActivity: (data: UpdatedRiskPremiumActivity) => data.id,
291-
UsingAsCollateralActivity: (data: UsingAsCollateralActivity) => data.id,
292-
WithdrawActivity: (data: WithdrawActivity) => data.id,
293-
RepayActivity: (data: RepayActivity) => data.id,
294-
TokenInfo: (data: TokenInfo) => data.id,
295281

296282
// Entities with address field as key
297283
Erc20Token: (data: Erc20Token) => data.address,
@@ -300,29 +286,13 @@ export const exchange = cacheExchange({
300286
Chain: (data: Chain) => data.chainId.toString(),
301287
NativeToken: (data: NativeToken) => data.chain.chainId.toString(),
302288

303-
// Entities without keys will be embedded directly on the parent entity
304-
PaginatedActivitiesResult: () => null,
305-
PaginatedResultInfo: () => null,
306-
PaginatedSpokePositionManagerResult: () => null,
307-
PaginatedSpokeUserPositionManagerResult: () => null,
308-
PaginatedUserSwapsResult: () => null,
309-
SpokePositionManger: () => null,
310-
SpokeUserPositionManager: () => null,
311-
SwapReceipt: () => null,
312-
SwapTransactionRequest: () => null,
313-
314289
// Value objects and result types
315290
ApySample: () => null,
316-
Asset: () => null,
317291
AssetAmountWithChange: () => null,
318292
AssetBorrowSample: () => null,
319-
AssetCategoryBorrowSample: () => null,
320-
AssetCategorySupplySample: () => null,
321293
AssetPriceSample: () => null,
322294
AssetSummary: () => null,
323295
AssetSupplySample: () => null,
324-
CancelSwapTypedData: () => null,
325-
CancelSwapTypeDefinition: () => null,
326296
DecimalNumber: () => null,
327297
DecimalNumberWithChange: () => null,
328298
DomainData: () => null,
@@ -342,11 +312,19 @@ export const exchange = cacheExchange({
342312
HubSummarySample: () => null,
343313
InsufficientBalanceError: () => null,
344314
NativeAmount: () => null,
315+
PaginatedActivitiesResult: () => null,
316+
PaginatedResultInfo: () => null,
317+
PaginatedSpokePositionManagerResult: () => null,
318+
PaginatedSpokeUserPositionManagerResult: () => null,
319+
PaginatedUserSwapsResult: () => null,
345320
PercentNumber: () => null,
346321
PercentNumberVariation: () => null,
347322
PercentNumberWithChange: () => null,
348323
PermitMessageData: () => null,
349324
PermitTypedDataResponse: () => null,
325+
PositionSwapAdapterContractApproval: () => null,
326+
PositionSwapByIntentApprovalsRequired: () => null,
327+
PositionSwapPositionManagerApproval: () => null,
350328
PreContractActionRequired: () => null,
351329
PrepareSwapCancelResult: () => null,
352330
PreviewUserPosition: () => null,
@@ -355,16 +333,22 @@ export const exchange = cacheExchange({
355333
ReserveStatus: () => null,
356334
ReserveSummary: () => null,
357335
ReserveUserState: () => null,
336+
SpokePositionManger: () => null,
337+
SpokeUserPositionManager: () => null,
358338
SwapApprovalRequired: () => null,
359-
SwapByIntentTypedData: () => null,
360-
SwapByIntentTypeDefinition: () => null,
339+
SwapByIntent: () => null,
340+
SwapByIntentWithApprovalRequired: () => null,
341+
SwapByTransaction: () => null,
361342
SwapCancelled: () => null,
362343
SwapExpired: () => null,
363344
SwapFulfilled: () => null,
364345
SwapOpen: () => null,
365346
SwapPendingSignature: () => null,
366347
SwapQuote: () => null,
367348
SwapQuoteCosts: () => null,
349+
SwapReceipt: () => null,
350+
SwapTransactionRequest: () => null,
351+
SwapTypedData: () => null,
368352
TransactionRequest: () => null,
369353
TypeDefinition: () => null,
370354
TypeField: () => null,

0 commit comments

Comments
 (0)