Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
Show All 28 Lines | import { | ||||
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, setWalletRefreshInterval] = useState( | const [walletRefreshInterval, setWalletRefreshInterval] = useState( | ||||
currency.walletRefreshInterval, | currency.websocketDisconnectedRefreshInterval, | ||||
); | ); | ||||
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); | ||||
▲ Show 20 Lines • Show All 166 Lines • ▼ Show 20 Lines | const useWallet = () => { | ||||
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 | // 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(currency.walletRefreshInterval); | setWalletRefreshInterval( | ||||
currency.websocketConnectedRefreshInterval, | |||||
); | |||||
} | } | ||||
try { | try { | ||||
if (!wallet) { | if (!wallet) { | ||||
return; | return; | ||||
} | } | ||||
const cashAddresses = [ | const cashAddresses = [ | ||||
wallet.Path245.cashAddress, | wallet.Path245.cashAddress, | ||||
wallet.Path145.cashAddress, | wallet.Path145.cashAddress, | ||||
▲ Show 20 Lines • Show All 806 Lines • ▼ Show 20 Lines | const initializeWebsocket = async (wallet, fiatPrice) => { | ||||
// Fired before a reconnect attempt is made: | // Fired before a reconnect attempt is made: | ||||
console.log( | console.log( | ||||
'Reconnecting websocket, disconnection cause: ', | 'Reconnecting websocket, disconnection cause: ', | ||||
e, | e, | ||||
); | ); | ||||
}, | }, | ||||
onConnect: e => { | onConnect: e => { | ||||
console.log(`Chronik websocket connected`, e); | console.log(`Chronik websocket connected`, e); | ||||
console.log( | |||||
`Websocket connected, adjusting wallet refresh interval to ${ | |||||
currency.websocketConnectedRefreshInterval / 1000 | |||||
}s`, | |||||
); | |||||
setWalletRefreshInterval( | |||||
currency.websocketConnectedRefreshInterval, | |||||
); | |||||
}, | }, | ||||
}); | }); | ||||
// Wait for websocket to be connected: | // Wait for websocket to be connected: | ||||
await ws.waitForOpen(); | await ws.waitForOpen(); | ||||
} else { | } else { | ||||
/* | /* | ||||
If the websocket connection is not null, initializeWebsocket was called | If the websocket connection is not null, initializeWebsocket was called | ||||
▲ Show 20 Lines • Show All 428 Lines • Show Last 20 Lines |