Commit 8290cf56 authored by Ian Lapham's avatar Ian Lapham Committed by Noah Zinsmeister

update market rate calculation and input parsing for 0 decimal tokens (#601)

* update market rate calculation and input parsing for 0 decimal tokens

* remove style bump and revert tokens
parent 895f4cd8
...@@ -208,15 +208,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals, ...@@ -208,15 +208,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals,
if (invert) { if (invert) {
return inputValue return inputValue
.mul(factor) .mul(factor)
.div(outputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals))) .mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals))) .div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(outputValue)
} else { } else {
return outputValue return outputValue
.mul(factor) .mul(factor)
.div(inputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals))) .mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals))) .div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(inputValue)
} }
} }
} catch {} } catch {}
...@@ -560,7 +560,7 @@ export default function ExchangePage({ initialCurrency, sending = false, params ...@@ -560,7 +560,7 @@ export default function ExchangePage({ initialCurrency, sending = false, params
) )
const percentSlippage = const percentSlippage =
exchangeRate && marketRate exchangeRate && marketRate && !marketRate.isZero()
? exchangeRate ? exchangeRate
.sub(marketRate) .sub(marketRate)
.abs() .abs()
......
...@@ -178,15 +178,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals, ...@@ -178,15 +178,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals,
if (invert) { if (invert) {
return inputValue return inputValue
.mul(factor) .mul(factor)
.div(outputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals))) .mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals))) .div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(outputValue)
} else { } else {
return outputValue return outputValue
.mul(factor) .mul(factor)
.div(inputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals))) .mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals))) .div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(inputValue)
} }
} }
} catch {} } catch {}
...@@ -221,6 +221,7 @@ export default function AddLiquidity({ params }) { ...@@ -221,6 +221,7 @@ export default function AddLiquidity({ params }) {
const [outputValueParsed, setOutputValueParsed] = useState() const [outputValueParsed, setOutputValueParsed] = useState()
const [inputError, setInputError] = useState() const [inputError, setInputError] = useState()
const [outputError, setOutputError] = useState() const [outputError, setOutputError] = useState()
const [zeroDecimalError, setZeroDecimalError] = useState()
const [brokenTokenWarning, setBrokenTokenWarning] = useState() const [brokenTokenWarning, setBrokenTokenWarning] = useState()
...@@ -353,6 +354,8 @@ export default function AddLiquidity({ params }) { ...@@ -353,6 +354,8 @@ export default function AddLiquidity({ params }) {
if (brokenTokenWarning) { if (brokenTokenWarning) {
contextualInfo = t('brokenToken') contextualInfo = t('brokenToken')
isError = true isError = true
} else if (zeroDecimalError) {
contextualInfo = zeroDecimalError
} else if (inputError || outputError) { } else if (inputError || outputError) {
contextualInfo = inputError || outputError contextualInfo = inputError || outputError
isError = true isError = true
...@@ -433,14 +436,18 @@ export default function AddLiquidity({ params }) { ...@@ -433,14 +436,18 @@ export default function AddLiquidity({ params }) {
useEffect(() => { useEffect(() => {
if (isNewExchange) { if (isNewExchange) {
setZeroDecimalError()
if (inputValue) { if (inputValue) {
const parsedInputValue = ethers.utils.parseUnits(inputValue, 18) const parsedInputValue = ethers.utils.parseUnits(inputValue, 18)
setInputValueParsed(parsedInputValue) setInputValueParsed(parsedInputValue)
} }
if (outputValue) { if (outputValue) {
const parsedOutputValue = ethers.utils.parseUnits(outputValue, decimals) try {
setOutputValueParsed(parsedOutputValue) const parsedOutputValue = ethers.utils.parseUnits(outputValue, decimals)
setOutputValueParsed(parsedOutputValue)
} catch {
setZeroDecimalError('Invalid input. For 0 decimal tokens only supply whole number token amounts.')
}
} }
} }
}, [decimals, inputValue, isNewExchange, outputValue]) }, [decimals, inputValue, isNewExchange, outputValue])
...@@ -568,7 +575,8 @@ export default function AddLiquidity({ params }) { ...@@ -568,7 +575,8 @@ export default function AddLiquidity({ params }) {
}, [outputValueParsed, allowance, t]) }, [outputValueParsed, allowance, t])
const isActive = active && account const isActive = active && account
const isValid = (inputError === null || outputError === null) && !showUnlock && !brokenTokenWarning const isValid =
(inputError === null || outputError === null) && !zeroDecimalError && !showUnlock && !brokenTokenWarning
return ( return (
<> <>
......
...@@ -110,15 +110,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals, ...@@ -110,15 +110,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals,
if (invert) { if (invert) {
return inputValue return inputValue
.mul(factor) .mul(factor)
.div(outputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals))) .mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals))) .div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(outputValue)
} else { } else {
return outputValue return outputValue
.mul(factor) .mul(factor)
.div(inputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals))) .mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals))) .div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(inputValue)
} }
} }
} catch {} } catch {}
...@@ -222,22 +222,25 @@ export default function RemoveLiquidity({ params }) { ...@@ -222,22 +222,25 @@ export default function RemoveLiquidity({ params }) {
ownershipPercentage && ownershipPercentage &&
exchangeTokenBalance.mul(ownershipPercentage).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))) exchangeTokenBalance.mul(ownershipPercentage).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
const ETHPer = const ETHPer = exchangeETHBalance
exchangeETHBalance && totalPoolTokens && !totalPoolTokens.isZero() ? exchangeETHBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
? exchangeETHBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))).div(totalPoolTokens) : undefined
: undefined const tokenPer = exchangeTokenBalance
const tokenPer = ? exchangeTokenBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
exchangeTokenBalance && totalPoolTokens && !totalPoolTokens.isZero() : undefined
? exchangeTokenBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))).div(totalPoolTokens)
: undefined
const ethWithdrawn = const ethWithdrawn =
ETHPer && valueParsed ETHPer && valueParsed && totalPoolTokens && !totalPoolTokens.isZero()
? ETHPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))) ? ETHPer.mul(valueParsed)
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
.div(totalPoolTokens)
: undefined : undefined
const tokenWithdrawn = const tokenWithdrawn =
tokenPer && valueParsed tokenPer && valueParsed && totalPoolTokens && !totalPoolTokens.isZero()
? tokenPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))) ? tokenPer
.mul(valueParsed)
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
.div(totalPoolTokens)
: undefined : undefined
const ethWithdrawnMin = ethWithdrawn ? calculateSlippageBounds(ethWithdrawn).minimum : undefined const ethWithdrawnMin = ethWithdrawn ? calculateSlippageBounds(ethWithdrawn).minimum : undefined
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment