Fix price conversion

This commit is contained in:
Aleksandr Kraiz
2023-08-05 01:10:20 +04:00
parent 230c399bdc
commit b6cb9ae1a1
5 changed files with 26 additions and 26 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.19.44",
"version": "0.19.45",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",

View File

@@ -7,30 +7,30 @@ const calculateFeeInFeeAsset = (
amount: BigNumber.Value,
gasPriceGwei: BigNumber.Value,
feePercent: BigNumber.Value,
baseAssetAddress: string,
baseCurrencyAddress: string,
feeAssetAddress: string,
baseAssetName: string,
baseCurrencyName: string,
feeAssetName: string,
prices: Partial<Record<string, string>>,
) => {
const feeAssetPrice = prices[feeAssetAddress];
const feeAssetPrice = prices[feeAssetName];
if (feeAssetPrice === undefined) throw Error(`Fee asset price not found. Available prices: ${Object.keys(prices).join(', ')}`);
const baseAssetPrice = prices[baseAssetAddress];
const baseAssetPrice = prices[baseAssetName];
if (baseAssetPrice === undefined) throw Error(`Base asset price not found. Available prices: ${Object.keys(prices).join(', ')}`);
const baseCurrencyPrice = prices[baseCurrencyAddress]; // ETH, BNB, MATIC, etc.
const baseCurrencyPrice = prices[baseCurrencyName]; // ETH, BNB, MATIC, etc.
if (baseCurrencyPrice === undefined) throw Error(`Base currency price not found. Available prices: ${Object.keys(prices).join(', ')}`);
const serviceFeeInFeeAsset = calculateServiceFeeInFeeAsset(
amount,
baseAssetAddress,
feeAssetAddress,
baseAssetName,
feeAssetName,
feePercent,
prices,
);
const networkFeeInFeeAsset = calculateNetworkFeeInFeeAsset(
gasPriceGwei,
FILL_ORDERS_GAS_LIMIT,
baseCurrencyAddress,
feeAssetAddress,
baseCurrencyName,
feeAssetName,
prices,
);

View File

@@ -5,16 +5,16 @@ import convertPrice from './convertPrice.js';
const calculateNetworkFeeInFeeAsset = (
gasPriceGwei: BigNumber.Value,
gasLimit: BigNumber.Value,
baseCurrencyAddress: string,
feeAssetAddress: string,
baseCurrencyName: string,
feeAssetName: string,
prices: Partial<Record<string, string>>
) => {
const networkFee = calculateNetworkFee(gasPriceGwei, gasLimit);
return convertPrice(
networkFee,
baseCurrencyAddress, // from
feeAssetAddress, // to
baseCurrencyName, // from
feeAssetName, // to
prices
);
};

View File

@@ -3,8 +3,8 @@ import convertPrice from './convertPrice.js';
export default function calculateServiceFeeInFeeAsset(
amount: BigNumber.Value,
baseAssetAddress: string,
feeAssetAddress: string,
baseAssetName: string,
feeAssetName: string,
feePercent: BigNumber.Value,
prices: Partial<Record<string, string>>
) {
@@ -12,8 +12,8 @@ export default function calculateServiceFeeInFeeAsset(
const feeAssetAmount = convertPrice(
feeAmount,
baseAssetAddress,
feeAssetAddress,
baseAssetName,
feeAssetName,
prices
);

View File

@@ -2,15 +2,15 @@ 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
assetInName: string,
assetOutName: string,
prices: Partial<Record<string, string>> // quoted in quoteAsset. [name]: priceQuotedInQuoteAsset
) {
const assetInPrice = prices[assetInAddress.toLowerCase()];
if (assetInPrice === undefined) throw Error(`Price conversion: AssetIn (${assetInAddress}) price is undefined`);
const assetInPrice = prices[assetInName];
if (assetInPrice === undefined) throw Error(`Price conversion: AssetIn (${assetInName}) price is undefined`);
const assetOutPrice = prices[assetOutAddress.toLowerCase()];
if (assetOutPrice === undefined) throw Error(`Price conversion: AssetOut (${assetOutAddress}) price is undefined`);
const assetOutPrice = prices[assetOutName];
if (assetOutPrice === undefined) throw Error(`Price conversion: AssetOut (${assetOutName}) price is undefined`);
const assetInPriceBN = new BigNumber(assetInPrice);
const assetOutPriceBN = new BigNumber(assetOutPrice);