Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
/* eslint-disable react-hooks/exhaustive-deps */ | /* eslint-disable react-hooks/exhaustive-deps */ | ||||
import React, { useState, useEffect } from 'react'; | import React, { useState, useEffect } from 'react'; | ||||
import Paragraph from 'antd/lib/typography/Paragraph'; | import Paragraph from 'antd/lib/typography/Paragraph'; | ||||
import { notification } from 'antd'; | import { notification } from 'antd'; | ||||
import useAsyncTimeout from '@hooks/useAsyncTimeout'; | import useAsyncTimeout from '@hooks/useAsyncTimeout'; | ||||
import usePrevious from '@hooks/usePrevious'; | import usePrevious from '@hooks/usePrevious'; | ||||
import useBCH from '@hooks/useBCH'; | import useBCH from '@hooks/useBCH'; | ||||
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import { | import { | ||||
fromSmallestDenomination, | fromSmallestDenomination, | ||||
loadStoredWallet, | loadStoredWallet, | ||||
isValidStoredWallet, | isValidStoredWallet, | ||||
} from '@utils/cashMethods'; | } from '@utils/cashMethods'; | ||||
import localforage from 'localforage'; | import localforage from 'localforage'; | ||||
import { currency } from '@components/Common/Ticker'; | import { currency } from '@components/Common/Ticker'; | ||||
import _ from 'lodash'; | import isEmpty from 'lodash.isempty'; | ||||
import isEqual from 'lodash.isequal'; | |||||
const useWallet = () => { | const useWallet = () => { | ||||
const [wallet, setWallet] = useState(false); | const [wallet, setWallet] = useState(false); | ||||
const [fiatPrice, setFiatPrice] = useState(null); | const [fiatPrice, setFiatPrice] = useState(null); | ||||
const [ws, setWs] = useState(null); | const [ws, setWs] = useState(null); | ||||
const [apiError, setApiError] = useState(false); | const [apiError, setApiError] = useState(false); | ||||
const [walletState, setWalletState] = useState({ | const [walletState, setWalletState] = useState({ | ||||
balances: {}, | balances: {}, | ||||
▲ Show 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | const haveUtxosChanged = (wallet, utxos, previousUtxos) => { | ||||
let previousUtxosToCompare; | let previousUtxosToCompare; | ||||
if (wallet.state && wallet.state.utxos) { | if (wallet.state && wallet.state.utxos) { | ||||
previousUtxosToCompare = wallet.state.utxos; | previousUtxosToCompare = wallet.state.utxos; | ||||
} else { | } else { | ||||
previousUtxosToCompare = previousUtxos; | previousUtxosToCompare = previousUtxos; | ||||
} | } | ||||
// Compare utxo sets | // Compare utxo sets | ||||
const utxoArraysUnchanged = _.isEqual(utxos, previousUtxosToCompare); | const utxoArraysUnchanged = isEqual(utxos, previousUtxosToCompare); | ||||
// If utxos are not the same as previousUtxos | // If utxos are not the same as previousUtxos | ||||
if (utxoArraysUnchanged) { | if (utxoArraysUnchanged) { | ||||
// then utxos have not changed | // then utxos have not changed | ||||
return false; | return false; | ||||
// otherwise, | // otherwise, | ||||
} else { | } else { | ||||
// utxos have changed | // utxos have changed | ||||
Show All 12 Lines | const update = async ({ wallet, setWalletState }) => { | ||||
wallet.Path245.cashAddress, | wallet.Path245.cashAddress, | ||||
wallet.Path145.cashAddress, | wallet.Path145.cashAddress, | ||||
wallet.Path1899.cashAddress, | wallet.Path1899.cashAddress, | ||||
]; | ]; | ||||
const utxos = await getUtxos(BCH, cashAddresses); | const utxos = await getUtxos(BCH, cashAddresses); | ||||
// If an error is returned or utxos from only 1 address are returned | // If an error is returned or utxos from only 1 address are returned | ||||
if (!utxos || _.isEmpty(utxos) || utxos.error || utxos.length < 2) { | if (!utxos || isEmpty(utxos) || utxos.error || utxos.length < 2) { | ||||
// Throw error here to prevent more attempted api calls | // Throw error here to prevent more attempted api calls | ||||
// as you are likely already at rate limits | // as you are likely already at rate limits | ||||
throw new Error('Error fetching utxos'); | throw new Error('Error fetching utxos'); | ||||
} | } | ||||
setUtxos(utxos); | setUtxos(utxos); | ||||
// Need to call with wallet as a parameter rather than trusting it is in state, otherwise can sometimes get wallet=false from haveUtxosChanged | // Need to call with wallet as a parameter rather than trusting it is in state, otherwise can sometimes get wallet=false from haveUtxosChanged | ||||
const utxosHaveChanged = haveUtxosChanged( | const utxosHaveChanged = haveUtxosChanged( | ||||
▲ Show 20 Lines • Show All 1,110 Lines • Show Last 20 Lines |