@@ -9,46 +9,59 @@ import { OrmlTraitsAssetRegistryAssetMetadata } from '../../hooks/useBuyout/type
9
9
import { GasForm , IssueFormValues } from './GasForm' ;
10
10
import { calculateForCurrentFromToken , calculatePriceNativeForCurrentFromToken } from './helpers' ;
11
11
import { GasSuccessDialog } from './GasSuccessDialog' ;
12
+ import { GasSkeleton } from './GasSkeleton' ;
12
13
13
14
const Gas = ( ) => {
14
15
const { currencies, buyoutNativeToken, sellFee, nativeCurrency, handleBuyout } = useBuyout ( ) ;
15
16
const { pricesCache } = usePriceFetcher ( ) ;
16
17
17
- const [ selectedFromToken , setSelectedFromToken ] = useState < BlockchainAsset | undefined > ( currencies [ 0 ] ) ;
18
+ const [ selectedFromToken , setSelectedFromToken ] = useState < BlockchainAsset | undefined > ( undefined ) ;
18
19
const [ selectedFromTokenPriceUSD , setSelectedFromTokenPriceUSD ] = useState < number > ( 0 ) ;
19
20
const [ nativeTokenPrice , setNativeTokenPrice ] = useState < number > ( 0 ) ;
20
21
const [ submissionPending , setSubmissionPending ] = useState < boolean > ( false ) ;
21
22
const [ confirmationDialogVisible , setConfirmationDialogVisible ] = useState < boolean > ( false ) ;
22
23
23
24
useEffect ( ( ) => {
24
25
const fetchPricesCache = async ( ) => {
25
- const tokensPrices = await pricesCache ;
26
+ if ( nativeCurrency && isOrmlAsset ( selectedFromToken ) ) {
27
+ const tokensPrices = await pricesCache ;
26
28
27
- if ( ! isEmpty ( tokensPrices ) && isOrmlAsset ( selectedFromToken ) ) {
28
- setSelectedFromTokenPriceUSD ( tokensPrices [ selectedFromToken . metadata . symbol ] ) ;
29
- const nativeTokenPrice = tokensPrices [ nativeCurrency . metadata . symbol ] ;
30
- // We add the sellFee to the native price to already accommodate for it in the calculations
31
- const nativeTokenPriceWithFee = sellFee . addSelfToBase ( nativeTokenPrice ) ;
32
- setNativeTokenPrice ( nativeTokenPriceWithFee ) ;
29
+ if ( ! isEmpty ( tokensPrices ) ) {
30
+ setSelectedFromTokenPriceUSD ( tokensPrices [ selectedFromToken . metadata . symbol ] ) ;
31
+ const nativeTokenPrice = tokensPrices [ nativeCurrency . metadata . symbol ] ;
32
+ // We add the sellFee to the native price to already accommodate for it in the calculations
33
+ const nativeTokenPriceWithFee = sellFee . addSelfToBase ( nativeTokenPrice ) ;
34
+ setNativeTokenPrice ( nativeTokenPriceWithFee ) ;
35
+ }
33
36
}
34
37
} ;
35
38
36
39
fetchPricesCache ( ) . catch ( console . error ) ;
37
- } , [ nativeCurrency . metadata . symbol , pricesCache , selectedFromToken , sellFee ] ) ;
40
+ } , [ nativeCurrency , pricesCache , selectedFromToken , sellFee ] ) ;
41
+
42
+ useEffect ( ( ) => {
43
+ if ( ! selectedFromToken ) {
44
+ setSelectedFromToken ( currencies [ 0 ] as OrmlTraitsAssetRegistryAssetMetadata ) ;
45
+ }
46
+ } , [ selectedFromToken , currencies ] ) ;
38
47
39
48
const onSubmit = async ( data : IssueFormValues ) => {
40
- // If the user has selected the min or max amount by clicking the badge button, we call the buyout extrinsic in the
41
- // direction of the native token being the input token. This way we ensure that the amount is perfectly within the buyout limits and
42
- // the transaction does not fail due to imprecise calculations.
43
- const isExchangeAmount = data . isMin || data . isMax ;
44
- const token = isExchangeAmount ? nativeCurrency : ( selectedFromToken as OrmlTraitsAssetRegistryAssetMetadata ) ;
45
- const amount = data . isMin ? buyoutNativeToken . min : data . isMax ? buyoutNativeToken . max : Number ( data . fromAmount ) ;
49
+ if ( nativeCurrency && selectedFromToken ) {
50
+ // If the user has selected the min or max amount by clicking the badge button, we call the buyout extrinsic in the
51
+ // direction of the native token being the input token. This way we ensure that the amount is perfectly within the buyout limits and
52
+ // the transaction does not fail due to imprecise calculations.
53
+ const isExchangeAmount = data . isMin || data . isMax ;
54
+ const token = isExchangeAmount ? nativeCurrency : ( selectedFromToken as OrmlTraitsAssetRegistryAssetMetadata ) ;
55
+ const amount = data . isMin ? buyoutNativeToken . min : data . isMax ? buyoutNativeToken . max : Number ( data . fromAmount ) ;
46
56
47
- handleBuyout ( token , amount , setSubmissionPending , setConfirmationDialogVisible , isExchangeAmount ) ;
57
+ handleBuyout ( token , amount , setSubmissionPending , setConfirmationDialogVisible , isExchangeAmount ) ;
58
+ }
48
59
} ;
49
60
50
- const selectedTokenDecimals = ( selectedFromToken as OrmlTraitsAssetRegistryAssetMetadata ) . metadata . decimals ;
51
- const nativeDecimals = ( nativeCurrency as OrmlTraitsAssetRegistryAssetMetadata ) . metadata . decimals ;
61
+ const selectedTokenDecimals = ( selectedFromToken as OrmlTraitsAssetRegistryAssetMetadata ) ?. metadata . decimals ?? 0 ;
62
+ const nativeDecimals = ( nativeCurrency as OrmlTraitsAssetRegistryAssetMetadata ) ?. metadata . decimals ?? 0 ;
63
+
64
+ if ( ! selectedFromToken || ! nativeCurrency ) return < GasSkeleton /> ;
52
65
53
66
return (
54
67
< div className = "h-full flex items-center justify-center mt-4" >
@@ -62,7 +75,7 @@ const Gas = () => {
62
75
< h1 className = "text-[28px] mb-8" > Get AMPE</ h1 >
63
76
< GasForm
64
77
submissionPending = { submissionPending }
65
- currencies = { currencies }
78
+ currencies = { currencies . length ? currencies : [ ] }
66
79
selectedFromToken = { selectedFromToken }
67
80
setSelectedFromToken = { setSelectedFromToken }
68
81
nativeCurrency = { nativeCurrency }
0 commit comments