Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/chronik.js
Show First 20 Lines • Show All 696 Lines • ▼ Show 20 Lines | export const parseChronikTx = (BCH, tx, wallet, tokenInfoById) => { | ||||
// Convert from BigNumber to string | // Convert from BigNumber to string | ||||
xecAmount = xecAmount.toString(); | xecAmount = xecAmount.toString(); | ||||
// Get decimal info for correct etokenAmount | // Get decimal info for correct etokenAmount | ||||
let genesisInfo = {}; | let genesisInfo = {}; | ||||
if (isEtokenTx) { | if (isEtokenTx) { | ||||
// Get token genesis info from cache | // Get token genesis info from cache | ||||
let decimals = 0; | let decimals = 0; | ||||
try { | try { | ||||
genesisInfo = tokenInfoById[tx.slpTxData.slpMeta.tokenId]; | genesisInfo = tokenInfoById[tx.slpTxData.slpMeta.tokenId]; | ||||
if (typeof genesisInfo !== 'undefined') { | |||||
genesisInfo.success = true; | genesisInfo.success = true; | ||||
// tokenGenesisInfo should be there for every tx in tx history, since it's already been cached for every utxo in the wallet | |||||
// but try...catch just in case | |||||
decimals = genesisInfo.decimals; | decimals = genesisInfo.decimals; | ||||
etokenAmount = etokenAmount.shiftedBy(-1 * decimals); | etokenAmount = etokenAmount.shiftedBy(-1 * decimals); | ||||
} else { | |||||
genesisInfo = { success: false }; | |||||
} | |||||
} catch (err) { | } catch (err) { | ||||
console.log( | console.log( | ||||
`Error getting token info from cache in parseChronikTx`, | `Error getting token info from cache in parseChronikTx`, | ||||
err, | err, | ||||
); | ); | ||||
// To keep this function synchronous, do not get this info from the API if it is not in cache | // To keep this function synchronous, do not get this info from the API if it is not in cache | ||||
// Instead, return a flag so that useWallet.js knows and can fetch this info + add it to cache | // Instead, return a flag so that useWallet.js knows and can fetch this info + add it to cache | ||||
genesisInfo.success = false; | genesisInfo.success = false; | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | ) => { | ||||
const flatTxHistoryArray = flattenChronikTxHistory(txHistoryOfAllAddresses); | const flatTxHistoryArray = flattenChronikTxHistory(txHistoryOfAllAddresses); | ||||
console.log(`flatTxHistoryArray`, flatTxHistoryArray); | console.log(`flatTxHistoryArray`, flatTxHistoryArray); | ||||
const sortedTxHistoryArray = sortAndTrimChronikTxHistory( | const sortedTxHistoryArray = sortAndTrimChronikTxHistory( | ||||
flatTxHistoryArray, | flatTxHistoryArray, | ||||
currency.txHistoryCount, | currency.txHistoryCount, | ||||
); | ); | ||||
// Parse txs | // Parse txs | ||||
const parsedTxs = []; | const chronikTxHistory = []; | ||||
const uncachedTokenIds = []; | |||||
for (let i = 0; i < sortedTxHistoryArray.length; i += 1) { | for (let i = 0; i < sortedTxHistoryArray.length; i += 1) { | ||||
const sortedTx = sortedTxHistoryArray[i]; | const sortedTx = sortedTxHistoryArray[i]; | ||||
// Add token genesis info so parsing function can calculate amount by decimals | // Add token genesis info so parsing function can calculate amount by decimals | ||||
sortedTx.parsed = parseChronikTx(BCH, sortedTx, wallet, tokenInfoById); | sortedTx.parsed = parseChronikTx(BCH, sortedTx, wallet, tokenInfoById); | ||||
parsedTxs.push(sortedTx); | // Check to see if this tx was a token tx with uncached tokenInfoById | ||||
if ( | |||||
sortedTx.parsed.isEtokenTx && | |||||
sortedTx.parsed.genesisInfo && | |||||
!sortedTx.parsed.genesisInfo.success | |||||
) { | |||||
// Only add if the token id is not already in uncachedTokenIds | |||||
const uncachedTokenId = sortedTx.parsed.slpMeta.tokenId; | |||||
if (!uncachedTokenIds.includes(uncachedTokenId)) | |||||
uncachedTokenIds.push(uncachedTokenId); | |||||
} | |||||
chronikTxHistory.push(sortedTx); | |||||
} | |||||
const txHistoryNewTokensToCache = uncachedTokenIds.length > 0; | |||||
if (!txHistoryNewTokensToCache) { | |||||
// This will almost always be the case | |||||
// Edge case to find uncached token info in tx history that was not caught in processing utxos | |||||
// Requires performing transactions in one wallet, then loading the same wallet in another browser later | |||||
return { | |||||
chronikTxHistory, | |||||
txHistoryUpdatedTokenInfoById: tokenInfoById, | |||||
txHistoryNewTokensToCache, | |||||
}; | |||||
} | |||||
// Iterate over uncachedTokenIds to get genesis info and add to cache | |||||
const getTokenInfoPromises = []; | |||||
for (let i = 0; i < uncachedTokenIds.length; i += 1) { | |||||
const thisTokenId = uncachedTokenIds[i]; | |||||
const thisTokenInfoPromise = returnGetTokenInfoChronikPromise( | |||||
chronik, | |||||
thisTokenId, | |||||
); | |||||
getTokenInfoPromises.push(thisTokenInfoPromise); | |||||
} | |||||
// Get all the token info you need | |||||
let tokenInfoArray = []; | |||||
try { | |||||
tokenInfoArray = await Promise.all(getTokenInfoPromises); | |||||
} catch (err) { | |||||
console.log( | |||||
`Error in Promise.all(getTokenInfoPromises) in getTxHistoryChronik`, | |||||
err, | |||||
); | |||||
} | |||||
// Add the token info you received from those API calls to | |||||
// your token info cache object, cachedTokenInfoByTokenId | |||||
const txHistoryUpdatedTokenInfoById = tokenInfoById; | |||||
for (let i = 0; i < tokenInfoArray.length; i += 1) { | |||||
/* tokenInfoArray is an array of objects that look like | |||||
{ | |||||
"tokenTicker": "ST", | |||||
"tokenName": "ST", | |||||
"tokenDocumentUrl": "developer.bitcoin.com", | |||||
"tokenDocumentHash": "", | |||||
"decimals": 0, | |||||
"tokenId": "bf24d955f59351e738ecd905966606a6837e478e1982943d724eab10caad82fd" | |||||
} | } | ||||
*/ | |||||
return parsedTxs; | const thisTokenInfo = tokenInfoArray[i]; | ||||
const thisTokenId = thisTokenInfo.tokenId; | |||||
// Add this entry to updatedTokenInfoById | |||||
txHistoryUpdatedTokenInfoById[thisTokenId] = thisTokenInfo; | |||||
} | |||||
return { | |||||
chronikTxHistory, | |||||
txHistoryUpdatedTokenInfoById, | |||||
txHistoryNewTokensToCache, | |||||
}; | |||||
}; | }; |