From 8bf01e055003ba938d72a690ac815efe8b1ec354 Mon Sep 17 00:00:00 2001 From: Rafal Czajkowski Date: Thu, 16 May 2024 18:58:50 +0200 Subject: [PATCH] Update the `Tbtc` module Insted of exposing the `tbtcApi` property we define `getDepositsByOwner` function that fetches the data from tBTC API and converts types. --- sdk/src/lib/api/TbtcApi.ts | 29 ++++++++------------- sdk/src/modules/staking/index.ts | 4 +-- sdk/src/modules/tbtc/Tbtc.ts | 44 +++++++++++++++++++++++++++----- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/sdk/src/lib/api/TbtcApi.ts b/sdk/src/lib/api/TbtcApi.ts index 54579dcf0..542c2ac1d 100644 --- a/sdk/src/lib/api/TbtcApi.ts +++ b/sdk/src/lib/api/TbtcApi.ts @@ -3,11 +3,12 @@ import { BitcoinTxOutpoint, ChainIdentifier, } from "@keep-network/tbtc-v2.ts" -import { ethers } from "ethers" import HttpApi from "./HttpApi" -import { Hex } from "../utils" -type Deposit = { +/** + * Represents the Deposit entity returned by the tBTC API. + */ +type Deposit = { id: string depositKey: string createdAt: number @@ -24,7 +25,7 @@ type Deposit = { referral: number status: DepositStatus txHash: string - initialAmount: NumberType + initialAmount: string } /** @@ -78,9 +79,11 @@ export default class TbtcApi extends HttpApi { } } - async getDepositsByOwner( - depositOwner: ChainIdentifier, - ): Promise[]> { + /** + * @param depositOwner Depositor as EVM-chain identifier. + * @returns All owner deposits, including queued deposits. + */ + async getDepositsByOwner(depositOwner: ChainIdentifier): Promise { const response = await this.getRequest( `deposits/${depositOwner.identifierHex}`, ) @@ -89,17 +92,7 @@ export default class TbtcApi extends HttpApi { const responseData = (await response.json()) as Deposit[] - return responseData.map((deposit) => ({ - ...deposit, - initialAmount: BigInt(deposit.initialAmount), - depositKey: ethers.solidityPackedKeccak256( - ["bytes32", "uint32"], - [ - Hex.from(deposit.txHash).reverse().toPrefixedString(), - deposit.outputIndex, - ], - ), - })) + return responseData } } diff --git a/sdk/src/modules/staking/index.ts b/sdk/src/modules/staking/index.ts index ac0f95797..1c0145892 100644 --- a/sdk/src/modules/staking/index.ts +++ b/sdk/src/modules/staking/index.ts @@ -185,9 +185,7 @@ class StakingModule { subgraphData.map((data) => [data.depositKey, data]), ) - const tbtcData = await this.#tbtc.tbtcApi.getDepositsByOwner( - depositOwnerEvmAddress, - ) + const tbtcData = await this.#tbtc.getDepositsByOwner(depositOwnerEvmAddress) return tbtcData.map((deposit) => { const depositFromSubgraph = initializedOrFinalizedDepositsMap.get( diff --git a/sdk/src/modules/tbtc/Tbtc.ts b/sdk/src/modules/tbtc/Tbtc.ts index a96de07a5..6c0d0f8ce 100644 --- a/sdk/src/modules/tbtc/Tbtc.ts +++ b/sdk/src/modules/tbtc/Tbtc.ts @@ -1,17 +1,21 @@ import { ChainIdentifier, TBTC as TbtcSdk } from "@keep-network/tbtc-v2.ts" -import TbtcApi from "../../lib/api/TbtcApi" +import { ethers } from "ethers" +import TbtcApi, { DepositStatus } from "../../lib/api/TbtcApi" import { BitcoinDepositor } from "../../lib/contracts" import { EthereumNetwork } from "../../lib/ethereum" +import { + Hex, + IEthereumSignerCompatibleWithEthersV5 as EthereumSignerCompatibleWithEthersV5, +} from "../../lib/utils" import Deposit from "./Deposit" -import { IEthereumSignerCompatibleWithEthersV5 as EthereumSignerCompatibleWithEthersV5 } from "../../lib/utils" /** * Represents the tBTC module. */ export default class Tbtc { - public readonly tbtcApi: TbtcApi + readonly #tbtcApi: TbtcApi readonly #tbtcSdk: TbtcSdk @@ -22,7 +26,7 @@ export default class Tbtc { tbtcSdk: TbtcSdk, bitcoinDepositor: BitcoinDepositor, ) { - this.tbtcApi = tbtcApi + this.#tbtcApi = tbtcApi this.#tbtcSdk = tbtcSdk this.#bitcoinDepositor = bitcoinDepositor } @@ -105,10 +109,38 @@ export default class Tbtc { application: "acre", } - const revealSaved: boolean = await this.tbtcApi.saveReveal(revealData) + const revealSaved: boolean = await this.#tbtcApi.saveReveal(revealData) if (!revealSaved) throw new Error("Reveal not saved properly in the database") - return new Deposit(this.tbtcApi, tbtcDeposit, revealData) + return new Deposit(this.#tbtcApi, tbtcDeposit, revealData) + } + + /** + * @param depositOwner Depositor as EVM-chain identifier. + * @returns All owner deposits, including queued deposits. + */ + async getDepositsByOwner(depositOwner: ChainIdentifier): Promise< + { + txHash: string + depositKey: string + initialAmount: bigint + status: DepositStatus + }[] + > { + const deposits = await this.#tbtcApi.getDepositsByOwner(depositOwner) + + return deposits.map((deposit) => ({ + status: deposit.status, + initialAmount: BigInt(deposit.initialAmount), + depositKey: ethers.solidityPackedKeccak256( + ["bytes32", "uint32"], + [ + Hex.from(deposit.txHash).reverse().toPrefixedString(), + deposit.outputIndex, + ], + ), + txHash: deposit.txHash, + })) } }