refactor: using quote service

This commit is contained in:
Francisco Bezzecchi
2025-04-11 15:52:00 -03:00
parent ff46f68ab0
commit 5e2872981a
2 changed files with 25 additions and 22 deletions

View File

@@ -2,7 +2,8 @@ import { NextFunction, Request, Response } from "express";
import { getAddress } from "viem";
import { getAssetConfig, getFeeReceiverAddress } from "../../config/index.js";
import { QuoterError } from "../../exceptions/base.exception.js";
import { quoteProvider, web3Provider } from "../../providers/index.js";
import { web3Provider } from "../../providers/index.js";
import { quoteService } from "../../services/index.js";
import { QuoteMarshall } from "../../types.js";
import { encodeWithdrawalData } from "../../utils.js";
@@ -16,17 +17,15 @@ export async function relayQuoteHandler(
const chainId = Number(req.body.chainId!);
const amountIn = BigInt(req.body.amount!.toString());
const tokenAddress = getAddress(req.body.asset!.toString())
const assetAddress = getAddress(req.body.asset!.toString())
const config = getAssetConfig(chainId, tokenAddress);
const config = getAssetConfig(chainId, assetAddress);
if (config === undefined)
return next(QuoterError.assetNotSupported(`Asset ${tokenAddress} for chain ${chainId} is not supported`));
return next(QuoterError.assetNotSupported(`Asset ${assetAddress} for chain ${chainId} is not supported`));
const gasPrice = await web3Provider.getGasPrice(chainId);
const value = 0n; // for future features
const quote = await quoteProvider.quoteNativeTokenInERC20(chainId, tokenAddress, amountIn);
const feeBPS = await quoteProvider.quoteFeeBPSNative(config.fee_bps, amountIn, quote, gasPrice, value);
const feeBPS = await quoteService.quoteFeeBPSNative({
chainId, amountIn, assetAddress, baseFeeBPS: config.fee_bps, value: 0n
});
const recipient = req.body.recipient ? getAddress(req.body.recipient.toString()) : undefined

View File

@@ -1,18 +1,22 @@
import { uniswapService } from "./index.js";
import { Address } from "viem";
import { quoteProvider, web3Provider } from "../providers/index.js";
interface QuoteFeeBPSParams {
chainId: number,
assetAddress: Address,
amountIn: bigint,
value: bigint,
baseFeeBPS: bigint
};
export class QuoteService {
static feeNet: bigint = 100n;
static txCost: bigint = 700_000n;
static async quoteFeeBPSNative(balance: bigint, nativeQuote: bigint, gasPrice: bigint, value: bigint): Promise<bigint> {
let tokenQuote = await uniswapService.quote({
chainId: 137,
inToken: "0x0000000000000000000000000000000000000000",
outToken: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
inAmount: 10000000000000000000n
});
console.log(tokenQuote)
return 0n
async quoteFeeBPSNative(quoteParams: QuoteFeeBPSParams): Promise<bigint> {
const { chainId, assetAddress, amountIn, baseFeeBPS, value } = quoteParams;
const gasPrice = await web3Provider.getGasPrice(chainId);
const quote = await quoteProvider.quoteNativeTokenInERC20(chainId, assetAddress, amountIn);
const feeBPS = await quoteProvider.quoteFeeBPSNative(baseFeeBPS, amountIn, quote, gasPrice, value);
return feeBPS
}
}