Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useWallet.js
import { useState, useEffect } from 'react'; | import { useState, useEffect } from 'react'; | ||||
import usePrevious from 'hooks/usePrevious'; | import usePrevious from 'hooks/usePrevious'; | ||||
import useInterval from './useInterval'; | import useInterval from './useInterval'; | ||||
import useBCH from 'hooks/useBCH'; | import useBCH from 'hooks/useBCH'; | ||||
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
import { | import { | ||||
loadStoredWallet, | loadStoredWallet, | ||||
isValidStoredWallet, | isValidStoredWallet, | ||||
isLegacyMigrationRequired, | isLegacyMigrationRequired, | ||||
getHashArrayFromWallet, | getHashArrayFromWallet, | ||||
checkWalletForTokenInfo, | |||||
isActiveWebsocket, | isActiveWebsocket, | ||||
getWalletBalanceFromUtxos, | getWalletBalanceFromUtxos, | ||||
} from 'utils/cashMethods'; | } from 'utils/cashMethods'; | ||||
import { | import { | ||||
isValidCashtabSettings, | isValidCashtabSettings, | ||||
isValidCashtabCache, | isValidCashtabCache, | ||||
isValidContactList, | isValidContactList, | ||||
parseInvalidSettingsForMigration, | parseInvalidSettingsForMigration, | ||||
▲ Show 20 Lines • Show All 855 Lines • ▼ Show 20 Lines | const processChronikWsMsg = async (msg, wallet, fiatPrice) => { | ||||
return; | return; | ||||
} | } | ||||
// If you see a tx from your subscribed addresses added to the mempool, then the wallet utxo set has changed | // If you see a tx from your subscribed addresses added to the mempool, then the wallet utxo set has changed | ||||
// Update it | // Update it | ||||
setWalletRefreshInterval(10); | setWalletRefreshInterval(10); | ||||
// get txid info | // get txid info | ||||
const txid = msg.txid; | const txid = msg.txid; | ||||
const txDetails = await chronik.tx(txid); | |||||
let incomingTxDetails; | |||||
try { | |||||
incomingTxDetails = await chronik.tx(txid); | |||||
} catch (err) { | |||||
// In this case, no notification | |||||
return console.log( | |||||
`Error in chronik.tx(${txid} while processing an incoming websocket tx`, | |||||
err, | |||||
); | |||||
} | |||||
// Get tokenInfoById from cashtabCache to parse this tx | |||||
let tokenInfoById = {}; | |||||
try { | |||||
tokenInfoById = cashtabCache.tokenInfoById; | |||||
} catch (err) { | |||||
console.log( | |||||
`Error getting tokenInfoById from cache on incoming tx`, | |||||
err, | |||||
); | |||||
} | |||||
// parse tx for notification | // parse tx for notification | ||||
const parsedChronikTx = parseChronikTx(txDetails, wallet); | const parsedChronikTx = parseChronikTx( | ||||
BCH, | |||||
incomingTxDetails, | |||||
wallet, | |||||
tokenInfoById, | |||||
); | |||||
/* If this is an incoming eToken tx and parseChronikTx was not able to get genesis info | |||||
from cache, then get genesis info from API and add to cache */ | |||||
if (parsedChronikTx.incoming) { | if (parsedChronikTx.incoming) { | ||||
if (parsedChronikTx.isEtokenTx) { | if (parsedChronikTx.isEtokenTx) { | ||||
let eTokenAmountReceived = parsedChronikTx.etokenAmount; | |||||
if (parsedChronikTx.genesisInfo.success) { | |||||
// Send this info to the notification function | |||||
eTokenReceivedNotification( | |||||
currency, | |||||
parsedChronikTx.genesisInfo.tokenTicker, | |||||
eTokenAmountReceived, | |||||
parsedChronikTx.genesisInfo.tokenName, | |||||
); | |||||
} else { | |||||
// Get genesis info from API and add to cache | |||||
try { | try { | ||||
// Get the tokenID | // Get the tokenID | ||||
const incomingTokenId = parsedChronikTx.slpMeta.tokenId; | const incomingTokenId = parsedChronikTx.slpMeta.tokenId; | ||||
// Check cache for token info | |||||
// NB this procedure will change when chronik utxo formatting is implemented | |||||
let incomingTokenInfo = checkWalletForTokenInfo( | |||||
incomingTokenId, | |||||
wallet, | |||||
); | |||||
let eTokenAmountReceived; | |||||
if (!incomingTokenInfo) { | |||||
// chronik call to genesis tx to get this info | // chronik call to genesis tx to get this info | ||||
const tokenGenesisInfo = await chronik.tx( | const tokenGenesisInfo = await chronik.tx( | ||||
incomingTokenId, | incomingTokenId, | ||||
); | ); | ||||
incomingTokenInfo = { | const { genesisInfo } = tokenGenesisInfo.slpTxData; | ||||
decimals: | // Add this to cashtabCache | ||||
tokenGenesisInfo.slpTxData.genesisInfo.decimals, | let tokenInfoByIdUpdatedForThisToken = tokenInfoById; | ||||
name: tokenGenesisInfo.slpTxData.genesisInfo | tokenInfoByIdUpdatedForThisToken[incomingTokenId] = | ||||
.tokenName, | genesisInfo; | ||||
ticker: tokenGenesisInfo.slpTxData.genesisInfo | writeTokenInfoByIdToCache( | ||||
.tokenTicker, | tokenInfoByIdUpdatedForThisToken, | ||||
}; | ); | ||||
} | // Update the tokenInfoById key in cashtabCache | ||||
setCashtabCache({ | |||||
...cashtabCache, | |||||
tokenInfoById: tokenInfoByIdUpdatedForThisToken, | |||||
}); | |||||
// Calculate eToken amount with decimals | // Calculate eToken amount with decimals | ||||
eTokenAmountReceived = new BigNumber( | eTokenAmountReceived = new BigNumber( | ||||
parsedChronikTx.etokenAmount, | parsedChronikTx.etokenAmount, | ||||
).shiftedBy(-1 * incomingTokenInfo.decimals); | ).shiftedBy(-1 * genesisInfo.decimals); | ||||
// Send this info to the notification function | // Send this info to the notification function | ||||
eTokenReceivedNotification( | eTokenReceivedNotification( | ||||
currency, | currency, | ||||
incomingTokenInfo.ticker, | genesisInfo.tokenTicker, | ||||
eTokenAmountReceived, | eTokenAmountReceived, | ||||
incomingTokenInfo.name, | genesisInfo.tokenName, | ||||
); | ); | ||||
} catch (err) { | } catch (err) { | ||||
// In this case, no incoming tx notification is received | |||||
// This is an acceptable error condition, no need to fallback to another notification option | |||||
console.log( | console.log( | ||||
`Error parsing eToken data for incoming tx notification`, | `Error in getting and setting new token info for incoming eToken tx`, | ||||
err, | err, | ||||
); | ); | ||||
} | } | ||||
} | |||||
} else { | } else { | ||||
xecReceivedNotificationWebsocket( | xecReceivedNotificationWebsocket( | ||||
parsedChronikTx.xecAmount, | parsedChronikTx.xecAmount, | ||||
cashtabSettings, | cashtabSettings, | ||||
fiatPrice, | fiatPrice, | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 522 Lines • Show Last 20 Lines |