Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show All 30 Lines | const { | ||||
getSlpBalancesAndUtxos, | getSlpBalancesAndUtxos, | ||||
getTxHistory, | getTxHistory, | ||||
getTxData, | getTxData, | ||||
addTokenTxData, | addTokenTxData, | ||||
} = useBCH(); | } = useBCH(); | ||||
const [loading, setLoading] = useState(true); | const [loading, setLoading] = useState(true); | ||||
const [apiIndex, setApiIndex] = useState(0); | const [apiIndex, setApiIndex] = useState(0); | ||||
const [BCH, setBCH] = useState(getBCH(apiIndex)); | const [BCH, setBCH] = useState(getBCH(apiIndex)); | ||||
const [utxos, setUtxos] = useState(null); | const { balances, tokens, utxos } = isValidStoredWallet(wallet) | ||||
const { balances, tokens } = isValidStoredWallet(wallet) | |||||
? wallet.state | ? wallet.state | ||||
: { | : { | ||||
balances: {}, | balances: {}, | ||||
tokens: [], | tokens: [], | ||||
utxos: null, | |||||
}; | }; | ||||
const previousBalances = usePrevious(balances); | const previousBalances = usePrevious(balances); | ||||
const previousTokens = usePrevious(tokens); | const previousTokens = usePrevious(tokens); | ||||
const previousWallet = usePrevious(wallet); | const previousWallet = usePrevious(wallet); | ||||
const previousUtxos = usePrevious(utxos); | const previousUtxos = usePrevious(utxos); | ||||
// If you catch API errors, call this function | // If you catch API errors, call this function | ||||
const tryNextAPI = () => { | const tryNextAPI = () => { | ||||
▲ Show 20 Lines • Show All 150 Lines • ▼ Show 20 Lines | const update = async ({ wallet }) => { | ||||
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); | |||||
// 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( | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
previousUtxos, | previousUtxos, | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 912 Lines • Show Last 20 Lines |