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 @@ -32,6 +32,7 @@ getPreliminaryTokensArray, finalizeTokensArray, finalizeSlpUtxos, + getTxHistoryChronik, } from 'utils/chronik'; import { ChronikClient } from 'chronik-client'; // For XEC, eCash chain: @@ -228,6 +229,12 @@ // Preserve bch-api for tx history for now, as this will take another stacked diff to migrate to chronik const txHistory = await getTxHistory(BCH, cashAddresses); + const chronikTxHistory = await getTxHistoryChronik( + chronik, + hash160AndAddressObjArray, + ); + console.log(`chronikTxHistory`, chronikTxHistory); + // public keys are used to determined if a tx is incoming outgoing const parsedTxHistory = await getTxData( BCH, diff --git a/web/cashtab/src/utils/chronik.js b/web/cashtab/src/utils/chronik.js --- a/web/cashtab/src/utils/chronik.js +++ b/web/cashtab/src/utils/chronik.js @@ -1,5 +1,6 @@ // Chronik methods import BigNumber from 'bignumber.js'; +import { currency } from 'components/Common/Ticker'; // Return false if do not get a valid response export const getTokenStats = async (chronik, tokenId) => { @@ -333,3 +334,54 @@ } return finalizedSlpUtxos; }; + +export const returnGetTxHistoryChronikPromise = ( + chronik, + hash160AndAddressObj, +) => { + /* + Chronik thinks in hash160s, but people and wallets think in addresses + Add the address to each utxo + */ + return new Promise((resolve, reject) => { + chronik + .script('p2pkh', hash160AndAddressObj.hash160) + .history(/*page=*/ 0, /*page_size=*/ currency.txHistoryCount) + .then( + result => { + console.log( + `result for ${hash160AndAddressObj.hash160}, result`, + ); + resolve(result); + }, + err => { + reject(err); + }, + ); + }); +}; + +export const getTxHistoryChronik = async ( + chronik, + hash160AndAddressObjArray, +) => { + // Create array of promises to get chronik history for each address + // Combine them all and sort by blockheight and firstSeen + // Add all the info cashtab needs to make them useful + + let txHistoryPromises = []; + for (let i = 0; i < hash160AndAddressObjArray.length; i += 1) { + const txHistoryPromise = returnGetTxHistoryChronikPromise( + chronik, + hash160AndAddressObjArray[i], + ); + txHistoryPromises.push(txHistoryPromise); + } + let txHistoryOfAllAddresses; + try { + txHistoryOfAllAddresses = await Promise.all(txHistoryPromises); + } catch (err) { + console.log(`Error in Promise.all(txHistoryPromises)`, err); + } + return txHistoryOfAllAddresses; +};