Skip to content

Commit

Permalink
Integrate redemption flow with SDK v2
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kpyszkowski committed Nov 8, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8196b10 commit 2eb2b11
Showing 2 changed files with 25 additions and 46 deletions.
33 changes: 15 additions & 18 deletions src/hooks/tbtc/useFetchRedemptionDetails.ts
Original file line number Diff line number Diff line change
@@ -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,
38 changes: 10 additions & 28 deletions src/threshold-ts/tbtc/index.ts
Original file line number Diff line number Diff line change
@@ -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<string> => {
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<DepositRequest> => {
@@ -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<string> => {
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
)
}

0 comments on commit 2eb2b11

Please sign in to comment.