-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Integrate withdrawals with OrangeKit and tBTC SDK
This branch expects two dependencies changes that are not yet merged: - @keep-network/tbtc-v2.ts: keep-network/tbtc-v2#814 - @orangekit/sdk: mezo-org/passport#83
- Loading branch information
Showing
9 changed files
with
211 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { OrangeKitSdk } from "@orangekit/sdk" | ||
|
||
import { AcreContracts } from "../lib/contracts" | ||
|
||
import Tbtc from "./tbtc" | ||
|
||
export default class WithdrawalService { | ||
readonly #contracts: AcreContracts | ||
|
||
readonly #tbtc: Tbtc | ||
|
||
#orangeKitSdk: OrangeKitSdk | ||
|
||
#account: { | ||
bitcoinAddress: string | ||
ethereumAddress: string | ||
publicKey: string | ||
} | ||
|
||
constructor( | ||
contracts: AcreContracts, | ||
tbtc: Tbtc, | ||
orangeKitSdk: OrangeKitSdk, | ||
account: { | ||
bitcoinAddress: string | ||
ethereumAddress: string | ||
publicKey: string | ||
}, | ||
) { | ||
this.#contracts = contracts | ||
this.#tbtc = tbtc | ||
this.#orangeKitSdk = orangeKitSdk | ||
this.#account = account | ||
} | ||
|
||
async initiateWithdrawal( | ||
destinationBitcoinAddress: string, | ||
sharesAmount: bigint, | ||
bitcoinSignMessageFn: (message: string) => Promise<string>, | ||
): Promise<string> { | ||
// Estimate the amount of tBTC released after redeeming stBTC tokens. | ||
// The value is required by the tBTC SDK to determine the tBTC Wallet from | ||
// which the Bitcoin will be bridged to the depositor's Bitcoin address. | ||
const tbtcAmount = await this.#contracts.stBTC.previewRedeem(sharesAmount) | ||
|
||
const requestRedemptionWithOrangeKit = async ( | ||
tbtcRedemptionData: string, | ||
) => { | ||
const safeTxData = | ||
this.#contracts.stBTC.encodeRedeemToBitcoinFunctionData( | ||
sharesAmount, | ||
tbtcRedemptionData, | ||
) | ||
|
||
const transactionHash = await this.#orangeKitSdk.sendTransaction( | ||
this.#contracts.stBTC.getAddress(), | ||
"0x0", | ||
safeTxData, | ||
this.#account.bitcoinAddress, | ||
this.#account.publicKey, | ||
bitcoinSignMessageFn, | ||
) | ||
|
||
// TODO: CHANGE RESULT | ||
return transactionHash | ||
} | ||
|
||
return this.#tbtc.initiateRedemption( | ||
this.#account.ethereumAddress, | ||
destinationBitcoinAddress, | ||
tbtcAmount, | ||
requestRedemptionWithOrangeKit, | ||
) | ||
} | ||
} |