@shapeshiftoss/swapper 9.2.1 → 9.3.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.
@@ -75,6 +75,11 @@ async function getCowSwapTradeQuote(deps, input) {
75
75
  .div((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision))
76
76
  .multipliedBy((0, bignumber_1.bnOrZero)(sellAssetUsdRate))
77
77
  .toString();
78
+ // If original sellAmount is < minQuoteSellAmount, we don't want to replace it with normalizedSellAmount
79
+ // The purpose of this was to get a quote from CowSwap even with small amounts
80
+ const quoteSellAmount = (0, bignumber_1.bnOrZero)(sellAmount).lt(minQuoteSellAmount)
81
+ ? sellAmount
82
+ : normalizedSellAmount;
78
83
  return {
79
84
  rate,
80
85
  minimum,
@@ -90,7 +95,7 @@ async function getCowSwapTradeQuote(deps, input) {
90
95
  },
91
96
  tradeFee: tradeFeeFiat
92
97
  },
93
- sellAmount: normalizedSellAmount,
98
+ sellAmount: quoteSellAmount,
94
99
  buyAmount: quote.buyAmount,
95
100
  sources: constants_1.DEFAULT_SOURCE,
96
101
  allowanceContract: constants_1.COW_SWAP_VAULT_RELAYER_ADDRESS,
@@ -27,13 +27,6 @@ const getThorTradeQuote = async ({ deps, input }) => {
27
27
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
28
28
  details: { chainId }
29
29
  });
30
- const buyAssetId = buyAsset.assetId;
31
- const feeAssetId = (0, caip_1.getFeeAssetIdFromAssetId)(buyAssetId);
32
- if (!feeAssetId)
33
- throw new api_1.SwapError(`[getThorTradeQuote] - No feeAssetId found for ${buyAssetId}.`, {
34
- code: api_1.SwapErrorTypes.VALIDATION_FAILED,
35
- details: { buyAssetId }
36
- });
37
30
  const tradeRate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId, sellAmount, deps);
38
31
  const rate = (0, bignumber_1.bnOrZero)(1).div(tradeRate).toString();
39
32
  const buyAmount = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)((0, bignumber_1.fromBaseUnit)(sellAmount, sellAsset.precision)).times(tradeRate), buyAsset.precision);
@@ -16,10 +16,6 @@ const thorTradeApprovalNeeded = async ({ deps, input }) => {
16
16
  const { chainNamespace } = (0, caip_1.fromChainId)(sellAsset.chainId);
17
17
  if (chainNamespace !== caip_1.CHAIN_NAMESPACE.Ethereum)
18
18
  return { approvalNeeded: false };
19
- // No approval needed for selling a fee asset
20
- if (sellAsset.assetId === (0, caip_1.getFeeAssetIdFromAssetId)(sellAsset.assetId)) {
21
- return { approvalNeeded: false };
22
- }
23
19
  const accountNumber = quote.sellAssetAccountNumber;
24
20
  const adapter = adapterManager.get(sellAsset.chainId);
25
21
  if (!adapter)
@@ -27,6 +23,10 @@ const thorTradeApprovalNeeded = async ({ deps, input }) => {
27
23
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
28
24
  details: { chainId: sellAsset.chainId }
29
25
  });
26
+ // No approval needed for selling a fee asset
27
+ if (sellAsset.assetId === adapter.getFeeAssetId()) {
28
+ return { approvalNeeded: false };
29
+ }
30
30
  const bip44Params = adapter.buildBIP44Params({ accountNumber });
31
31
  const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
32
32
  if (!quote.allowanceContract) {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.estimateTradeFee = void 0;
4
+ const asset_service_1 = require("@shapeshiftoss/asset-service");
4
5
  const caip_1 = require("@shapeshiftoss/caip");
5
6
  const api_1 = require("../../../../api");
6
7
  const bignumber_1 = require("../../../utils/bignumber");
@@ -40,28 +41,35 @@ const estimateTradeFee = async (deps, buyAsset) => {
40
41
  details: { thorPoolChainId }
41
42
  });
42
43
  const gasRate = inboundInfo.gas_rate;
