@@ -19,13 +19,18 @@ import { wallets } from "integrations";
1919import { KeplrWalletManager } from "integrations/Keplr" ;
2020import { getChainFromAddress } from "integrations/utils" ;
2121import { useAtom , useAtomValue , useSetAtom } from "jotai" ;
22- import { useCallback , useEffect , useRef , useState } from "react" ;
23- import { NamadaAsset } from "types " ;
22+ import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
23+ import { toDisplayAmount } from "utils " ;
2424import { SwapSource } from "./SwapSource" ;
2525import { useSwapSimulation } from "./hooks/useSwapSimulation" ;
2626import { useSwapValidation } from "./hooks/useSwapValidation" ;
2727import { SwapQuote , SwapState , SwapStatus } from "./state" ;
28- import { swapQuoteAtom , swapStateAtom , swapStatusAtom } from "./state/atoms" ;
28+ import {
29+ swapMinAmountAtom ,
30+ swapQuoteAtom ,
31+ swapStateAtom ,
32+ swapStatusAtom ,
33+ } from "./state/atoms" ;
2934
3035const ValidationMessages : Record < string , string > = {
3136 NoSellAssetSelected : "Select a token to sell" ,
@@ -54,6 +59,7 @@ export const SwapCalculations = (): JSX.Element => {
5459 const [ swapState , setSwapState ] = useAtom ( swapStateAtom ) ;
5560 const { data : quote } = useAtomValue ( swapQuoteAtom ) ;
5661 const setStatus = useSetAtom ( swapStatusAtom ) ;
62+ const minAmount = useAtomValue ( swapMinAmountAtom ) ;
5763
5864 // Global state
5965 const sortedAssets = useAtomValue ( namadaAssetsSortedAtom ) ;
@@ -111,10 +117,9 @@ export const SwapCalculations = (): JSX.Element => {
111117 sellAmount : a ,
112118 } ) ) ;
113119 } else {
114- setSwapState ( ( s ) => ( {
120+ setSwapState ( {
115121 mode : "none" ,
116- sellAmountPerOneBuy : s . sellAmountPerOneBuy ,
117- } ) ) ;
122+ } ) ;
118123 }
119124 } , [ ] ) ;
120125
@@ -126,10 +131,9 @@ export const SwapCalculations = (): JSX.Element => {
126131 buyAmount : a ,
127132 } ) ) ;
128133 } else {
129- setSwapState ( ( s ) => ( {
134+ setSwapState ( {
130135 mode : "none" ,
131- sellAmountPerOneBuy : s . sellAmountPerOneBuy ,
132- } ) ) ;
136+ } ) ;
133137 }
134138 } , [ ] ) ;
135139
@@ -141,7 +145,6 @@ export const SwapCalculations = (): JSX.Element => {
141145 mode : newMode ,
142146 sellAmount : s . buyAmount ,
143147 buyAmount : s . sellAmount ,
144- sellAmountPerOneBuy : s . sellAmountPerOneBuy ,
145148 } ;
146149 }
147150
@@ -240,20 +243,15 @@ export const SwapCalculations = (): JSX.Element => {
240243 isSubmitting = { false }
241244 label = "Buy"
242245 />
243- { feeProps &&
244- swapState . sellAmountPerOneBuy &&
245- sellAsset &&
246- buyAsset &&
247- tokenPrices && (
248- < SwapCalculationsFooter
249- feeProps = { feeProps }
250- sellAmountPerOneBuy = { swapState . sellAmountPerOneBuy }
251- selectedAsset = { sellAsset }
252- selectedTargetAsset = { buyAsset }
253- tokenPrice = { tokenPrices [ buyAsset . address ] }
254- quote = { quote }
255- />
256- ) }
246+ { feeProps && sellAsset && buyAsset && tokenPrices && (
247+ < SwapCalculationsFooter
248+ feeProps = { feeProps }
249+ swapState = { swapState }
250+ minAmount = { minAmount }
251+ tokenPrice = { tokenPrices [ buyAsset . address ] }
252+ quote = { quote }
253+ />
254+ ) }
257255
258256 < ActionButton
259257 outlineColor = "yellow"
@@ -299,21 +297,20 @@ export const SwapCalculations = (): JSX.Element => {
299297
300298type SwapCalculationsFooterProps = {
301299 feeProps : TransactionFeeProps ;
302- sellAmountPerOneBuy : BigNumber ;
303- selectedAsset : NamadaAsset ;
304- selectedTargetAsset : NamadaAsset ;
305300 tokenPrice : BigNumber ;
306301 quote ?: SwapQuote ;
302+ swapState : SwapState ;
303+ minAmount ?: BigNumber ;
307304} ;
308305
309306const SwapCalculationsFooter = ( {
310307 feeProps,
311- sellAmountPerOneBuy,
312- selectedAsset,
313- selectedTargetAsset,
308+ swapState,
314309 tokenPrice,
315310 quote,
311+ minAmount,
316312} : SwapCalculationsFooterProps ) : JSX . Element => {
313+ const { sellAsset, buyAsset } = swapState ;
317314 // Quote cache, prevents blinking when quote is temporarily undefined
318315 const lastValidQuoteRef = useRef < typeof quote > ( ) ;
319316 useEffect ( ( ) => {
@@ -323,7 +320,24 @@ const SwapCalculationsFooter = ({
323320 } , [ quote ] ) ;
324321
325322 const quoteToUse = quote ?? lastValidQuoteRef . current ;
326- if ( ! quoteToUse ) {
323+
324+ const sellAmountPerOneBuy = useMemo ( ( ) => {
325+ if ( ! quoteToUse || ! buyAsset || ! minAmount ) {
326+ return ;
327+ }
328+
329+ const baseAmount =
330+ [ "sell" , "none" ] . includes ( swapState . mode ) ?
331+ quoteToUse . amountIn
332+ : quoteToUse . amountOut ;
333+
334+ return toDisplayAmount (
335+ buyAsset ,
336+ minAmount . div ( toDisplayAmount ( buyAsset , baseAmount ) )
337+ ) ;
338+ } , [ buyAsset ?. symbol , quoteToUse ] ) ;
339+
340+ if ( ! quoteToUse || ! sellAmountPerOneBuy || ! sellAsset || ! buyAsset ) {
327341 return < > </ > ;
328342 }
329343
@@ -338,8 +352,8 @@ const SwapCalculationsFooter = ({
338352 direction = "horizontal"
339353 >
340354 < div className = "underline" >
341- 1 { selectedAsset . symbol } ≈ { sellAmountPerOneBuy . toFixed ( 6 ) } { " " }
342- { selectedTargetAsset . symbol } (${ valFiat . toFixed ( 6 ) } )
355+ 1 { sellAsset . symbol } ≈ { sellAmountPerOneBuy . toFixed ( 6 ) } { " " }
356+ { buyAsset . symbol } (${ valFiat . toFixed ( 6 ) } )
343357 </ div >
344358 < TransactionFeeButton
345359 compact = { true }
0 commit comments