Skip to content

Commit

Permalink
Merge pull request #185 from eoscostarica/feature/both-input-fields
Browse files Browse the repository at this point in the history
feat: allow type the amount to receive
  • Loading branch information
xavier506 authored Sep 10, 2020
2 parents e842666 + 0f8ae91 commit 7ed0dc6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
42 changes: 39 additions & 3 deletions src/routes/Evodex/BackLayer/Exchange/ExchangeBackLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
const [youGive, setYouGive] = useState({ walletBalance: {} })
const [loading, setLoading] = useState(false)
const [helperTextReceive, setHelperTextReceive] = useState('')
const [userChangeInput, setUserChangeInput] = useState('')
const [, setLastInterval] = useState('')

const getTokenValue = (token) => {
if (!pair) {
Expand Down Expand Up @@ -207,6 +209,7 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
...prevState,
...value
}))
setUserChangeInput(key)
}

const handleOnSwitchValues = () => {
Expand Down Expand Up @@ -298,6 +301,10 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
])

useEffect(() => {
if (userChangeInput !== 'youGive') {
return
}

if (!pair || !youGive.inputValue) {
setYouReceive((prevState) => ({
...prevState,
Expand All @@ -309,13 +316,43 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
}

const assets = evolutiondex.getExchangeAssets(youGive.inputValue, pair)

setAssets(assets)
setYouReceive((prevState) => ({
...prevState,
inputValue: assets.assetToReceive.toString().split(' ')[0]
}))
}, [pair, youGive.inputValue])
}, [userChangeInput, pair, youGive.inputValue])

useEffect(() => {
if (userChangeInput !== 'youReceive') {
return
}

setLastInterval((lastValue) => {
clearInterval(lastValue)

return null
})

if (!pair || !youReceive.inputValue) {
setYouGive((prevState) => ({
...prevState,
inputValue: ''
}))
setAssets(null)

return
}

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

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

useEffect(() => {
if (!exchangeState.currentPair) {
Expand Down Expand Up @@ -424,7 +461,6 @@ const ExchangeBackLayer = ({ onReload, ual, isLightMode, showMessage }) => {
options={options.youReceive}
onChange={handleOnChange('youReceive')}
value={youReceive}
inputDisabled
helperText={
pair && (
<Typography
Expand Down
30 changes: 29 additions & 1 deletion src/utils/evolutiondex.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,35 @@ const getExchangeAssets = (amount, pair) => {
pair.from.asset.amount.plus(assetToGive.amount),
pair.fee
).abs()

assetToReceive.set_amount(computeForwardAmount)

return {
assetToGive,
assetToReceive,
price: amountToAsset(
(
parseFloat(assetToReceive.toString().split(' ')[0]) /
parseFloat(assetToGive.toString().split(' ')[0])
).toFixed(assetToReceive.symbol.precision()),
assetToReceive
).toString()
}
}
const getExchangeAssetsFromToken2 = (amount, pair) => {
const assetToGive = numberToAsset(0, pair.from.asset.symbol)
const assetToReceive = amountToAsset(amount, pair.to.asset)
const amountToReceive = assetToReceive.amount
amountToReceive.plus(
amountToReceive.multiply(pair.fee).plus(9999).divide(10000)
)
const computeForwardAmount = computeForward(
amountToReceive,
pair.from.asset.amount,
pair.to.asset.amount.minus(amountToReceive),
0
).abs()
assetToGive.set_amount(computeForwardAmount)

return {
assetToGive,
assetToReceive,
Expand Down Expand Up @@ -530,6 +557,7 @@ export const evolutiondex = {
getTokensFor,
getPair,
getExchangeAssets,
getExchangeAssetsFromToken2,
exchange,
getUserPools,
getAddLiquidityAssets,
Expand Down

0 comments on commit 7ed0dc6

Please sign in to comment.