Calculate fee

This commit is contained in:
Aleksandr Kraiz
2023-08-01 21:27:17 +04:00
parent 7eebb2bb9e
commit f9605e3d1a
9 changed files with 212 additions and 50 deletions

26
src/utils/convertPrice.ts Normal file
View 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;
}