Skip to content

Commit

Permalink
hot fixes for decimal and liquidity bugs (Uniswap#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister authored May 27, 2019
1 parent eb09660 commit 8649ddc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
23 changes: 13 additions & 10 deletions src/pages/Pool/AddLiquidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ export default function AddLiquidity() {

// parse input value
useEffect(() => {
if (isNewExchange === false && inputValue && marketRate && lastEditedField === INPUT) {
if (isNewExchange === false && inputValue && marketRate && lastEditedField === INPUT && decimals) {
try {
const parsedValue = ethers.utils.parseUnits(inputValue, 18)

Expand All @@ -353,10 +353,12 @@ export default function AddLiquidity() {
const currencyAmount = marketRate
.mul(parsedValue)
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18 - decimals)))

setOutputValueParsed(currencyAmount)
dispatchAddLiquidityState({
type: 'UPDATE_DEPENDENT_VALUE',
payload: { field: OUTPUT, value: amountFormatter(currencyAmount, 18, 4, false) }
payload: { field: OUTPUT, value: amountFormatter(currencyAmount, decimals, 4, false) }
})

return () => {
Expand All @@ -372,13 +374,13 @@ export default function AddLiquidity() {
setOutputError(t('inputNotValid'))
}
}
}, [inputValue, isNewExchange, lastEditedField, marketRate, t])
}, [inputValue, isNewExchange, lastEditedField, marketRate, decimals, t])

// parse output value
useEffect(() => {
if (isNewExchange === false && outputValue && marketRateInverted && lastEditedField === OUTPUT) {
if (isNewExchange === false && outputValue && marketRateInverted && lastEditedField === OUTPUT && decimals) {
try {
const parsedValue = ethers.utils.parseUnits(outputValue, 18)
const parsedValue = ethers.utils.parseUnits(outputValue, decimals)

if (parsedValue.lte(ethers.constants.Zero) || parsedValue.gte(ethers.constants.MaxUint256)) {
throw Error()
Expand All @@ -388,7 +390,8 @@ export default function AddLiquidity() {

const currencyAmount = marketRateInverted
.mul(parsedValue)
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
.div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(decimals)))

setInputValueParsed(currencyAmount)
dispatchAddLiquidityState({
type: 'UPDATE_DEPENDENT_VALUE',
Expand All @@ -408,7 +411,7 @@ export default function AddLiquidity() {
setInputError(t('inputNotValid'))
}
}
}, [outputValue, isNewExchange, lastEditedField, marketRateInverted, t])
}, [outputValue, isNewExchange, lastEditedField, marketRateInverted, decimals, t])

// input validation
useEffect(() => {
Expand All @@ -420,14 +423,14 @@ export default function AddLiquidity() {
}
}

if (outputValueParsed && outputBalance) {
if (outputValueParsed.gt(outputBalance)) {
if (outputValueMax && outputBalance) {
if (outputValueMax.gt(outputBalance)) {
setOutputError(t('insufficientBalance'))
} else {
setOutputError(null)
}
}
}, [inputValueParsed, inputBalance, outputValueParsed, outputBalance, t])
}, [inputValueParsed, inputBalance, outputValueMax, outputBalance, t])

const allowance = useAddressAllowance(account, outputCurrency, exchangeAddress)
const [showUnlock, setShowUnlock] = useState(false)
Expand Down
20 changes: 10 additions & 10 deletions src/pages/Pool/RemoveLiquidity.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ export default function RemoveLiquidity() {
: undefined

const ethWithdrawn =
ETHPer &&
valueParsed &&
ETHPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
ETHPer && valueParsed
? ETHPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
: undefined
const tokenWithdrawn =
tokenPer &&
valueParsed &&
tokenPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
tokenPer && valueParsed
? tokenPer.mul(valueParsed).div(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
: undefined

const ethWithdrawnMin = ethWithdrawn ? calculateSlippageBounds(ethWithdrawn).minimum : undefined
const tokenWithdrawnMin = tokenWithdrawn ? calculateSlippageBounds(tokenWithdrawn).minimum : undefined
Expand Down Expand Up @@ -284,20 +284,20 @@ export default function RemoveLiquidity() {
</OversizedPanel>
<CurrencyInputPanel
title={t('output')}
description={ethWithdrawn && tokenWithdrawn ? `(${t('estimated')})` : ''}
description={ethWithdrawnMin && tokenWithdrawnMin ? `(${t('estimated')})` : ''}
key="remove-liquidity-input"
renderInput={() =>
ethWithdrawn && tokenWithdrawn ? (
ethWithdrawnMin && tokenWithdrawnMin ? (
<div className="remove-liquidity__output">
<div className="remove-liquidity__output-text">{`${amountFormatter(
ethWithdrawn,
ethWithdrawnMin,
18,
4,
false
)} ETH`}</div>
<div className="remove-liquidity__output-plus"> + </div>
<div className="remove-liquidity__output-text">{`${amountFormatter(
tokenWithdrawn,
tokenWithdrawnMin,
decimals,
Math.min(4, decimals)
)} ${symbol}`}</div>
Expand Down

0 comments on commit 8649ddc

Please sign in to comment.