-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And expose the `getDepositsByOwner` function that returns all deposits for a given depositor.
- Loading branch information
1 parent
8420275
commit b886fb6
Showing
1 changed file
with
79 additions
and
0 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
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, | ||
} | ||
}) | ||
} | ||
} |