Skip to content
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

Swaps amount view: use max for tokens #2052

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions app/components/UI/Swaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ import Engine from '../../../core/Engine';
import handleInput from '../../Base/Keypad/rules/native';
import useModalHandler from '../../Base/hooks/useModalHandler';
import Device from '../../../util/Device';
import {
fromTokenMinimalUnit,
renderFromTokenMinimalUnit,
renderFromWei,
toTokenMinimalUnit
} from '../../../util/number';
import { setQuotesNavigationsParams } from './utils';
import { renderFromTokenMinimalUnit, renderFromWei, toTokenMinimalUnit } from '../../../util/number';

import { strings } from '../../../../locales/i18n';
import { colors } from '../../../styles/common';

Expand Down Expand Up @@ -56,6 +62,9 @@ const styles = StyleSheet.create({
amountInvalid: {
color: colors.red
},
linkText: {
color: colors.blue
},
horizontalRuleContainer: {
flexDirection: 'row',
paddingHorizontal: 30,
Expand Down Expand Up @@ -160,6 +169,13 @@ function SwapsAmountView({ tokens, accounts, selectedAddress, balances }) {
return tokenAddress in balances ? renderFromTokenMinimalUnit(balances[tokenAddress], sourceToken.decimals) : 0;
}, [accounts, balances, selectedAddress, sourceToken]);

const hasBalance = useMemo(() => {
if (!balance || !sourceToken || sourceToken.symbol === 'ETH') {
return false;
}

return new BigNumber(balance).gt(0);
}, [balance, sourceToken]);
const hasEnoughBalance = useMemo(() => amountBigNumber.lte(new BigNumber(balance)), [amountBigNumber, balance]);

/* Navigation handler */
Expand Down Expand Up @@ -217,6 +233,10 @@ function SwapsAmountView({ tokens, accounts, selectedAddress, balances }) {
[toggleDestinationModal]
);

const handleUseMax = useCallback(() => {
setAmount(fromTokenMinimalUnit(balances[toChecksumAddress(sourceToken.address)], sourceToken.decimals));
wachunei marked this conversation as resolved.
Show resolved Hide resolved
}, [balances, sourceToken.address, sourceToken.decimals]);

return (
<ScreenView contentContainerStyle={styles.screen} keyboardShouldPersistTaps="handled">
<View style={styles.content}>
Expand Down Expand Up @@ -256,10 +276,18 @@ function SwapsAmountView({ tokens, accounts, selectedAddress, balances }) {
})}
</Text>
) : (
<Text>
{sourceToken && balance !== null
? strings('swaps.available_to_swap', { asset: `${balance} ${sourceToken.symbol}` })
: ''}
<Text centered>
{sourceToken &&
balance !== null &&
strings('swaps.available_to_swap', {
asset: `${balance} ${sourceToken.symbol}`
})}
{hasBalance && (
<Text style={styles.linkText} onPress={handleUseMax}>
{' '}
{strings('swaps.use_max')}
</Text>
)}
</Text>
)}
</View>
Expand Down
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,8 @@
"select_a_token": "Select a token",
"search_token": "Search for a token",
"no_tokens_result": "No tokens match “{{searchString}}”",
"available_to_swap": "{{asset}} available to swap",
"available_to_swap": "{{asset}} available to swap.",
"use_max": "Use max",
"not_enough": "Not enough {{symbol}} to complete this swap",
"max_slippage": "Max slippage {{slippage}}",
"allows_up_to_decimals": "{{symbol}} allows up to {{decimals}} decimals",
Expand Down
3 changes: 2 additions & 1 deletion locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,8 @@
"select_a_token": "Selecciona un token",
"search_token": "Buscar un token",
"no_tokens_result": "No se encontraron tokens para “{{searchString}}”",
"available_to_swap": "{{asset}} disponible para convertir",
"available_to_swap": "{{asset}} disponible para convertir.",
"use_max": "Usar máximo",
"not_enough": "Insuficiente {{symbol}} para esta conversión",
"max_slippage": "Deslizamiento máx. {{slippage}}",
"allows_up_to_decimals": "{{symbol}} soporta hasta {{decimals}} decimales",
Expand Down