-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
486 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
app/components/UI/Stake/components/MaxInputModal/MaxInputModal.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { StyleSheet } from 'react-native'; | ||
|
||
const createMaxInputModalStyles = () => | ||
StyleSheet.create({ | ||
container: { | ||
paddingHorizontal: 16, | ||
}, | ||
textContainer: { | ||
paddingBottom: 16, | ||
paddingRight: 16, | ||
}, | ||
buttonContainer: { | ||
flexDirection: 'row', | ||
gap: 16, | ||
paddingHorizontal: 16, | ||
paddingBottom: 16, | ||
}, | ||
button: { | ||
flex: 1, | ||
}, | ||
}); | ||
|
||
export default createMaxInputModalStyles; |
30 changes: 30 additions & 0 deletions
30
app/components/UI/Stake/components/MaxInputModal/MaxInputModal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react'; | ||
import renderWithProvider from '../../../../../util/test/renderWithProvider'; | ||
import MaxInputModal from '.'; | ||
import { SafeAreaProvider } from 'react-native-safe-area-context'; | ||
|
||
const mockNavigate = jest.fn(); | ||
|
||
jest.mock('@react-navigation/native', () => { | ||
const actualReactNavigation = jest.requireActual('@react-navigation/native'); | ||
return { | ||
...actualReactNavigation, | ||
useNavigation: () => ({ | ||
navigate: mockNavigate, | ||
}), | ||
}; | ||
}); | ||
|
||
const renderMaxInputModal = () => | ||
renderWithProvider( | ||
<SafeAreaProvider> | ||
<MaxInputModal />, | ||
</SafeAreaProvider>, | ||
); | ||
|
||
describe('MaxInputModal', () => { | ||
it('render matches snapshot', () => { | ||
const { toJSON } = renderMaxInputModal(); | ||
expect(toJSON()).toMatchSnapshot(); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
app/components/UI/Stake/components/MaxInputModal/__snapshots__/MaxInputModal.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MaxInputModal render matches snapshot 1`] = ` | ||
<RNCSafeAreaProvider | ||
onInsetsChange={[Function]} | ||
style={ | ||
[ | ||
{ | ||
"flex": 1, | ||
}, | ||
undefined, | ||
] | ||
} | ||
/> | ||
`; |
79 changes: 79 additions & 0 deletions
79
app/components/UI/Stake/components/MaxInputModal/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { useRef } from 'react'; | ||
import { View } from 'react-native'; | ||
import BottomSheet, { | ||
type BottomSheetRef, | ||
} from '../../../../../component-library/components/BottomSheets/BottomSheet'; | ||
import Text, { | ||
TextVariant, | ||
} from '../../../../../component-library/components/Texts/Text'; | ||
import Button, { | ||
ButtonSize, | ||
ButtonVariants, | ||
ButtonWidthTypes, | ||
} from '../../../../../component-library/components/Buttons/Button'; | ||
import { strings } from '../../../../../../locales/i18n'; | ||
import BottomSheetHeader from '../../../../../component-library/components/BottomSheets/BottomSheetHeader'; | ||
import createMaxInputModalStyles from './MaxInputModal.styles'; | ||
import { useRoute, RouteProp } from '@react-navigation/native'; | ||
|
||
const styles = createMaxInputModalStyles(); | ||
|
||
interface MaxInputModalRouteParams { | ||
handleMaxPress: () => void; | ||
} | ||
|
||
const MaxInputModal = () => { | ||
const route = | ||
useRoute<RouteProp<{ params: MaxInputModalRouteParams }, 'params'>>(); | ||
const sheetRef = useRef<BottomSheetRef>(null); | ||
|
||
const { handleMaxPress } = route.params; | ||
|
||
const handleCancel = () => { | ||
sheetRef.current?.onCloseBottomSheet(); | ||
}; | ||
|
||
const handleConfirm = () => { | ||
sheetRef.current?.onCloseBottomSheet(); | ||
handleMaxPress(); | ||
}; | ||
|
||
return ( | ||
<BottomSheet ref={sheetRef}> | ||
<View style={styles.container}> | ||
<BottomSheetHeader onClose={handleCancel}> | ||
<Text variant={TextVariant.HeadingMD}> | ||
{strings('stake.max_modal.title')} | ||
</Text> | ||
</BottomSheetHeader> | ||
<View style={styles.textContainer}> | ||
<Text variant={TextVariant.BodyMD}> | ||
{strings('stake.max_modal.description')} | ||
</Text> | ||
</View> | ||
</View> | ||
<View style={styles.buttonContainer}> | ||
<View style={styles.button}> | ||
<Button | ||
onPress={handleCancel} | ||
label={strings('stake.cancel')} | ||
variant={ButtonVariants.Secondary} | ||
width={ButtonWidthTypes.Full} | ||
size={ButtonSize.Lg} | ||
/> | ||
</View> | ||
<View style={styles.button}> | ||
<Button | ||
onPress={handleConfirm} | ||
label={strings('stake.use_max')} | ||
variant={ButtonVariants.Primary} | ||
width={ButtonWidthTypes.Full} | ||
size={ButtonSize.Lg} | ||
/> | ||
</View> | ||
</View> | ||
</BottomSheet> | ||
); | ||
}; | ||
|
||
export default MaxInputModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { useStakeContext } from './useStakeContext'; | ||
import type { PooledStakingContract } from '@metamask/stake-sdk'; | ||
import useGasPriceEstimation from '../../Ramp/hooks/useGasPriceEstimation'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectSelectedInternalAccountChecksummedAddress } from '../../../../selectors/accountsController'; | ||
import { formatEther } from 'ethers/lib/utils'; | ||
|
||
interface StakingGasFee { | ||
estimatedGasFeeETH: string; | ||
gasLimit: number; | ||
} | ||
|
||
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; | ||
const DEFAULT_GAS_LIMIT = 21000; | ||
const GAS_LIMIT_BUFFER = 1.3; | ||
|
||
const useStakingGasFee = (depositValueWei: string): StakingGasFee => { | ||
const sdk = useStakeContext(); | ||
const selectedAddress = | ||
useSelector(selectSelectedInternalAccountChecksummedAddress) || ''; | ||
const pooledStakingContract = sdk.stakingContract as PooledStakingContract; | ||
const [gasLimit, setGasLimit] = useState<number>(0); | ||
|
||
useEffect(() => { | ||
const fetchDepositGasLimit = async () => { | ||
try { | ||
const depositGasLimit = await pooledStakingContract.estimateDepositGas( | ||
formatEther(depositValueWei), | ||
selectedAddress, | ||
ZERO_ADDRESS, | ||
); | ||
|
||
const gasLimitWithBuffer = Math.ceil( | ||
depositGasLimit * GAS_LIMIT_BUFFER, | ||
); | ||
|
||
setGasLimit(gasLimitWithBuffer); | ||
} catch (error) { | ||
console.error('Error fetching gas price or gas limit:', error); | ||
setGasLimit(DEFAULT_GAS_LIMIT); | ||
} | ||
}; | ||
|
||
fetchDepositGasLimit(); | ||
}, [depositValueWei, pooledStakingContract, selectedAddress]); | ||
|
||
const gasPriceEstimation = useGasPriceEstimation({ | ||
gasLimit, | ||
estimateRange: 'high', | ||
}); | ||
|
||
const estimatedGasFeeETH = | ||
gasPriceEstimation?.estimatedGasFee?.toString() || '0'; | ||
|
||
return { estimatedGasFeeETH, gasLimit }; | ||
}; | ||
|
||
export default useStakingGasFee; |
Oops, something went wrong.