Skip to content

Commit

Permalink
add quote subscription across all dexes (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-Polyakov authored Sep 11, 2023
1 parent b9d3d04 commit 9d86af6
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 31 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.21.8",
"version": "1.21.9",
"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.21.8"
"@sora-substrate/types": "1.21.9"
}
}
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.21.8",
"version": "1.21.9",
"license": "Apache-2.0",
"main": "./build/index.js",
"typings": "./build/index.d.ts",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sora-substrate/api": "1.21.8"
"@sora-substrate/api": "1.21.9"
}
}
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.21.8",
"version": "1.21.9",
"license": "Apache-2.0",
"main": "./build/index.js",
"typings": "./build/index.d.ts",
"publishConfig": {
"access": "public"
},
"dependencies": {
"@sora-substrate/math": "1.21.8"
"@sora-substrate/math": "1.21.9"
}
}
6 changes: 5 additions & 1 deletion packages/liquidity-proxy/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ export type SwapQuote = (
outputAssetAddress: string,
value: NumberLike,
isExchangeB: boolean,
selectedSources: LiquiditySourceTypes[],
deduceFee: boolean
) => SwapResult;
) => {
result: SwapResult;
dexId: number;
};

export type QuotePaths = {
[key: string]: Array<LiquiditySourceTypes>;
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.21.8",
"version": "1.21.9",
"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.21.8",
"version": "1.21.9",
"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.21.8",
"version": "1.21.9",
"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.21.8"
"@sora-substrate/type-definitions": "1.21.9"
},
"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.21.8",
"version": "1.21.9",
"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.21.8",
"@sora-substrate/connection": "1.21.8",
"@sora-substrate/liquidity-proxy": "1.21.8",
"@sora-substrate/math": "1.21.8",
"@sora-substrate/types": "1.21.8",
"@sora-substrate/api": "1.21.9",
"@sora-substrate/connection": "1.21.9",
"@sora-substrate/liquidity-proxy": "1.21.9",
"@sora-substrate/math": "1.21.9",
"@sora-substrate/types": "1.21.9",
"axios": "^0.21.1",
"crypto-js": "^4.0.0",
"lodash": "^4.17.15"
Expand Down
125 changes: 111 additions & 14 deletions packages/util/src/swap/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from '@polkadot/util';
import isEmpty from 'lodash/fp/isEmpty';
import { combineLatest, map, distinctUntilChanged } from 'rxjs';
import { NumberLike, FPNumber, CodecString } from '@sora-substrate/math';
import {
Expand Down Expand Up @@ -33,6 +34,12 @@ import { Api } from '../api';
import type { AccountAsset, Asset } from '../assets/types';
import type { ReceiverHistoryItem, SwapTransferBatchData } from './types';

type SwapQuoteData = {
quote: SwapQuote;
isAvailable: boolean;
liquiditySources: LiquiditySourceTypes[];
};

const comparator = <T>(prev: T, curr: T): boolean => JSON.stringify(prev) === JSON.stringify(curr);

const toAssetId = (o: Observable<CommonPrimitivesAssetId32>): Observable<string> =>
Expand Down Expand Up @@ -453,32 +460,31 @@ export class SwapModule<T> {
* Get observable liquidity proxy quote function for two assets
* @param firstAssetAddress First swap token address
* @param secondAssetAddress Second swap token address
* @param selectedSources Selected liquidity sources for swap (not selected by default)
* @param sources Liquidity sources available for swap (all sources by default)
* @param dexId Selected Dex Id
*/
public async getSwapQuoteObservable(
firstAssetAddress: string,
secondAssetAddress: string,
selectedSources: LiquiditySourceTypes[] = [],
sources: LiquiditySourceTypes[] = [],
dexId = DexId.XOR
): Promise<Observable<SwapQuote>> {
const dexReservesObservable = await this.subscribeOnReserves(
firstAssetAddress,
secondAssetAddress,
selectedSources,
dexId
);
): Promise<Observable<SwapQuoteData>> {
const dexReservesObservable = await this.subscribeOnReserves(firstAssetAddress, secondAssetAddress, sources, dexId);

const quoteFnObservable = dexReservesObservable.pipe(
const swapQuoteObservable = dexReservesObservable.pipe(
map((payload) => {
return (
const { assetPaths, liquiditySources } = payload.sources;
const isAvailable = !isEmpty(assetPaths) && Object.values(assetPaths).every((paths) => !isEmpty(paths));

const quote: SwapQuote = (
inputAssetAddress: string,
outputAssetAddress: string,
value: NumberLike,
isExchangeB: boolean,
selectedSources: LiquiditySourceTypes[] = [],
deduceFee = true
) =>
this.getResult(
) => {
const result = this.getResult(
inputAssetAddress,
outputAssetAddress,
value,
Expand All @@ -488,10 +494,101 @@ export class SwapModule<T> {
dexId,
deduceFee
);

return { result, dexId };
};

return {
quote,
isAvailable,
liquiditySources,
};
})
);

return swapQuoteObservable;
}

/**
* Get observable liquidity proxy quote function for two assets across all Dexes
* @param firstAssetAddress First swap token address
* @param secondAssetAddress Second swap token address
* @param sources Liquidity sources for swap (all sources by default)
*/
public async getDexesSwapQuoteObservable(
firstAssetAddress: string,
secondAssetAddress: string,
sources: LiquiditySourceTypes[] = []
): Promise<Observable<SwapQuoteData>> {
const observables: Observable<SwapQuoteData>[] = [];

for (const { dexId } of this.root.dex.dexList) {
const swapQuoteDataObservable = await this.getSwapQuoteObservable(
firstAssetAddress,
secondAssetAddress,
sources,
dexId
);

observables.push(swapQuoteDataObservable);
}

const aggregated = combineLatest(observables).pipe(
map((swapQuoteData) => {
const isAvailable = swapQuoteData.some(({ isAvailable }) => !!isAvailable);
const liquiditySources = [...new Set(swapQuoteData.map(({ liquiditySources }) => liquiditySources).flat(1))];
const quote: SwapQuote = (
inputAssetAddress: string,
outputAssetAddress: string,
value: NumberLike,
isExchangeB: boolean,
selectedSources: LiquiditySourceTypes[] = [],
deduceFee = true
) => {
let bestDexId: number = DexId.XOR;

const results = swapQuoteData.reduce<{ [dexId: number]: SwapResult }>((buffer, { quote }) => {
const { dexId, result } = quote(
inputAssetAddress,
outputAssetAddress,
value,
isExchangeB,
selectedSources,
deduceFee
);

return { ...buffer, [dexId]: result };
}, {});

for (const currentDexId in results) {
const currAmount = FPNumber.fromCodecValue(results[currentDexId].amount);
const bestAmount = FPNumber.fromCodecValue(results[bestDexId].amount);

if (currAmount.isZero()) continue;

if (
(FPNumber.isLessThan(currAmount, bestAmount) && isExchangeB) ||
(FPNumber.isLessThan(bestAmount, currAmount) && !isExchangeB)
) {
bestDexId = +currentDexId;
}
}

return {
dexId: bestDexId,
result: results[bestDexId],
};
};

return {
quote,
isAvailable,
liquiditySources,
};
})
);

return quoteFnObservable;
return aggregated;
}

private calcTxParams(
Expand Down

0 comments on commit 9d86af6

Please sign in to comment.