diff --git a/subgraph/schema.graphql b/subgraph/schema.graphql index f8a8a2359..2e4cb17f8 100644 --- a/subgraph/schema.graphql +++ b/subgraph/schema.graphql @@ -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 { diff --git a/subgraph/src/acrebtc.ts b/subgraph/src/acrebtc.ts index 912a75012..10bee83cb 100644 --- a/subgraph/src/acrebtc.ts +++ b/subgraph/src/acrebtc.ts @@ -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 @@ -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() +} diff --git a/subgraph/src/utils.ts b/subgraph/src/utils.ts index 254425ba1..550f93de3 100644 --- a/subgraph/src/utils.ts +++ b/subgraph/src/utils.ts @@ -4,6 +4,7 @@ import { ethereum, Bytes, BigInt, + log as logger, } from "@graphprotocol/graph-ts" import { DepositOwner, @@ -11,6 +12,7 @@ import { Event, Withdraw, RedemptionKeyToPendingWithdrawal, + MidasWithdrawRequest, } from "../generated/schema" export function getOrCreateDepositOwner(depositOwnerId: Address): DepositOwner { @@ -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( @@ -105,6 +114,26 @@ export function findLogByEventSignatureInLogs( ) if (!log) { + const contractAddresses: string = logs + .map((l: ethereum.Log) => l.address.toHexString()) + .join(";") + + const topics: string = logs + .map((l: ethereum.Log) => + l.topics.map((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`, ) diff --git a/subgraph/src/withdrawal-queue.ts b/subgraph/src/withdrawal-queue.ts index d2b09cc11..afd0966cb 100644 --- a/subgraph/src/withdrawal-queue.ts +++ b/subgraph/src/withdrawal-queue.ts @@ -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" @@ -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( @@ -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() } diff --git a/subgraph/subgraph.mainnet.yaml b/subgraph/subgraph.mainnet.yaml index 9f345f98c..74e59a41b 100644 --- a/subgraph/subgraph.mainnet.yaml +++ b/subgraph/subgraph.mainnet.yaml @@ -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 @@ -51,6 +53,7 @@ dataSources: - Deposit - Withdraw - Event + - MidasWithdrawRequest abis: - name: WithdrawalQueue file: ./abis/WithdrawalQueue.json @@ -58,6 +61,9 @@ dataSources: - 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 @@ -116,6 +122,7 @@ dataSources: - DepositOwner - Deposit - Event + - Withdraw abis: - name: AcreBTC file: ./abis/acreBTC.json @@ -123,4 +130,7 @@ dataSources: - 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 diff --git a/subgraph/subgraph.template.yaml b/subgraph/subgraph.template.yaml index bde09ada6..2099851c8 100644 --- a/subgraph/subgraph.template.yaml +++ b/subgraph/subgraph.template.yaml @@ -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 @@ -51,6 +53,7 @@ dataSources: - Deposit - Withdraw - Event + - MidasWithdrawRequest abis: - name: WithdrawalQueue file: ./abis/WithdrawalQueue.json @@ -58,6 +61,9 @@ dataSources: - 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 @@ -116,6 +122,7 @@ dataSources: - DepositOwner - Deposit - Event + - Withdraw abis: - name: AcreBTC file: ./abis/acreBTC.json @@ -123,4 +130,7 @@ dataSources: - 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