diff --git a/web/cashtab/src/components/Common/Ticker.js b/web/cashtab/src/components/Common/Ticker.js --- a/web/cashtab/src/components/Common/Ticker.js +++ b/web/cashtab/src/components/Common/Ticker.js @@ -22,7 +22,6 @@ tokenLogo: tokenLogo, tokenPrefixes: ['etoken'], tokenIconsUrl: '', //https://tokens.bitcoin.com/32 for BCH SLP - useBlockchainWs: false, txHistoryCount: 5, hydrateUtxoBatchSize: 20, defaultSettings: { fiatCurrency: 'usd' }, diff --git a/web/cashtab/src/hooks/useWallet.js b/web/cashtab/src/hooks/useWallet.js --- a/web/cashtab/src/hooks/useWallet.js +++ b/web/cashtab/src/hooks/useWallet.js @@ -22,7 +22,6 @@ const [wallet, setWallet] = useState(false); const [cashtabSettings, setCashtabSettings] = useState(false); const [fiatPrice, setFiatPrice] = useState(null); - const [ws, setWs] = useState(null); const [apiError, setApiError] = useState(false); const [checkFiatInterval, setCheckFiatInterval] = useState(null); const { @@ -888,10 +887,8 @@ setLoading(false); }; - // Parse for incoming BCH transactions - // Only notify if websocket is not connected + // Parse for incoming XEC transactions if ( - (ws === null || ws.readyState !== 1) && previousBalances && balances && 'totalBalance' in previousBalances && @@ -1047,282 +1044,6 @@ }); }, 10000); - const initializeWebsocket = (cashAddress, slpAddress) => { - // console.log(`initializeWebsocket(${cashAddress}, ${slpAddress})`); - // This function parses 3 cases - // 1: edge case, websocket is in state but not properly connected - // > Remove it from state and forget about it, fall back to normal notifications - // 2: edge-ish case, websocket is in state and connected but user has changed wallet - // > Unsubscribe from old addresses and subscribe to new ones - // 3: most common: app is opening, creating websocket with existing addresses - - // If the websocket is already in state but is not properly connected - if (ws !== null && ws.readyState !== 1) { - // Forget about it and use conventional notifications - - // Close - ws.close(); - // Remove from state - setWs(null); - } - // If the websocket is in state and connected - else if (ws !== null) { - // console.log(`Websocket already in state`); - // console.log(`ws,`, ws); - // instead of initializing websocket, unsubscribe from old addresses and subscribe to new ones - const previousWsCashAddress = previousWallet.Path145.legacyAddress; - const previousWsSlpAddress = previousWallet.Path245.legacyAddress; - try { - // Unsubscribe from previous addresses - ws.send( - JSON.stringify({ - op: 'addr_unsub', - addr: previousWsCashAddress, - }), - ); - console.log( - `Unsubscribed from BCH address at ${previousWsCashAddress}`, - ); - ws.send( - JSON.stringify({ - op: 'addr_unsub', - addr: previousWsSlpAddress, - }), - ); - console.log( - `Unsubscribed from SLP address at ${previousWsSlpAddress}`, - ); - - // Subscribe to new addresses - ws.send( - JSON.stringify({ - op: 'addr_sub', - addr: cashAddress, - }), - ); - console.log(`Subscribed to BCH address at ${cashAddress}`); - // Subscribe to SLP address - ws.send( - JSON.stringify({ - op: 'addr_sub', - addr: slpAddress, - }), - ); - console.log(`Subscribed to SLP address at ${slpAddress}`); - // Reset onmessage; it was previously set with the old addresses - // Note this code is exactly identical to lines 431-490 - // TODO put in function - ws.onmessage = e => { - // TODO handle case where receive multiple messages on one incoming transaction - //console.log(`ws msg received`); - const incomingTx = JSON.parse(e.data); - console.log(incomingTx); - - let bchSatsReceived = 0; - // First, check the inputs - // If cashAddress or slpAddress are in the inputs, then this is a sent tx and should be ignored for notifications - if ( - incomingTx && - incomingTx.x && - incomingTx.x.inputs && - incomingTx.x.out - ) { - const inputs = incomingTx.x.inputs; - // Iterate over inputs and see if this transaction was sent by the active wallet - for (let i = 0; i < inputs.length; i += 1) { - if ( - inputs[i].prev_out.addr === cashAddress || - inputs[i].prev_out.addr === slpAddress - ) { - // console.log(`Found a sending tx, not notifying`); - // This is a sent transaction and should be ignored by notification handlers - return; - } - } - // Iterate over outputs to determine receiving address - const outputs = incomingTx.x.out; - - for (let i = 0; i < outputs.length; i += 1) { - if (outputs[i].addr === cashAddress) { - // console.log(`BCH transaction received`); - bchSatsReceived += outputs[i].value; - // handle - } - if (outputs[i].addr === slpAddress) { - console.log(`SLP transaction received`); - //handle - // you would want to get the slp info using this endpoint: - // https://rest.kingbch.com/v3/slp/txDetails/cb39dd04e07e172a37addfcb1d6e167dc52c01867ba21c9bf8b5acf4dd969a3f - // But it does not work for unconfirmed txs - // Hold off on slp tx notifications for now - } - } - } - // parse for receiving address - // if received at cashAddress, parse for BCH amount, notify BCH received - // if received at slpAddress, parse for token, notify SLP received - // if those checks fail, could be from a 'sent' tx, ignore - - // Note, when you send an SLP tx, you get SLP change to SLP address and BCH change to BCH address - - // Also note, when you send an SLP tx, you often have inputs from both BCH and SLP addresses - - // This causes a sent SLP tx to register 4 times from the websocket - - // Best way to ignore this is to ignore any incoming utx.x with BCH or SLP address in the inputs - - // Notification for received BCHA - if (bchSatsReceived > 0) { - notification.success({ - message: 'Transaction received', - description: ( - - You received {bchSatsReceived / 1e8}{' '} - {currency.ticker}! - - ), - duration: 3, - }); - } - }; - } catch (err) { - console.log( - `Error attempting to configure websocket for new wallet`, - ); - console.log(err); - console.log(`Closing connection`); - ws.close(); - setWs(null); - } - } else { - // If there is no websocket, create one, subscribe to addresses, and add notifications for incoming BCH transactions - - let newWs = new WebSocket('wss://ws.blockchain.info/bch/inv'); - - newWs.onopen = () => { - console.log(`Connected to bchWs`); - - // Subscribe to BCH address - newWs.send( - JSON.stringify({ - op: 'addr_sub', - addr: cashAddress, - }), - ); - - console.log(`Subscribed to BCH address at ${cashAddress}`); - // Subscribe to SLP address - newWs.send( - JSON.stringify({ - op: 'addr_sub', - addr: slpAddress, - }), - ); - console.log(`Subscribed to SLP address at ${slpAddress}`); - }; - newWs.onerror = e => { - // close and set to null - console.log(`Error in websocket connection for ${newWs}`); - console.log(e); - setWs(null); - }; - newWs.onclose = () => { - console.log(`Websocket connection closed`); - // Unsubscribe on close to prevent double subscribing - //{"op":"addr_unsub", "addr":"$bitcoin_address"} - newWs.send( - JSON.stringify({ - op: 'addr_unsub', - addr: cashAddress, - }), - ); - console.log(`Unsubscribed from BCH address at ${cashAddress}`); - newWs.send( - JSON.stringify({ - op: 'addr_sub', - addr: slpAddress, - }), - ); - console.log(`Unsubscribed from SLP address at ${slpAddress}`); - }; - newWs.onmessage = e => { - // TODO handle case where receive multiple messages on one incoming transaction - //console.log(`ws msg received`); - const incomingTx = JSON.parse(e.data); - console.log(incomingTx); - - let bchSatsReceived = 0; - // First, check the inputs - // If cashAddress or slpAddress are in the inputs, then this is a sent tx and should be ignored for notifications - if ( - incomingTx && - incomingTx.x && - incomingTx.x.inputs && - incomingTx.x.out - ) { - const inputs = incomingTx.x.inputs; - // Iterate over inputs and see if this transaction was sent by the active wallet - for (let i = 0; i < inputs.length; i += 1) { - if ( - inputs[i].prev_out.addr === cashAddress || - inputs[i].prev_out.addr === slpAddress - ) { - // console.log(`Found a sending tx, not notifying`); - // This is a sent transaction and should be ignored by notification handlers - return; - } - } - // Iterate over outputs to determine receiving address - const outputs = incomingTx.x.out; - - for (let i = 0; i < outputs.length; i += 1) { - if (outputs[i].addr === cashAddress) { - // console.log(`BCH transaction received`); - bchSatsReceived += outputs[i].value; - // handle - } - if (outputs[i].addr === slpAddress) { - console.log(`SLP transaction received`); - //handle - // you would want to get the slp info using this endpoint: - // https://rest.kingbch.com/v3/slp/txDetails/cb39dd04e07e172a37addfcb1d6e167dc52c01867ba21c9bf8b5acf4dd969a3f - // But it does not work for unconfirmed txs - // Hold off on slp tx notifications for now - } - } - } - // parse for receiving address - // if received at cashAddress, parse for BCH amount, notify BCH received - // if received at slpAddress, parse for token, notify SLP received - // if those checks fail, could be from a 'sent' tx, ignore - - // Note, when you send an SLP tx, you get SLP change to SLP address and BCH change to BCH address - - // Also note, when you send an SLP tx, you often have inputs from both BCH and SLP addresses - - // This causes a sent SLP tx to register 4 times from the websocket - - // Best way to ignore this is to ignore any incoming utx.x with BCH or SLP address in the inputs - - // Notification for received BCHA - if (bchSatsReceived > 0) { - notification.success({ - message: 'Transaction received', - description: ( - - You received {bchSatsReceived / 1e8}{' '} - {currency.ticker}! - - ), - duration: 3, - }); - } - }; - - setWs(newWs); - } - }; - const fetchBchPrice = async ( fiatCode = cashtabSettings ? cashtabSettings.fiatCurrency : 'usd', ) => { @@ -1364,23 +1085,6 @@ initializeFiatPriceApi(initialSettings.fiatCurrency); }, []); - useEffect(() => { - if ( - wallet && - wallet.Path145 && - wallet.Path145.cashAddress && - wallet.Path245 && - wallet.Path245.cashAddress - ) { - if (currency.useBlockchainWs) { - initializeWebsocket( - wallet.Path145.legacyAddress, - wallet.Path245.legacyAddress, - ); - } - } - }, [wallet]); - return { BCH, wallet,