Skip to content

Commit

Permalink
Merge pull request #4427 from BitGo/BTC-1084
Browse files Browse the repository at this point in the history
feat: fix getUnspentsForAddresses in BlockchairApi
  • Loading branch information
lcovar authored Apr 12, 2024
2 parents 4c1d696 + 48574c4 commit 57def36
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions modules/blockapis/src/impl/BlockchairApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type BlockchairUnspent = {
script_hex: string;
spending_transaction_hash: string;
spending_index: number;
address: string;
};

// https://blockchair.com/api/docs#link_300
Expand Down Expand Up @@ -144,19 +145,24 @@ export class BlockchairApi implements AddressApi, UtxoApi {
if (addr.length > 100) {
throw new Error(`invalid size`);
}
// https://blockchair.com/api/docs#link_300
return (
await this.get<BlockchairResponse<{ utxo: BlockchairUnspent[] }>>(`/dashboards/addresses/${addr.join(',')}`)
).map((body) => {
return addr.flatMap((a) => {
return body.data.utxo.map((unspent): Unspent => {
return {
id: formatOutputId({ txid: unspent.transaction_hash, vout: unspent.index }),
address: a,
value: unspent.value,
};
});
});

const response = await this.get<BlockchairResponse<{ utxo: BlockchairUnspent[] }>>(
`/dashboards/addresses/${addr.join(',')}`
);

return response.map((body) => {
return body.data.utxo
.flatMap((unspent): Unspent | undefined => {
if (addr.includes(unspent.address)) {
return {
id: formatOutputId({ txid: unspent.transaction_hash, vout: unspent.index }),
address: unspent.address,
value: unspent.value,
};
}
return undefined;
})
.filter((unspent): unspent is Unspent => unspent !== undefined);
});
}

Expand Down

0 comments on commit 57def36

Please sign in to comment.