Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/useBCH.js
Show First 20 Lines • Show All 85 Lines • ▼ Show 20 Lines | const parseTxData = txData => { | ||||
} | } | ||||
} | } | ||||
] | ] | ||||
*/ | */ | ||||
const parsedTxHistory = []; | const parsedTxHistory = []; | ||||
for (let i = 0; i < txData.length; i += 1) { | for (let i = 0; i < txData.length; i += 1) { | ||||
const tx = txData[i]; | const tx = txData[i]; | ||||
const parsedTx = {}; | const parsedTx = {}; | ||||
// Move over info that does not need to be calculated | // Move over info that does not need to be calculated | ||||
parsedTx.txid = tx.txid; | parsedTx.txid = tx.txid; | ||||
parsedTx.confirmations = tx.confirmations; | |||||
parsedTx.height = tx.height; | parsedTx.height = tx.height; | ||||
let destinationAddress = tx.address; | |||||
// If this tx had too many inputs to be parsed by getTxDataWithPassThrough, skip it | |||||
// When this occurs, the tx will only have txid and height | |||||
// So, it will not have 'vin' | |||||
if (!Object.keys(tx).includes('vin')) { | |||||
// Populate as a limited-info tx that can be expanded in a block explorer | |||||
parsedTxHistory.push(parsedTx); | |||||
continue; | |||||
} | |||||
parsedTx.confirmations = tx.confirmations; | |||||
parsedTx.blocktime = tx.blocktime; | parsedTx.blocktime = tx.blocktime; | ||||
let amountSent = 0; | let amountSent = 0; | ||||
let amountReceived = 0; | let amountReceived = 0; | ||||
// Assume an incoming transaction | // Assume an incoming transaction | ||||
let outgoingTx = false; | let outgoingTx = false; | ||||
let tokenTx = false; | let tokenTx = false; | ||||
let destinationAddress = tx.address; | |||||
// If vin includes tx address, this is an outgoing tx | // If vin includes tx address, this is an outgoing tx | ||||
// Note that with bch-input data, we do not have input amounts | // Note that with bch-input data, we do not have input amounts | ||||
for (let j = 0; j < tx.vin.length; j += 1) { | for (let j = 0; j < tx.vin.length; j += 1) { | ||||
const thisInput = tx.vin[j]; | const thisInput = tx.vin[j]; | ||||
if (thisInput.address === tx.address) { | if (thisInput.address === tx.address) { | ||||
// This is an outgoing transaction | // This is an outgoing transaction | ||||
outgoingTx = true; | outgoingTx = true; | ||||
Show All 22 Lines | const parseTxData = txData => { | ||||
amountReceived += thisOutput.value; | amountReceived += thisOutput.value; | ||||
} else if (outgoingTx) { | } else if (outgoingTx) { | ||||
amountSent += thisOutput.value; | amountSent += thisOutput.value; | ||||
// Assume there's only one destination address, i.e. it was sent by a Cashtab wallet | // Assume there's only one destination address, i.e. it was sent by a Cashtab wallet | ||||
destinationAddress = thisOutput.scriptPubKey.addresses[0]; | destinationAddress = thisOutput.scriptPubKey.addresses[0]; | ||||
} | } | ||||
} | } | ||||
// Construct parsedTx | // Construct parsedTx | ||||
parsedTx.txid = tx.txid; | |||||
parsedTx.amountSent = amountSent; | parsedTx.amountSent = amountSent; | ||||
parsedTx.amountReceived = amountReceived; | parsedTx.amountReceived = amountReceived; | ||||
parsedTx.tokenTx = tokenTx; | parsedTx.tokenTx = tokenTx; | ||||
parsedTx.outgoingTx = outgoingTx; | parsedTx.outgoingTx = outgoingTx; | ||||
parsedTx.destinationAddress = destinationAddress; | parsedTx.destinationAddress = destinationAddress; | ||||
parsedTxHistory.push(parsedTx); | parsedTxHistory.push(parsedTx); | ||||
} | } | ||||
Show All 19 Lines | const getTxHistory = async (BCH, addresses) => { | ||||
console.log(`Error in BCH.Electrumx.transactions(addresses):`); | console.log(`Error in BCH.Electrumx.transactions(addresses):`); | ||||
console.log(err); | console.log(err); | ||||
return err; | return err; | ||||
} | } | ||||
}; | }; | ||||
const getTxDataWithPassThrough = async (BCH, flatTx) => { | const getTxDataWithPassThrough = async (BCH, flatTx) => { | ||||
// necessary as BCH.RawTransactions.getTxData does not return address or blockheight | // necessary as BCH.RawTransactions.getTxData does not return address or blockheight | ||||
const txDataWithPassThrough = await BCH.RawTransactions.getTxData( | let txDataWithPassThrough = {}; | ||||
try { | |||||
txDataWithPassThrough = await BCH.RawTransactions.getTxData( | |||||
flatTx.txid, | flatTx.txid, | ||||
); | ); | ||||
} catch (err) { | |||||
console.log( | |||||
`Error in BCH.RawTransactions.getTxData(${flatTx.txid})`, | |||||
); | |||||
console.log(err); | |||||
// Include txid if you don't get it from the attempted response | |||||
txDataWithPassThrough.txid = flatTx.txid; | |||||
} | |||||
txDataWithPassThrough.height = flatTx.height; | txDataWithPassThrough.height = flatTx.height; | ||||
txDataWithPassThrough.address = flatTx.address; | txDataWithPassThrough.address = flatTx.address; | ||||
return txDataWithPassThrough; | return txDataWithPassThrough; | ||||
}; | }; | ||||
const getTxData = async (BCH, txHistory) => { | const getTxData = async (BCH, txHistory) => { | ||||
// Flatten tx history | // Flatten tx history | ||||
let flatTxs = flattenTransactions(txHistory); | let flatTxs = flattenTransactions(txHistory); | ||||
▲ Show 20 Lines • Show All 822 Lines • Show Last 20 Lines |