Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show All 15 Lines | 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: {}, | ||||
tokens: [], | tokens: [], | ||||
slpBalancesAndUtxos: [], | slpBalancesAndUtxos: [], | ||||
txHistory: [], | parsedTxHistory: [], | ||||
}); | }); | ||||
const { | const { | ||||
getBCH, | getBCH, | ||||
getUtxos, | getUtxos, | ||||
getHydratedUtxoDetails, | getHydratedUtxoDetails, | ||||
getSlpBalancesAndUtxos, | getSlpBalancesAndUtxos, | ||||
getTxHistory, | getTxHistory, | ||||
getTxData, | |||||
} = 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 [utxos, setUtxos] = useState(null); | ||||
const { balances, tokens, slpBalancesAndUtxos, txHistory } = walletState; | const { | ||||
balances, | |||||
tokens, | |||||
slpBalancesAndUtxos, | |||||
parsedTxHistory, | |||||
} = walletState; | |||||
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 = () => { | ||||
let currentApiIndex = apiIndex; | let currentApiIndex = apiIndex; | ||||
▲ Show 20 Lines • Show All 141 Lines • ▼ Show 20 Lines | const update = async ({ wallet, setWalletState }) => { | ||||
BCH, | BCH, | ||||
utxos, | utxos, | ||||
); | ); | ||||
const slpBalancesAndUtxos = await getSlpBalancesAndUtxos( | const slpBalancesAndUtxos = await getSlpBalancesAndUtxos( | ||||
hydratedUtxoDetails, | hydratedUtxoDetails, | ||||
); | ); | ||||
const txHistory = await getTxHistory(BCH, cashAddresses); | const txHistory = await getTxHistory(BCH, cashAddresses); | ||||
const parsedTxHistory = await getTxData(BCH, txHistory); | |||||
console.log(`parsedTxHistory`, parsedTxHistory); | |||||
console.log(`slpBalancesAndUtxos`, slpBalancesAndUtxos); | console.log(`slpBalancesAndUtxos`, slpBalancesAndUtxos); | ||||
if (typeof slpBalancesAndUtxos === 'undefined') { | if (typeof slpBalancesAndUtxos === 'undefined') { | ||||
console.log(`slpBalancesAndUtxos is undefined`); | console.log(`slpBalancesAndUtxos is undefined`); | ||||
throw new Error('slpBalancesAndUtxos is undefined'); | throw new Error('slpBalancesAndUtxos is undefined'); | ||||
} | } | ||||
const { tokens } = slpBalancesAndUtxos; | const { tokens } = slpBalancesAndUtxos; | ||||
const newState = { | const newState = { | ||||
balances: {}, | balances: {}, | ||||
tokens: [], | tokens: [], | ||||
slpBalancesAndUtxos: [], | slpBalancesAndUtxos: [], | ||||
}; | }; | ||||
newState.slpBalancesAndUtxos = normalizeSlpBalancesAndUtxos( | newState.slpBalancesAndUtxos = normalizeSlpBalancesAndUtxos( | ||||
slpBalancesAndUtxos, | slpBalancesAndUtxos, | ||||
wallet, | wallet, | ||||
); | ); | ||||
newState.balances = normalizeBalance(slpBalancesAndUtxos); | newState.balances = normalizeBalance(slpBalancesAndUtxos); | ||||
newState.tokens = tokens; | newState.tokens = tokens; | ||||
newState.txHistory = txHistory; | newState.parsedTxHistory = parsedTxHistory; | ||||
setWalletState(newState); | setWalletState(newState); | ||||
// If everything executed correctly, remove apiError | // If everything executed correctly, remove apiError | ||||
setApiError(false); | setApiError(false); | ||||
} catch (error) { | } catch (error) { | ||||
console.log(`Error in update({wallet, setWalletState})`); | console.log(`Error in update({wallet, setWalletState})`); | ||||
console.log(error); | console.log(error); | ||||
▲ Show 20 Lines • Show All 965 Lines • ▼ Show 20 Lines | |||||
return { | return { | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
fiatPrice, | fiatPrice, | ||||
slpBalancesAndUtxos, | slpBalancesAndUtxos, | ||||
balances, | balances, | ||||
tokens, | tokens, | ||||
txHistory, | parsedTxHistory, | ||||
loading, | loading, | ||||
apiError, | apiError, | ||||
getActiveWalletFromLocalForage, | getActiveWalletFromLocalForage, | ||||
getWallet, | getWallet, | ||||
validateMnemonic, | validateMnemonic, | ||||
getWalletDetails, | getWalletDetails, | ||||
getSavedWallets, | getSavedWallets, | ||||
migrateLegacyWallet, | migrateLegacyWallet, | ||||
Show All 31 Lines |