Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@ type Withdraw implements ActivityData @entity {
depositOwner: DepositOwner!
events: [Event!] @derivedFrom(field: "activity")
amount: BigInt
shares: BigInt
requestedAmount: BigInt
amountToRedeem: BigInt
bitcoinTransactionId: String
redeemerOutputScript: String
receiver: String
midasWithdrawRequest: MidasWithdrawRequest @derivedFrom(field: "withdraw")
}

type MidasWithdrawRequest @entity {
id: ID!
withdraw: Withdraw!
}

type Event @entity {
Expand Down
28 changes: 27 additions & 1 deletion subgraph/src/acrebtc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Address, dataSource } from "@graphprotocol/graph-ts"
import { Deposit as DepositEvent } from "../generated/AcreBTC/AcreBTC"
import {
Deposit as DepositEvent,
RedemptionRequested as RedemptionRequestedEvent,
} from "../generated/AcreBTC/AcreBTC"
import {
getOrCreateDepositOwner,
getOrCreateDeposit,
getOrCreateEvent,
getOrCreateWithdraw,
} from "./utils"

// eslint-disable-next-line import/prefer-default-export
Expand Down Expand Up @@ -38,3 +42,25 @@ export function handleDeposit(event: DepositEvent): void {
depositEntity.save()
eventEntity.save()
}

export function handleRedemptionRequested(
event: RedemptionRequestedEvent,
): void {
const depositOwnerEntity = getOrCreateDepositOwner(event.params.owner)

const withdrawEntity = getOrCreateWithdraw(event.params.requestId.toString())
withdrawEntity.depositOwner = depositOwnerEntity.id
withdrawEntity.shares = event.params.shares

const redemptionRequestedEvent = getOrCreateEvent(
`${event.transaction.hash.toHexString()}_RedemptionRequested`,
)

redemptionRequestedEvent.activity = withdrawEntity.id
redemptionRequestedEvent.timestamp = event.block.timestamp
redemptionRequestedEvent.type = "Requested"

depositOwnerEntity.save()
withdrawEntity.save()
redemptionRequestedEvent.save()
}
43 changes: 36 additions & 7 deletions subgraph/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
ethereum,
Bytes,
BigInt,
log as logger,
} from "@graphprotocol/graph-ts"
import {
DepositOwner,
Deposit,
Event,
Withdraw,
RedemptionKeyToPendingWithdrawal,
MidasWithdrawRequest,
} from "../generated/schema"

