Skip to content

Commit 8d75194

Browse files
committed
feat: add useSwapQuote and useSwapQuoteAction hooks
1 parent 36561bc commit 8d75194

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

packages/react/src/swap.ts

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
type SwapApprovalRequired,
4141
SwappableTokensQuery,
4242
type SwappableTokensRequest,
43+
SwapQuoteQuery,
4344
type Token,
4445
type TransactionRequest,
4546
UserSwapsQuery,
@@ -66,14 +67,73 @@ import {
6667
} from './helpers';
6768
import { type UseAsyncTask, useAsyncTask } from './helpers/tasks';
6869

70+
export type UseSwapQuoteArgs = Prettify<
71+
SwapQuoteRequest & CurrencyQueryOptions
72+
>;
73+
6974
/**
70-
* Fetches a swap quote for the specified trade parameters.
75+
* Fetch a swap quote for the specified trade parameters.
76+
*
77+
* This signature supports React Suspense:
7178
*
7279
* ```tsx
73-
* const [getQuote, gettingQuote] = useSwapQuote();
80+
* const { data } = useSwapQuote({
81+
* chainId: chainId(1),
82+
* buy: { erc20: evmAddress('0xA0b86a33E6...') },
83+
* sell: { erc20: evmAddress('0x6B175474E...') },
84+
* amount: bigDecimal('1000'),
85+
* kind: SwapKind.SELL,
86+
* suspense: true,
87+
* });
88+
* ```
89+
*/
90+
export function useSwapQuote(
91+
args: UseSwapQuoteArgs & Suspendable,
92+
): SuspenseResult<SwapQuote>;
93+
94+
/**
95+
* Fetch a swap quote for the specified trade parameters.
96+
*
97+
* ```tsx
98+
* const { data, error, loading } = useSwapQuote({
99+
* chainId: chainId(1),
100+
* buy: { erc20: evmAddress('0xA0b86a33E6...') },
101+
* sell: { erc20: evmAddress('0x6B175474E...') },
102+
* amount: bigDecimal('1000'),
103+
* kind: SwapKind.SELL,
104+
* });
105+
* ```
106+
*/
107+
export function useSwapQuote(args: UseSwapQuoteArgs): ReadResult<SwapQuote>;
108+
109+
export function useSwapQuote({
110+
suspense = false,
111+
currency = DEFAULT_QUERY_OPTIONS.currency,
112+
...request
113+
}: UseSwapQuoteArgs & {
114+
suspense?: boolean;
115+
}): SuspendableResult<SwapQuote> {
116+
return useSuspendableQuery({
117+
document: SwapQuoteQuery,
118+
variables: {
119+
request,
120+
currency,
121+
},
122+
suspense,
123+
});
124+
}
125+
126+
/**
127+
* Low-level hook to execute a swap quote action directly.
74128
*
75-
* const loading = gettingQuote.loading;
76-
* const error = gettingQuote.error;
129+
* @experimental This hook is experimental and may be subject to breaking changes.
130+
* @remarks
131+
* This hook **does not** actively watch for updated data on the swap quote.
132+
* Use this hook to retrieve quotes on demand as part of a larger workflow
133+
* (e.g., in an event handler to get a fresh quote before executing a swap).
134+
*
135+
* ```ts
136+
* const [getQuote, { called, data, error, loading }] = useSwapQuoteAction();
77137
*
78138
* // …
79139
*
@@ -85,15 +145,14 @@ import { type UseAsyncTask, useAsyncTask } from './helpers/tasks';
85145
* kind: SwapKind.SELL,
86146
* });
87147
*
88-
* if (result.isErr()) {
148+
* if (result.isOk()) {
149+
* console.log('Swap quote:', result.value);
150+
* } else {
89151
* console.error(result.error);
90-
* return;
91152
* }
92-
*
93-
* console.log('Swap quote:', result.value);
94153
* ```
95154
*/
96-
export function useSwapQuote(
155+
export function useSwapQuoteAction(
97156
options: Required<CurrencyQueryOptions> = DEFAULT_QUERY_OPTIONS,
98157
): UseAsyncTask<SwapQuoteRequest, SwapQuote, UnexpectedError> {
99158
const client = useAaveClient();

0 commit comments

Comments
 (0)