final bug fixes (#293)

* make jazzicon same as metamask

* fix token-token market rate

fix transactions bugs

add new contextual info to send

* fix .1 balance check on add liquidity

fix double click max balance bug
This commit is contained in:
Noah Zinsmeister 2019-05-16 16:54:15 -04:00 committed by GitHub
parent e27cd92cd2
commit 39312ab8c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 66 deletions

@ -40,8 +40,8 @@ export default function Web3Status() {
const { active, account } = useWeb3Context() const { active, account } = useWeb3Context()
const allTransactions = useAllTransactions() const allTransactions = useAllTransactions()
const pending = Object.keys(allTransactions).filter(t => allTransactions[t].completed === false) const pending = Object.keys(allTransactions).filter(hash => !allTransactions[hash].receipt)
const confirmed = Object.keys(allTransactions).filter(t => allTransactions[t].completed === true) const confirmed = Object.keys(allTransactions).filter(hash => allTransactions[hash].receipt)
const hasPendingTransactions = !!pending.length const hasPendingTransactions = !!pending.length
const hasConfirmedTransactions = !!confirmed.length const hasConfirmedTransactions = !!confirmed.length
@ -116,15 +116,7 @@ export default function Web3Status() {
return return
} else { } else {
el.innerHTML = '' el.innerHTML = ''
el.appendChild( el.appendChild(Jazzicon(16, parseInt(account.slice(2, 10), 16)))
Jazzicon(
16,
ethers.utils
.bigNumberify(account)
.mod(Number.MAX_SAFE_INTEGER)
.toNumber()
)
)
} }
}} }}
/> />

