feat: adding quote case for native asset

This commit is contained in:
Francisco Bezzecchi
2025-04-14 18:28:36 -03:00
parent 4e3df75a17
commit 49a9c1edeb
2 changed files with 23 additions and 10 deletions

View File

@@ -3,9 +3,6 @@ import { uniswapProvider } from "./index.js";
export class QuoteProvider {
// a typical withdrawal costs between 450k-650k gas
static txCost: bigint = 700_000n;
constructor() {
}
@@ -14,9 +11,4 @@ export class QuoteProvider {
return { num: out.amount, den: in_.amount };
}
async quoteFeeBPSNative(baseFee: bigint, balance: bigint, nativeQuote: { num: bigint, den: bigint }, gasPrice: bigint, value: bigint): Promise<bigint> {
const nativeCosts = (1n * gasPrice * QuoteProvider.txCost + value)
return baseFee + nativeQuote.den * 10_000n * nativeCosts / balance / nativeQuote.num;
}
}

View File

@@ -9,13 +9,34 @@ interface QuoteFeeBPSParams {
baseFeeBPS: bigint
};
const NativeAddress = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
export class QuoteService {
readonly txCost: bigint;
constructor() {
// a typical withdrawal costs between 450k-650k gas
this.txCost = 700_000n;
}
async netFeeBPSNative(baseFee: bigint, balance: bigint, nativeQuote: { num: bigint, den: bigint }, gasPrice: bigint, value: bigint): Promise<bigint> {
const nativeCosts = (1n * gasPrice * this.txCost + value)
return baseFee + nativeQuote.den * 10_000n * nativeCosts / balance / nativeQuote.num;
}
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);
let quote: { num: bigint, den: bigint };
if (assetAddress.toLowerCase() === NativeAddress.toLowerCase()) {
quote = { num: 1n, den: 1n };
} else {
quote = await quoteProvider.quoteNativeTokenInERC20(chainId, assetAddress, amountIn);
}
const feeBPS = await this.netFeeBPSNative(baseFeeBPS, amountIn, quote, gasPrice, value);
return feeBPS
}