@shapeshiftoss/swapper 15.8.0 → 16.0.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.
package/dist/api.d.ts CHANGED
@@ -73,9 +73,15 @@ export declare type ByPairInput = {
73
73
  sellAssetId: AssetId;
74
74
  buyAssetId: AssetId;
75
75
  };
76
- export declare type GetBestSwapperArgs = GetTradeQuoteInput & {
76
+ export declare type GetSwappersWithQuoteMetadataArgs = GetTradeQuoteInput & {
77
77
  feeAsset: Asset;
78
78
  };
79
+ export declare type SwapperWithQuoteMetadata = {
80
+ swapper: Swapper<ChainId>;
81
+ quote: TradeQuote<ChainId>;
82
+ inputOutputRatio: number | undefined;
83
+ };
84
+ export declare type GetSwappersWithQuoteMetadataReturn = SwapperWithQuoteMetadata[];
79
85
  export declare type BuyAssetBySellIdInput = {
80
86
  sellAssetId: AssetId;
81
87
  assetIds: AssetId[];
@@ -1,5 +1,5 @@
1
1
  import { ChainId } from '@shapeshiftoss/caip';
2
- import { BuyAssetBySellIdInput, ByPairInput, GetBestSwapperArgs, SupportedSellAssetsInput, Swapper } from '..';
2
+ import { BuyAssetBySellIdInput, ByPairInput, GetSwappersWithQuoteMetadataArgs, GetSwappersWithQuoteMetadataReturn, SupportedSellAssetsInput, Swapper } from '..';
3
3
  import { SwapperType } from '../api';
4
4
  export declare class SwapperManager {
5
5
  swappers: Map<SwapperType, Swapper<ChainId>>;
@@ -24,10 +24,12 @@ export declare class SwapperManager {
24
24
  removeSwapper(swapperType: SwapperType): this;
25
25
  /**
26
26
  *
27
- * @param args {GetBestSwapperArgs}
28
- * @returns {Promise<Swapper<ChainId> | undefined>}
27
+ * Returns an ordered list of SwapperWithQuoteMetadata objects, descending from best to worst input output ratios
28
+ *
29
+ * @param args {GetSwappersWithQuoteMetadataArgs}
30
+ * @returns {Promise<GetSwappersWithQuoteMetadataReturn>}
29
31
  */
30
- getBestSwapper(args: GetBestSwapperArgs): Promise<Swapper<ChainId> | undefined>;
32
+ getSwappersWithQuoteMetadata(args: GetSwappersWithQuoteMetadataArgs): Promise<GetSwappersWithQuoteMetadataReturn>;
31
33
  /**
32
34
  *
33
35
  * @param pair type {GetQuoteInput}
@@ -62,47 +62,32 @@ class SwapperManager {
62
62
  }
63
63
  /**
64
64
  *
65
- * @param args {GetBestSwapperArgs}
66
- * @returns {Promise<Swapper<ChainId> | undefined>}
65
+ * Returns an ordered list of SwapperWithQuoteMetadata objects, descending from best to worst input output ratios
66
+ *
67
+ * @param args {GetSwappersWithQuoteMetadataArgs}
68
+ * @returns {Promise<GetSwappersWithQuoteMetadataReturn>}
67
69
  */
68
- async getBestSwapper(args) {
70
+ async getSwappersWithQuoteMetadata(args) {
69
71
  const { sellAsset, buyAsset, feeAsset } = args;
70
72
  // Get all swappers that support the pair
71
73
  const supportedSwappers = this.getSwappersByPair({
72
74
  sellAssetId: sellAsset.assetId,
73
75
  buyAssetId: buyAsset.assetId,
74
76
  });
75
- // Get quotes from all swappers that support the pair
76
- const quotePromises = supportedSwappers.map(async (swapper) => [swapper, await swapper.getTradeQuote(args)]);
77
- const settledQuoteRequests = await Promise.allSettled(quotePromises);
78
- // For each swapper, get receive amount/(input amount + gas fee), where all values are in fiat
79
- const fulfilledQuoteTuples = settledQuoteRequests
77
+ const settledSwapperDetailRequests = await Promise.allSettled(supportedSwappers.map(async (swapper) => {
78
+ const quote = await swapper.getTradeQuote(args);
79
+ const ratio = await (0, utils_1.getRatioFromQuote)(quote, swapper, feeAsset);
80
+ return {
81
+ swapper,
82
+ quote,
83
+ inputOutputRatio: ratio,
84
+ };
85
+ }));
86
+ // Swappers with quote and ratio details, sorted by descending input output ratio (best to worst)
87
+ const swappersWithDetail = (0, lodash_1.sortBy)(settledSwapperDetailRequests
80
88
  .filter(typeGuards_1.isFulfilled)
81
- .map((quoteRequest) => quoteRequest.value);
82
- // The best swapper is the one with the highest ratio
83
- const bestQuoteTuple = await fulfilledQuoteTuples.reduce(async (acc, currentQuoteTuple) => {
84
- const resolvedAcc = await acc;
85
- const [currentSwapper, currentQuote] = currentQuoteTuple;
86
- const currentRatio = await (0, utils_1.getRatioFromQuote)(currentQuote, currentSwapper, feeAsset);
87
- // It's our first iteration, so we just return the current SwapperQuoteTuple
88
- if (!resolvedAcc)
89
- return Promise.resolve([currentSwapper, currentRatio]);
90
- const [, bestRatio] = resolvedAcc;
91
- const isCurrentSwapperBestSwapper = (() => {
92
- // Happy path - no div by 0's in getRatioFromQuote evaluations so we have both ratios
93
- if (!(0, lodash_1.isUndefined)(currentRatio) && !(0, lodash_1.isUndefined)(bestRatio))
94
- return currentRatio > bestRatio;
95
- // We don't know, neither has a ratio, so we can't compare (big edge case div by 0 scenario)
96
- if ((0, lodash_1.isUndefined)(currentRatio) && (0, lodash_1.isUndefined)(bestRatio))
97
- return false;
98
- // We don't have a best, but we do have a current, so current is the new best
99
- return !(0, lodash_1.isUndefined)(currentRatio);
100
- })();
101
- const currentSwapperRatioTuple = [currentSwapper, currentRatio];
102
- return Promise.resolve(isCurrentSwapperBestSwapper ? currentSwapperRatioTuple : resolvedAcc);
103
- }, undefined);
104
- const bestSwapper = bestQuoteTuple === null || bestQuoteTuple === void 0 ? void 0 : bestQuoteTuple[0];
105
- return bestSwapper;
89
+ .map((swapperDetailRequest) => swapperDetailRequest.value), ['inputOutputRatio']).reverse();
90
+ return swappersWithDetail;
106
91
  }
107
92
  /**
108
93
  *
@@ -105,10 +105,11 @@ const main = async () => {
105
105
  explorerTxLink: 'https://etherscan.io/tx/',
106
106
  };
107
107
  const { quoteInput } = (0, setupSwapQuote_1.setupQuote)();
108
- const swapper = await swapManager.getBestSwapper({
108
+ const swappersWithQuoteMetadata = await swapManager.getSwappersWithQuoteMetadata({
109
109
  ...quoteInput,
110
110
  feeAsset: ethereumAsset,
111
111
  });
112
+ const swapper = swappersWithQuoteMetadata[0].swapper;
112
113
  console.info(`using swapper ${swapper === null || swapper === void 0 ? void 0 : swapper.getType()}`);
113
114
  if (!swapper) {
114
115
  console.warn(`no swapper found for specified assets`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "15.8.0",
3
+ "version": "16.0.0",
4
4
  "description": "An interface and implementations to trade coins via various protocols",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",