Skip to content
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

Merged
merged 7 commits into from
Jul 12, 2023

Conversation

lukasz-zimnoch
Copy link
Member

Refs: threshold-network/token-dashboard#553

Here we improve the logic of findWalletForRedemption function to handle the corner case when a wallet's main UTXO known to the Bridge was actually spent on Bitcoin but that change was not yet reflected to the Bridge. So far, findWalletForRedemption relied on findAllUnspentTransactionOutputs which does not return spent UTXOs so determining the actual main UTXO was not possible in the aforementioned corner case. Now, the findWalletForRedemption looks for recent wallet transaction history on Bitcoin and tries to match the actual main UTXO based on that. This change will allow submitting new redemption requests without downtime, even if the target wallet is performing an action at the moment.

Here we expose a function allowing to get the transaction history for given
Bitcoin address.
Here we expose a function allowing to determine the wallet main UTXO based
on the wallet recent transactions history and the main UTXO hash stored on
the Bridge contract.
The `findWalletForRedemption` function should rely on `determineWalletMainUtxo`
wallet while seeking for current wallet main UTXO. Otherwise, it may not
find the main UTXO in case there is a wallet state drift between Bitcoin
and Ethereum chains.
Copy link
Member

@pdyraga pdyraga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good to me. I added some minor comments.

Leaving it in @r-czajkowski's hands.

* a specific number of last transaction. For example, limit = 5 will
* return only the last 5 transactions for the given address.
*/
getTransactionHistory(address: string, limit?: number): Promise<Transaction[]>
Copy link
Member

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.

historyItems.length > limit
) {
historyItems = historyItems.slice(-limit)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting blockchain_scripthash_getHistory to accept limit as a parameter but yeah, I see in the linked docs it is not possible. 😞

// eslint-disable-next-line camelcase
type HistoryItem = { height: number; tx_hash: string }

let historyItems: HistoryItem[] = await this.withBackoffRetrier<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking: Instead of having await, I think we could use .then to chain promises together.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think async/await style leads to more readable code and is generally preferred over then. It is also widely used in our codebase and I think mixing both styles is not necessarily the best idea.

typescript/src/wallet.ts Show resolved Hide resolved
@@ -108,6 +108,28 @@ describe("Electrum", () => {
})
})

describe("getTransactionHistory", () => {
it("should return proper transaction history for the given address", async () => {
// https://live.blockcypher.com/btc-testnet/address/tb1qumuaw3exkxdhtut0u85latkqfz4ylgwstkdzsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming: this is an address controlled by us and it will not execute any new transactions in the future? 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it is :)

typescript/test/wallet.test.ts Show resolved Hide resolved
Copy link
Contributor

@r-czajkowski r-czajkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left minor comments. Overall LGTM 🚀

typescript/src/electrum.ts Outdated Show resolved Hide resolved
typescript/src/wallet.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@r-czajkowski r-czajkowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@r-czajkowski r-czajkowski merged commit ec34e17 into main Jul 12, 2023
@r-czajkowski r-czajkowski deleted the improve-find-wallet-redemption branch July 12, 2023 09:49
@lukasz-zimnoch lukasz-zimnoch added this to the typescript/v1.3.0 milestone Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔌 typescript TypeScript library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants