Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show All 27 Lines | import { | ||||
xecReceivedNotificationWebsocket, | xecReceivedNotificationWebsocket, | ||||
eTokenReceivedNotification, | eTokenReceivedNotification, | ||||
} from 'components/Common/Notifications'; | } from 'components/Common/Notifications'; | ||||
import { ChronikClient } from 'chronik-client'; | import { ChronikClient } from 'chronik-client'; | ||||
// For XEC, eCash chain: | // For XEC, eCash chain: | ||||
const chronik = new ChronikClient(currency.chronikUrl); | const chronik = new ChronikClient(currency.chronikUrl); | ||||
const useWallet = () => { | const useWallet = () => { | ||||
const [walletRefreshInterval] = useState(currency.walletRefreshInterval); | const [walletRefreshInterval, setWalletRefreshInterval] = useState( | ||||
currency.walletRefreshInterval, | |||||
); | |||||
const [wallet, setWallet] = useState(false); | const [wallet, setWallet] = useState(false); | ||||
const [chronikWebsocket, setChronikWebsocket] = useState(null); | const [chronikWebsocket, setChronikWebsocket] = useState(null); | ||||
const [contactList, setContactList] = useState(false); | const [contactList, setContactList] = useState(false); | ||||
const [cashtabSettings, setCashtabSettings] = useState(false); | const [cashtabSettings, setCashtabSettings] = useState(false); | ||||
const [fiatPrice, setFiatPrice] = useState(null); | const [fiatPrice, setFiatPrice] = useState(null); | ||||
const [apiError, setApiError] = useState(false); | const [apiError, setApiError] = useState(false); | ||||
const [checkFiatInterval, setCheckFiatInterval] = useState(null); | const [checkFiatInterval, setCheckFiatInterval] = useState(null); | ||||
const [hasUpdated, setHasUpdated] = useState(false); | const [hasUpdated, setHasUpdated] = useState(false); | ||||
▲ Show 20 Lines • Show All 161 Lines • ▼ Show 20 Lines | const haveUtxosChanged = (wallet, utxos, previousUtxos) => { | ||||
// Compare utxo sets | // Compare utxo sets | ||||
return !isEqual(utxos, utxosToCompare); | return !isEqual(utxos, utxosToCompare); | ||||
}; | }; | ||||
const update = async ({ wallet }) => { | const update = async ({ wallet }) => { | ||||
//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 | |||||
// If walletRefreshInterval is 10, set it back to the usual refresh rate | |||||
if (walletRefreshInterval === 10) { | |||||
setWalletRefreshInterval(currency.walletRefreshInterval); | |||||
} | |||||
try { | try { | ||||
if (!wallet) { | if (!wallet) { | ||||
return; | return; | ||||
} | } | ||||
const cashAddresses = [ | const cashAddresses = [ | ||||
wallet.Path245.cashAddress, | wallet.Path245.cashAddress, | ||||
wallet.Path145.cashAddress, | wallet.Path145.cashAddress, | ||||
wallet.Path1899.cashAddress, | wallet.Path1899.cashAddress, | ||||
Show All 21 Lines | const update = async ({ wallet }) => { | ||||
} | } | ||||
// 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, | ||||
); | ); | ||||
// If the utxo set has not changed, | // If the utxo set has not changed, | ||||
if (!utxosHaveChanged) { | if (!utxosHaveChanged) { | ||||
// remove api error here; otherwise it will remain if recovering from a rate | // remove api error here; otherwise it will remain if recovering from a rate | ||||
// limit error with an unchanged utxo set | // limit error with an unchanged utxo set | ||||
setApiError(false); | setApiError(false); | ||||
// then wallet.state has not changed and does not need to be updated | // then wallet.state has not changed and does not need to be updated | ||||
//console.timeEnd("update"); | //console.timeEnd("update"); | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 675 Lines • ▼ Show 20 Lines | const processChronikWsMsg = async (msg, wallet, fiatPrice) => { | ||||
const { type } = msg; | const { type } = msg; | ||||
// For now, only act on "first seen" transactions, as the only logic to happen is first seen notifications | // For now, only act on "first seen" transactions, as the only logic to happen is first seen notifications | ||||
// Dev note: Other chronik msg types | // Dev note: Other chronik msg types | ||||
// "BlockConnected", arrives as new blocks are found | // "BlockConnected", arrives as new blocks are found | ||||
// "Confirmed", arrives as subscribed + seen txid is confirmed in a block | // "Confirmed", arrives as subscribed + seen txid is confirmed in a block | ||||
if (type !== 'AddedToMempool') { | if (type !== 'AddedToMempool') { | ||||
return; | return; | ||||
} | } | ||||
// If you see a tx from your subscribed addresses added to the mempool, then the wallet utxo set has changed | |||||
// Update it | |||||
setWalletRefreshInterval(10); | |||||
// get txid info | // get txid info | ||||
const txid = msg.txid; | const txid = msg.txid; | ||||
const txDetails = await chronik.tx(txid); | const txDetails = await chronik.tx(txid); | ||||
// parse tx for notification | // parse tx for notification | ||||
const hash160Array = getHashArrayFromWallet(wallet); | const hash160Array = getHashArrayFromWallet(wallet); | ||||
const parsedChronikTx = parseChronikTx(txDetails, hash160Array); | const parsedChronikTx = parseChronikTx(txDetails, hash160Array); | ||||
if (parsedChronikTx.incoming) { | if (parsedChronikTx.incoming) { | ||||
▲ Show 20 Lines • Show All 517 Lines • Show Last 20 Lines |