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,
if (invert) {
return inputValue
.mul(factor)
.div(outputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(outputValue)
} else {
return outputValue
.mul(factor)
.div(inputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(inputValue)
}
}
} catch {}
......@@ -560,7 +560,7 @@ export default function ExchangePage({ initialCurrency, sending = false, params
)
const percentSlippage =
exchangeRate && marketRate
exchangeRate && marketRate && !marketRate.isZero()
? exchangeRate
.sub(marketRate)
.abs()
......
......@@ -178,15 +178,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals,
if (invert) {
return inputValue
.mul(factor)
.div(outputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(outputValue)
} else {
return outputValue
.mul(factor)
.div(inputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(inputValue)
}
}
} catch {}
......@@ -221,6 +221,7 @@ export default function AddLiquidity({ params }) {
const [outputValueParsed, setOutputValueParsed] = useState()
const [inputError, setInputError] = useState()
const [outputError, setOutputError] = useState()
const [zeroDecimalError, setZeroDecimalError] = useState()
const [brokenTokenWarning, setBrokenTokenWarning] = useState()
......@@ -353,6 +354,8 @@ export default function AddLiquidity({ params }) {
if (brokenTokenWarning) {
contextualInfo = t('brokenToken')
isError = true
} else if (zeroDecimalError) {
contextualInfo = zeroDecimalError
} else if (inputError || outputError) {
contextualInfo = inputError || outputError
isError = true
......@@ -433,14 +436,18 @@ export default function AddLiquidity({ params }) {
useEffect(() => {
if (isNewExchange) {
setZeroDecimalError()
if (inputValue) {
const parsedInputValue = ethers.utils.parseUnits(inputValue, 18)
setInputValueParsed(parsedInputValue)
}
if (outputValue) {
try {
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])
......@@ -568,7 +575,8 @@ export default function AddLiquidity({ params }) {
}, [outputValueParsed, allowance, t])
const isActive = active && account
const isValid = (inputError === null || outputError === null) && !showUnlock && !brokenTokenWarning
const isValid =
(inputError === null || outputError === null) && !zeroDecimalError && !showUnlock && !brokenTokenWarning
return (
<>
......
......@@ -110,15 +110,15 @@ function getExchangeRate(inputValue, inputDecimals, outputValue, outputDecimals,
if (invert) {
return inputValue
.mul(factor)
.div(outputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(outputValue)
} else {
return outputValue
.mul(factor)
.div(inputValue)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(inputDecimals)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(outputDecimals)))
.div(inputValue)
}
}
} catch {}
......@@ -222,22 +222,25 @@ export default function RemoveLiquidity({ params }) {
ownershipPercentage &&
exchangeTokenBalance.mul(ownershipPercentage).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
const ETHPer =
exchangeETHBalance && totalPoolTokens && !totalPoolTokens.isZero()
? exchangeETHBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))).div(totalPoolTokens)
const ETHPer = exchangeETHBalance
? exchangeETHBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
: undefined
const tokenPer =
exchangeTokenBalance && totalPoolTokens && !totalPoolTokens.isZero()
? exchangeTokenBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))).div(totalPoolTokens)
const tokenPer = exchangeTokenBalance
? exchangeTokenBalance.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
: undefined
const ethWithdrawn =
ETHPer && valueParsed
? ETHPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
ETHPer && valueParsed && totalPoolTokens && !totalPoolTokens.isZero()
? ETHPer.mul(valueParsed)
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
.div(totalPoolTokens)
: undefined
const tokenWithdrawn =
tokenPer && valueParsed
? tokenPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
tokenPer && valueParsed && totalPoolTokens && !totalPoolTokens.isZero()
? tokenPer
.mul(valueParsed)
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
.div(totalPoolTokens)
: 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