@@ -2,7 +2,7 @@ import BigNumber from 'bignumber.js';
22import { useCallback , useEffect , useMemo , useState } from 'react' ;
33
44import { cn } from '@venusprotocol/ui' ;
5- import { useRepay , useSwapTokensAndRepay } from 'clients/api' ;
5+ import { useGetSwapQuote , useRepay , useSwapTokensAndRepay } from 'clients/api' ;
66import {
77 Delimiter ,
88 LabeledInlineContent ,
@@ -12,14 +12,13 @@ import {
1212 TokenTextField ,
1313} from 'components' ;
1414import useFormatTokensToReadableValue from 'hooks/useFormatTokensToReadableValue' ;
15- import useGetSwapInfo from 'hooks/useGetSwapInfo' ;
1615import useGetSwapTokenUserBalances from 'hooks/useGetSwapTokenUserBalances' ;
1716import { useIsFeatureEnabled } from 'hooks/useIsFeatureEnabled' ;
1817import useTokenApproval from 'hooks/useTokenApproval' ;
1918import { VError } from 'libs/errors' ;
2019import { useTranslation } from 'libs/translations' ;
2120import { useAccountAddress } from 'libs/wallet' ;
22- import type { Asset , BalanceMutation , Pool , Swap , SwapError , TokenBalance } from 'types' ;
21+ import type { Asset , BalanceMutation , Pool , SwapQuote , TokenBalance } from 'types' ;
2322import {
2423 areTokensEqual ,
2524 convertMantissaToTokens ,
@@ -29,9 +28,11 @@ import {
2928 getUniqueTokenBalances ,
3029} from 'utilities' ;
3130
31+ import { NULL_ADDRESS } from 'constants/address' ;
3232import { ConnectWallet } from 'containers/ConnectWallet' ;
3333import useDebounceValue from 'hooks/useDebounceValue' ;
3434import { useGetContractAddress } from 'hooks/useGetContractAddress' ;
35+ import { useGetUserSlippageTolerance } from 'hooks/useGetUserSlippageTolerance' ;
3536import { useSimulateBalanceMutations } from 'hooks/useSimulateBalanceMutations' ;
3637import { useAnalytics } from 'libs/analytics' ;
3738import { ApyBreakdown } from '../../ApyBreakdown' ;
@@ -71,8 +72,8 @@ export interface RepayWithWalletBalanceFormUiProps
7172 isRevokeFromTokenWalletSpendingLimitLoading : boolean ;
7273 onSubmitSuccess ?: ( ) => void ;
7374 fromTokenWalletSpendingLimitTokens ?: BigNumber ;
74- swap ?: Swap ;
75- swapError ?: SwapError ;
75+ swapQuote ?: SwapQuote ;
76+ swapQuoteErrorCode ?: string ;
7677}
7778
7879export const RepayWithWalletBalanceFormUi : React . FC < RepayWithWalletBalanceFormUiProps > = ( {
@@ -98,8 +99,8 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
9899 fromTokenWalletSpendingLimitTokens,
99100 revokeFromTokenWalletSpendingLimit,
100101 isRevokeFromTokenWalletSpendingLimitLoading,
101- swap ,
102- swapError ,
102+ swapQuote ,
103+ swapQuoteErrorCode ,
103104} ) => {
104105 const { t } = useTranslation ( ) ;
105106 const { captureAnalyticEvent } = useAnalytics ( ) ;
@@ -111,7 +112,7 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
111112
112113 const fromTokenUserWalletBalanceTokens = useMemo ( ( ) => {
113114 // Get wallet balance from the list of fetched token balances if integrated
114- // swap feature is enabled and the selected token is different from the
115+ // swapQuote feature is enabled and the selected token is different from the
115116 // asset object
116117 if ( isUsingSwap || isWrappingNativeToken ) {
117118 const tokenBalance = tokenBalances . find ( item =>
@@ -140,7 +141,7 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
140141 const debouncedFormAmountTokens = useDebounceValue ( formValues . amountTokens ) ;
141142
142143 let toTokenAmountTokens = isUsingSwap
143- ? getSwapToTokenAmountReceivedTokens ( swap )
144+ ? getSwapToTokenAmountReceivedTokens ( swapQuote )
144145 : debouncedFormAmountTokens ;
145146 toTokenAmountTokens = new BigNumber ( toTokenAmountTokens || 0 ) ;
146147
@@ -167,8 +168,8 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
167168 fromTokenUserBorrowBalanceTokens : asset . userBorrowBalanceTokens ,
168169 fromTokenWalletSpendingLimitTokens,
169170 isUsingSwap,
170- swap ,
171- swapError ,
171+ swapQuote ,
172+ swapQuoteErrorCode ,
172173 onSubmitSuccess,
173174 onSubmit,
174175 formValues,
@@ -192,7 +193,7 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
192193 fromTokenUserWalletBalanceTokens || 0 ,
193194 ) ;
194195
195- // If using swap , set input amount to wallet balance
196+ // If using swapQuote , set input amount to wallet balance
196197 if ( isUsingSwap ) {
197198 amountTokens = new BigNumber ( fromTokenUserWalletBalanceTokens || 0 ) ;
198199 }
@@ -241,7 +242,7 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
241242 } ;
242243
243244 const canRepayFullLoan =
244- // If user is using swap , we don't know if they can repay the full loan after conversion
245+ // If user is using swapQuote , we don't know if they can repay the full loan after conversion
245246 ! isUsingSwap &&
246247 limitTokens . isGreaterThan ( 0 ) &&
247248 limitTokens . isGreaterThanOrEqualTo ( asset . userBorrowBalanceTokens ) ;
@@ -394,7 +395,7 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
394395 isFormValid = { isFormValid }
395396 isSubmitting = { isSubmitting }
396397 isRepayingFullLoan = { isRepayingFullLoan }
397- priceImpactPercentage = { swap ?. priceImpactPercentage }
398+ priceImpactPercentage = { swapQuote ?. priceImpactPercentage }
398399 />
399400
400401 < div className = "space-y-2" >
@@ -420,14 +421,14 @@ export const RepayWithWalletBalanceFormUi: React.FC<RepayWithWalletBalanceFormUi
420421 simulatedPool = { simulatedPool }
421422 balanceMutations = { balanceMutations }
422423 isUsingSwap = { isUsingSwap }
423- swap = { swap }
424+ swap = { swapQuote }
424425 />
425426 </ div >
426427
427428 < SubmitSection
428429 isFormSubmitting = { isSubmitting }
429430 isFormValid = { isFormValid }
430- swap = { swap }
431+ swapQuote = { swapQuote }
431432 isSwapLoading = { isSwapLoading }
432433 fromToken = { formValues . fromToken }
433434 approveFromToken = { approveFromToken }
@@ -502,8 +503,7 @@ const RepayWithWalletBalanceForm: React.FC<RepayWithWalletBalanceFormProps> = ({
502503 } , [ accountAddress , initialFormValues ] ) ;
503504
504505 const { address : swapRouterContractAddress } = useGetContractAddress ( {
505- name : 'SwapRouter' ,
506- poolComptrollerContractAddress : pool . comptrollerAddress ,
506+ name : 'SwapRouterV2' ,
507507 } ) ;
508508
509509 // a user is trying to wrap the chain's native token if
@@ -592,7 +592,7 @@ const RepayWithWalletBalanceForm: React.FC<RepayWithWalletBalanceFormProps> = ({
592592 } ) ;
593593
594594 const onSubmit : RepayWithWalletBalanceFormUiProps [ 'onSubmit' ] = useCallback (
595- async ( { fromToken, fromTokenAmountTokens, swap , fixedRepayPercentage } ) => {
595+ async ( { fromToken, fromTokenAmountTokens, swapQuote , fixedRepayPercentage } ) => {
596596 const repayFullLoan = fixedRepayPercentage === 100 ;
597597 const amountMantissa = convertTokensToMantissa ( {
598598 value : new BigNumber ( fromTokenAmountTokens . trim ( ) ) ,
@@ -611,18 +611,18 @@ const RepayWithWalletBalanceForm: React.FC<RepayWithWalletBalanceFormProps> = ({
611611 } ) ;
612612 }
613613
614- // Throw an error if we're meant to execute a swap but no swap was
614+ // Throw an error if we're meant to execute a swapQuote but no swapQuote was
615615 // passed through props. This should never happen since the form is
616- // disabled while swap infos are being fetched, but we add this logic
616+ // disabled while swapQuote infos are being fetched, but we add this logic
617617 // as a safeguard
618- if ( ! swap ) {
618+ if ( ! swapQuote ) {
619619 throw new VError ( { type : 'unexpected' , code : 'somethingWentWrong' } ) ;
620620 }
621621
622- // Handle swap and repay flow
622+ // Handle swapQuote and repay flow
623623 return onSwapAndRepay ( {
624624 repayFullLoan,
625- swap ,
625+ swapQuote ,
626626 poolName : pool . name ,
627627 poolComptrollerContractAddress : pool . comptrollerAddress ,
628628 vToken : asset . vToken ,
@@ -639,24 +639,35 @@ const RepayWithWalletBalanceForm: React.FC<RepayWithWalletBalanceFormProps> = ({
639639 ] ,
640640 ) ;
641641
642- const swapDirection = formValues . fixedRepayPercentage ? 'exactAmountOut' : 'exactAmountIn' ;
642+ const { address : swapRouterV2ContractAddress } = useGetContractAddress ( {
643+ name : 'SwapRouterV2' ,
644+ } ) ;
643645
644646 const debouncedFormAmountTokens = useDebounceValue ( formValues . amountTokens ) ;
647+ const fromTokenAmountTokens = new BigNumber ( debouncedFormAmountTokens || 0 ) ;
645648
646- const swapInfo = useGetSwapInfo ( {
647- fromToken : formValues . fromToken || asset . vToken . underlyingToken ,
648- fromTokenAmountTokens :
649- swapDirection === 'exactAmountIn' ? debouncedFormAmountTokens : undefined ,
650- toToken : asset . vToken . underlyingToken ,
651- toTokenAmountTokens : formValues . fixedRepayPercentage
652- ? calculatePercentageOfUserBorrowBalance ( {
653- token : asset . vToken . underlyingToken ,
654- userBorrowBalanceTokens : asset . userBorrowBalanceTokens ,
655- percentage : formValues . fixedRepayPercentage ,
656- } )
657- : undefined ,
658- direction : swapDirection ,
659- } ) ;
649+ const { userSlippageTolerancePercentage } = useGetUserSlippageTolerance ( ) ;
650+
651+ const {
652+ data : swapQuoteData ,
653+ error : swapQuoteError ,
654+ isLoading : swapQuoteLoading ,
655+ } = useGetSwapQuote (
656+ {
657+ fromToken : formValues . fromToken ,
658+ fromTokenAmountTokens,
659+ toToken : asset . vToken . underlyingToken ,
660+ direction : 'exact-in' ,
661+ recipientAddress : swapRouterV2ContractAddress || NULL_ADDRESS ,
662+ slippagePercentage : userSlippageTolerancePercentage ,
663+ } ,
664+ {
665+ enabled :
666+ isUsingSwap && ! ! swapRouterV2ContractAddress && fromTokenAmountTokens . isGreaterThan ( 0 ) ,
667+ } ,
668+ ) ;
669+
670+ const swapQuote = swapQuoteData ?. swapQuote ;
660671
661672 return (
662673 < RepayWithWalletBalanceFormUi
@@ -674,9 +685,9 @@ const RepayWithWalletBalanceForm: React.FC<RepayWithWalletBalanceFormProps> = ({
674685 integratedSwapTokenBalances = { integratedSwapTokenBalancesData }
675686 onSubmit = { onSubmit }
676687 isSubmitting = { isSubmitting }
677- swap = { swapInfo . swap }
678- swapError = { swapInfo . error }
679- isSwapLoading = { swapInfo . isLoading }
688+ swapQuote = { swapQuote }
689+ swapQuoteErrorCode = { swapQuoteError ?. code }
690+ isSwapLoading = { swapQuoteLoading }
680691 isFromTokenApproved = { isFromTokenApproved }
681692 approveFromToken = { approveFromToken }
682693 isApproveFromTokenLoading = { isApproveFromTokenLoading }
0 commit comments