Skip to content

Commit

Permalink
Update the Tbtc module
Browse files Browse the repository at this point in the history
Insted of exposing the `tbtcApi` property we define `getDepositsByOwner`
function that fetches the data from tBTC API and converts types.
  • Loading branch information
r-czajkowski committed May 16, 2024
1 parent 74a5647 commit 300dd16
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
29 changes: 11 additions & 18 deletions sdk/src/lib/api/TbtcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NumberType = string> = {
/**
* Represents the Deposit entity returned by the tBTC API.
*/
type Deposit = {
id: string
depositKey: string
createdAt: number
Expand All @@ -24,7 +25,7 @@ type Deposit<NumberType = string> = {
referral: number
status: DepositStatus
txHash: string
initialAmount: NumberType
initialAmount: string
}

/**
Expand Down Expand Up @@ -78,9 +79,11 @@ export default class TbtcApi extends HttpApi {
}
}

async getDepositsByOwner(
depositOwner: ChainIdentifier,
): Promise<Deposit<bigint>[]> {
/**
* @param depositOwner Depositor as EVM-chain identifier.
* @returns All owner deposits, including queued deposits.
*/
async getDepositsByOwner(depositOwner: ChainIdentifier): Promise<Deposit[]> {
const response = await this.getRequest(
`deposits/${depositOwner.identifierHex}`,
)
Expand All @@ -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
}
}

Expand Down
4 changes: 1 addition & 3 deletions sdk/src/modules/staking/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
44 changes: 38 additions & 6 deletions sdk/src/modules/tbtc/Tbtc.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -22,7 +26,7 @@ export default class Tbtc {
tbtcSdk: TbtcSdk,
bitcoinDepositor: BitcoinDepositor,
) {
this.tbtcApi = tbtcApi
this.#tbtcApi = tbtcApi
this.#tbtcSdk = tbtcSdk
this.#bitcoinDepositor = bitcoinDepositor
}
Expand Down Expand Up @@ -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,
}))
}
}

0 comments on commit 300dd16

Please sign in to comment.