Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/chronik.js
Show First 20 Lines • Show All 366 Lines • ▼ Show 20 Lines | ) => { | ||||
for (let i = 0; i < flatTxHistoryArray.length; i += 1) { | for (let i = 0; i < flatTxHistoryArray.length; i += 1) { | ||||
const thisTx = flatTxHistoryArray[i]; | const thisTx = flatTxHistoryArray[i]; | ||||
if (typeof thisTx.block === 'undefined') { | if (typeof thisTx.block === 'undefined') { | ||||
unconfirmedTxs.push(thisTx); | unconfirmedTxs.push(thisTx); | ||||
} else { | } else { | ||||
confirmedTxs.push(thisTx); | confirmedTxs.push(thisTx); | ||||
} | } | ||||
} | } | ||||
console.log(`confirmed txs`, confirmedTxs); | |||||
console.log(`unconfirmed txs`, unconfirmedTxs); | |||||
// Sort confirmed txs by blockheight, and then timeFirstSeen | // Sort confirmed txs by blockheight, and then timeFirstSeen | ||||
const sortedConfirmedTxHistoryArray = confirmedTxs.sort( | const sortedConfirmedTxHistoryArray = confirmedTxs.sort( | ||||
(a, b) => | (a, b) => | ||||
// We want more recent blocks i.e. higher blockheights to have earlier array indices | // We want more recent blocks i.e. higher blockheights to have earlier array indices | ||||
b.block.height - a.block.height || | b.block.height - a.block.height || | ||||
// For blocks with the same height, we want more recent timeFirstSeen i.e. higher timeFirstSeen to have earlier array indices | // For blocks with the same height, we want more recent timeFirstSeen i.e. higher timeFirstSeen to have earlier array indices | ||||
b.timeFirstSeen - a.timeFirstSeen, | b.timeFirstSeen - a.timeFirstSeen, | ||||
); | ); | ||||
Show All 21 Lines | /* | ||||
Add the address to each utxo | Add the address to each utxo | ||||
*/ | */ | ||||
return new Promise((resolve, reject) => { | return new Promise((resolve, reject) => { | ||||
chronik | chronik | ||||
.script('p2pkh', hash160AndAddressObj.hash160) | .script('p2pkh', hash160AndAddressObj.hash160) | ||||
.history(/*page=*/ 0, /*page_size=*/ currency.txHistoryCount) | .history(/*page=*/ 0, /*page_size=*/ currency.txHistoryCount) | ||||
.then( | .then( | ||||
result => { | result => { | ||||
console.log( | |||||
`result for ${hash160AndAddressObj.hash160}`, | |||||
result, | |||||
); | |||||
resolve(result); | resolve(result); | ||||
}, | }, | ||||
err => { | err => { | ||||
reject(err); | reject(err); | ||||
}, | }, | ||||
); | ); | ||||
}); | }); | ||||
}; | }; | ||||
Show All 34 Lines | for (let i = 0; i < inputs.length; i += 1) { | ||||
// If this is an etoken tx, check for token burn | // If this is an etoken tx, check for token burn | ||||
if ( | if ( | ||||
isEtokenTx && | isEtokenTx && | ||||
typeof thisInput.slpBurn !== 'undefined' && | typeof thisInput.slpBurn !== 'undefined' && | ||||
thisInput.slpBurn.token && | thisInput.slpBurn.token && | ||||
thisInput.slpBurn.token.amount && | thisInput.slpBurn.token.amount && | ||||
thisInput.slpBurn.token.amount !== '0' | thisInput.slpBurn.token.amount !== '0' | ||||
) { | ) { | ||||
console.log(`Token burn at ${tx.txid}`); | |||||
// Assume that any eToken tx with a burn is a burn tx | // Assume that any eToken tx with a burn is a burn tx | ||||
isTokenBurn = true; | isTokenBurn = true; | ||||
try { | try { | ||||
const thisEtokenBurnAmount = new BigNumber( | const thisEtokenBurnAmount = new BigNumber( | ||||
thisInput.slpBurn.token.amount, | thisInput.slpBurn.token.amount, | ||||
); | ); | ||||
// Need to know the total output amount to compare to total input amount and tell if this is a burn transaction | // Need to know the total output amount to compare to total input amount and tell if this is a burn transaction | ||||
etokenAmount = etokenAmount.plus(thisEtokenBurnAmount); | etokenAmount = etokenAmount.plus(thisEtokenBurnAmount); | ||||
Show All 23 Lines | for (let i = 0; i < inputs.length; i += 1) { | ||||
thisInputSendingHash160.lastIndexOf('88ac'), | thisInputSendingHash160.lastIndexOf('88ac'), | ||||
); | ); | ||||
let replyAddressBchFormat = | let replyAddressBchFormat = | ||||
BCH.Address.hash160ToCash(originatingHash160); | BCH.Address.hash160ToCash(originatingHash160); | ||||
const { type, hash } = cashaddr.decode(replyAddressBchFormat); | const { type, hash } = cashaddr.decode(replyAddressBchFormat); | ||||
replyAddress = cashaddr.encode('ecash', type, hash); | replyAddress = cashaddr.encode('ecash', type, hash); | ||||
console.log(`replyAddressXecFormat`, replyAddress); | |||||
} catch (err) { | } catch (err) { | ||||
console.log(`err from ${originatingHash160}`, err); | console.log(`err from ${originatingHash160}`, err); | ||||
// If the transaction is nonstandard, don't worry about a reply address for now | // If the transaction is nonstandard, don't worry about a reply address for now | ||||
originatingHash160 = 'N/A'; | originatingHash160 = 'N/A'; | ||||
} | } | ||||
for (let j = 0; j < walletHash160s.length; j += 1) { | for (let j = 0; j < walletHash160s.length; j += 1) { | ||||
const thisWalletHash160 = walletHash160s[j]; | const thisWalletHash160 = walletHash160s[j]; | ||||
if (thisInputSendingHash160.includes(thisWalletHash160)) { | if (thisInputSendingHash160.includes(thisWalletHash160)) { | ||||
▲ Show 20 Lines • Show All 203 Lines • ▼ Show 20 Lines | if (isEtokenTx) { | ||||
} | } | ||||
} 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 }; | ||||
} | } | ||||
} | } | ||||
etokenAmount = etokenAmount.toString(); | etokenAmount = etokenAmount.toString(); | ||||
if (isTokenBurn) { | |||||
console.log(`${etokenAmount} of ${genesisInfo.tokenName} burned`); | |||||
} | |||||
// Convert opReturnMessage to string | // Convert opReturnMessage to string | ||||
opReturnMessage = Buffer.from(opReturnMessage).toString(); | opReturnMessage = Buffer.from(opReturnMessage).toString(); | ||||
// Return eToken specific fields if eToken tx | // Return eToken specific fields if eToken tx | ||||
if (isEtokenTx) { | if (isEtokenTx) { | ||||
const { slpMeta } = tx.slpTxData; | const { slpMeta } = tx.slpTxData; | ||||
return { | return { | ||||
Show All 34 Lines | export const getTxHistoryChronik = async ( | ||||
chronik, | chronik, | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
tokenInfoById, | tokenInfoById, | ||||
) => { | ) => { | ||||
// Create array of promises to get chronik history for each address | // Create array of promises to get chronik history for each address | ||||
// Combine them all and sort by blockheight and firstSeen | // Combine them all and sort by blockheight and firstSeen | ||||
// Add all the info cashtab needs to make them useful | // Add all the info cashtab needs to make them useful | ||||
console.log(`tokenInfoById`, tokenInfoById); | |||||
const hash160AndAddressObjArray = [ | const hash160AndAddressObjArray = [ | ||||
{ | { | ||||
address: wallet.Path145.cashAddress, | address: wallet.Path145.cashAddress, | ||||
hash160: wallet.Path145.hash160, | hash160: wallet.Path145.hash160, | ||||
}, | }, | ||||
{ | { | ||||
address: wallet.Path245.cashAddress, | address: wallet.Path245.cashAddress, | ||||
Show All 14 Lines | for (let i = 0; i < hash160AndAddressObjArray.length; i += 1) { | ||||
txHistoryPromises.push(txHistoryPromise); | txHistoryPromises.push(txHistoryPromise); | ||||
} | } | ||||
let txHistoryOfAllAddresses; | let txHistoryOfAllAddresses; | ||||
try { | try { | ||||
txHistoryOfAllAddresses = await Promise.all(txHistoryPromises); | txHistoryOfAllAddresses = await Promise.all(txHistoryPromises); | ||||
} catch (err) { | } catch (err) { | ||||
console.log(`Error in Promise.all(txHistoryPromises)`, err); | console.log(`Error in Promise.all(txHistoryPromises)`, err); | ||||
} | } | ||||
console.log(`txHistoryOfAllAddresses`, txHistoryOfAllAddresses); | |||||
const flatTxHistoryArray = flattenChronikTxHistory(txHistoryOfAllAddresses); | const flatTxHistoryArray = flattenChronikTxHistory(txHistoryOfAllAddresses); | ||||
console.log(`flatTxHistoryArray`, flatTxHistoryArray); | |||||
const sortedTxHistoryArray = sortAndTrimChronikTxHistory( | const sortedTxHistoryArray = sortAndTrimChronikTxHistory( | ||||
flatTxHistoryArray, | flatTxHistoryArray, | ||||
currency.txHistoryCount, | currency.txHistoryCount, | ||||
); | ); | ||||
// Parse txs | // Parse txs | ||||
const chronikTxHistory = []; | const chronikTxHistory = []; | ||||
const uncachedTokenIds = []; | const uncachedTokenIds = []; | ||||
▲ Show 20 Lines • Show All 82 Lines • Show Last 20 Lines |