-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement speedup v3 events (#341)
* feat: implement speedup v3 events * Fix bugs * Fix import * Fix --------- Co-authored-by: amateima <[email protected]>
- Loading branch information
Showing
13 changed files
with
201 additions
and
25 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
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
76 changes: 76 additions & 0 deletions
76
src/modules/scraper/adapter/messaging/SpeedUpEventsV3Consumer.ts
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,76 @@ | ||
import { OnQueueFailed, Process, Processor } from "@nestjs/bull"; | ||
import { Logger } from "@nestjs/common"; | ||
import { Job } from "bull"; | ||
import { DepositFilledDateQueueMessage, SpeedUpEventsV3QueueMessage, ScraperQueue } from "."; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { Deposit, RequestedSpeedUpDepositTxV3 } from "../../../deposit/model/deposit.entity"; | ||
import { Repository } from "typeorm"; | ||
import { ScraperQueuesService } from "../../service/ScraperQueuesService"; | ||
import BigNumber from "bignumber.js"; | ||
|
||
@Processor(ScraperQueue.SpeedUpEventsV3) | ||
export class SpeedUpEventsV3Consumer { | ||
private logger = new Logger(SpeedUpEventsV3Consumer.name); | ||
|
||
constructor( | ||
@InjectRepository(Deposit) private depositRepository: Repository<Deposit>, | ||
private scraperQueuesService: ScraperQueuesService, | ||
) {} | ||
|
||
@Process() | ||
private async process(job: Job<SpeedUpEventsV3QueueMessage>) { | ||
const { depositId, depositSourceChainId } = job.data; | ||
const deposit = await this.depositRepository.findOne({ where: { sourceChainId: depositSourceChainId, depositId } }); | ||
|
||
if (!deposit) throw new Error("Deposit not found"); | ||
if (this.isSpeedUpAlreadyProcessed(deposit, job.data)) return; | ||
|
||
await this.processSpeedUpEventQueueMessage(deposit, job.data); | ||
} | ||
|
||
public async processSpeedUpEventQueueMessage(deposit: Deposit, data: SpeedUpEventsV3QueueMessage) { | ||
const { | ||
transactionHash, | ||
blockNumber, | ||
depositSourceChainId, | ||
updatedOutputAmount, | ||
updatedMessage, | ||
updatedRecipient, | ||
} = data; | ||
|
||
const sortedSpeedUps = [ | ||
...deposit.speedUps, | ||
{ | ||
hash: transactionHash, | ||
updatedOutputAmount, | ||
blockNumber, | ||
depositSourceChainId, | ||
updatedMessage, | ||
updatedRecipient, | ||
}, | ||
].sort((a, b) => b.blockNumber - a.blockNumber) as RequestedSpeedUpDepositTxV3[]; | ||
const wei = new BigNumber(10).pow(18); | ||
const feePct = new BigNumber(updatedOutputAmount).multipliedBy(wei).div(deposit.amount); | ||
return this.depositRepository.update( | ||
{ id: deposit.id }, | ||
{ | ||
speedUps: sortedSpeedUps, | ||
message: sortedSpeedUps[0].updatedMessage, | ||
recipientAddr: sortedSpeedUps[0].updatedRecipient, | ||
outputAmount: sortedSpeedUps[0].updatedOutputAmount, | ||
depositRelayerFeePct: feePct.toFixed(0), | ||
}, | ||
); | ||
} | ||
|
||
public isSpeedUpAlreadyProcessed(deposit: Deposit, speedUp: SpeedUpEventsV3QueueMessage) { | ||
const { transactionHash } = speedUp; | ||
const speedUpTxIndex = deposit.speedUps.findIndex((speedUpTx) => speedUpTx.hash === transactionHash); | ||
return speedUpTxIndex !== -1; | ||
} | ||
|
||
@OnQueueFailed() | ||
private onQueueFailed(job: Job, error: Error) { | ||
this.logger.error(`${ScraperQueue.SpeedUpEvents} ${JSON.stringify(job.data)} failed: ${error}`); | ||
} | ||
} |
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
Oops, something went wrong.