-
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.
feat: suggested fees consumer (#176)
* feat: add `suggestedRelayerFeePct` column to deposit * feat: add `SuggestedFeesConsumer` * refactor: review requests - move suggested fees service into nearer located file - remove if-clause to terminate suggested relayer fee population if already exists
- Loading branch information
Showing
11 changed files
with
181 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class Deposit1670847543409 implements MigrationInterface { | ||
name = "Deposit1670847543409"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "deposit" ADD "suggestedRelayerFeePct" numeric`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "deposit" DROP COLUMN "suggestedRelayerFeePct"`); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/modules/scraper/adapter/across-serverless-api/suggested-fees-service.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,38 @@ | ||
import { HttpService } from "@nestjs/axios"; | ||
import { Injectable } from "@nestjs/common"; | ||
|
||
import { AppConfig } from "../../../configuration/configuration.service"; | ||
|
||
type SuggestedFeesApiParams = { | ||
amount: string; | ||
token: string; | ||
destinationChainId: number; | ||
originChainId: number; | ||
}; | ||
|
||
type SuggestedFeesApiResponse = { | ||
data: { | ||
capitalFeePct: string; | ||
capitalFeeTotal: string; | ||
relayGasFeePct: string; | ||
relayGasFeeTotal: string; | ||
relayFeePct: string; | ||
relayFeeTotal: string; | ||
lpFeePct: string; | ||
timestamp: string; | ||
isAmountTooLow: boolean; | ||
}; | ||
}; | ||
|
||
@Injectable() | ||
export class SuggestedFeesService { | ||
constructor(private appConfig: AppConfig, private httpService: HttpService) {} | ||
|
||
public async getFromApi(params: SuggestedFeesApiParams) { | ||
const response = await this.httpService.axiosRef.get<SuggestedFeesApiParams, SuggestedFeesApiResponse>( | ||
this.appConfig.values.suggestedFees.apiUrl, | ||
{ params }, | ||
); | ||
return response?.data; | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
src/modules/scraper/adapter/messaging/SuggestedFeesConsumer.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,66 @@ | ||
import { OnQueueFailed, Process, Processor } from "@nestjs/bull"; | ||
import { Logger } from "@nestjs/common"; | ||
import { Job } from "bull"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { Repository } from "typeorm"; | ||
import { DateTime } from "luxon"; | ||
import { utils } from "ethers"; | ||
|
||
import { SuggestedFeesService } from "../across-serverless-api/suggested-fees-service"; | ||
import { SuggestedFeesQueueMessage, ScraperQueue } from "."; | ||
import { Deposit } from "../../model/deposit.entity"; | ||
import { AppConfig } from "../../../configuration/configuration.service"; | ||
|
||
@Processor(ScraperQueue.SuggestedFees) | ||
export class SuggestedFeesConsumer { | ||
private logger = new Logger(SuggestedFeesConsumer.name); | ||
|
||
constructor( | ||
private appConfig: AppConfig, | ||
private suggestedFeesService: SuggestedFeesService, | ||
@InjectRepository(Deposit) private depositRepository: Repository<Deposit>, | ||
) {} | ||
|
||
@Process() | ||
private async process(job: Job<SuggestedFeesQueueMessage>) { | ||
const { depositId } = job.data; | ||
|
||
const deposit = await this.depositRepository.findOne({ | ||
where: { id: depositId }, | ||
}); | ||
|
||
if (!deposit) { | ||
this.logger.verbose(`${ScraperQueue.SuggestedFees} deposit with id '${depositId}' does not exist in db`); | ||
return; | ||
} | ||
|
||
if (!deposit.depositDate) { | ||
throw new Error(`Deposit with id '${depositId}' needs 'depositDate' entry in order to fetch suggested fees`); | ||
} | ||
|
||
let suggestedRelayerFeePct: string; | ||
|
||
const diffToNowHours = DateTime.fromJSDate(deposit.depositDate).diffNow().as("hours"); | ||
if (Math.abs(diffToNowHours) >= this.appConfig.values.suggestedFees.fallbackThresholdHours) { | ||
// Due to the inability to retrieve historic suggested fees, we fallback | ||
// to 1bp for deposits that were made more than configured hours ago. | ||
suggestedRelayerFeePct = utils.parseEther("0.0001").toString(); | ||
} else { | ||
// For deposits that were made tolerable hours ago, we assume somewhat constant suggested fees. | ||
const suggestedFeesFromApi = await this.suggestedFeesService.getFromApi({ | ||
amount: deposit.amount, | ||
token: deposit.tokenAddr, | ||
destinationChainId: deposit.destinationChainId, | ||
originChainId: deposit.sourceChainId, | ||
}); | ||
suggestedRelayerFeePct = suggestedFeesFromApi.relayFeePct; | ||
} | ||
|
||
await this.depositRepository.update({ id: depositId }, { suggestedRelayerFeePct }); | ||
} | ||
|
||
@OnQueueFailed() | ||
private onQueueFailed(job: Job, error: Error) { | ||
this.logger.error(`${ScraperQueue.SuggestedFees} ${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
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