From 2eb2b11a53ccb8bf9880e5c546e407b9b1471522 Mon Sep 17 00:00:00 2001 From: Kamil Pyszkowski Date: Wed, 8 Nov 2023 18:18:28 +0100 Subject: [PATCH] Integrate redemption flow with SDK v2 Attempted to integrate redemption flow. There are still some changes to apply. They are not introduced because I couldn't find stable solution. Everything is explained in the pull request. --- src/hooks/tbtc/useFetchRedemptionDetails.ts | 33 ++++++++---------- src/threshold-ts/tbtc/index.ts | 38 ++++++--------------- 2 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/hooks/tbtc/useFetchRedemptionDetails.ts b/src/hooks/tbtc/useFetchRedemptionDetails.ts index ff51a635c..05af6678c 100644 --- a/src/hooks/tbtc/useFetchRedemptionDetails.ts +++ b/src/hooks/tbtc/useFetchRedemptionDetails.ts @@ -1,5 +1,6 @@ import { BigNumber } from "ethers" import { useEffect, useState } from "react" +import { Hex } from "tbtc-sdk-v2" import { useThreshold } from "../../contexts/ThresholdContext" import { isValidType, @@ -79,10 +80,7 @@ export const useFetchRedemptionDetails = ( // reduce the number of records - any user can request redemption for // the same wallet. const redemptionRequestedEvent = ( - await threshold.tbtc.getRedemptionRequestedEvents({ - walletPublicKeyHash, - redeemer, - }) + await threshold.tbtc.sdk.tbtcContracts.bridge.getRedemptionRequestedEvents() ).find( (event) => // It's not possible that the redemption request with the same @@ -104,10 +102,12 @@ export const useFetchRedemptionDetails = ( // // In that case we must know exactly which redemption request we // want to fetch. - event.txHash === redemptionRequestedTxHash && + + event.transactionHash.toPrefixedString() === + redemptionRequestedTxHash && threshold.tbtc.buildRedemptionKey( - event.walletPublicKeyHash, - event.redeemerOutputScript + event.walletPublicKeyHash.toString(), + event.redeemerOutputScript.toString() ) === redemptionKey ) @@ -121,12 +121,7 @@ export const useFetchRedemptionDetails = ( // We need to check if the redemption has `pending` or `timedOut` status. const { isPending, isTimedOut, requestedAt } = - await threshold.tbtc.getRedemptionRequest( - threshold.tbtc.buildRedemptionKey( - walletPublicKeyHash, - redeemerOutputScript - ) - ) + await threshold.tbtc.getRedemptionRequest(redemptionKey) // Find the transaction hash where the timeout was reported by // scanning the `RedemptionTimedOut` event by the `walletPubKeyHash` @@ -161,9 +156,10 @@ export const useFetchRedemptionDetails = ( ) { setRedemptionData({ requestedAmount: fromSatoshiToTokenPrecision( - redemptionRequestedEvent.amount + redemptionRequestedEvent.requestedAmount ).toString(), - redemptionRequestedTxHash: redemptionRequestedEvent.txHash, + redemptionRequestedTxHash: + redemptionRequestedEvent.transactionHash.toString(), redemptionCompletedTxHash: undefined, requestedAt: requestedAt, redemptionTimedOutTxHash: timedOutTxHash, @@ -197,7 +193,7 @@ export const useFetchRedemptionDetails = ( const redemptionBitcoinTransfer = await findRedemptionInBitcoinTx( redemptionBitcoinTxHash, redemptionCompletedBlockNumber, - redemptionRequestedEvent.redeemerOutputScript + redemptionRequestedEvent.redeemerOutputScript.toString() ) if (!redemptionBitcoinTransfer) continue @@ -207,10 +203,11 @@ export const useFetchRedemptionDetails = ( setRedemptionData({ requestedAmount: fromSatoshiToTokenPrecision( - redemptionRequestedEvent.amount + redemptionRequestedEvent.requestedAmount ).toString(), receivedAmount, - redemptionRequestedTxHash: redemptionRequestedEvent.txHash, + redemptionRequestedTxHash: + redemptionRequestedEvent.transactionHash.toString(), redemptionCompletedTxHash: { chain: txHash, bitcoin: redemptionBitcoinTxHash, diff --git a/src/threshold-ts/tbtc/index.ts b/src/threshold-ts/tbtc/index.ts index e33ea15b4..1bd5c3a0b 100644 --- a/src/threshold-ts/tbtc/index.ts +++ b/src/threshold-ts/tbtc/index.ts @@ -165,7 +165,6 @@ export class TBTC implements ITBTC { bitcoinConfig.client && !bitcoinConfig.credentials if (hasMockedBitcoinClient) { - console.log("Using custom bitcoin client") const depositorAddress = await ethereumAddressFromSigner(signer) const ethereumNetwork = await ethereumNetworkFromSigner(signer) @@ -184,7 +183,6 @@ export class TBTC implements ITBTC { depositorAddress && this._sdk?.deposits.setDefaultDepositor(depositorAddress) - console.log({ tbtcContracts, bitcoinClient }) return } @@ -319,16 +317,8 @@ export class TBTC implements ITBTC { revealDeposit = async (utxo: BitcoinUtxo): Promise => { const { value, ...transactionOutpoint } = utxo const chainHash = await this.deposit.initiateMinting(transactionOutpoint) - console.log({ chainHash }) return chainHash.toPrefixedString() - // return await tBTCRevealDeposit( - // utxo, - // depositScriptParameters, - // this._bitcoinClient, - // this._bridge, - // getChainIdentifier(this._tbtcVault.address) - // ) } getRevealedDeposit = async (utxo: BitcoinUtxo): Promise => { @@ -556,8 +546,8 @@ export class TBTC implements ITBTC { const { timestamp: eventTimestamp } = await this._bridgeContract.provider.getBlock(event.blockNumber) const redemptionKey = this.buildRedemptionKey( - event.walletPublicKeyHash, - event.redeemerOutputScript + Hex.from(event.walletPublicKeyHash).toString(), + Hex.from(event.redeemerOutputScript).toString() ) const redemptionDetails = await this.getRedemptionRequest(redemptionKey) @@ -624,18 +614,13 @@ export class TBTC implements ITBTC { btcAddress: string, amount: BigNumberish ): Promise => { - if (this._isValidBitcoinAddressForRedemption(btcAddress)) { - throw new Error( - "Unsupported BTC address! Supported type addresses are: P2PKH, P2WPKH, P2SH, P2WSH." + const { targetChainTxHash } = + await this._sdk!.redemptions.requestRedemption( + btcAddress, + BigNumber.from(amount) ) - } - - const { targetChainTxHash } = await this.sdk.redemptions.requestRedemption( - btcAddress, - BigNumber.from(amount) - ) - return targetChainTxHash.toString() + return targetChainTxHash.toPrefixedString() } findWalletForRedemption = async ( @@ -701,12 +686,9 @@ export class TBTC implements ITBTC { walletPublicKeyHash: string, redeemerOutputScript: string ) => { - return utils.solidityKeccak256( - ["bytes32", "bytes20"], - [ - utils.solidityKeccak256(["bytes"], [redeemerOutputScript]), - walletPublicKeyHash, - ] + return EthereumBridge.buildRedemptionKey( + walletPublicKeyHash, + redeemerOutputScript ) }