mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-28 16:48:00 +03:00
Calculate fee
This commit is contained in:
@@ -5,32 +5,33 @@ import calculateServiceFeeInFeeAsset from './calculateServiceFeeInFeeAsset.js';
|
||||
|
||||
const calculateFeeInFeeAsset = (
|
||||
amount: BigNumber.Value,
|
||||
feeAssetPrice: BigNumber.Value,
|
||||
baseAssetPrice: BigNumber.Value,
|
||||
baseCurrencyPrice: BigNumber.Value,
|
||||
gasPriceGwei: BigNumber.Value,
|
||||
feePercent: BigNumber.Value,
|
||||
feeAsset: string,
|
||||
assetPrices: Partial<Record<string, string>>,
|
||||
baseAssetAddress: string,
|
||||
baseCurrencyAddress: string,
|
||||
feeAssetAddress: string,
|
||||
prices: Partial<Record<string, string>>,
|
||||
) => {
|
||||
const feeAssetPriceInQuoteAsset = assetPrices[feeAsset];
|
||||
if (feeAssetPriceInQuoteAsset === undefined) throw Error('feeAssetPriceInQuoteAsset is undefined');
|
||||
|
||||
const feeAssetPriceInQuoteAssetBN = new BigNumber(feeAssetPriceInQuoteAsset);
|
||||
const feeAssetPrice = prices[feeAssetAddress];
|
||||
if (feeAssetPrice === undefined) throw Error(`Fee asset price not found. Available prices: ${Object.keys(prices).join(', ')}`);
|
||||
const baseAssetPrice = prices[baseAssetAddress];
|
||||
if (baseAssetPrice === undefined) throw Error(`Base asset price not found. Available prices: ${Object.keys(prices).join(', ')}`);
|
||||
const baseCurrencyPrice = prices[baseCurrencyAddress]; // ETH, BNB, MATIC, etc.
|
||||
if (baseCurrencyPrice === undefined) throw Error(`Base currency price not found. Available prices: ${Object.keys(prices).join(', ')}`);
|
||||
|
||||
const serviceFeeInFeeAsset = calculateServiceFeeInFeeAsset(
|
||||
amount,
|
||||
feeAssetPrice,
|
||||
baseAssetPrice,
|
||||
baseAssetAddress,
|
||||
feeAssetAddress,
|
||||
feePercent,
|
||||
feeAssetPriceInQuoteAssetBN
|
||||
prices,
|
||||
);
|
||||
const networkFeeInFeeAsset = calculateNetworkFeeInFeeAsset(
|
||||
gasPriceGwei,
|
||||
FILL_ORDERS_GAS_LIMIT,
|
||||
baseCurrencyPrice,
|
||||
feeAssetPrice,
|
||||
feeAssetPriceInQuoteAssetBN
|
||||
baseCurrencyAddress,
|
||||
feeAssetAddress,
|
||||
prices,
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,20 +1,22 @@
|
||||
import { BigNumber } from 'bignumber.js';
|
||||
import type { BigNumber } from 'bignumber.js';
|
||||
import calculateNetworkFee from './calculateNetworkFee.js';
|
||||
import convertPrice from './convertPrice.js';
|
||||
|
||||
const calculateNetworkFeeInFeeAsset = (
|
||||
gasPriceGwei: BigNumber.Value,
|
||||
gasLimit: BigNumber.Value,
|
||||
baseCurrencyPrice: BigNumber.Value,
|
||||
feeAssetPrice: BigNumber.Value,
|
||||
feeAssetPriceInQuoteAsset: BigNumber.Value,
|
||||
baseCurrencyAddress: string,
|
||||
feeAssetAddress: string,
|
||||
prices: Partial<Record<string, string>>
|
||||
) => {
|
||||
const networkFee = calculateNetworkFee(gasPriceGwei, gasLimit);
|
||||
|
||||
const networkFeeInQuoteAsset = new BigNumber(networkFee).multipliedBy(baseCurrencyPrice);
|
||||
const networkFeeInFeeAsset = networkFeeInQuoteAsset
|
||||
.div(new BigNumber(feeAssetPriceInQuoteAsset).multipliedBy(feeAssetPrice));
|
||||
|
||||
return networkFeeInFeeAsset.toString();
|
||||
return convertPrice(
|
||||
networkFee,
|
||||
baseCurrencyAddress, // from
|
||||
feeAssetAddress, // to
|
||||
prices
|
||||
);
|
||||
};
|
||||
|
||||
export default calculateNetworkFeeInFeeAsset;
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
import { BigNumber } from 'bignumber.js';
|
||||
import convertPrice from './convertPrice.js';
|
||||
|
||||
export default function calculateServiceFeeInFeeAsset(
|
||||
amount: BigNumber.Value,
|
||||
feeAssetPrice: BigNumber.Value,
|
||||
baseAssetPrice: BigNumber.Value,
|
||||
baseAssetAddress: string,
|
||||
feeAssetAddress: string,
|
||||
feePercent: BigNumber.Value,
|
||||
feeAssetPriceInQuoteAsset: BigNumber.Value,
|
||||
prices: Partial<Record<string, string>>
|
||||
) {
|
||||
const result = new BigNumber(amount)
|
||||
.multipliedBy(new BigNumber(feePercent).div(100))
|
||||
.multipliedBy(baseAssetPrice)
|
||||
.div(new BigNumber(feeAssetPriceInQuoteAsset).multipliedBy(feeAssetPrice))
|
||||
.toString();
|
||||
const feeAmount = new BigNumber(amount).multipliedBy(new BigNumber(feePercent).div(100));
|
||||
|
||||
return result;
|
||||
const feeAssetAmount = convertPrice(
|
||||
feeAmount,
|
||||
baseAssetAddress,
|
||||
feeAssetAddress,
|
||||
prices
|
||||
);
|
||||
|
||||
return feeAssetAmount;
|
||||
}
|
||||
|
||||
26
src/utils/convertPrice.ts
Normal file
26
src/utils/convertPrice.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { BigNumber } from 'bignumber.js';
|
||||
|
||||
export default function convertPrice(
|
||||
amount: BigNumber.Value,
|
||||
assetInAddress: string,
|
||||
assetOutAddress: string,
|
||||
prices: Partial<Record<string, string>> // quoted in quoteAsset. [address]: priceQuotedInQuoteAsset
|
||||
) {
|
||||
const assetInPrice = prices[assetInAddress];
|
||||
if (assetInPrice === undefined) throw Error('assetInPrice is undefined');
|
||||
|
||||
const assetOutPrice = prices[assetOutAddress];
|
||||
if (assetOutPrice === undefined) throw Error('assetOutPrice is undefined');
|
||||
|
||||
const assetInPriceBN = new BigNumber(assetInPrice);
|
||||
const assetOutPriceBN = new BigNumber(assetOutPrice);
|
||||
|
||||
const assetInAmountBN = new BigNumber(amount);
|
||||
|
||||
const assetInAmountInQuoteAsset = assetInAmountBN.multipliedBy(assetInPriceBN);
|
||||
const assetInAmountInQuoteAssetBN = new BigNumber(assetInAmountInQuoteAsset);
|
||||
|
||||
const assetOutAmountInQuoteAsset = assetInAmountInQuoteAssetBN.dividedBy(assetOutPriceBN);
|
||||
|
||||
return assetOutAmountInQuoteAsset;
|
||||
}
|
||||
Reference in New Issue
Block a user