66} from "@radix-ui/react-icons" ;
77import { useQuery } from "@tanstack/react-query" ;
88import { useState } from "react" ;
9+ import type { prepare as BuyPrepare } from "../../../../../bridge/Buy.js" ;
910import { Buy , Sell } from "../../../../../bridge/index.js" ;
11+ import type { prepare as SellPrepare } from "../../../../../bridge/Sell.js" ;
1012import type { BridgeChain } from "../../../../../bridge/types/Chain.js" ;
1113import type { TokenWithPrices } from "../../../../../bridge/types/Token.js" ;
1214import { defineChain } from "../../../../../chains/utils.js" ;
@@ -28,6 +30,10 @@ import {
2830 type Theme ,
2931} from "../../../../core/design-system/index.js" ;
3032import { useWalletBalance } from "../../../../core/hooks/others/useWalletBalance.js" ;
33+ import type {
34+ BridgePrepareRequest ,
35+ BridgePrepareResult ,
36+ } from "../../../../core/hooks/useBridgePrepare.js" ;
3137import { ConnectButton } from "../../ConnectWallet/ConnectButton.js" ;
3238import { ArrowUpDownIcon } from "../../ConnectWallet/icons/ArrowUpDownIcon.js" ;
3339import { WalletDotIcon } from "../../ConnectWallet/icons/WalletDotIcon.js" ;
@@ -57,15 +63,14 @@ type SwapUIProps = {
5763 connectOptions : SwapWidgetConnectOptions | undefined ;
5864 currency : SupportedFiatCurrency ;
5965 showThirdwebBranding : boolean ;
60- onSwap : (
61- quote : Buy . quote . Result | Sell . quote . Result ,
62- selection : {
63- buyToken : TokenWithPrices ;
64- sellTokenBalance : bigint ;
65- sellToken : TokenWithPrices ;
66- mode : "buy" | "sell" ;
67- } ,
68- ) => void ;
66+ onSwap : ( data : {
67+ result : Extract < BridgePrepareResult , { type : "buy" | "sell" } > ;
68+ request : Extract < BridgePrepareRequest , { type : "buy" | "sell" } > ;
69+ buyToken : TokenWithPrices ;
70+ sellTokenBalance : bigint ;
71+ sellToken : TokenWithPrices ;
72+ mode : "buy" | "sell" ;
73+ } ) => void ;
6974 buyToken : TokenSelection | undefined ;
7075 sellToken : TokenSelection | undefined ;
7176 setBuyToken : ( token : TokenSelection | undefined ) => void ;
@@ -191,7 +196,10 @@ function SwapUIBase(
191196 ! ! buyTokenWithPrices &&
192197 ! ! sellTokenWithPrices &&
193198 ! ! props . amountSelection . amount ,
194- queryFn : async ( ) => {
199+ queryFn : async ( ) : Promise < {
200+ result : Extract < BridgePrepareResult , { type : "buy" | "sell" } > ;
201+ request : Extract < BridgePrepareRequest , { type : "buy" | "sell" } > ;
202+ } > => {
195203 if (
196204 ! buyTokenWithPrices ||
197205 ! sellTokenWithPrices ||
@@ -202,8 +210,8 @@ function SwapUIBase(
202210 }
203211
204212 if ( props . amountSelection . type === "buy" ) {
205- const res = await Buy . quote ( {
206- buyAmountWei : toUnits (
213+ const buyRequestOptions : BuyPrepare . Options = {
214+ amount : toUnits (
207215 props . amountSelection . amount ,
208216 buyTokenWithPrices . decimals ,
209217 ) ,
@@ -214,11 +222,23 @@ function SwapUIBase(
214222 destinationChainId : buyTokenWithPrices . chainId ,
215223 destinationTokenAddress : buyTokenWithPrices . address ,
216224 client : props . client ,
217- } ) ;
225+ receiver : props . activeWalletInfo . activeAccount . address ,
226+ sender : props . activeWalletInfo . activeAccount . address ,
227+ } ;
228+
229+ const buyRequest : BridgePrepareRequest = {
230+ type : "buy" ,
231+ ...buyRequestOptions ,
232+ } ;
233+
234+ const res = await Buy . prepare ( buyRequest ) ;
218235
219- return res ;
236+ return {
237+ result : { type : "buy" , ...res } ,
238+ request : buyRequest ,
239+ } ;
220240 } else if ( props . amountSelection . type === "sell" ) {
221- const res = await Sell . prepare ( {
241+ const sellRequestOptions : SellPrepare . Options = {
222242 amount : toUnits (
223243 props . amountSelection . amount ,
224244 sellTokenWithPrices . decimals ,
@@ -232,12 +252,22 @@ function SwapUIBase(
232252 client : props . client ,
233253 receiver : props . activeWalletInfo . activeAccount . address ,
234254 sender : props . activeWalletInfo . activeAccount . address ,
235- } ) ;
255+ } ;
256+
257+ const res = await Sell . prepare ( sellRequestOptions ) ;
258+
259+ const sellRequest : BridgePrepareRequest = {
260+ type : "sell" ,
261+ ...sellRequestOptions ,
262+ } ;
236263
237- return res ;
264+ return {
265+ result : { type : "sell" , ...res } ,
266+ request : sellRequest ,
267+ } ;
238268 }
239269
240- return null ;
270+ throw new Error ( "Invalid amount selection type" ) ;
241271 } ,
242272 refetchInterval : 20000 ,
243273 } ) ;
@@ -249,7 +279,7 @@ function SwapUIBase(
249279 props . amountSelection . type === "buy" &&
250280 sellTokenWithPrices
251281 ? toTokens (
252- preparedResultQuery . data . originAmount ,
282+ preparedResultQuery . data . result . originAmount ,
253283 sellTokenWithPrices . decimals ,
254284 )
255285 : "" ;
@@ -261,7 +291,7 @@ function SwapUIBase(
261291 props . amountSelection . type === "sell" &&
262292 buyTokenWithPrices
263293 ? toTokens (
264- preparedResultQuery . data . destinationAmount ,
294+ preparedResultQuery . data . result . destinationAmount ,
265295 buyTokenWithPrices . decimals ,
266296 )
267297 : "" ;
@@ -396,7 +426,9 @@ function SwapUIBase(
396426 sellTokenWithPrices &&
397427 sellTokenBalanceQuery . data
398428 ) {
399- props . onSwap ( preparedResultQuery . data , {
429+ props . onSwap ( {
430+ result : preparedResultQuery . data . result ,
431+ request : preparedResultQuery . data . request ,
400432 buyToken : buyTokenWithPrices ,
401433 sellToken : sellTokenWithPrices ,
402434 sellTokenBalance : sellTokenBalanceQuery . data . value ,
0 commit comments