@galacticcouncil/sdk-next 1.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +141 -0
  2. package/build/index.cjs +1 -0
  3. package/build/index.mjs +1 -0
  4. package/build/types/api/Papi.d.ts +375 -0
  5. package/build/types/api/client.d.ts +2 -0
  6. package/build/types/api/index.d.ts +2 -0
  7. package/build/types/client/AssetClient.d.ts +24 -0
  8. package/build/types/client/BalanceClient.d.ts +17 -0
  9. package/build/types/client/index.d.ts +2 -0
  10. package/build/types/consts.d.ts +8 -0
  11. package/build/types/errors.d.ts +10 -0
  12. package/build/types/index.d.ts +8 -0
  13. package/build/types/pool/PoolClient.d.ts +25 -0
  14. package/build/types/pool/PoolContextProvider.d.ts +26 -0
  15. package/build/types/pool/PoolFactory.d.ts +4 -0
  16. package/build/types/pool/index.d.ts +7 -0
  17. package/build/types/pool/lbp/LbpMath.d.ts +7 -0
  18. package/build/types/pool/lbp/LbpPool.d.ts +49 -0
  19. package/build/types/pool/lbp/LbpPoolClient.d.ts +18 -0
  20. package/build/types/pool/lbp/index.d.ts +3 -0
  21. package/build/types/pool/omni/OmniMath.d.ts +19 -0
  22. package/build/types/pool/omni/OmniPool.d.ts +45 -0
  23. package/build/types/pool/omni/OmniPoolClient.d.ts +13 -0
  24. package/build/types/pool/omni/index.d.ts +3 -0
  25. package/build/types/pool/stable/StableMath.d.ts +14 -0
  26. package/build/types/pool/stable/StableSwap.d.ts +46 -0
  27. package/build/types/pool/stable/StableSwapClient.d.ts +16 -0
  28. package/build/types/pool/stable/index.d.ts +3 -0
  29. package/build/types/pool/types.d.ts +84 -0
  30. package/build/types/pool/xyk/XykMath.d.ts +12 -0
  31. package/build/types/pool/xyk/XykPool.d.ts +23 -0
  32. package/build/types/pool/xyk/XykPoolClient.d.ts +12 -0
  33. package/build/types/pool/xyk/index.d.ts +3 -0
  34. package/build/types/sor/Router.d.ts +66 -0
  35. package/build/types/sor/TradeRouter.d.ts +155 -0
  36. package/build/types/sor/TradeUtils.d.ts +12 -0
  37. package/build/types/sor/index.d.ts +4 -0
  38. package/build/types/sor/route/bfs.d.ts +37 -0
  39. package/build/types/sor/route/graph.d.ts +12 -0
  40. package/build/types/sor/route/index.d.ts +3 -0
  41. package/build/types/sor/route/suggester.d.ts +24 -0
  42. package/build/types/sor/types.d.ts +31 -0
  43. package/build/types/types.d.ts +40 -0
  44. package/build/types/utils/Queue.d.ts +13 -0
  45. package/build/types/utils/Stack.d.ts +15 -0
  46. package/build/types/utils/big.d.ts +3 -0
  47. package/build/types/utils/evm.d.ts +3 -0
  48. package/build/types/utils/format.d.ts +4 -0
  49. package/build/types/utils/index.d.ts +6 -0
  50. package/build/types/utils/json.d.ts +3 -0
  51. package/build/types/utils/math.d.ts +62 -0
  52. package/build/types/utils/traversal/bfs.d.ts +27 -0
  53. package/build/types/utils/xc.d.ts +1 -0
  54. package/package.json +53 -0
