-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This changeset adds redemptions to the monitoring system. Specifically, two new system events are supported now: ### Redemption requested An *informational system event* indicating that a new redemption was requested from the on-chain Bridge contract. This event is directly sent to Discord as a notification that does not require any action. ### Large redemption requested A *warning system event* indicating that a large redemption was requested from the on-chain Bridge contract. This event is sent to Sentry hub and should get team’s attention. The default action is making sure that the redemption is not a result of a malicious action, and if not, that the redemption is handled correctly by the system.
- Loading branch information
1 parent
34a7b09
commit 8e1f06e
Showing
7 changed files
with
140 additions
and
28 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
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
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,23 @@ | ||
import { context, Environment } from "./context" | ||
|
||
import type { BitcoinTransactionHash, Hex } from "@keep-network/tbtc-v2.ts" | ||
|
||
const ethTxUrlPrefixMapping = { | ||
[Environment.Mainnet]: "https://etherscan.io/tx", | ||
[Environment.Testnet]: "https://goerli.etherscan.io/tx", | ||
} | ||
|
||
export function createEthTxUrl(txHash: Hex) { | ||
return `${ | ||
ethTxUrlPrefixMapping[context.environment] | ||
}/${txHash.toPrefixedString()}` | ||
} | ||
|
||
const btcTxUrlPrefixMapping = { | ||
[Environment.Mainnet]: "https://mempool.space/tx", | ||
[Environment.Testnet]: "https://mempool.space/testnet/tx", | ||
} | ||
|
||
export function createBtcTxUrl(txHash: BitcoinTransactionHash) { | ||
return `${btcTxUrlPrefixMapping[context.environment]}/${txHash.toString()}` | ||
} |
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
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
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
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,88 @@ | ||
import { BigNumber } from "ethers" | ||
|
||
import { context } from "./context" | ||
import { SystemEventType } from "./system-event" | ||
import { satsToRoundedBTC } from "./deposit-monitor" | ||
import { createEthTxUrl } from "./block-explorer" | ||
|
||
import type { RedemptionRequestedEvent as RedemptionRequestedChainEvent } from "@keep-network/tbtc-v2.ts/dist/src/redemption" | ||
import type { Bridge } from "@keep-network/tbtc-v2.ts/dist/src/chain" | ||
import type { Monitor as SystemEventMonitor, SystemEvent } from "./system-event" | ||
|
||
const RedemptionRequested = ( | ||
chainEvent: RedemptionRequestedChainEvent | ||
): SystemEvent => { | ||
const ethRequestTxHashURL = createEthTxUrl(chainEvent.transactionHash) | ||
|
||
return { | ||
title: "Redemption requested", | ||
type: SystemEventType.Informational, | ||
data: { | ||
walletPublicKeyHash: chainEvent.walletPublicKeyHash, | ||
redeemerOutputScript: chainEvent.redeemerOutputScript, | ||
requestedAmountBTC: satsToRoundedBTC(chainEvent.requestedAmount), | ||
ethRequestTxHash: chainEvent.transactionHash.toPrefixedString(), | ||
ethRequestTxHashURL, | ||
}, | ||
block: chainEvent.blockNumber, | ||
} | ||
} | ||
|
||
const LargeRedemptionRequested = ( | ||
chainEvent: RedemptionRequestedChainEvent | ||
): SystemEvent => { | ||
const ethRequestTxHashURL = createEthTxUrl(chainEvent.transactionHash) | ||
|
||
return { | ||
title: "Large redemption requested", | ||
type: SystemEventType.Warning, | ||
data: { | ||
walletPublicKeyHash: chainEvent.walletPublicKeyHash, | ||
redeemerOutputScript: chainEvent.redeemerOutputScript, | ||
requestedAmountBTC: satsToRoundedBTC(chainEvent.requestedAmount), | ||
ethRequestTxHash: chainEvent.transactionHash.toPrefixedString(), | ||
ethRequestTxHashURL, | ||
}, | ||
block: chainEvent.blockNumber, | ||
} | ||
} | ||
|
||
export class RedemptionMonitor implements SystemEventMonitor { | ||
private bridge: Bridge | ||
|
||
constructor(bridge: Bridge) { | ||
this.bridge = bridge | ||
} | ||
|
||
async check(fromBlock: number, toBlock: number): Promise<SystemEvent[]> { | ||
// eslint-disable-next-line no-console | ||
console.log("running redemption monitor check") | ||
|
||
const chainEvents = await this.bridge.getRedemptionRequestedEvents({ | ||
fromBlock, | ||
toBlock, | ||
}) | ||
|
||
const systemEvents: SystemEvent[] = [] | ||
|
||
// eslint-disable-next-line no-plusplus | ||
for (let i = 0; i < chainEvents.length; i++) { | ||
const chainEvent = chainEvents[i] | ||
|
||
systemEvents.push(RedemptionRequested(chainEvent)) | ||
|
||
if ( | ||
chainEvent.requestedAmount.gt( | ||
BigNumber.from(context.largeRedemptionThresholdSat) | ||
) | ||
) { | ||
systemEvents.push(LargeRedemptionRequested(chainEvent)) | ||
} | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("completed redemption monitor check") | ||
|
||
return systemEvents | ||
} | ||
} |