@ -87,7 +87,7 @@ export default function Provider({ children }) {
dispatch({ type: ADD, payload: { networkId, hash, response } }) dispatch({ type: ADD, payload: { networkId, hash, response } })
}, []) }, [])
const check = useCallback((networkId, hash, blockNumber) => { const check = useCallback((networkId, hash, blockNumber) => {
dispatch({ type: ADD, payload: { networkId, hash, blockNumber } }) dispatch({ type: CHECK, payload: { networkId, hash, blockNumber } })
}, []) }, [])
const finalize = useCallback((networkId, hash, receipt) => { const finalize = useCallback((networkId, hash, receipt) => {
dispatch({ type: FINALIZE, payload: { networkId, hash, receipt } }) dispatch({ type: FINALIZE, payload: { networkId, hash, receipt } })
@ -104,37 +104,37 @@ export function Updater() {
const globalBlockNumber = useBlockNumber() const globalBlockNumber = useBlockNumber()
const [state, { check, finalize }] = useTransactionsContext() const [state, { check, finalize }] = useTransactionsContext()
const allTransactions = safeAccess(state, [networkId]) || {}
useEffect(() => { useEffect(() => {
if ((networkId || networkId === 0) && library) { if ((networkId || networkId === 0) && library) {
const allTransactions = safeAccess(state, [networkId]) || {}
const allUncheckedTransactions = Object.keys(allTransactions).filter(
hash => !allTransactions[hash][RECEIPT] && allTransactions[hash][BLOCK_NUMBER_CHECKED] !== globalBlockNumber
)
let stale = false let stale = false
Object.keys(allUncheckedTransactions).forEach(hash => { Object.keys(allTransactions)
library .filter(
.getTransactionReceipt(hash) hash => !allTransactions[hash][RECEIPT] && allTransactions[hash][BLOCK_NUMBER_CHECKED] !== globalBlockNumber
.then(receipt => { )
if (!stale) { .forEach(hash => {
if (!receipt) { library
check(networkId, hash, globalBlockNumber) .getTransactionReceipt(hash)
} else { .then(receipt => {
finalize(networkId, hash, receipt) if (!stale) {
if (!receipt) {
check(networkId, hash, globalBlockNumber)
} else {
finalize(networkId, hash, receipt)
}
} }
} })
}) .catch(() => {
.catch(() => { check(networkId, hash, globalBlockNumber)
check(networkId, hash, globalBlockNumber) })
}) })
})
return () => { return () => {
stale = true stale = true
} }
} }
}, [networkId, library, state, globalBlockNumber, check, finalize]) }, [networkId, library, allTransactions, globalBlockNumber, check, finalize])
return null return null
} }
@ -155,7 +155,6 @@ export function useTransactionAdder() {
if (!hash) { if (!hash) {
throw Error('No transaction hash found.') throw Error('No transaction hash found.')
} }
add(networkId, hash, response) add(networkId, hash, response)
}, },
[networkId, add] [networkId, add]
@ -175,11 +174,15 @@ export function usePendingApproval(tokenAddress) {
return ( return (
Object.keys(allTransactions).filter(hash => { Object.keys(allTransactions).filter(hash => {
if ( if (allTransactions[hash][RECEIPT]) {
allTransactions[hash][RECEIPT] || return false
allTransactions[hash][RESPONSE].to !== tokenAddress || } else if (!allTransactions[hash][RESPONSE]) {
return false
} else if (allTransactions[hash][RESPONSE].to !== tokenAddress) {
return false
} else if (
allTransactions[hash][RESPONSE].data.substring(0, 10) !== allTransactions[hash][RESPONSE].data.substring(0, 10) !==
ethers.utils.id('approve(address,uint256)').substring(0, 10) ethers.utils.id('approve(address,uint256)').substring(0, 10)
) { ) {
return false return false
} else { } else {

@ -413,7 +413,7 @@ export default function AddLiquidity() {
// input validation // input validation
useEffect(() => { useEffect(() => {
if (inputValueParsed && inputBalance) { if (inputValueParsed && inputBalance) {
if (inputValueParsed.gt(inputBalance.sub(ethers.utils.parseEther('.1')))) { if (inputValueParsed.gt(inputBalance)) {
setInputError(t('insufficientBalance')) setInputError(t('insufficientBalance'))
} else { } else {
setInputError(null) setInputError(null)

@ -5,7 +5,7 @@ import { useWeb3Context } from 'web3-react'
import { ethers } from 'ethers' import { ethers } from 'ethers'
import CurrencyInputPanel from '../../components/CurrencyInputPanel' import CurrencyInputPanel from '../../components/CurrencyInputPanel'
import ContextualInfo from '../../components/ContextualInfo' import NewContextualInfo from '../../components/ContextualInfoNew'
import OversizedPanel from '../../components/OversizedPanel' import OversizedPanel from '../../components/OversizedPanel'
import AddressInputPanel from '../../components/AddressInputPanel' import AddressInputPanel from '../../components/AddressInputPanel'
import ArrowDownBlue from '../../assets/images/arrow-down-blue.svg' import ArrowDownBlue from '../../assets/images/arrow-down-blue.svg'
@ -177,13 +177,13 @@ function getMarketRate(
invert = false invert = false
) { ) {
if (swapType === ETH_TO_TOKEN) { if (swapType === ETH_TO_TOKEN) {
return getExchangeRate(outputReserveETH, inputDecimals, outputReserveToken, outputDecimals, invert) return getExchangeRate(outputReserveETH, 18, outputReserveToken, outputDecimals, invert)
} else if (swapType === TOKEN_TO_ETH) { } else if (swapType === TOKEN_TO_ETH) {
return getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, outputDecimals, invert) return getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, 18, invert)
} else if (swapType === TOKEN_TO_TOKEN) { } else if (swapType === TOKEN_TO_TOKEN) {
const factor = ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)) const factor = ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))
const firstRate = getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, outputDecimals) const firstRate = getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, 18)
const secondRate = getExchangeRate(outputReserveETH, inputDecimals, outputReserveToken, outputDecimals) const secondRate = getExchangeRate(outputReserveETH, 18, outputReserveToken, outputDecimals)
try { try {
return !!(firstRate && secondRate) ? firstRate.mul(secondRate).div(factor) : undefined return !!(firstRate && secondRate) ? firstRate.mul(secondRate).div(factor) : undefined
} catch {} } catch {}
@ -431,18 +431,16 @@ export default function Swap() {
) )
const percentSlippage = const percentSlippage =
exchangeRate && exchangeRate && marketRate
marketRate && ? exchangeRate
amountFormatter( .sub(marketRate)
exchangeRate .abs()
.sub(marketRate) .mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)))
.abs() .div(marketRate)
.mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))) .sub(ethers.utils.bigNumberify(3).mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(15))))
.div(marketRate) : undefined
.sub(ethers.utils.bigNumberify(3).mul(ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(15)))), const percentSlippageFormatted = percentSlippage && amountFormatter(percentSlippage, 16, 2)
16, const slippageWarning = percentSlippage && percentSlippage.gte(ethers.utils.parseEther('.1')) // 10%
2
)
const isValid = exchangeRate && inputError === null && independentError === null && recipientError === null const isValid = exchangeRate && inputError === null && independentError === null && recipientError === null
@ -485,7 +483,7 @@ export default function Swap() {
{t('orTransFail')} {t('orTransFail')}
</div> </div>
<div className="send__last-summary-text"> <div className="send__last-summary-text">
{t('priceChange')} {b(`${percentSlippage}%`)}. {t('priceChange')} {b(`${percentSlippageFormatted}%`)}.
</div> </div>
</div> </div>
) )
@ -501,7 +499,7 @@ export default function Swap() {
Math.min(4, independentDecimals) Math.min(4, independentDecimals)
)} ${outputSymbol}` )} ${outputSymbol}`
)}{' '} )}{' '}
{t('to')} {b(recipient.name || recipient.address)}. {t('to')} {b(recipient.address)}.
</div> </div>
<div className="send__last-summary-text"> <div className="send__last-summary-text">
{t('itWillCost')}{' '} {t('itWillCost')}{' '}
@ -515,7 +513,7 @@ export default function Swap() {
{t('orTransFail')} {t('orTransFail')}
</div> </div>
<div className="send__last-summary-text"> <div className="send__last-summary-text">
{t('priceChange')} {b(`${percentSlippage}%`)}. {t('priceChange')} {b(`${percentSlippageFormatted}%`)}.
</div> </div>
</div> </div>
) )
@ -543,10 +541,11 @@ export default function Swap() {
} }
return ( return (
<ContextualInfo <NewContextualInfo
openDetailsText={t('transactionDetails')} openDetailsText={t('transactionDetails')}
closeDetailsText={t('hideDetails')} closeDetailsText={t('hideDetails')}
contextualInfo={contextualInfo} contextualInfo={contextualInfo ? contextualInfo : slippageWarning ? t('slippageWarning') : ''}
allowExpand={!!(inputCurrency && outputCurrency && inputValueParsed && outputValueParsed && recipient.address)}
isError={isError} isError={isError}
renderTransactionDetails={renderTransactionDetails} renderTransactionDetails={renderTransactionDetails}
/> />

@ -119,10 +119,11 @@ function swapStateReducer(state, action) {
} }
case 'UPDATE_INDEPENDENT': { case 'UPDATE_INDEPENDENT': {
const { field, value } = action.payload const { field, value } = action.payload
const { dependentValue, independentValue } = state
return { return {
...state, ...state,
independentValue: value, independentValue: value,
dependentValue: '', dependentValue: value === independentValue ? dependentValue : '',
independentField: field independentField: field
} }
} }
@ -176,13 +177,13 @@ function getMarketRate(
invert = false invert = false
) { ) {
if (swapType === ETH_TO_TOKEN) { if (swapType === ETH_TO_TOKEN) {
return getExchangeRate(outputReserveETH, inputDecimals, outputReserveToken, outputDecimals, invert) return getExchangeRate(outputReserveETH, 18, outputReserveToken, outputDecimals, invert)
} else if (swapType === TOKEN_TO_ETH) { } else if (swapType === TOKEN_TO_ETH) {
return getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, outputDecimals, invert) return getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, 18, invert)
} else if (swapType === TOKEN_TO_TOKEN) { } else if (swapType === TOKEN_TO_TOKEN) {
const factor = ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18)) const factor = ethers.utils.bigNumberify(10).pow(ethers.utils.bigNumberify(18))
const firstRate = getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, outputDecimals) const firstRate = getExchangeRate(inputReserveToken, inputDecimals, inputReserveETH, 18)
const secondRate = getExchangeRate(outputReserveETH, inputDecimals, outputReserveToken, outputDecimals) const secondRate = getExchangeRate(outputReserveETH, 18, outputReserveToken, outputDecimals)
try { try {
return !!(firstRate && secondRate) ? firstRate.mul(secondRate).div(factor) : undefined return !!(firstRate && secondRate) ? firstRate.mul(secondRate).div(factor) : undefined
} catch {} } catch {}