Skip to content

Commit

Permalink
Update initializeWithdrawal fn
Browse files Browse the repository at this point in the history
We want to know when the transaction data are built. We add callback that is triggered after building data.
  • Loading branch information
kkosiorowska committed Oct 30, 2024
1 parent 9e6fbe6 commit 730ce12
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dapp/src/acre-react/hooks/useInitializeWithdraw.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCallback } from "react"
import {
DataBuildedStepCallback,
MessageSignedStepCallback,
OnSignMessageStepCallback,
} from "@acre-btc/sdk/dist/src/lib/redeemer-proxy"
Expand All @@ -11,13 +12,15 @@ export default function useInitializeWithdraw() {
return useCallback(
async (
amount: bigint,
dataBuildedStepCallback?: DataBuildedStepCallback,
onSignMessageStep?: OnSignMessageStepCallback,
messageSignedStep?: MessageSignedStepCallback,
) => {
if (!acre || !isConnected) throw new Error("Account not connected")

return acre.account.initializeWithdrawal(
amount,
dataBuildedStepCallback,
onSignMessageStep,
messageSignedStep,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export default function SignMessageModal() {
}
}, [])

const dataBuildedStepCallback = useCallback(() => Promise.resolve(), [])

const onSignMessageCallback = useCallback(async () => {
setWaitingStatus("signature")
return Promise.race([
Expand Down Expand Up @@ -104,6 +106,7 @@ export default function SignMessageModal() {

const { redemptionKey } = await initializeWithdraw(
amount,
dataBuildedStepCallback,
onSignMessageCallback,
messageSignedCallback,
)
Expand Down
6 changes: 6 additions & 0 deletions sdk/src/lib/redeemer-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { OrangeKitSdk } from "@orangekit/sdk"
import { AcreContracts } from "./contracts"
import { BitcoinProvider } from "./bitcoin"

export type DataBuildedStepCallback = (safeTxData: Hex) => Promise<void>
export type OnSignMessageStepCallback = (messageToSign: string) => Promise<void>
export type MessageSignedStepCallback = (signedMessage: string) => Promise<void>

Expand All @@ -25,6 +26,8 @@ export default class OrangeKitTbtcRedeemerProxy implements TbtcRedeemerProxy {

#sharesAmount: bigint

#dataBuildedStepCallback?: DataBuildedStepCallback

#onSignMessageStepCallback?: OnSignMessageStepCallback

#messageSignedStepCallback?: MessageSignedStepCallback
Expand All @@ -39,6 +42,7 @@ export default class OrangeKitTbtcRedeemerProxy implements TbtcRedeemerProxy {
},
bitcoinProvider: BitcoinProvider,
sharesAmount: bigint,
dataBuildedStepCallback?: DataBuildedStepCallback,
onSignMessageStepCallback?: OnSignMessageStepCallback,
messageSignedStepCallback?: MessageSignedStepCallback,
) {
Expand All @@ -47,6 +51,7 @@ export default class OrangeKitTbtcRedeemerProxy implements TbtcRedeemerProxy {
this.#account = account
this.#bitcoinProvider = bitcoinProvider
this.#sharesAmount = sharesAmount
this.#dataBuildedStepCallback = dataBuildedStepCallback
this.#onSignMessageStepCallback = onSignMessageStepCallback
this.#messageSignedStepCallback = messageSignedStepCallback
}
Expand All @@ -61,6 +66,7 @@ export default class OrangeKitTbtcRedeemerProxy implements TbtcRedeemerProxy {
this.#sharesAmount,
redemptionData,
)
await this.#dataBuildedStepCallback?.(safeTxData)

const transactionHash = await this.#orangeKitSdk.sendTransaction(
`0x${this.#contracts.stBTC.getChainIdentifier().identifierHex}`,
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/modules/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Tbtc from "./tbtc"
import AcreSubgraphApi from "../lib/api/AcreSubgraphApi"
import { DepositStatus } from "../lib/api/TbtcApi"
import OrangeKitTbtcRedeemerProxy, {
DataBuildedStepCallback,
MessageSignedStepCallback,
OnSignMessageStepCallback,
} from "../lib/redeemer-proxy"
Expand Down Expand Up @@ -183,6 +184,8 @@ export default class Account {
/**
* Initializes the withdrawal process.
* @param amount Bitcoin amount to withdraw in 1e8 satoshi precision.
* @param dataBuildedStepCallback A callback triggered after the data
* building step.
* @param onSignMessageStepCallback A callback triggered before the message
* signing step.
* @param messageSignedStepCallback A callback triggered after the message
Expand All @@ -191,6 +194,7 @@ export default class Account {
*/
async initializeWithdrawal(
btcAmount: bigint,
dataBuildedStepCallback?: DataBuildedStepCallback,
onSignMessageStepCallback?: OnSignMessageStepCallback,
messageSignedStepCallback?: MessageSignedStepCallback,
): Promise<{ transactionHash: string; redemptionKey: string }> {
Expand All @@ -209,6 +213,7 @@ export default class Account {
},
this.#bitcoinProvider,
shares,
dataBuildedStepCallback,
onSignMessageStepCallback,
messageSignedStepCallback,
)
Expand Down

0 comments on commit 730ce12

Please sign in to comment.