export function getOrCreateDepositOwner(depositOwnerId: Address): DepositOwner {
Expand Down Expand Up @@ -69,28 +71,35 @@ export function getOrCreateWithdraw(id: string): Withdraw {
return withdraw
}

export function getOrCreateMidasWithdrawRequest(
id: string,
): MidasWithdrawRequest {
let midasWithdrawRequest = MidasWithdrawRequest.load(id)
if (!midasWithdrawRequest) {
midasWithdrawRequest = new MidasWithdrawRequest(id)
}

return midasWithdrawRequest
}

export function getLogByEventSignatureInLogs(
logs: ethereum.Log[],
eventSignature: ByteArray,
contractAddress: Address,
): ethereum.Log | null {
let logIndex = -1
for (let i = 0; i < logs.length; i += 1) {
const receiptLog = logs[i]

if (
receiptLog.address.equals(contractAddress) &&
receiptLog.topics.length > 0 &&
receiptLog.topics[0].equals(eventSignature)
) {
logIndex = i
return receiptLog
}
}

if (logIndex < 0) {
return null
}

return logs[logIndex]
return null
}

export function findLogByEventSignatureInLogs(
Expand All @@ -105,6 +114,26 @@ export function findLogByEventSignatureInLogs(
)

if (!log) {
const contractAddresses: string = logs
.map<string>((l: ethereum.Log) => l.address.toHexString())
.join(";")

const topics: string = logs
.map<string>((l: ethereum.Log) =>
l.topics.map<string>((t: Bytes) => t.toHexString()).join(";"),
)
.join(";")

logger.error(
"Cannot find event (signature : {}, contract address : {}) in transaction logs with topics: [{}] and contract addresses: [{}]",
[
eventSignature.toHexString(),
contractAddress.toHexString(),
topics,
contractAddresses,
],
)

throw new Error(
`Cannot find event (signature: ${eventSignature.toHexString()}) in transaction logs`,
)
Expand Down
46 changes: 40 additions & 6 deletions subgraph/src/withdrawal-queue.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { log } from "@graphprotocol/graph-ts"
import { Withdraw } from "../generated/schema"
import {
RedeemRequested,
RedeemAndBridgeRequested,
RequestRedeemAndBridgeCall,
} from "../generated/WithdrawalQueue/WithdrawalQueue"
import {
getOrCreateDepositOwner,
getOrCreateEvent,
getOrCreateMidasWithdrawRequest,
getOrCreateWithdraw,
} from "./utils"

Expand All @@ -18,25 +20,31 @@ export function handleRedeemAndBridgeRequested(
): void {
const ownerEntity = getOrCreateDepositOwner(event.params.redeemer)

const withdraw = getOrCreateWithdraw(event.params.requestId.toString())
const withdrawEntity = getOrCreateWithdraw(event.params.requestId.toString())

withdraw.depositOwner = ownerEntity.id
withdraw.requestedAmount = event.params.tbtcAmount.plus(
withdrawEntity.depositOwner = ownerEntity.id
withdrawEntity.requestedAmount = event.params.tbtcAmount.plus(
event.params.exitFeeInTbtc,
)
withdraw.amountToRedeem = event.params.tbtcAmount
withdrawEntity.amountToRedeem = event.params.tbtcAmount

const redemptionRequestedEvent = getOrCreateEvent(
`${event.transaction.hash.toHexString()}_RedeemAndBridgeRequested`,
)

redemptionRequestedEvent.activity = withdraw.id
redemptionRequestedEvent.timestamp = event.block.timestamp
redemptionRequestedEvent.activity = withdrawEntity.id
redemptionRequestedEvent.type = "Requested"

const midasWithdrawRequestEntity = getOrCreateMidasWithdrawRequest(
event.params.midasRequestId.toString(),
)
midasWithdrawRequestEntity.withdraw = withdrawEntity.id

ownerEntity.save()
withdraw.save()
withdrawEntity.save()
redemptionRequestedEvent.save()
midasWithdrawRequestEntity.save()
}

export function handleRequestRedeemAndBridgeCall(
Expand All @@ -60,6 +68,32 @@ export function handleRequestRedeemAndBridgeCall(
}

withdrawEntity.redeemerOutputScript = redeemerOutputScript
// eslint-disable-next-line no-underscore-dangle
withdrawEntity.shares = call.inputs._shares

withdrawEntity.save()
}

export function handleRedeemRequested(event: RedeemRequested): void {
const withdrawEntity = getOrCreateWithdraw(event.params.requestId.toString())

withdrawEntity.receiver = event.params.receiver.toHexString()
withdrawEntity.amount = event.params.tbtcAmount

const redemptionRequestedEvent = getOrCreateEvent(
`${event.transaction.hash.toHexString()}_RedeemRequested`,
)

redemptionRequestedEvent.activity = withdrawEntity.id
redemptionRequestedEvent.timestamp = event.block.timestamp
redemptionRequestedEvent.type = "Requested"

const midasWithdrawRequestEntity = getOrCreateMidasWithdrawRequest(
event.params.midasRequestId.toString(),
)
midasWithdrawRequestEntity.withdraw = withdrawEntity.id

withdrawEntity.save()
redemptionRequestedEvent.save()
midasWithdrawRequestEntity.save()
}
10 changes: 10 additions & 0 deletions subgraph/subgraph.mainnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dataSources:
abis:
- name: BitcoinDepositor
file: ./abis/BitcoinDepositor.json
- name: TbtcBridge
file: ./abis/TbtcBridge.json
eventHandlers:
- event: DepositInitialized(indexed uint256,indexed address,indexed address,uint256)
handler: handleDepositInitialized
Expand All @@ -51,13 +53,17 @@ dataSources:
- Deposit
- Withdraw
- Event
- MidasWithdrawRequest
abis:
- name: WithdrawalQueue
file: ./abis/WithdrawalQueue.json
eventHandlers:
- event: RedeemAndBridgeRequested(indexed uint256,indexed address,indexed uint256,uint256,uint256,uint256)
handler: handleRedeemAndBridgeRequested
receipt: true
- event: RedeemRequested(indexed uint256,indexed address,indexed uint256,uint256,uint256)
handler: handleRedeemRequested
receipt: true
callHandlers:
- function: requestRedeemAndBridge(uint256,address,bytes,uint256)
handler: handleRequestRedeemAndBridgeCall
Expand Down Expand Up @@ -116,11 +122,15 @@ dataSources:
- DepositOwner
- Deposit
- Event
- Withdraw
abis:
- name: AcreBTC
file: ./abis/acreBTC.json
eventHandlers:
- event: Deposit(indexed address,indexed address,uint256,uint256)
handler: handleDeposit
receipt: true
- event: RedemptionRequested(indexed uint256,indexed address,indexed address,address,uint256)
handler: handleRedemptionRequested
receipt: true
file: ./src/acrebtc.ts
10 changes: 10 additions & 0 deletions subgraph/subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dataSources:
abis:
- name: BitcoinDepositor
file: ./abis/BitcoinDepositor.json
- name: TbtcBridge
file: ./abis/TbtcBridge.json
eventHandlers:
- event: DepositInitialized(indexed uint256,indexed address,indexed address,uint256)
handler: handleDepositInitialized
Expand All @@ -51,13 +53,17 @@ dataSources:
- Deposit
- Withdraw
- Event
- MidasWithdrawRequest
abis:
- name: WithdrawalQueue
file: ./abis/WithdrawalQueue.json
eventHandlers:
- event: RedeemAndBridgeRequested(indexed uint256,indexed address,indexed uint256,uint256,uint256,uint256)
handler: handleRedeemAndBridgeRequested
receipt: true
- event: RedeemRequested(indexed uint256,indexed address,indexed uint256,uint256,uint256)
handler: handleRedeemRequested
receipt: true
callHandlers:
- function: requestRedeemAndBridge(uint256,address,bytes,uint256)
handler: handleRequestRedeemAndBridgeCall
Expand Down Expand Up @@ -116,11 +122,15 @@ dataSources:
- DepositOwner
- Deposit
- Event
- Withdraw
abis:
- name: AcreBTC
file: ./abis/acreBTC.json
eventHandlers:
- event: Deposit(indexed address,indexed address,uint256,uint256)
handler: handleDeposit
receipt: true
- event: RedemptionRequested(indexed uint256,indexed address,indexed address,address,uint256)
handler: handleRedemptionRequested
receipt: true
file: ./src/acrebtc.ts
Loading