43
- const { chainId, assetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
44
- const feeAssetId = (0, caip_1.getFeeAssetIdFromAssetId)(buyAsset.assetId);
45
- if (!feeAssetId)
44
+ const { chainId: buyChainId, assetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
45
+ const buyAdapter = deps.adapterManager.get(buyChainId);
46
+ if (!buyAdapter)
47
+ throw new api_1.SwapError('[estimateTradeFee] - unable to get buy asset adapter', {
48
+ code: api_1.SwapErrorTypes.VALIDATION_FAILED,
49
+ details: { buyChainId }
50
+ });
51
+ const buyFeeAssetId = buyAdapter.getFeeAssetId();
52
+ if (!buyFeeAssetId)
46
53
  throw new api_1.SwapError('[estimateTradeFee] - no fee assetId', {
47
54
  code: api_1.SwapErrorTypes.VALIDATION_FAILED,
48
55
  details: { buyAssetId: buyAsset.assetId }
49
56
  });
50
- const feeAssetRatio = buyAsset.assetId !== feeAssetId
57
+ const feeAssetRatio = buyAsset.assetId !== buyFeeAssetId
51
58
  ? await (0, getPriceRatio_1.getPriceRatio)(deps, {
52
59
  sellAssetId: buyAsset.assetId,
53
- buyAssetId: feeAssetId
60
+ buyAssetId: buyFeeAssetId
54
61
  })
55
62
  : '1';
56
- switch (chainId) {
63
+ const buyFeeAsset = new asset_service_1.AssetService().getAll()[buyFeeAssetId];
64
+ switch (buyChainId) {
57
65
  case 'bip122:000000000019d6689c085ae165831e93':
58
- return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(btcEstimate(gasRate)).times(feeAssetRatio).dp(0), buyAsset.precision);
66
+ return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(btcEstimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
59
67
  case 'eip155:1':
60
68
  switch (assetNamespace) {
61
69
  case 'slip44':
62
- return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(ethEstimate(gasRate)).times(feeAssetRatio).dp(0), buyAsset.precision);
70
+ return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(ethEstimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
63
71
  case 'erc20':
64
- return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(erc20Estimate(gasRate)).times(feeAssetRatio).dp(0), buyAsset.precision);
72
+ return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(erc20Estimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
65
73
  default:
66
74
  throw new api_1.SwapError('[estimateTradeFee] - unsupported asset namespace', {
67
75
  code: api_1.SwapErrorTypes.VALIDATION_FAILED,
@@ -71,7 +79,7 @@ const estimateTradeFee = async (deps, buyAsset) => {
71
79
  default:
72
80
  throw new api_1.SwapError('[estimateTradeFee] - unsupported chain id', {
73
81
  code: api_1.SwapErrorTypes.VALIDATION_FAILED,
74
- details: { chainId }
82
+ details: { chainId: buyChainId }
75
83
  });
76
84
  }
77
85
  };
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.setupThorswapDeps = void 0;
7
+ const caip_1 = require("@shapeshiftoss/caip");
7
8
  const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
8
9
  const web3_1 = __importDefault(require("web3"));
9
10
  jest.mock('web3');
@@ -22,6 +23,7 @@ const setupThorswapDeps = () => {
22
23
  buildBIP44Params: jest.fn(() => ({ purpose: 44, coinType: 60, accountNumber: 0 })),
23
24
  getAddress: jest.fn(() => Promise.resolve('0xthisIsMyAddress')),
24
25
  getFeeData: jest.fn(() => feeData),
26
+ getFeeAssetId: jest.fn(() => caip_1.ethAssetId),
25
27
  getGasFeeData: jest.fn(() => ({
26
28
  [chain_adapters_1.FeeDataKey.Slow]: {
27
29
  gasPrice: '1',
@@ -18,7 +18,7 @@ async function zrxApprovalNeeded({ adapter, web3 }, { quote, wallet }) {
18
18
  });
19
19
  }
20
20
  // No approval needed for selling a fee asset
21
- if (sellAsset.assetId === (0, caip_1.getFeeAssetIdFromAssetId)(sellAsset.assetId)) {
21
+ if (sellAsset.assetId === adapter.getFeeAssetId()) {
22
22
  return { approvalNeeded: false };
23
23
  }
24
24
  const accountNumber = quote.sellAssetAccountNumber;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "9.2.1",
3
+ "version": "9.3.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",