@@ -0,0 +1,45 @@
1
+ import { BuyCtx, Pool, PoolBase, PoolFee, PoolFees, PoolPair, PoolToken, PoolType, SellCtx } from '../types';
2
+ export type OmniPoolPair = PoolPair & {
3
+ hubReservesIn: bigint;
4
+ hubReservesOut: bigint;
5
+ sharesIn: bigint;
6
+ sharesOut: bigint;
7
+ tradeableIn: number;
8
+ tradeableOut: number;
9
+ };
10
+ export type OmniPoolToken = PoolToken & {
11
+ cap: bigint;
12
+ hubReserves: bigint;
13
+ protocolShares: bigint;
14
+ shares: bigint;
15
+ };
16
+ export type OmniPoolFees = PoolFees & {
17
+ assetFee: PoolFee;
18
+ protocolFee: PoolFee;
19
+ };
20
+ export type OmniPoolBase = PoolBase & {
21
+ hubAssetId: number;
22
+ };
23
+ export declare class OmniPool implements Pool {
24
+ type: PoolType;
25
+ address: string;
26
+ tokens: OmniPoolToken[];
27
+ maxInRatio: bigint;
28
+ maxOutRatio: bigint;
29
+ minTradingLimit: bigint;
30
+ hubAssetId: number;
31
+ static fromPool(pool: OmniPoolBase): OmniPool;
32
+ constructor(address: string, tokens: OmniPoolToken[], maxInRation: bigint, maxOutRatio: bigint, minTradeLimit: bigint, hubAssetId: number);
33
+ validatePair(_tokenIn: number, tokenOut: number): boolean;
34
+ parsePair(tokenIn: number, tokenOut: number): OmniPoolPair;
35
+ validateAndBuy(poolPair: OmniPoolPair, amountOut: bigint, fees: OmniPoolFees): BuyCtx;
36
+ validateAndSell(poolPair: OmniPoolPair, amountIn: bigint, fees: OmniPoolFees): SellCtx;
37
+ calculateInGivenOut(poolPair: OmniPoolPair, amountOut: bigint, fees?: OmniPoolFees): bigint;
38
+ calculateLrnaInGivenOut(poolPair: OmniPoolPair, amountOut: bigint, fees?: OmniPoolFees): bigint;
39
+ calculateOutGivenIn(poolPair: OmniPoolPair, amountIn: bigint, fees?: OmniPoolFees): bigint;
40
+ calculateOutGivenLrnaIn(poolPair: OmniPoolPair, amountIn: bigint, fees?: OmniPoolFees): bigint;
41
+ spotPriceInGivenOut(poolPair: OmniPoolPair): bigint;
42
+ spotPriceLrnaInGivenOut(poolPair: OmniPoolPair): bigint;
43
+ spotPriceOutGivenIn(poolPair: OmniPoolPair): bigint;
44
+ spotPriceOutGivenLrnaIn(poolPair: OmniPoolPair): bigint;
45
+ }
@@ -0,0 +1,13 @@
1
+ import { type Observable } from 'rxjs';
2
+ import { PoolType, PoolFees } from '../types';
3
+ import { PoolClient } from '../PoolClient';
4
+ import { OmniPoolBase } from './OmniPool';
5
+ export declare class OmniPoolClient extends PoolClient<OmniPoolBase> {
6
+ protected loadPools(): Promise<OmniPoolBase[]>;
7
+ private getPoolAddress;
8
+ private getPoolLimits;
9
+ getPoolFees(_pool: OmniPoolBase, feeAsset: number): Promise<PoolFees>;
10
+ getPoolType(): PoolType;
11
+ isSupported(): Promise<boolean>;
12
+ subscribePoolChange(pool: OmniPoolBase): Observable<OmniPoolBase>;
13
+ }
@@ -0,0 +1,3 @@
1
+ export * from './OmniMath';
2
+ export * from './OmniPool';
3
+ export * from './OmniPoolClient';
@@ -0,0 +1,14 @@
1
+ export declare class StableMath {
2
+ static getPoolAddress(assetId: number): Uint8Array;
3
+ static defaultPegs(size: number): string[][];
4
+ static calculateAmplification(initialAmp: string, finalAmp: string, initialBlock: string, finalBlock: string, currentBlock: string): string;
5
+ static calculateInGivenOut(reserves: string, assetIn: number, assetOut: number, amountOut: string, amplification: string, fee: string, pegs: string): string;
6
+ static calculateAddOneAsset(reserves: string, shares: string, assetIn: number, amplification: string, shareIssuance: string, fee: string, pegs: string): string;
7
+ static calculateSharesForAmount(reserves: string, assetIn: number, amount: string, amplification: string, shareIssuance: string, fee: string, pegs: string): string;
8
+ static calculateOutGivenIn(reserves: string, assetIn: number, assetOut: number, amountIn: string, amplification: string, fee: string, pegs: string): string;
9
+ static calculateLiquidityOutOneAsset(reserves: string, shares: string, assetOut: number, amplification: string, shareIssuance: string, withdrawFee: string, pegs: string): string;
10
+ static calculateShares(reserves: string, assets: string, amplification: string, shareIssuance: string, fee: string, pegs: string): string;
11
+ static calculateSpotPriceWithFee(poolId: string, reserves: string, amplification: string, assetIn: string, assetOut: string, shareIssuance: string, fee: string, pegs: string): string;
12
+ static calculatePoolTradeFee(amount: string, feeNumerator: number, feeDenominator: number): string;
13
+ static recalculatePegs(currentPegs: string, targetPegs: string, currentBlock: string, maxPegUpdate: string, poolFee: string): string;
14
+ }
@@ -0,0 +1,46 @@
1
+ import { BuyCtx, Pool, PoolBase, PoolFee, PoolFees, PoolPair, PoolToken, PoolType, SellCtx } from '../types';
2
+ export type StableSwapPair = PoolPair & {
3
+ tradeableIn: number;
4
+ tradeableOut: number;
5
+ };
6
+ export type StableSwapFees = PoolFees & {
7
+ fee: PoolFee;
8
+ };
9
+ export type StableSwapBase = PoolBase & {
10
+ amplification: bigint;
11
+ id: string;
12
+ fee: PoolFee;
13
+ totalIssuance: bigint;
14
+ };
15
+ export declare class StableSwap implements Pool {
16
+ type: PoolType;
17
+ address: string;
18
+ tokens: PoolToken[];
19
+ maxInRatio: bigint;
20
+ maxOutRatio: bigint;
21
+ minTradingLimit: bigint;
22
+ amplification: bigint;
23
+ id: number;
24
+ fee: PoolFee;
25
+ totalIssuance: bigint;
26
+ static fromPool(pool: StableSwapBase): StableSwap;
27
+ constructor(address: string, tokens: PoolToken[], maxInRation: bigint, maxOutRatio: bigint, minTradeLimit: bigint, amplification: bigint, id: number, fee: PoolFee, totalIssuance: bigint);
28
+ validatePair(_tokenIn: number, _tokenOut: number): boolean;
29
+ parsePair(tokenIn: number, tokenOut: number): StableSwapPair;
30
+ validateAndBuy(poolPair: StableSwapPair, amountOut: bigint, fees: StableSwapFees): BuyCtx;
31
+ validateAndSell(poolPair: StableSwapPair, amountIn: bigint, fees: StableSwapFees): SellCtx;
32
+ private calculateIn;
33
+ private calculateAddOneAsset;
34
+ private calculateSharesForAmount;
35
+ calculateInGivenOut(poolPair: PoolPair, amountOut: bigint, fees?: StableSwapFees): bigint;
36
+ spotPriceInGivenOut(poolPair: PoolPair): bigint;
37
+ private calculateOut;
38
+ private calculateWithdrawOneAsset;
39
+ private calculateShares;
40
+ calculateOutGivenIn(poolPair: PoolPair, amountIn: bigint, fees?: StableSwapFees): bigint;
41
+ spotPriceOutGivenIn(poolPair: PoolPair): bigint;
42
+ calculateTradeFee(amount: bigint, fees: StableSwapFees): bigint;
43
+ private getPegs;
44
+ private getReserves;
45
+ private getAssets;
46
+ }
@@ -0,0 +1,16 @@
1
+ import { type Observable } from 'rxjs';
2
+ import { PoolType, PoolFees } from '../types';
3
+ import { PoolClient } from '../PoolClient';
4
+ import { StableSwapBase } from './StableSwap';
5
+ export declare class StableSwapClient extends PoolClient<StableSwapBase> {
6
+ private poolsData;
7
+ protected loadPools(): Promise<StableSwapBase[]>;
8
+ private getPoolDelta;
9
+ private getPoolTokens;
10
+ private getPoolAddress;
11
+ private getPoolLimits;
12
+ getPoolFees(pool: StableSwapBase): Promise<PoolFees>;
13
+ getPoolType(): PoolType;
14
+ isSupported(): Promise<boolean>;
15
+ subscribePoolChange(pool: StableSwapBase): Observable<StableSwapBase>;
16
+ }
@@ -0,0 +1,3 @@
1
+ export * from './StableMath';
2
+ export * from './StableSwap';
3
+ export * from './StableSwapClient';
@@ -0,0 +1,84 @@
1
+ import type { AssetType } from '../types';
2
+ export declare enum PoolType {
3
+ Aave = "Aave",
4
+ LBP = "LBP",
5
+ Omni = "Omnipool",
6
+ Stable = "Stableswap",
7
+ XYK = "XYK"
8
+ }
9
+ export declare enum PoolError {
10
+ InsufficientTradingAmount = "InsufficientTradingAmount",
11
+ MaxInRatioExceeded = "MaxInRatioExceeded",
12
+ MaxOutRatioExceeded = "MaxOutRatioExceeded",
13
+ TradeNotAllowed = "TradeNotAllowed",
14
+ UnknownError = "UnknownError"
15
+ }
16
+ export interface PoolPair {
17
+ assetIn: number;
18
+ assetOut: number;
19
+ decimalsIn: number;
20
+ decimalsOut: number;
21
+ balanceIn: bigint;
22
+ balanceOut: bigint;
23
+ assetInEd: bigint;
24
+ assetOutEd: bigint;
25
+ }
26
+ export type PoolBase = {
27
+ address: string;
28
+ id?: number;
29
+ type: PoolType;
30
+ tokens: PoolToken[];
31
+ maxInRatio: bigint;
32
+ maxOutRatio: bigint;
33
+ minTradingLimit: bigint;
34
+ };
35
+ export interface PoolToken {
36
+ id: number;
37
+ balance: bigint;
38
+ decimals?: number;
39
+ existentialDeposit: bigint;
40
+ tradeable?: number;
41
+ type: AssetType;
42
+ }
43
+ export type PoolTokenOverride = Pick<PoolToken, 'id' | 'decimals'>;
44
+ export type PoolLimits = Pick<PoolBase, 'maxInRatio' | 'maxOutRatio' | 'minTradingLimit'>;
45
+ export type PoolFee = [numerator: number, denominator: number];
46
+ export type PoolFees = {
47
+ min?: PoolFee;
48
+ max?: PoolFee;
49
+ };
50
+ export type PoolSell = {
51
+ calculatedOut: bigint;
52
+ };
53
+ export type PoolBuy = {
54
+ calculatedIn: bigint;
55
+ };
56
+ export type PoolSwap = {
57
+ amountIn: bigint;
58
+ amountOut: bigint;
59
+ feePct: number;
60
+ errors: PoolError[];
61
+ };
62
+ export type SellCtx = PoolSwap & PoolSell;
63
+ export type BuyCtx = PoolSwap & PoolBuy;
64
+ export interface Pool extends PoolBase {
65
+ validatePair(tokenIn: number, tokenOut: number): boolean;
66
+ parsePair(tokenIn: number, tokenOut: number): PoolPair;
67
+ validateAndBuy(poolPair: PoolPair, amountOut: bigint, dynamicFees: PoolFees | null): BuyCtx;
68
+ validateAndSell(poolPair: PoolPair, amountOut: bigint, dynamicFees: PoolFees | null): SellCtx;
69
+ calculateInGivenOut(poolPair: PoolPair, amountOut: bigint): bigint;
70
+ calculateOutGivenIn(poolPair: PoolPair, amountIn: bigint): bigint;
71
+ spotPriceInGivenOut(poolPair: PoolPair): bigint;
72
+ spotPriceOutGivenIn(poolPair: PoolPair): bigint;
73
+ }
74
+ export interface IPoolCtxProvider {
75
+ getPools(): Promise<PoolBase[]>;
76
+ getPoolFees(pool: PoolBase, feeAsset: number): Promise<PoolFees>;
77
+ }
78
+ export type Hop = {
79
+ pool: PoolType;
80
+ poolAddress: string;
81
+ poolId?: number;
82
+ assetIn: number;
83
+ assetOut: number;
84
+ };
@@ -0,0 +1,12 @@
1
+ export declare class XykMath {
2
+ static getSpotPrice(balanceA: string, balanceB: string, amount: string): string;
3
+ static calculateInGivenOut(balanceIn: string, balanceOut: string, amountOut: string): string;
4
+ static calculateOutGivenIn(balanceIn: string, balanceOut: string, amountIn: string): string;
5
+ static calculatePoolTradeFee(amount: string, feeNumerator: number, feeDenominator: number): string;
6
+ static calculateLiquidityIn(reserveA: string, reserveB: string, amountA: string): string;
7
+ static calculateSpotPrice(balanceA: string, balanceB: string): string;
8
+ static calculateSpotPriceWithFee(balanceA: string, balanceB: string, feeNumerator: string, feeDenominator: string): string;
9
+ static calculateShares(reserveA: string, amountA: string, totalShares: string): string;
10
+ static calculateLiquidityOutAssetA(reserveA: string, reserveB: string, shares: string, totalShares: string): string;
11
+ static calculateLiquidityOutAssetB(reserveA: string, reserveB: string, shares: string, totalShares: string): string;
12
+ }
@@ -0,0 +1,23 @@
1
+ import { BuyCtx, Pool, PoolBase, PoolFee, PoolFees, PoolPair, PoolToken, PoolType, SellCtx } from '../types';
2
+ export type XykPoolFees = PoolFees & {
3
+ exchangeFee: PoolFee;
4
+ };
5
+ export declare class XykPool implements Pool {
6
+ type: PoolType;
7
+ address: string;
8
+ tokens: PoolToken[];
9
+ maxInRatio: bigint;
10
+ maxOutRatio: bigint;
11
+ minTradingLimit: bigint;
12
+ static fromPool(pool: PoolBase): XykPool;
13
+ constructor(address: string, tokens: PoolToken[], maxInRation: bigint, maxOutRatio: bigint, minTradeLimit: bigint);
14
+ validatePair(_tokenIn: number, _tokenOut: number): boolean;
15
+ parsePair(tokenIn: number, tokenOut: number): PoolPair;
16
+ validateAndBuy(poolPair: PoolPair, amountOut: bigint, fees: XykPoolFees): BuyCtx;
17
+ validateAndSell(poolPair: PoolPair, amountIn: bigint, fees: XykPoolFees): SellCtx;
18
+ calculateInGivenOut(poolPair: PoolPair, amountOut: bigint): bigint;
19
+ calculateOutGivenIn(poolPair: PoolPair, amountIn: bigint): bigint;
20
+ spotPriceInGivenOut(poolPair: PoolPair): bigint;
21
+ spotPriceOutGivenIn(poolPair: PoolPair): bigint;
22
+ calculateTradeFee(amount: bigint, fees: XykPoolFees): bigint;
23
+ }
@@ -0,0 +1,12 @@
1
+ import { type Observable } from 'rxjs';
2
+ import { PoolBase, PoolType, PoolFees } from '../types';
3
+ import { PoolClient } from '../PoolClient';
4
+ export declare class XykPoolClient extends PoolClient<PoolBase> {
5
+ protected loadPools(): Promise<PoolBase[]>;
6
+ private getExchangeFee;
7
+ private getPoolLimits;
8
+ getPoolFees(): Promise<PoolFees>;
9
+ getPoolType(): PoolType;
10
+ isSupported(): Promise<boolean>;
11
+ subscribePoolChange(pool: PoolBase): Observable<PoolBase>;
12
+ }
@@ -0,0 +1,3 @@
1
+ export * from './XykMath';
2
+ export * from './XykPool';
3
+ export * from './XykPoolClient';
@@ -0,0 +1,66 @@
1
+ import { Hop, IPoolCtxProvider, Pool, PoolBase, PoolType } from '../pool';
2
+ export type RouterOptions = {
3
+ useOnly: PoolType[];
4
+ };
5
+ export declare class Router {
6
+ private readonly routeSuggester;
7
+ private readonly routerOptions;
8
+ protected readonly ctx: IPoolCtxProvider;
9
+ private readonly defaultRouterOptions;
10
+ constructor(ctx: IPoolCtxProvider, routerOptions?: RouterOptions);
11
+ /**
12
+ * List trading pools
13
+ */
14
+ getPools(): Promise<PoolBase[]>;
15
+ /**
16
+ * List all possible trading routes for given pair
17
+ *
18
+ * @param {number} assetIn - assetIn id
19
+ * @param {number} assetOut - assetOut id
20
+ */
21
+ getRoutes(assetIn: number, assetOut: number): Promise<Hop[][]>;
22
+ /**
23
+ * List tradeable assets
24
+ */
25
+ getTradeableAssets(): Promise<number[]>;
26
+ /**
27
+ * Ckeck if asset pair is valid
28
+ *
29
+ * @param {number} assetIn - assetIn id
30
+ * @param {number} assetOut - assetOut id
31
+ * @param {PoolBase[]} pools - trading pools
32
+ */
33
+ protected validateInput(assetIn: number, assetOut: number, pools: PoolBase[]): Map<string, Pool>;
34
+ /**
35
+ * List tradeable assets ASC
36
+ *
37
+ * @param {PoolBase[]} pools - trading pools
38
+ */
39
+ protected getAssets(pools: PoolBase[]): Set<number>;
40
+ /**
41
+ * List all possible routes between assetIn & assetOut
42
+ *
43
+ * @param {number} assetIn - assetIn id
44
+ * @param {number} assetOut - assetOut id
45
+ * @param {PoolBase[]} pools - trading pools
46
+ */
47
+ protected getPaths(assetIn: number, assetOut: number | null, pools: PoolBase[]): Hop[][];
48
+ /**
49
+ * Check if path is valid -> all edges are valid asset pairs
50
+ *
51
+ * @param proposedPath - proposed path
52
+ * @param poolsMap - pools map
53
+ * @returns only valid paths
54
+ */
55
+ private validPath;
56
+ /**
57
+ * Check if edge (asset pair) of corresponding pool is valid
58
+ *
59
+ * @param edge - current edge (asset pair)
60
+ * @param poolsMap - pools map
61
+ * @returns true if edge (asset pair) is valid, otherwise false
62
+ */
63
+ private validEdge;
64
+ private toPoolsMap;
65
+ private toHops;
66
+ }
@@ -0,0 +1,155 @@
1
+ import { Router } from './Router';
2
+ import { Trade } from './types';
3
+ import { Hop } from '../pool';
4
+ import { Amount } from '../types';
5
+ export declare class TradeRouter extends Router {
6
+ /**
7
+ * Check whether trade is direct or not
8
+ *
9
+ * @param {Swap[]} swaps - trade route swaps
10
+ * @returns true if direct trade, otherwise false
11
+ */
12
+ private isDirectTrade;
13
+ /**
14
+ * Find the best sell swap without errors
15
+ *
16
+ * @param {SellSwap[]} swaps - all possible sell routes
17
+ * @returns best sell swap if exist, otherwise first one found
18
+ */
19
+ private findBestSellRoute;
20
+ /**
21
+ * Route fee range [min,max] in case pool is using dynamic fees
22
+ *
23
+ * @param {Swap[]} swaps - trade routes
24
+ * @returns min & max fee range if swapping through the pool with
25
+ * dynamic fees support
26
+ */
27
+ private getRouteFeeRange;
28
+ /**
29
+ * Pool fee range [min,max] in case pool is using dynamic fees
30
+ *
31
+ * @param {PoolFees} fees - pool fees
32
+ * @returns min & max fee range if swapping through the pool with
33
+ * dynamic fees support
34
+ */
35
+ private getPoolFeeRange;
36
+ /**
37
+ * Calculate and return best possible sell trade for assetIn>assetOut
38
+ *
39
+ * @param {string} assetIn - assetIn id
40
+ * @param {string} assetOut - assetOut id
41
+ * @param {bigint} amountIn - amount of assetIn to sell for assetOut
42
+ * @returns best possible sell trade of given token pair
43
+ */
44
+ getBestSell(assetIn: number, assetOut: number, amountIn: bigint | string): Promise<Trade>;
45
+ /**
46
+ * Calculate and return sell spot price for assetIn>assetOut
47
+ *
48
+ * @param route - best possible trade route (sell)
49
+ * @returns sell spot price
50
+ */
51
+ private getSellSpot;
52
+ /**
53
+ * Calculate and return sell trade for assetIn>assetOut
54
+ *
55
+ * @param {string} assetIn - assetIn id
56
+ * @param {string} assetOut - assetOut id
57
+ * @param {bigint} amountIn - amount of assetIn to sell for assetOut
58
+ * @param {Hop[]} route - explicit route to use for trade
59
+ * @returns sell trade of given token pair
60
+ */
61
+ getSell(assetIn: number, assetOut: number, amountIn: bigint | string, route?: Hop[]): Promise<Trade>;
62
+ /**
63
+ * Calculate the amount out for best possible trade if fees are zero
64
+ *
65
+ * @param amountIn - amount of assetIn to sell for assetOut
66
+ * @param route - best possible trade route (sell)
67
+ * @param poolsMap - pools map
68
+ * @returns the amount out for best possible trade if fees are zero
69
+ */
70
+ private calculateDelta0Y;
71
+ /**
72
+ * Calculate and return sell swaps for given path
73
+ * - final amount of previous swap is entry to next one
74
+ *
75
+ * @param amountIn - amount of assetIn to sell for assetOut
76
+ * @param path - current path
77
+ * @param poolsMap - pools map
78
+ * @returns sell swaps for given path
79
+ */
80
+ private toSellSwaps;
81
+ /**
82
+ * Calculate and return most liquid route for tokenIn>tokenOut
83
+ *
84
+ * To avoid routing through the pools with low liquidity, 0.1% from the
85
+ * most liquid pool asset is used as reference value to determine the
86
+ * sweet spot.
87
+ *
88
+ * @param {number} assetIn - assetIn id
89
+ * @param {number} assetOut - assetout id
90
+ * @return Most liquid route of given token pair
91
+ */
92
+ getMostLiquidRoute(assetIn: number, assetOut: number): Promise<Hop[]>;
93
+ /**
94
+ * Calculate and return best spot price for tokenIn>tokenOut
95
+ *
96
+ * @param {number} assetIn - assetIn id
97
+ * @param {number} assetOut - assetOut id
98
+ * @return best possible spot price of given asset pair, or undefined
99
+ * if given pair swap is not supported
100
+ */
101
+ getSpotPrice(assetIn: number, assetOut: number): Promise<Amount>;
102
+ /**
103
+ * Find the best buy swap without errors, if there is none return first one found
104
+ *
105
+ * @param {BuySwap[]} swaps - all possible buy routes
106
+ * @returns best buy swap if exist, otherwise first one found
107
+ */
108
+ private findBestBuyRoute;
109
+ /**
110
+ * Calculate and return best possible buy trade for assetIn>assetOut
111
+ *
112
+ * @param {number} assetIn - assetIn id
113
+ * @param {number} assetOut - assetOut id
114
+ * @param {bigint} amountOut - amount of tokenOut to buy for tokenIn
115
+ * @returns best possible buy trade of given token pair
116
+ */
117
+ getBestBuy(assetIn: number, assetOut: number, amountOut: bigint | string): Promise<Trade>;
118
+ /**
119
+ * Calculate and return buy spot price for assetIn>assetOut
120
+ *
121
+ * @param route - best possible trade route (buy)
122
+ * @returns buy spot price
123
+ */
124
+ private getBuySpot;
125
+ /**
126
+ * Calculate and return buy trade for assetIn>assetOut
127
+ *
128
+ * @param {number} assetIn - assetIn id
129
+ * @param {number} assetOut - assetOut id
130
+ * @param {bigint} amountOut - amount of tokenOut to buy for tokenIn
131
+ * @param {Hop[]} route - explicit route to use for trade
132
+ * @returns buy trade of given token pair
133
+ */
134
+ getBuy(assetIn: number, assetOut: number, amountOut: bigint | string, route?: Hop[]): Promise<Trade>;
135
+ /**
136
+ * Calculate the amount in for best possible trade if fees are zero
137
+ *
138
+ * @param amountOut - amount of assetOut to buy for assetIn
139
+ * @param bestRoute - best possible trade route (buy)
140
+ * @param poolsMap - pools map
141
+ * @returns the amount in for best possible trade if fees are zero
142
+ */
143
+ private calculateDelta0X;
144
+ /**
145
+ * Calculate and return buy swaps for given path
146
+ * - final amount of previous swap is entry to next one
147
+ * - calculation is done backwards (swaps in reversed order)
148
+ *
149
+ * @param amountOut - amount of assetOut to buy for assetIn
150
+ * @param path - current path
151
+ * @param poolsMap - pools map
152
+ * @returns buy swaps for given path
153
+ */
154
+ private toBuySwaps;
155
+ }
@@ -0,0 +1,12 @@
1
+ import { Binary, PolkadotClient } from 'polkadot-api';
2
+ import { Papi } from '../api';
3
+ import { Trade } from './types';
4
+ export declare class TradeUtils extends Papi {
5
+ constructor(client: PolkadotClient);
6
+ private isDirectOmnipoolTrade;
7
+ private tradeCheck;
8
+ buildBuyTx(trade: Trade, slippagePct?: number): Promise<Binary>;
9
+ buildSellTx(trade: Trade, slippagePct?: number): Promise<Binary>;
10
+ buildSellAllTx(trade: Trade, slippagePct?: number): Promise<Binary>;
11
+ private buildRoute;
12
+ }
@@ -0,0 +1,4 @@
1
+ export { Router } from './Router';
2
+ export { TradeRouter } from './TradeRouter';
3
+ export { TradeUtils } from './TradeUtils';
4
+ export * from './types';
@@ -0,0 +1,37 @@
1
+ export type Path = Node[];
2
+ export type Node = [id: number, from: string];
3
+ /**
4
+ * Breadth First Search.
5
+ *
6
+ * - uses Queue to find the shortest path
7
+ * - slower than DFS (Depth First Search)
8
+ * - better when dst is closer to src
9
+ * - complexity O(N+E) where N are nodes and E are edges
10
+ */
11
+ export declare class Bfs {
12
+ /**
13
+ * Check if current node is present in path or traversal within the same pool was already done
14
+ *
15
+ * @param x - current node
16
+ * @param path - path
17
+ * @returns true if node in path, otherwise false
18
+ */
19
+ isNotVisited(x: Node, path: Path): boolean;
20
+ /**
21
+ * Finding paths in graph from given source to destination
22
+ *
23
+ * @param g - routes graph containing nodes & corresponding edges
24
+ * @param src - source node
25
+ * @param dst - destination node or null if requesting all posible paths from src
26
+ * @returns paths
27
+ */
28
+ findPaths(g: Map<number, Path>, src: number, dst: number | null): Path[];
29
+ /**
30
+ * Build and populate graph
31
+ *
32
+ * @param nodes - list of pool assets
33
+ * @param edges - list of all edges [id, from, to] between assets
34
+ * @returns - traversal graph
35
+ */
36
+ buildAndPopulateGraph(nodes: string[], edges: [string, number, number][]): Map<number, Path>;
37
+ }
@@ -0,0 +1,12 @@
1
+ import { PoolBase } from '../../pool';
2
+ export type Edge = [address: string, from: number, to: number];
3
+ export type NodeEdges = {
4
+ [node: string]: Edge[];
5
+ };
6
+ /**
7
+ * Calculate nodes & edges from substrate pools
8
+ *
9
+ * @param pools - given substrate pools
10
+ * @returns nodes & corresponding edges
11
+ */
12
+ export declare function getNodesAndEdges(pools: PoolBase[]): NodeEdges;
@@ -0,0 +1,3 @@
1
+ export { Bfs, Path } from './bfs';
2
+ export { Edge, getNodesAndEdges } from './graph';
3
+ export { RouteSuggester } from './suggester';
@@ -0,0 +1,24 @@
1
+ import { Edge } from './graph';
2
+ import { PoolBase } from '../../pool';
3
+ export declare class RouteSuggester {
4
+ /**
5
+ * Proposals are ideal paths from
6
+ * 1) tokenIn to tokenOut
7
+ * 2) tokenIn to *(all possible paths are requested)
8
+ *
9
+ * calculated from all permutations of tokens of given pools.
10
+ *
11
+ * E.g. permutation of pool A={1,3} is 2, such as {1,3}, {3,1} where 1 are 3
12
+ * are pool assets(tokens)
13
+ *
14
+ * Filtering of valid paths and corresponding asset pairs is done by router itself!!!
15
+ *
16
+ * @param tokenIn - tokenIn
17
+ * @param tokenOut - tokenOut or null if all possible paths from tokenIn are requested
18
+ * @param pools - substrate based pools
19
+ * @returns all possible path proposals
20
+ */
21
+ getProposals(tokenIn: number, tokenOut: number | null, pools: PoolBase[]): Edge[][];
22
+ private parsePaths;
23
+ private toEdge;
24
+ }
@@ -0,0 +1,31 @@
1
+ import type { Hop, PoolBuy, PoolError, PoolSell } from '../pool';
2
+ export interface Humanizer {
3
+ toHuman(): any;
4
+ }
5
+ export type Swap = Hop & Humanizer & {
6
+ assetInDecimals: number;
7
+ assetOutDecimals: number;
8
+ amountIn: bigint;
9
+ amountOut: bigint;
10
+ spotPrice: bigint;
11
+ tradeFeePct: number;
12
+ tradeFeeRange?: [number, number];
13
+ priceImpactPct: number;
14
+ errors: PoolError[];
15
+ };
16
+ export type SellSwap = Swap & PoolSell;
17
+ export type BuySwap = Swap & PoolBuy;
18
+ export declare enum TradeType {
19
+ Buy = "Buy",
20
+ Sell = "Sell"
21
+ }
22
+ export interface Trade extends Humanizer {
23
+ type: TradeType;
24
+ amountIn: bigint;
25
+ amountOut: bigint;
26
+ spotPrice: bigint;
27
+ tradeFee: bigint;
28
+ tradeFeePct: number;
29
+ priceImpactPct: number;
30
+ swaps: Swap[];
31
+ }