Skip to content

Commit

Permalink
Create AcreSubgraphApi
Browse files Browse the repository at this point in the history
And expose the `getDepositsByOwner` function that returns all deposits
for a given depositor.
  • Loading branch information
r-czajkowski committed May 15, 2024
1 parent 8420275 commit b886fb6
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions sdk/src/lib/api/AcreSubgraphApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ChainIdentifier } from "@keep-network/tbtc-v2.ts"
import HttpApi from "./HttpApi"
import { DepositStatus } from "./TbtcApi"

type DepositDataResponse = {
data: {
deposits: {
id: string
bitcoinTransactionId: string
amountToDeposit: string
initialDepositAmount: string
events: { type: "Initialized" | "Finalized" }[]
}[]
}
}

type Deposit = {
depositKey: string
txHash: string
amount: bigint
status: DepositStatus
}

export default class AcreSubgraphApi extends HttpApi {
async getDepositsByOwner(
depositOwnerId: ChainIdentifier,
): Promise<Deposit[]> {
const query = `
query {
deposits(
where: {depositOwner_: {id: "0x${depositOwnerId.identifierHex}"}}
) {
id
bitcoinTransactionId
initialDepositAmount
events {
type
}
amountToDeposit
}
}`
const response = await this.postRequest(
"",
{ query },
{ credentials: undefined },
)

if (!response.ok) {
throw new Error(
`Could not get deposits by deposit owner: ${response.status}`,
)
}

const responseData = (await response.json()) as DepositDataResponse

return responseData.data.deposits.map((deposit) => {
const {
bitcoinTransactionId: txHash,
amountToDeposit,
initialDepositAmount,
events,
id,
} = deposit

// The subgraph indexes only initialized or finalized deposits.
const status = events.some(({ type }) => type === "Finalized")
? DepositStatus.Finalized
: DepositStatus.Initialized

return {
depositKey: id,
txHash,
amount: BigInt(amountToDeposit ?? initialDepositAmount),
type: "deposit",
status,
}
})
}
}

0 comments on commit b886fb6

Please sign in to comment.