Skip to content

Commit

Permalink
Update the TbtcApi class
Browse files Browse the repository at this point in the history
Expose the `getDepositsByOwner` function that returns all deposits for a
given deposit. The main difference between this function and from
`AcreSubgraphApi` class is that tbtc api stores queued deposits - this
means they have not yet been initialized or finalized. Subgraph only
indexes initialized or finalized deposits.
  • Loading branch information
r-czajkowski committed May 15, 2024
1 parent b886fb6 commit d8d05c2
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion sdk/src/lib/api/TbtcApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import { BitcoinTxHash, BitcoinTxOutpoint } from "@keep-network/tbtc-v2.ts"
import {
BitcoinTxHash,
BitcoinTxOutpoint,
ChainIdentifier,
} from "@keep-network/tbtc-v2.ts"
import { ethers } from "ethers"
import HttpApi from "./HttpApi"
import { Hex } from "../utils"

type Deposit<NumberType = string> = {
id: string
depositKey: string
createdAt: number
outputIndex: number
owner: string
receipt: {
blindingFactor: string
depositor: string
extraData: string
refundLocktime: string
refundPublicKeyHash: string
walletPublicKeyHash: string
}
referral: number
status: DepositStatus
txHash: string
initialAmount: NumberType
}

/**
* Represents a class for integration with tBTC API.
Expand Down Expand Up @@ -51,6 +77,30 @@ export default class TbtcApi extends HttpApi {
},
}
}

async getDepositsByOwner(
depositOwner: ChainIdentifier,
): Promise<Deposit<bigint>[]> {
const response = await this.getRequest(
`deposits/${depositOwner.identifierHex}`,
)
if (!response.ok)
throw new Error(`Failed to fetch deposits: ${response.status}`)

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,
],
),
}))
}
}

/**
Expand Down

0 comments on commit d8d05c2

Please sign in to comment.