Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show First 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | const loadWalletFromStorageOnStartup = async setWallet => { | ||||
return setLoading(false); | return setLoading(false); | ||||
} | } | ||||
console.log(`Active wallet is not valid, loading params from API`); | console.log(`Active wallet is not valid, loading params from API`); | ||||
// Loading will remain true until API calls populate this legacy wallet | // Loading will remain true until API calls populate this legacy wallet | ||||
setWallet(wallet); | setWallet(wallet); | ||||
}; | }; | ||||
const update = async ({ wallet }) => { | const update = async ({ wallet }) => { | ||||
if (wallet && wallet.name) { | |||||
console.log(`update loop called on ${wallet.name}`); | |||||
} | |||||
//console.log(`tick()`); | //console.log(`tick()`); | ||||
//console.time("update"); | //console.time("update"); | ||||
// Check if walletRefreshInterval is set to 10, i.e. this was called by websocket tx detection | // Check if walletRefreshInterval is set to 10, i.e. this was called by websocket tx detection | ||||
// If walletRefreshInterval is 10, set it back to the usual refresh rate | // If walletRefreshInterval is 10, set it back to the usual refresh rate | ||||
if (walletRefreshInterval === 10) { | if (walletRefreshInterval === 10) { | ||||
setWalletRefreshInterval( | setWalletRefreshInterval( | ||||
currency.websocketConnectedRefreshInterval, | currency.websocketConnectedRefreshInterval, | ||||
▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | const update = async ({ wallet }) => { | ||||
// If you were missing any token info for tokens in this tx history, get it | // If you were missing any token info for tokens in this tx history, get it | ||||
const newState = { | const newState = { | ||||
balances: getWalletBalanceFromUtxos(nonSlpUtxos), | balances: getWalletBalanceFromUtxos(nonSlpUtxos), | ||||
tokens: finalTokenArray, | tokens: finalTokenArray, | ||||
slpBalancesAndUtxos: { | slpBalancesAndUtxos: { | ||||
slpUtxos: finalizedSlpUtxos, | slpUtxos: finalizedSlpUtxos, | ||||
nonSlpUtxos, | nonSlpUtxos, | ||||
tokens, | |||||
}, | }, | ||||
parsedTxHistory: chronikTxHistory, | parsedTxHistory: chronikTxHistory, | ||||
utxos: chronikUtxos, | utxos: chronikUtxos, | ||||
}; | }; | ||||
const walletStateAfterThisStack = { | const walletStateAfterThisStack = { | ||||
balances: getWalletBalanceFromUtxos(nonSlpUtxos), | balances: getWalletBalanceFromUtxos(nonSlpUtxos), | ||||
slpUtxos: finalizedSlpUtxos, | slpUtxos: finalizedSlpUtxos, | ||||
nonSlpUtxos, | nonSlpUtxos, | ||||
tokens: finalTokenArray, | tokens: finalTokenArray, | ||||
parsedTxHistory: chronikTxHistory, | parsedTxHistory: chronikTxHistory, | ||||
}; | }; | ||||
console.log(`walletStateAfterThisStack`, walletStateAfterThisStack); | console.log(`walletStateAfterThisStack`, walletStateAfterThisStack); | ||||
// Set wallet with new state field | // Set wallet with new state field | ||||
wallet.state = newState; | wallet.state = newState; | ||||
setWallet(wallet); | setWallet(wallet); | ||||
// Write this state to indexedDb using localForage | // Write this state to indexedDb using localForage | ||||
writeWalletState(wallet, newState); | writeWalletState(wallet, 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})`); | console.log(`Error in update({wallet})`); | ||||
console.log(error); | console.log(error); | ||||
// Set this in state so that transactions are disabled until the issue is resolved | // Set this in state so that transactions are disabled until the issue is resolved | ||||
setApiError(true); | setApiError(true); | ||||
//console.timeEnd("update"); | //console.timeEnd("update"); | ||||
▲ Show 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | const writeTokenInfoByIdToCache = async tokenInfoById => { | ||||
} | } | ||||
}; | }; | ||||
const writeWalletState = async (wallet, newState) => { | const writeWalletState = async (wallet, newState) => { | ||||
// Add new state as an object on the active wallet | // Add new state as an object on the active wallet | ||||
wallet.state = newState; | wallet.state = newState; | ||||
try { | try { | ||||
await localforage.setItem('wallet', wallet); | await localforage.setItem('wallet', wallet); | ||||
console.log( | |||||
`Wallet ${wallet.name} saved without duplicate token object`, | |||||
); | |||||
} catch (err) { | } catch (err) { | ||||
console.log(`Error in writeWalletState()`); | console.log(`Error in writeWalletState()`); | ||||
console.log(err); | console.log(err); | ||||
} | } | ||||
}; | }; | ||||
const getWalletDetails = async wallet => { | const getWalletDetails = async wallet => { | ||||
if (!wallet) { | if (!wallet) { | ||||
▲ Show 20 Lines • Show All 1,033 Lines • Show Last 20 Lines |