-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix zero output token addresses
- Loading branch information
amateima
committed
Oct 28, 2024
1 parent
9eea690
commit eec4fd5
Showing
2 changed files
with
83 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Injectable, Logger } from "@nestjs/common"; | ||
import { DataSource } from "typeorm"; | ||
import { ethers } from "ethers"; | ||
import { Cron, CronExpression } from "@nestjs/schedule"; | ||
|
||
import { Deposit } from "../../deposit/model/deposit.entity"; | ||
import { ScraperQueuesService } from "./ScraperQueuesService"; | ||
import { | ||
ScraperQueue, | ||
SuggestedFeesQueueMessage, | ||
TokenDetailsQueueMessage, | ||
} from "../adapter/messaging"; | ||
import { DepositService } from "../../deposit/service"; | ||
|
||
@Injectable() | ||
export class FixOutputTokenAddressCron { | ||
private logger = new Logger(FixOutputTokenAddressCron.name); | ||
private lock = false; | ||
|
||
constructor( | ||
private dataSource: DataSource, | ||
private scraperQueuesService: ScraperQueuesService, | ||
private depositService: DepositService, | ||
) {} | ||
|
||
@Cron(CronExpression.EVERY_MINUTE) | ||
async run() { | ||
try { | ||
if (this.lock) { | ||
this.logger.warn("FixOutputTokenAddressCron is locked"); | ||
return; | ||
} | ||
this.lock = true; | ||
await this.fixOutputTokenAddress(); | ||
this.lock = false; | ||
} catch (error) { | ||
this.logger.error(error); | ||
this.lock = false; | ||
} | ||
} | ||
|
||
private async fixOutputTokenAddress() { | ||
const query = this.dataSource | ||
.createQueryBuilder() | ||
.select() | ||
.from(Deposit, "d") | ||
.where("d.outputTokenAddress = :address", { address: ethers.constants.AddressZero }) | ||
.andWhere("d.depositDate > '2024-01-01'"); | ||
const deposits = await query.getMany(); | ||
this.logger.verbose(`Found ${deposits.length} deposits with outputTokenAddress = ${ethers.constants.AddressZero}`); | ||
|
||
for (const deposit of deposits) { | ||
const outputTokenAddress = await this.depositService.deriveOutputTokenAddress( | ||
deposit.sourceChainId, | ||
deposit.tokenAddr, | ||
deposit.destinationChainId, | ||
deposit.quoteTimestamp.getTime() / 1000, | ||
); | ||
if (outputTokenAddress) { | ||
await this.dataSource | ||
.createQueryBuilder() | ||
.update(Deposit) | ||
.set({ outputTokenAddress }) | ||
.where("id = :id", { id: deposit.id }) | ||
.execute(); | ||
console.log(`Updated deposit ${deposit.id} with outputTokenAddress = ${outputTokenAddress}`); | ||
await this.scraperQueuesService.publishMessage<TokenDetailsQueueMessage>(ScraperQueue.TokenDetails, { | ||
depositId: deposit.id, | ||
}); | ||
await this.scraperQueuesService.publishMessage<SuggestedFeesQueueMessage>(ScraperQueue.SuggestedFees, { | ||
depositId: deposit.id, | ||
}); | ||
} | ||
} | ||
} | ||
} |