prices with quote asset

This commit is contained in:
Kirill Litvinov
2023-07-31 22:49:32 +03:00
parent 58dfb58067
commit e4d523e220
7 changed files with 45 additions and 36 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.19.40",
"version": "0.19.41-rc1",
"description": "Orion Protocol SDK",
"main": "./lib/index.cjs",
"module": "./lib/index.js",
@@ -108,4 +108,4 @@
"overrides": {
"tsconfig-paths": "^4.0.0"
}
}
}

View File

@@ -46,7 +46,7 @@ export default async function getSwapInfo({
const nativeCryptocurrencyName = getNativeCryptocurrencyName(assetToAddress);
const feeAssets = await simpleFetch(blockchainService.getTokensFee)();
const pricesInOrn = await simpleFetch(blockchainService.getPrices)();
const allPrices = await simpleFetch(blockchainService.getPricesWithQuoteAsset)();
const gasPriceWei = await simpleFetch(blockchainService.getGasPriceWei)();
const gasPriceGwei = ethers.utils.formatUnits(gasPriceWei, 'gwei').toString();
@@ -121,12 +121,12 @@ export default async function getSwapInfo({
if (baseAssetAddress === undefined) throw new Error(`No asset address for ${baseAssetName}`);
// Fee calculation
const baseAssetPriceInOrn = pricesInOrn[baseAssetAddress];
if (baseAssetPriceInOrn === undefined) throw new Error(`Base asset price ${baseAssetName} in ORN not found`);
const baseCurrencyPriceInOrn = pricesInOrn[ethers.constants.AddressZero];
if (baseCurrencyPriceInOrn === undefined) throw new Error('Base currency price in ORN not found');
const feeAssetPriceInOrn = pricesInOrn[feeAssetAddress];
if (feeAssetPriceInOrn === undefined) throw new Error(`Fee asset price ${feeAsset} in ORN not found`);
const baseAssetPriceInQuoteAsset = allPrices.prices[baseAssetAddress];
if (baseAssetPriceInQuoteAsset === undefined) throw new Error(`Base asset price ${baseAssetName} in ${allPrices.quoteAsset} not found`);
const baseCurrencyPriceInQuoteAsset = allPrices.prices[ethers.constants.AddressZero];
if (baseCurrencyPriceInQuoteAsset === undefined) throw new Error(`Base currency price in ${allPrices.quoteAsset} not found`);
const feeAssetPriceInQuoteAsset = allPrices.prices[feeAssetAddress];
if (feeAssetPriceInQuoteAsset === undefined) throw new Error(`Fee asset price ${feeAsset} in ${allPrices.quoteAsset} not found`);
const feePercent = feeAssets[feeAsset];
if (feePercent === undefined) throw new Error(`Fee asset ${feeAsset} not available`);
@@ -135,9 +135,9 @@ export default async function getSwapInfo({
networkFeeInFeeAsset,
} = calculateFeeInFeeAsset(
swapInfo.orderInfo.amount,
feeAssetPriceInOrn,
baseAssetPriceInOrn,
baseCurrencyPriceInOrn,
feeAssetPriceInQuoteAsset,
baseAssetPriceInQuoteAsset,
baseCurrencyPriceInQuoteAsset,
gasPriceGwei,
feePercent,
);

View File

@@ -90,7 +90,7 @@ export default async function swapLimit({
const exchangeContract = Exchange__factory.connect(exchangeContractAddress, provider);
const feeAssets = await simpleFetch(blockchainService.getTokensFee)();
const pricesInOrn = await simpleFetch(blockchainService.getPrices)();
const allPrices = await simpleFetch(blockchainService.getPricesWithQuoteAsset)();
const gasPriceWei = await simpleFetch(blockchainService.getGasPriceWei)();
const { factories } = await simpleFetch(blockchainService.getPoolsConfig)();
const poolExchangesList = factories !== undefined ? Object.keys(factories) : [];
@@ -372,20 +372,20 @@ export default async function swapLimit({
});
// Fee calculation
const baseAssetPriceInOrn = pricesInOrn[baseAssetAddress];
if (baseAssetPriceInOrn === undefined) throw new Error(`Base asset price ${baseAssetName} in ORN not found`);
const baseCurrencyPriceInOrn = pricesInOrn[ethers.constants.AddressZero];
if (baseCurrencyPriceInOrn === undefined) throw new Error('Base currency price in ORN not found');
const feeAssetPriceInOrn = pricesInOrn[feeAssetAddress];
if (feeAssetPriceInOrn === undefined) throw new Error(`Fee asset price ${feeAsset} in ORN not found`);
const baseAssetPriceInQuoteAsset = allPrices.prices[baseAssetAddress];
if (baseAssetPriceInQuoteAsset === undefined) throw new Error(`Base asset price ${baseAssetName} in ${allPrices.quoteAsset} not found`);
const baseCurrencyPriceInQuoteAsset = allPrices.prices[ethers.constants.AddressZero];
if (baseCurrencyPriceInQuoteAsset === undefined) throw new Error(`Base currency price in ${allPrices.quoteAsset} not found`);
const feeAssetPriceInQuoteAsset = allPrices.prices[feeAssetAddress];
if (feeAssetPriceInQuoteAsset === undefined) throw new Error(`Fee asset price ${feeAsset} in ${allPrices.quoteAsset} not found`);
const feePercent = feeAssets[feeAsset];
if (feePercent === undefined) throw new Error(`Fee asset ${feeAsset} not available`);
const { serviceFeeInFeeAsset, networkFeeInFeeAsset, totalFeeInFeeAsset } = calculateFeeInFeeAsset(
swapInfo.orderInfo.amount,
feeAssetPriceInOrn,
baseAssetPriceInOrn,
baseCurrencyPriceInOrn,
feeAssetPriceInQuoteAsset,
baseAssetPriceInQuoteAsset,
baseCurrencyPriceInQuoteAsset,
gasPriceGwei,
feePercent,
);

View File

@@ -75,7 +75,7 @@ export default async function swapMarket({
const exchangeContract = Exchange__factory.connect(exchangeContractAddress, provider);
const feeAssets = await simpleFetch(blockchainService.getTokensFee)();
const pricesInOrn = await simpleFetch(blockchainService.getPrices)();
const allPrices = await simpleFetch(blockchainService.getPricesWithQuoteAsset)();
const gasPriceWei = await simpleFetch(blockchainService.getGasPriceWei)();
const { factories } = await simpleFetch(blockchainService.getPoolsConfig)();
const poolExchangesList = factories !== undefined ? Object.keys(factories) : [];
@@ -330,20 +330,20 @@ export default async function swapMarket({
});
// Fee calculation
const baseAssetPriceInOrn = pricesInOrn[baseAssetAddress];
if (baseAssetPriceInOrn === undefined) throw new Error(`Base asset price ${baseAssetName} in ORN not found`);
const baseCurrencyPriceInOrn = pricesInOrn[ethers.constants.AddressZero];
if (baseCurrencyPriceInOrn === undefined) throw new Error('Base currency price in ORN not found');
const feeAssetPriceInOrn = pricesInOrn[feeAssetAddress];
if (feeAssetPriceInOrn === undefined) throw new Error(`Fee asset price ${feeAsset} in ORN not found`);
const baseAssetPriceInQuoteAsset = allPrices.prices[baseAssetAddress];
if (baseAssetPriceInQuoteAsset === undefined) throw new Error(`Base asset price ${baseAssetName} in ${allPrices.quoteAsset}not found`);
const baseCurrencyPriceInQuoteAsset = allPrices.prices[ethers.constants.AddressZero];
if (baseCurrencyPriceInQuoteAsset === undefined) throw new Error(`Base currency price in ${allPrices.quoteAsset} not found`);
const feeAssetPriceInQuoteAsset = allPrices.prices[feeAssetAddress];
if (feeAssetPriceInQuoteAsset === undefined) throw new Error(`Fee asset price ${feeAsset} in ${allPrices.quoteAsset} not found`);
const feePercent = feeAssets[feeAsset];
if (feePercent === undefined) throw new Error(`Fee asset ${feeAsset} not available`);
const { serviceFeeInFeeAsset, networkFeeInFeeAsset, totalFeeInFeeAsset } = calculateFeeInFeeAsset(
swapInfo.orderInfo.amount,
feeAssetPriceInOrn,
baseAssetPriceInOrn,
baseCurrencyPriceInOrn,
feeAssetPriceInQuoteAsset,
baseAssetPriceInQuoteAsset,
baseCurrencyPriceInQuoteAsset,
gasPriceGwei,
feePercent,
);

View File

@@ -13,6 +13,7 @@ import {
governancePoolsSchema,
governancePoolSchema,
governanceChainsInfoSchema,
pricesWithQuoteAssetSchema,
} from './schemas/index.js';
import type redeemOrderSchema from '../Aggregator/schemas/redeemOrderSchema.js';
import { sourceAtomicHistorySchema, targetAtomicHistorySchema } from './schemas/atomicHistorySchema.js';
@@ -81,7 +82,7 @@ class BlockchainService {
this.getUserVotes = this.getUserVotes.bind(this);
this.getUserEarned = this.getUserEarned.bind(this);
this.getHistory = this.getHistory.bind(this);
this.getPrices = this.getPrices.bind(this);
this.getPricesWithQuoteAsset = this.getPricesWithQuoteAsset.bind(this);
this.getTokensFee = this.getTokensFee.bind(this);
this.getGasPriceWei = this.getGasPriceWei.bind(this);
this.checkFreeRedeemAvailable = this.checkFreeRedeemAvailable.bind(this);
@@ -206,9 +207,9 @@ class BlockchainService {
{ headers: this.basicAuthHeaders }
);
getPrices = () => fetchWithValidation(
`${this.apiUrl}/api/prices`,
z.record(z.string()).transform(makePartial),
getPricesWithQuoteAsset = () => fetchWithValidation(
`http://localhost:57303/prices.json`,
pricesWithQuoteAssetSchema,
{ headers: this.basicAuthHeaders }
);

View File

@@ -16,3 +16,4 @@ export { default as governanceContractsSchema } from './governanceContractsSchem
export { default as governancePoolsSchema } from './governancePoolsSchema.js';
export { default as governancePoolSchema } from './governancePoolSchema.js';
export { default as governanceChainsInfoSchema } from './governanceChainsInfoSchema.js';
export { pricesWithQuoteAssetSchema } from './pricesWithQuoteAssetSchema.js';

View File

@@ -0,0 +1,7 @@
import { z } from 'zod';
import { makePartial } from '../../../utils/index.js';
export const pricesWithQuoteAssetSchema = z.object({
quoteAsset: z.string(),
prices: z.record(z.string()).transform(makePartial)
});