@@ -1485,7 +1485,7 @@ export const getInputData = async ({
14851485 ? [ vout . scriptPubKey . address ]
14861486 : [ ] ;
14871487 const value = vout . value ;
1488- const key = data . tx_hash ;
1488+ const key = ` ${ data . tx_hash } ${ vout . n } ` ;
14891489 inputData [ key ] = { addresses, value } ;
14901490 } ) ;
14911491 }
@@ -1543,38 +1543,36 @@ export const formatTransactions = async ({
15431543 let addresses = { } as IAddresses ;
15441544 let changeAddresses = { } as IAddresses ;
15451545
1546- await Promise . all ( [
1547- addressTypes . map ( ( addressType ) => {
1548- // Check if addresses of this type have been generated. If not, skip.
1549- if ( Object . keys ( currentAddresses [ addressType ] ) ?. length > 0 ) {
1550- addresses = { ...addresses , ...currentAddresses [ addressType ] } ;
1551- }
1552- } ) ,
1553- addressTypes . map ( ( addressType ) => {
1554- // Check if change addresses of this type have been generated. If not, skip.
1555- if ( Object . keys ( currentChangeAddresses [ addressType ] ) ?. length > 0 ) {
1556- changeAddresses = {
1557- ...changeAddresses ,
1558- ...currentChangeAddresses [ addressType ] ,
1559- } ;
1560- }
1561- } ) ,
1562- ] ) ;
1546+ addressTypes . map ( ( addressType ) => {
1547+ // Check if addresses of this type have been generated. If not, skip.
1548+ if ( Object . keys ( currentAddresses [ addressType ] ) ?. length > 0 ) {
1549+ addresses = { ...addresses , ...currentAddresses [ addressType ] } ;
1550+ }
1551+ } ) ;
1552+ addressTypes . map ( ( addressType ) => {
1553+ // Check if change addresses of this type have been generated. If not, skip.
1554+ if ( Object . keys ( currentChangeAddresses [ addressType ] ) ?. length > 0 ) {
1555+ changeAddresses = {
1556+ ...changeAddresses ,
1557+ ...currentChangeAddresses [ addressType ] ,
1558+ } ;
1559+ }
1560+ } ) ;
15631561
1564- const addressScriptHashes = Object . keys ( addresses ) ;
1565- const changeAddressScriptHashes = Object . keys ( changeAddresses ) ;
1566- const [ addressArray , changeAddressArray ] = await Promise . all ( [
1567- addressScriptHashes . map ( ( key ) => {
1568- return addresses [ key ] . address ;
1569- } ) ,
1570- changeAddressScriptHashes . map ( ( key ) => {
1571- return changeAddresses [ key ] . address ;
1572- } ) ,
1573- ] ) ;
1562+ // Create combined address/change-address object for easier/faster reference later on.
1563+ const combinedAddressObj : { [ key : string ] : IAddress } = { } ;
1564+ [ ...Object . values ( addresses ) , ...Object . values ( changeAddresses ) ] . map (
1565+ ( data ) => {
1566+ combinedAddressObj [ data . address ] = data ;
1567+ } ,
1568+ ) ;
15741569
15751570 const formattedTransactions : IFormattedTransactions = { } ;
1571+ transactions . map ( async ( { data, result } ) => {
1572+ if ( ! result ?. txid ) {
1573+ return ;
1574+ }
15761575
1577- transactions . map ( ( { data, result } ) => {
15781576 let totalInputValue = 0 ; // Total value of all inputs.
15791577 let matchedInputValue = 0 ; // Total value of all inputs with addresses that belong to this wallet.
15801578 let totalOutputValue = 0 ; // Total value of all outputs.
@@ -1583,7 +1581,7 @@ export const formatTransactions = async ({
15831581
15841582 //Iterate over each input
15851583 const vin = result ?. vin ?? [ ] ;
1586- vin . map ( ( { txid, scriptSig } ) => {
1584+ vin . map ( ( { txid, scriptSig, vout } ) => {
15871585 //Push any OP_RETURN messages to messages array
15881586 try {
15891587 const asm = scriptSig . asm ;
@@ -1593,14 +1591,11 @@ export const formatTransactions = async ({
15931591 }
15941592 } catch { }
15951593
1596- const { addresses : _addresses , value } = inputData [ txid ] ;
1594+ const { addresses : _addresses , value } = inputData [ ` ${ txid } ${ vout } ` ] ;
15971595 totalInputValue = totalInputValue + value ;
15981596 Array . isArray ( _addresses ) &&
15991597 _addresses . map ( ( address ) => {
1600- if (
1601- addressArray . includes ( address ) ||
1602- changeAddressArray . includes ( address )
1603- ) {
1598+ if ( address in combinedAddressObj ) {
16041599 matchedInputValue = matchedInputValue + value ;
16051600 }
16061601 } ) ;
@@ -1617,19 +1612,12 @@ export const formatTransactions = async ({
16171612 totalOutputValue = totalOutputValue + value ;
16181613 Array . isArray ( _addresses ) &&
16191614 _addresses . map ( ( address ) => {
1620- if (
1621- addressArray . includes ( address ) ||
1622- changeAddressArray . includes ( address )
1623- ) {
1615+ if ( address in combinedAddressObj ) {
16241616 matchedOutputValue = matchedOutputValue + value ;
16251617 }
16261618 } ) ;
16271619 } ) ;
16281620
1629- if ( ! result ?. txid ) {
1630- return ;
1631- }
1632-
16331621 const txid = result . txid ;
16341622 const type =
16351623 matchedInputValue > matchedOutputValue
0 commit comments