mirror of
https://github.com/0xbow-io/privacy-pools-core.git
synced 2026-01-09 17:37:58 -05:00
feat: baseFeeBPS and feeBPS
This commit is contained in:
@@ -2,35 +2,34 @@ import { NextFunction, Request, Response } from "express";
|
||||
import { getAddress } from "viem";
|
||||
import { getAssetConfig } from "../../config/index.js";
|
||||
import { QuoterError } from "../../exceptions/base.exception.js";
|
||||
import { web3Provider } from "../../providers/index.js";
|
||||
import { QuoteProvider } from "../../providers/quote.provider.js";
|
||||
import { quoteProvider, web3Provider } from "../../providers/index.js";
|
||||
import { QuoteMarshall } from "../../types.js";
|
||||
|
||||
const TIME_30_SECS = 30 * 1000;
|
||||
|
||||
export async function relayQuoteHandler(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) {
|
||||
|
||||
const chainId = Number(req.body.chain_id!);
|
||||
const chainId = Number(req.body.chainId!);
|
||||
const amountIn = BigInt(req.body.amount!.toString());
|
||||
const tokenAddress = getAddress(req.body.token!.toString())
|
||||
const tokenAddress = getAddress(req.body.asset!.toString())
|
||||
|
||||
const config = getAssetConfig(chainId, tokenAddress);
|
||||
if (config === undefined)
|
||||
return next(QuoterError.assetNotSupported(`Asset ${tokenAddress} for chain ${chainId} is not supported`));
|
||||
|
||||
const quoteProvider = new QuoteProvider(config.fee_bps);
|
||||
const gasPrice = await web3Provider.getGasPrice(chainId);
|
||||
const value = 0n;
|
||||
const value = 0n; // for future features
|
||||
|
||||
const quote = await quoteProvider.quoteNativeTokenInERC20(chainId, tokenAddress, amountIn);
|
||||
const feeBPS = await quoteProvider.quoteFeeBPSNative(amountIn, quote, gasPrice, value);
|
||||
const feeBPS = await quoteProvider.quoteFeeBPSNative(config.fee_bps, amountIn, quote, gasPrice, value);
|
||||
|
||||
res
|
||||
.status(200)
|
||||
.json(res.locals.marshalResponse(new QuoteMarshall({ feeBPS, expiration: Number(new Date()) + TIME_30_SECS, relayToken: "" })));
|
||||
.json(res.locals.marshalResponse(new QuoteMarshall({
|
||||
baseFeeBPS: config.fee_bps,
|
||||
feeBPS,
|
||||
})));
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ export interface QuotetBody {
|
||||
}
|
||||
|
||||
export interface QuoteResponse {
|
||||
baseFeeBPS: bigint,
|
||||
feeBPS: bigint,
|
||||
expiration: number,
|
||||
relayToken: string
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Web3Provider } from "./web3.provider.js";
|
||||
import { UniswapProvider } from "./uniswap.provider.js"
|
||||
import { QuoteProvider } from "./quote.provider.js";
|
||||
|
||||
export { db } from "./db.provider.js";
|
||||
export { SdkProvider } from "./sdk.provider.js";
|
||||
@@ -8,3 +9,4 @@ export { UniswapProvider } from "./uniswap.provider.js"
|
||||
|
||||
export const web3Provider = new Web3Provider();
|
||||
export const uniswapProvider = new UniswapProvider();
|
||||
export const quoteProvider = new QuoteProvider();
|
||||
|
||||
@@ -3,9 +3,10 @@ import { uniswapProvider } from "./index.js";
|
||||
|
||||
export class QuoteProvider {
|
||||
|
||||
// a typical withdrawal costs between 450k-650k gas
|
||||
static txCost: bigint = 700_000n;
|
||||
|
||||
constructor(readonly baseFee: bigint) {
|
||||
constructor() {
|
||||
}
|
||||
|
||||
async quoteNativeTokenInERC20(chainId: number, addressIn: Address, amountIn: bigint): Promise<{ num: bigint, den: bigint }> {
|
||||
@@ -13,9 +14,9 @@ export class QuoteProvider {
|
||||
return { num: out.amount, den: in_.amount };
|
||||
}
|
||||
|
||||
async quoteFeeBPSNative(balance: bigint, nativeQuote: { num: bigint, den: bigint }, gasPrice: bigint, value: bigint): Promise<bigint> {
|
||||
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 this.baseFee + nativeQuote.den * 10_000n * nativeCosts / balance / nativeQuote.num;
|
||||
return baseFee + nativeQuote.den * 10_000n * nativeCosts / balance / nativeQuote.num;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,9 +43,8 @@ export class QuoteMarshall extends RelayerMarshall {
|
||||
}
|
||||
override toJSON(): object {
|
||||
return {
|
||||
baseFeeBPS: this.response.baseFeeBPS.toString(),
|
||||
feeBPS: this.response.feeBPS.toString(),
|
||||
expiration: this.response.expiration,
|
||||
relayToken: this.response.relayToken
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user