@shapeshiftoss/swapper 11.4.0 → 11.4.1

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.
@@ -4,16 +4,25 @@ exports.getPriceRatio = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../../../api");
6
6
  const bignumber_1 = require("../../../utils/bignumber");
7
+ const isRune_1 = require("../../utils/isRune/isRune");
7
8
  const thorService_1 = require("../thorService");
8
9
  const getPriceRatio = async (deps, input) => {
9
10
  const { buyAssetId, sellAssetId } = input;
10
11
  try {
11
12
  const buyPoolId = caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAssetId });
12
13
  const sellPoolId = caip_1.adapters.assetIdToPoolAssetId({ assetId: sellAssetId });
13
- if (!buyPoolId || !sellPoolId) {
14
- throw new api_1.SwapError(`[getPriceRatio]: No thorchain pool found`, {
15
- code: api_1.SwapErrorTypes.RESPONSE_ERROR,
16
- details: { buyPoolId, sellPoolId },
14
+ if (!buyPoolId && !(0, isRune_1.isRune)(buyAssetId)) {
15
+ throw new api_1.SwapError(`[getPriceRatio]: No buyPoolId found for asset ${buyAssetId}`, {
16
+ code: api_1.SwapErrorTypes.POOL_NOT_FOUND,
17
+ fn: 'getPriceRatio',
18
+ details: { buyAssetId },
19
+ });
20
+ }
21
+ if (!sellPoolId && !(0, isRune_1.isRune)(sellAssetId)) {
22
+ throw new api_1.SwapError(`[getPriceRatio]: No sellPoolId found for asset ${sellAssetId}`, {
23
+ code: api_1.SwapErrorTypes.POOL_NOT_FOUND,
24
+ fn: 'getPriceRatio',
25
+ details: { sellAssetId },
17
26
  });
18
27
  }
19
28
  const { data: responseData } = await thorService_1.thorService.get(`${deps.daemonUrl}/lcd/thorchain/pools`);
@@ -17,8 +17,17 @@ const getSwapOutput = (inputAmount, pool, toRune) => {
17
17
  return numerator.div(denominator);
18
18
  };
19
19
  const getDoubleSwapOutput = (input, inputPool, outputPool) => {
20
- const runeToOutput = getSwapOutput(input, inputPool, true);
21
- return getSwapOutput(runeToOutput, outputPool, false);
20
+ if (inputPool && outputPool) {
21
+ const runeToOutput = getSwapOutput(input, inputPool, true);
22
+ return getSwapOutput(runeToOutput, outputPool, false);
23
+ }
24
+ if (inputPool && !outputPool) {
25
+ return getSwapOutput(input, inputPool, true);
26
+ }
27
+ if (!inputPool && outputPool) {
28
+ return getSwapOutput(input, outputPool, false);
29
+ }
30
+ return (0, bignumber_1.bn)(0); // We should never reach this, but this makes tsc happy
22
31
  };
23
32
  // https://docs.thorchain.org/how-it-works/prices
24
33
  // TODO this does not support swaps between native "RUNE"
@@ -48,14 +57,22 @@ const getTradeRate = async (sellAsset, buyAssetId, sellAmount, deps) => {
48
57
  });
49
58
  }
50
59
  const { data } = await thorService_1.thorService.get(`${deps.daemonUrl}/lcd/thorchain/pools`);
51
- const buyPool = buyPoolId && data.find((response) => response.asset === buyPoolId);
52
- const sellPool = sellPoolId && data.find((response) => response.asset === sellPoolId);
53
- if (!buyPool || !sellPool)
54
- throw new api_1.SwapError(`[getPriceRatio]: no pools found`, {
60
+ const buyPool = buyPoolId ? data.find((response) => response.asset === buyPoolId) : null;
61
+ const sellPool = sellPoolId ? data.find((response) => response.asset === sellPoolId) : null;
62
+ if (!buyPool && !(0, isRune_1.isRune)(buyAssetId)) {
63
+ throw new api_1.SwapError(`[getTradeRate]: no pools found`, {
64
+ code: api_1.SwapErrorTypes.POOL_NOT_FOUND,
65
+ fn: 'getTradeRate',
66
+ details: { buyPoolId, sellPoolId },
67
+ });
68
+ }
69
+ if (!sellPool && !(0, isRune_1.isRune)(sellAsset.assetId)) {
70
+ throw new api_1.SwapError(`[getTradeRate]: no pools found`, {
55
71
  code: api_1.SwapErrorTypes.POOL_NOT_FOUND,
56
- fn: 'getPriceRatio',
72
+ fn: 'getTradeRate',
57
73
  details: { buyPoolId, sellPoolId },
58
74
  });
75
+ }
59
76
  // All thorchain pool amounts are base 8 regardless of token precision
60
77
  const sellBaseAmount = (0, bignumber_1.bn)((0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)(sellAmount, sellAsset.precision), THOR_PRECISION));
61
78
  const outputAmountBase8 = getDoubleSwapOutput(sellBaseAmount, sellPool, buyPool);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "11.4.0",
3
+ "version": "11.4.1",
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",