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

feat: add warning for exchange with amount higher than user balance #188

Merged
merged 2 commits into from
Sep 12, 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
10 changes: 5 additions & 5 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"trailingComma": "none",
"singleQuote": true,
"semi": false,
"tabWidth": 2
}
"trailingComma": "none",
"singleQuote": true,
"semi": false,
"tabWidth": 2
}
28 changes: 22 additions & 6 deletions src/components/InputTextAndSelect/InputTextAndSelect.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React, { useState, useEffect, useRef } from 'react'
import clsx from 'clsx'

import PropTypes from 'prop-types'
import { useTranslation } from 'react-i18next'
import { makeStyles } from '@material-ui/styles'
Expand Down Expand Up @@ -76,6 +78,12 @@ const useStyles = makeStyles((theme) => ({
borderBottom: '2px solid rgba(255,255,255,0.38)',
padding: '5px 14px 0 14px'
},
rootContainerError: {
borderBottom: `2px solid ${theme.palette.error.main}`,
'& p': {
color: theme.palette.error.main
}
},
inputText: {
fontSize: 16.2,
lineHeight: 1.48,
Expand Down Expand Up @@ -123,7 +131,8 @@ const InputTextAndSelect = ({
value,
inputDisabled,
useHelperTextAsNode,
placeholder
placeholder,
hasError
}) => {
const classes = useStyles()
const { t } = useTranslation('translations')
Expand All @@ -150,7 +159,13 @@ const InputTextAndSelect = ({

return (
<Box className={classes.boxInputContainer}>
<form autoComplete="off" className={classes.rootContainer}>
<form
autoComplete="off"
className={clsx({
[classes.rootContainer]: true,
[classes.rootContainerError]: hasError
})}
>
<Box className={classes.inputWrapper}>
<Typography className={classes.labelText} variant="body1">
{label}
Expand Down Expand Up @@ -198,8 +213,8 @@ const InputTextAndSelect = ({
{useHelperTextAsNode ? (
helperText
) : (
<Typography className={classes.helperText}>{helperText}</Typography>
)}
<Typography className={classes.helperText}>{helperText}</Typography>
)}
</Box>
)
}
Expand All @@ -214,14 +229,15 @@ InputTextAndSelect.propTypes = {
value: PropTypes.any,
inputDisabled: PropTypes.bool,
useHelperTextAsNode: PropTypes.bool,
placeholder: PropTypes.string
placeholder: PropTypes.string,
hasError: PropTypes.bool
}

InputTextAndSelect.defaultProps = {
label: '',
selected: null,
helperText: null,
onChange: () => { },
onChange: () => {},
options: [],
useHelperTextAsNode: false,
placeholder: null
Expand Down
48 changes: 39 additions & 9 deletions src/routes/Evodex/BackLayer/Exchange/ExchangeBackLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
const [loading, setLoading] = useState(false)
const [helperTextReceive, setHelperTextReceive] = useState('')
const [userChangeInput, setUserChangeInput] = useState('')
const [userBalance, setUserBalance] = useState()
const [, setLastInterval] = useState('')

const getTokenValue = (token) => {
Expand Down Expand Up @@ -344,14 +345,21 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
return
}

const assets = evolutiondex.getExchangeAssetsFromToken2(youReceive.inputValue, pair)
const assets = evolutiondex.getExchangeAssetsFromToken2(
youReceive.inputValue,
pair
)

setAssets(assets)
setYouGive((prevState) => ({
...prevState,
inputValue: assets.assetToGive.toString().split(' ')[0]
}))
setLastInterval(setTimeout(() => { setUserChangeInput('youGive') }, 2000))
setLastInterval(
setTimeout(() => {
setUserChangeInput('youGive')
}, 2000)
)
}, [userChangeInput, pair, youReceive.inputValue])

useEffect(() => {
Expand Down Expand Up @@ -383,8 +391,13 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
}, [showMessage, exchangeState.currentPair, ual.activeUser])

useEffect(() => {
if (!pair) {
return
}

const getCurrencyBalance = async () => {
let walletPool = {}
let userbalance = null

if (ual.activeUser) {
const pool1 = await ual.activeUser.rpc.get_currency_balance(
Expand All @@ -407,17 +420,25 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
? pool2[0]
: `0 ${pair.pool2.asset.symbol.code().toString()}`
}

userbalance = {
[pair.pool1.asset.symbol.code().toString()]: {
token: pair.pool1.asset.symbol.code().toString(),
amount: pool1.length ? parseFloat(pool1[0].split(' ')[0]) : 0
},
[pair.pool2.asset.symbol.code().toString()]: {
token: pair.pool2.asset.symbol.code().toString(),
amount: pool2.length ? parseFloat(pool2[0].split(' ')[0]) : 0
}
}
}

setYouGive((prevValue) => ({
...prevValue,
walletBalance: walletPool
}))
getTokenValue(youReceive.selectValue)
}

if (!pair) {
return
setUserBalance(userbalance)
}

getCurrencyBalance()
Expand All @@ -440,17 +461,26 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
onChange={handleOnChange('youGive')}
value={youGive}
helperText={
pair &&
ual.activeUser && (
pair && (
<Typography
variant="body1"
className={clsx([classes.textInfo, classes.helperText])}
>
{`Your Wallet: ${youGive.walletBalance[youGive.selectValue]}`}
{ual.activeUser
? `Your Wallet: ${
youGive.walletBalance[youGive.selectValue] || ''
}`
: `Your Wallet: 0 ${youGive.selectValue}`}
</Typography>
)
}
useHelperTextAsNode
hasError={
userBalance
? (userBalance[youGive.selectValue]?.amount || 0) <
parseFloat(youGive.inputValue || 0)
: false
}
/>
<IconButton aria-label="switch" onClick={handleOnSwitchValues}>
{isDesktop ? <SwapHorizIcon /> : <ImportExportIcon />}
Expand Down