From 1e59cf94b9b900af772075a53d72decfc2b27dcc Mon Sep 17 00:00:00 2001 From: Nikita Polyakov <53777036+Nikita-Polyakov@users.noreply.github.com> Date: Wed, 19 Jul 2023 15:22:49 +0300 Subject: [PATCH] add dynamic fee to calculations (#589) --- packages/api/package.json | 4 ++-- packages/connection/package.json | 4 ++-- packages/liquidity-proxy/package.json | 4 ++-- packages/liquidity-proxy/src/consts.ts | 3 +-- packages/liquidity-proxy/src/quote/band.ts | 4 ++++ .../liquidity-proxy/src/quote/oracleProxy.ts | 6 ++++- packages/liquidity-proxy/src/quote/xst.ts | 22 ++++++++++++++++--- packages/liquidity-proxy/src/types.ts | 1 + packages/math/package.json | 2 +- packages/type-definitions/package.json | 2 +- packages/types/package.json | 4 ++-- packages/util/package.json | 12 +++++----- packages/util/src/swap/index.ts | 5 +++-- 13 files changed, 49 insertions(+), 24 deletions(-) diff --git a/packages/api/package.json b/packages/api/package.json index b98bcce8b..f78d70e41 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -1,6 +1,6 @@ { "name": "@sora-substrate/api", - "version": "1.18.5", + "version": "1.18.6", "license": "Apache-2.0", "main": "./build/index.js", "typings": "./build/index.d.ts", @@ -10,6 +10,6 @@ "dependencies": { "@open-web3/orml-api-derive": "1.1.4", "@polkadot/api": "9.14.2", - "@sora-substrate/types": "1.18.5" + "@sora-substrate/types": "1.18.6" } } diff --git a/packages/connection/package.json b/packages/connection/package.json index f6e02f971..2e232f6b3 100644 --- a/packages/connection/package.json +++ b/packages/connection/package.json @@ -1,6 +1,6 @@ { "name": "@sora-substrate/connection", - "version": "1.18.5", + "version": "1.18.6", "license": "Apache-2.0", "main": "./build/index.js", "typings": "./build/index.d.ts", @@ -8,6 +8,6 @@ "access": "public" }, "dependencies": { - "@sora-substrate/api": "1.18.5" + "@sora-substrate/api": "1.18.6" } } diff --git a/packages/liquidity-proxy/package.json b/packages/liquidity-proxy/package.json index f88f8e53d..eb2d14b59 100644 --- a/packages/liquidity-proxy/package.json +++ b/packages/liquidity-proxy/package.json @@ -1,6 +1,6 @@ { "name": "@sora-substrate/liquidity-proxy", - "version": "1.18.5", + "version": "1.18.6", "license": "Apache-2.0", "main": "./build/index.js", "typings": "./build/index.d.ts", @@ -8,6 +8,6 @@ "access": "public" }, "dependencies": { - "@sora-substrate/math": "1.18.5" + "@sora-substrate/math": "1.18.6" } } diff --git a/packages/liquidity-proxy/src/consts.ts b/packages/liquidity-proxy/src/consts.ts index 46742f00b..9ee7e9d89 100644 --- a/packages/liquidity-proxy/src/consts.ts +++ b/packages/liquidity-proxy/src/consts.ts @@ -18,9 +18,8 @@ export class Consts { /** TBCD token */ static readonly TBCD = '0x02000a0000000000000000000000000000000000000000000000000000000000'; - /** XYK, TBC, XST fees the same */ + /** XYK, TBC fees the same */ static readonly XYK_FEE = new FPNumber(0.003); - static readonly XST_FEE = new FPNumber(0.00666); static readonly TBC_FEE = Consts.XYK_FEE; /** Max `Rust` number value */ static readonly MAX = new FPNumber('170141183460469231731.687303715884105727'); diff --git a/packages/liquidity-proxy/src/quote/band.ts b/packages/liquidity-proxy/src/quote/band.ts index 815ff34d1..bc8f5943f 100644 --- a/packages/liquidity-proxy/src/quote/band.ts +++ b/packages/liquidity-proxy/src/quote/band.ts @@ -22,3 +22,7 @@ export const bandQuote = (symbol: string, payload: QuotePayload): OracleRate => return rate; }; + +export const bandQuoteUnchecked = (symbol: string, payload: QuotePayload): OracleRate => { + return payload.rates[symbol]; +}; diff --git a/packages/liquidity-proxy/src/quote/oracleProxy.ts b/packages/liquidity-proxy/src/quote/oracleProxy.ts index 493138d00..bb356ffb5 100644 --- a/packages/liquidity-proxy/src/quote/oracleProxy.ts +++ b/packages/liquidity-proxy/src/quote/oracleProxy.ts @@ -1,7 +1,11 @@ -import { bandQuote } from './band'; +import { bandQuote, bandQuoteUnchecked } from './band'; import type { QuotePayload, OracleRate } from '../types'; export const oracleProxyQuote = (symbol: string, payload: QuotePayload): OracleRate => { return bandQuote(symbol, payload); }; + +export const oracleProxyQuoteUnchecked = (symbol: string, payload: QuotePayload): OracleRate => { + return bandQuoteUnchecked(symbol, payload); +}; diff --git a/packages/liquidity-proxy/src/quote/xst.ts b/packages/liquidity-proxy/src/quote/xst.ts index 0d84a358f..f21155643 100644 --- a/packages/liquidity-proxy/src/quote/xst.ts +++ b/packages/liquidity-proxy/src/quote/xst.ts @@ -3,7 +3,7 @@ import { FPNumber } from '@sora-substrate/math'; import { LiquiditySourceTypes, Consts, PriceVariant } from '../consts'; import { safeDivide, isAssetAddress, safeQuoteResult } from '../utils'; import { getAveragePrice } from './price'; -import { oracleProxyQuote } from './oracleProxy'; +import { oracleProxyQuote, oracleProxyQuoteUnchecked } from './oracleProxy'; import type { QuotePayload, QuoteResult, PrimaryMarketsEnabledAssets } from '../types'; @@ -17,6 +17,22 @@ const ensureBaseAssetAmountWithinLimit = (amount: FPNumber, payload: QuotePayloa } }; +const getAggregatedFee = ( + syntheticAssetId: string, + payload: QuotePayload, + enabledAssets: PrimaryMarketsEnabledAssets +): FPNumber => { + const asset = enabledAssets.xst[syntheticAssetId]; + + if (!asset) throw new Error(`Synthetic asset "${syntheticAssetId}" does not exist`); + + const { feeRatio, referenceSymbol } = asset; + const rate = oracleProxyQuoteUnchecked(referenceSymbol, payload); + const dynamicFee = FPNumber.fromCodecValue(rate?.dynamicFee ?? '0'); + + return feeRatio.add(dynamicFee); +}; + const xstReferencePrice = ( assetAddress: string, priceVariant: PriceVariant, @@ -93,7 +109,7 @@ const xstBuyPriceWithFee = ( enabledAssets: PrimaryMarketsEnabledAssets, checkLimits = true // check on XST buy-sell limit (no need for price impact) ): QuoteResult => { - const feeRatio = enabledAssets.xst[syntheticAsset]?.feeRatio ?? Consts.XST_FEE; + const feeRatio = getAggregatedFee(syntheticAsset, payload, enabledAssets); if (isDesiredInput) { const outputAmount = xstBuyPrice(syntheticAsset, amount, isDesiredInput, payload, enabledAssets); @@ -149,7 +165,7 @@ const xstSellPriceWithFee = ( enabledAssets: PrimaryMarketsEnabledAssets, checkLimits = true // check on XST buy-sell limit (no need for price impact) ): QuoteResult => { - const feeRatio = enabledAssets.xst[syntheticAsset]?.feeRatio ?? Consts.XST_FEE; + const feeRatio = getAggregatedFee(syntheticAsset, payload, enabledAssets); if (isDesiredInput) { ensureBaseAssetAmountWithinLimit(amount, payload, checkLimits); diff --git a/packages/liquidity-proxy/src/types.ts b/packages/liquidity-proxy/src/types.ts index 0759a6a52..72e595875 100644 --- a/packages/liquidity-proxy/src/types.ts +++ b/packages/liquidity-proxy/src/types.ts @@ -53,6 +53,7 @@ export type QuotePaths = { export type OracleRate = { value: CodecString; lastUpdated: number; + dynamicFee: CodecString; }; export type QuotePayload = { diff --git a/packages/math/package.json b/packages/math/package.json index a4bea3d73..eb49775f7 100644 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -1,6 +1,6 @@ { "name": "@sora-substrate/math", - "version": "1.18.5", + "version": "1.18.6", "license": "Apache-2.0", "main": "./build/index.js", "typings": "./build/index.d.ts", diff --git a/packages/type-definitions/package.json b/packages/type-definitions/package.json index eb08c2e32..868882b80 100644 --- a/packages/type-definitions/package.json +++ b/packages/type-definitions/package.json @@ -1,6 +1,6 @@ { "name": "@sora-substrate/type-definitions", - "version": "1.18.5", + "version": "1.18.6", "license": "Apache-2.0", "main": "./build/index.js", "typings": "./build/index.d.ts", diff --git a/packages/types/package.json b/packages/types/package.json index a262e5199..cc395e76a 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -1,6 +1,6 @@ { "name": "@sora-substrate/types", - "version": "1.18.5", + "version": "1.18.6", "license": "Apache-2.0", "main": "./build/index.js", "typings": "./build/index.d.ts", @@ -13,7 +13,7 @@ "@polkadot/api": "9.14.2", "@polkadot/typegen": "9.14.2", "@polkadot/types": "9.14.2", - "@sora-substrate/type-definitions": "1.18.5" + "@sora-substrate/type-definitions": "1.18.6" }, "devDependencies": { "@types/websocket": "^1.0.0", diff --git a/packages/util/package.json b/packages/util/package.json index 7fdf15765..0c04483df 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -1,6 +1,6 @@ { "name": "@sora-substrate/util", - "version": "1.18.5", + "version": "1.18.6", "license": "Apache-2.0", "main": "./build/index.js", "typings": "./build/index.d.ts", @@ -9,11 +9,11 @@ }, "dependencies": { "@polkadot/ui-keyring": "2.12.1", - "@sora-substrate/api": "1.18.5", - "@sora-substrate/connection": "1.18.5", - "@sora-substrate/liquidity-proxy": "1.18.5", - "@sora-substrate/math": "1.18.5", - "@sora-substrate/types": "1.18.5", + "@sora-substrate/api": "1.18.6", + "@sora-substrate/connection": "1.18.6", + "@sora-substrate/liquidity-proxy": "1.18.6", + "@sora-substrate/math": "1.18.6", + "@sora-substrate/types": "1.18.6", "axios": "^0.21.1", "crypto-js": "^4.0.0", "lodash": "^4.17.15" diff --git a/packages/util/src/swap/index.ts b/packages/util/src/swap/index.ts index 57d7287bd..3bc755cbc 100644 --- a/packages/util/src/swap/index.ts +++ b/packages/util/src/swap/index.ts @@ -69,10 +69,11 @@ const toBandRate = (o: Observable>): Observable o.pipe( map((codec) => { const data = codec.unwrap(); - const value = new FPNumber(data.value).toCodecString(); + const value = data.value.toString(); const lastUpdated = data.lastUpdated.toNumber(); + const dynamicFee = data.dynamicFee.inner.toString(); - return { value, lastUpdated }; + return { value, lastUpdated, dynamicFee }; }) );