-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add fee estimation for Withdrawing CELO and pay for Withdrawing and Selling CELO fees in CELO #5345
Changes from 3 commits
e1218a0
ee4faf4
4274aac
c830b74
b7d9d9a
750d1f6
dc240a9
00fd474
2d614f0
cea9876
9150ae9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ import fontStyles from '@celo/react-components/styles/fonts' | |
import variables from '@celo/react-components/styles/variables' | ||
import { StackScreenProps } from '@react-navigation/stack' | ||
import BigNumber from 'bignumber.js' | ||
import React, { useState } from 'react' | ||
import React, { useEffect, useState } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { StyleSheet, Text } from 'react-native' | ||
import { SafeAreaView } from 'react-native-safe-area-context' | ||
|
@@ -25,14 +25,20 @@ import { HeaderTitleWithBalance, headerWithBackButton } from 'src/navigator/Head | |
import { Screens } from 'src/navigator/Screens' | ||
import { StackParamList } from 'src/navigator/types' | ||
import useSelector from 'src/redux/useSelector' | ||
import { getSendFee } from 'src/send/saga' | ||
import { useDailyTransferLimitValidator } from 'src/send/utils' | ||
import DisconnectBanner from 'src/shared/DisconnectBanner' | ||
import { divideByWei } from 'src/utils/formatting' | ||
import Logger from 'src/utils/Logger' | ||
|
||
type Props = StackScreenProps<StackParamList, Screens.WithdrawCeloScreen> | ||
|
||
const RANDOM_ADDRESS = '0xDCE9762d6C1fe89FF4f3857832131Ca18eE15C66' | ||
|
||
function WithdrawCeloScreen({ navigation }: Props) { | ||
const [accountAddress, setAccountAddress] = useState('') | ||
const [celoInput, setCeloToTransfer] = useState('') | ||
const [feeEstimate, setFeeEstimate] = useState(new BigNumber(0)) | ||
|
||
const goldBalance = useSelector((state) => state.goldToken.balance) | ||
const goldBalanceNumber = new BigNumber(goldBalance || 0) | ||
|
@@ -51,6 +57,25 @@ function WithdrawCeloScreen({ navigation }: Props) { | |
CURRENCY_ENUM.GOLD | ||
) | ||
|
||
useEffect(() => { | ||
getSendFee( | ||
accountAddress, | ||
CURRENCY_ENUM.GOLD, | ||
{ | ||
recipientAddress: RANDOM_ADDRESS, | ||
amount: goldBalanceNumber, | ||
comment: '', | ||
}, | ||
false | ||
) | ||
.then((estimate: BigNumber) => { | ||
setFeeEstimate(divideByWei(estimate)) | ||
}) | ||
.catch((error) => { | ||
Logger.warn('WithdrawCeloScreen', `Error found while estimating gas fee: ${error}`) | ||
}) | ||
}, []) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Async functions should be avoided in general in
Now we have CalculateFee which uses I originally created it as a component because most components we had at the time, were class components. Added benefit is it provides loading/error states so we can display a spinner while it's calculating and it displays an error if it fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for this! I had run into this many times and always solved it in this inelegant way, the |
||
const onConfirm = async () => { | ||
if (isTransferLimitReached) { | ||
showLimitReachedBanner() | ||
|
@@ -63,6 +88,7 @@ function WithdrawCeloScreen({ navigation }: Props) { | |
navigation.navigate(Screens.WithdrawCeloReviewScreen, { | ||
amount: celoToTransfer, | ||
recipientAddress: accountAddress, | ||
feeEstimate, | ||
}) | ||
} | ||
|
||
|
@@ -86,6 +112,7 @@ function WithdrawCeloScreen({ navigation }: Props) { | |
inputStyle={styles.input} | ||
onCeloChanged={setCeloToTransfer} | ||
celo={celoInput} | ||
feeEstimate={feeEstimate} | ||
/> | ||
</KeyboardAwareScrollView> | ||
<Button | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have an address here yet to do the estimation, and estimating requires an address, so I used a random one. Could that cause any issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, we already do this here as well.