-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve find wallet for redemption logic #651
Changes from all commits
97d56b3
4994672
10ab6da
0e369b2
056cac5
7b0b9bd
f2e5b9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import bcoin from "bcoin" | |
import pTimeout from "p-timeout" | ||
import { | ||
Client as BitcoinClient, | ||
createOutputScriptFromAddress, | ||
RawTransaction, | ||
Transaction, | ||
TransactionHash, | ||
|
@@ -232,7 +233,7 @@ export class Client implements BitcoinClient { | |
): Promise<UnspentTransactionOutput[]> { | ||
return this.withElectrum<UnspentTransactionOutput[]>( | ||
async (electrum: Electrum) => { | ||
const script = bcoin.Script.fromAddress(address).toRaw().toString("hex") | ||
const script = createOutputScriptFromAddress(address).toString() | ||
|
||
// eslint-disable-next-line camelcase | ||
type UnspentOutput = { tx_pos: number; value: number; tx_hash: string } | ||
|
@@ -253,6 +254,58 @@ export class Client implements BitcoinClient { | |
) | ||
} | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* @see {BitcoinClient#getTransactionHistory} | ||
*/ | ||
getTransactionHistory( | ||
address: string, | ||
limit?: number | ||
): Promise<Transaction[]> { | ||
return this.withElectrum<Transaction[]>(async (electrum: Electrum) => { | ||
const script = createOutputScriptFromAddress(address).toString() | ||
|
||
// eslint-disable-next-line camelcase | ||
type HistoryItem = { height: number; tx_hash: string } | ||
|
||
let historyItems: HistoryItem[] = await this.withBackoffRetrier< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpicking: Instead of having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
HistoryItem[] | ||
>()(async () => { | ||
return await electrum.blockchain_scripthash_getHistory( | ||
computeScriptHash(script) | ||
) | ||
}) | ||
|
||
// According to https://electrumx.readthedocs.io/en/latest/protocol-methods.html#blockchain-scripthash-get-history | ||
// unconfirmed items living in the mempool are appended at the end of the | ||
// returned list and their height value is either -1 or 0. That means | ||
// we need to take all items with height >0 to obtain a confirmed txs | ||
// history. | ||
historyItems = historyItems.filter((item) => item.height > 0) | ||
|
||
// The list returned from blockchain.scripthash.get_history is sorted by | ||
// the block height in the ascending order though we are sorting it | ||
// again just in case (e.g. API contract changes). | ||
historyItems = historyItems.sort( | ||
(item1, item2) => item1.height - item2.height | ||
) | ||
|
||
if ( | ||
typeof limit !== "undefined" && | ||
limit > 0 && | ||
historyItems.length > limit | ||
) { | ||
historyItems = historyItems.slice(-limit) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was expecting |
||
|
||
const transactions = historyItems.map((item) => | ||
this.getTransaction(TransactionHash.from(item.tx_hash)) | ||
) | ||
|
||
return Promise.all(transactions) | ||
}) | ||
} | ||
|
||
// eslint-disable-next-line valid-jsdoc | ||
/** | ||
* @see {BitcoinClient#getTransaction} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea for the future: we could add a parameter specifying the required number of confirmations.
The "confirmed transaction" is a fuzzy term. In this context, it's a transaction with 1 confirmation but in some other contexts, we do not consider the transaction as confirmed if it has less than 6 confirmations.