Skip to content

Commit

Permalink
add dynamic fee to calculations (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Polyakov authored Jul 19, 2023
1 parent 5b8215d commit 1e59cf9
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/connection/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"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",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sora-substrate/api": "1.18.5"
"@sora-substrate/api": "1.18.6"
}
}
4 changes: 2 additions & 2 deletions packages/liquidity-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"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",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sora-substrate/math": "1.18.5"
"@sora-substrate/math": "1.18.6"
}
}
3 changes: 1 addition & 2 deletions packages/liquidity-proxy/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions packages/liquidity-proxy/src/quote/band.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
6 changes: 5 additions & 1 deletion packages/liquidity-proxy/src/quote/oracleProxy.ts
Original file line number Diff line number Diff line change
@@ -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);
};
22 changes: 19 additions & 3 deletions packages/liquidity-proxy/src/quote/xst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/liquidity-proxy/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export type QuotePaths = {
export type OracleRate = {
value: CodecString;
lastUpdated: number;
dynamicFee: CodecString;
};

export type QuotePayload = {
Expand Down
2 changes: 1 addition & 1 deletion packages/math/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/type-definitions/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions packages/types/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions packages/util/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions packages/util/src/swap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ const toBandRate = (o: Observable<Option<BandBandRate>>): Observable<OracleRate>
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 };
})
);

Expand Down

0 comments on commit 1e59cf9

Please sign in to comment.