@shapeshiftoss/swapper 9.3.1 → 9.3.2

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.
@@ -5,10 +5,6 @@ import { KnownChainIds } from '@shapeshiftoss/types';
5
5
  import Web3 from 'web3';
6
6
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs } from '../../api';
7
7
  import { CowTrade } from './types';
8
- /**
9
- * CowSwap only supports ERC-20 swaps, hence ETH is not supported
10
- * In order to get rates correctly, we need WETH asset to be passed as feeAsset
11
- */
12
8
  export declare type CowSwapperDeps = {
13
9
  apiUrl: string;
14
10
  adapter: ethereum.ChainAdapter;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CowSwapper = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const types_1 = require("@shapeshiftoss/types");
4
6
  const api_1 = require("../../api");
5
7
  const cowApprovalNeeded_1 = require("./cowApprovalNeeded/cowApprovalNeeded");
6
8
  const cowApproveInfinite_1 = require("./cowApproveInfinite/cowApproveInfinite");
@@ -38,15 +40,15 @@ class CowSwapper {
38
40
  }
39
41
  filterBuyAssetsBySellAssetId(args) {
40
42
  const { assetIds = [], sellAssetId } = args;
41
- if (!(sellAssetId === null || sellAssetId === void 0 ? void 0 : sellAssetId.startsWith('eip155:1/erc20')) ||
43
+ if ((0, caip_1.fromAssetId)(sellAssetId).assetNamespace !== 'erc20' ||
42
44
  blacklist_1.COWSWAP_UNSUPPORTED_ASSETS.includes(sellAssetId))
43
45
  return [];
44
46
  return assetIds.filter((id) => id !== sellAssetId &&
45
- id.startsWith('eip155:1/erc20') &&
47
+ (0, caip_1.fromAssetId)(id).chainId === types_1.KnownChainIds.EthereumMainnet &&
46
48
  !blacklist_1.COWSWAP_UNSUPPORTED_ASSETS.includes(id));
47
49
  }
48
50
  filterAssetIdsBySellable(assetIds) {
49
- return assetIds.filter((id) => id.startsWith('eip155:1/erc20') && !blacklist_1.COWSWAP_UNSUPPORTED_ASSETS.includes(id));
51
+ return assetIds.filter((id) => (0, caip_1.fromAssetId)(id).assetNamespace === 'erc20' && !blacklist_1.COWSWAP_UNSUPPORTED_ASSETS.includes(id));
50
52
  }
51
53
  async getTradeTxs(args) {
52
54
  return (0, cowGetTradeTxs_1.cowGetTradeTxs)(this.deps, args);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cowBuildTrade = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
+ const types_1 = require("@shapeshiftoss/types");
5
6
  const api_1 = require("../../../api");
6
7
  const erc20Allowance_abi_1 = require("../../utils/abi/erc20Allowance-abi");
7
8
  const bignumber_1 = require("../../utils/bignumber");
@@ -14,13 +15,20 @@ async function cowBuildTrade(deps, input) {
14
15
  const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber, wallet } = input;
15
16
  const { adapter, web3 } = deps;
16
17
  const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
17
- const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
18
- if (buyAssetNamespace !== 'erc20' || sellAssetNamespace !== 'erc20') {
19
- throw new api_1.SwapError('[cowBuildTrade] - Both assets need to be ERC-20 to use CowSwap', {
18
+ const { assetReference: buyAssetErc20Address, chainId: buyAssetChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
19
+ if (sellAssetNamespace !== 'erc20') {
20
+ throw new api_1.SwapError('[cowBuildTrade] - Sell asset needs to be ERC-20 to use CowSwap', {
20
21
  code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
21
- details: { buyAssetNamespace, sellAssetNamespace }
22
+ details: { sellAssetNamespace }
22
23
  });
23
24
  }
25
+ if (buyAssetChainId !== types_1.KnownChainIds.EthereumMainnet) {
26
+ throw new api_1.SwapError('[cowBuildTrade] - Buy asset needs to be on ETH mainnet to use CowSwap', {
27
+ code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
28
+ details: { buyAssetChainId }
29
+ });
30
+ }
31
+ const buyToken = buyAsset.assetId !== caip_1.ethAssetId ? buyAssetErc20Address : constants_1.COW_SWAP_ETH_MARKER_ADDRESS;
24
32
  const receiveAddress = await adapter.getAddress({ wallet });
25
33
  const normalizedSellAmount = (0, helpers_1.normalizeAmount)(sellAmount);
26
34
  /**
@@ -39,7 +47,7 @@ async function cowBuildTrade(deps, input) {
39
47
  */
40
48
  const quoteResponse = await cowService_1.cowService.post(`${deps.apiUrl}/v1/quote/`, {
41
49
  sellToken: sellAssetErc20Address,
42
- buyToken: buyAssetErc20Address,
50
+ buyToken,
43
51
  receiver: receiveAddress,
44
52
  validTo: (0, helpers_2.getNowPlusThirtyMinutesTimestamp)(),
45
53
  appData: constants_1.DEFAULT_APP_DATA,
@@ -4,6 +4,7 @@ exports.cowExecuteTrade = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
5
  const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
6
6
  const hdwallet_core_1 = require("@shapeshiftoss/hdwallet-core");
7
+ const types_1 = require("@shapeshiftoss/types");
7
8
  const ethers_1 = require("ethers");
8
9
  const api_1 = require("../../../api");
9
10
  const constants_1 = require("../utils/constants");
@@ -13,17 +14,24 @@ async function cowExecuteTrade({ apiUrl, adapter }, { trade, wallet }) {
13
14
  const cowTrade = trade;
14
15
  const { sellAsset, buyAsset, feeAmountInSellToken, sellAmountWithoutFee } = cowTrade;
15
16
  const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
16
- const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
17
- if (buyAssetNamespace !== 'erc20' || sellAssetNamespace !== 'erc20') {
18
- throw new api_1.SwapError('[cowExecuteTrade] - Both assets need to be ERC-20 to use CowSwap', {
17
+ const { assetReference: buyAssetErc20Address, chainId: buyAssetChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
18
+ if (sellAssetNamespace !== 'erc20') {
19
+ throw new api_1.SwapError('[cowExecuteTrade] - Sell asset needs to be ERC-20 to use CowSwap', {
19
20
  code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
20
- details: { buyAssetNamespace, sellAssetNamespace }
21
+ details: { sellAssetNamespace }
21
22
  });
22
23
  }
24
+ if (buyAssetChainId !== types_1.KnownChainIds.EthereumMainnet) {
25
+ throw new api_1.SwapError('[cowExecuteTrade] - Buy asset needs to be on ETH mainnet to use CowSwap', {
26
+ code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
27
+ details: { buyAssetChainId }
28
+ });
29
+ }
30
+ const buyToken = buyAsset.assetId !== caip_1.ethAssetId ? buyAssetErc20Address : constants_1.COW_SWAP_ETH_MARKER_ADDRESS;
23
31
  try {
24
32
  const orderToSign = {
25
33
  sellToken: sellAssetErc20Address,
26
- buyToken: buyAssetErc20Address,
34
+ buyToken,
27
35
  sellAmount: sellAmountWithoutFee,
28
36
  buyAmount: trade.buyAmount,
29
37
  validTo: (0, helpers_1.getNowPlusThirtyMinutesTimestamp)(),
@@ -1,14 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCowSwapMinMax = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const types_1 = require("@shapeshiftoss/types");
4
6
  const api_1 = require("../../../api");
5
7
  const bignumber_1 = require("../../utils/bignumber");
6
8
  const constants_1 = require("../utils/constants");
7
9
  const helpers_1 = require("../utils/helpers/helpers");
8
10
  const getCowSwapMinMax = async (deps, sellAsset, buyAsset) => {
9
11
  try {
10
- if (!sellAsset.assetId.startsWith('eip155:1/erc20') ||
11
- !buyAsset.assetId.startsWith('eip155:1/erc20')) {
12
+ const { assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
13
+ const { chainId: buyAssetChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
14
+ if (sellAssetNamespace !== 'erc20' || buyAssetChainId !== types_1.KnownChainIds.EthereumMainnet) {
12
15
  throw new api_1.SwapError('[getCowSwapMinMax]', { code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR });
13
16
  }
14
17
  const usdRate = await (0, helpers_1.getUsdRate)(deps, sellAsset);
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCowSwapTradeQuote = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
+ const types_1 = require("@shapeshiftoss/types");
5
6
  const api_1 = require("../../../api");
6
7
  const bignumber_1 = require("../../utils/bignumber");
7
8
  const helpers_1 = require("../../utils/helpers/helpers");
@@ -14,20 +15,27 @@ async function getCowSwapTradeQuote(deps, input) {
14
15
  const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber, wallet } = input;
15
16
  const { adapter, web3 } = deps;
16
17
  const { assetReference: sellAssetErc20Address, assetNamespace: sellAssetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
17
- const { assetReference: buyAssetErc20Address, assetNamespace: buyAssetNamespace } = (0, caip_1.fromAssetId)(buyAsset.assetId);
18
- if (buyAssetNamespace !== 'erc20' || sellAssetNamespace !== 'erc20') {
19
- throw new api_1.SwapError('[getCowSwapTradeQuote] - Both assets need to be ERC-20 to use CowSwap', {
18
+ const { assetReference: buyAssetErc20Address, chainId: buyAssetChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
19
+ if (sellAssetNamespace !== 'erc20') {
20
+ throw new api_1.SwapError('[getCowSwapTradeQuote] - Sell asset needs to be ERC-20 to use CowSwap', {
20
21
  code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
21
- details: { buyAssetNamespace, sellAssetNamespace }
22
+ details: { sellAssetNamespace }
22
23
  });
23
24
  }
25
+ if (buyAssetChainId !== types_1.KnownChainIds.EthereumMainnet) {
26
+ throw new api_1.SwapError('[getCowSwapTradeQuote] - Buy asset needs to be on ETH mainnet to use CowSwap', {
27
+ code: api_1.SwapErrorTypes.UNSUPPORTED_PAIR,
28
+ details: { buyAssetChainId }
29
+ });
30
+ }
31
+ const buyToken = buyAsset.assetId !== caip_1.ethAssetId ? buyAssetErc20Address : constants_1.COW_SWAP_ETH_MARKER_ADDRESS;
24
32
  const { minimum, maximum } = await (0, getCowSwapMinMax_1.getCowSwapMinMax)(deps, sellAsset, buyAsset);
25
33
  const minQuoteSellAmount = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
26
34
  // making sure we do not have decimals for cowswap api (can happen at least from minQuoteSellAmount)
27
35
  const normalizedSellAmount = (0, helpers_1.normalizeIntegerAmount)((0, bignumber_1.bnOrZero)(sellAmount).lt(minQuoteSellAmount) ? minQuoteSellAmount : sellAmount);
28
36
  const apiInput = {
29
37
  sellToken: sellAssetErc20Address,
30
- buyToken: buyAssetErc20Address,
38
+ buyToken,
31
39
  receiver: constants_1.DEFAULT_ADDRESS,
32
40
  validTo: (0, helpers_2.getNowPlusThirtyMinutesTimestamp)(),
33
41
  appData: constants_1.DEFAULT_APP_DATA,
@@ -15,3 +15,4 @@ export declare const ORDER_KIND_BUY = "buy";
15
15
  export declare const SIGNING_SCHEME = "ethsign";
16
16
  export declare const ERC20_TOKEN_BALANCE = "erc20";
17
17
  export declare const ORDER_STATUS_FULFILLED = "fulfilled";
18
+ export declare const COW_SWAP_ETH_MARKER_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ORDER_STATUS_FULFILLED = exports.ERC20_TOKEN_BALANCE = exports.SIGNING_SCHEME = exports.ORDER_KIND_BUY = exports.ORDER_KIND_SELL = exports.WETH_ASSET_ID = exports.COW_SWAP_SETTLEMENT_ADDRESS = exports.COW_SWAP_VAULT_RELAYER_ADDRESS = exports.DEFAULT_APP_DATA = exports.DEFAULT_ADDRESS = exports.DEFAULT_SOURCE = exports.MIN_COWSWAP_VALUE_USD = exports.MAX_COWSWAP_TRADE = exports.MAX_ALLOWANCE = void 0;
3
+ exports.COW_SWAP_ETH_MARKER_ADDRESS = exports.ORDER_STATUS_FULFILLED = exports.ERC20_TOKEN_BALANCE = exports.SIGNING_SCHEME = exports.ORDER_KIND_BUY = exports.ORDER_KIND_SELL = exports.WETH_ASSET_ID = exports.COW_SWAP_SETTLEMENT_ADDRESS = exports.COW_SWAP_VAULT_RELAYER_ADDRESS = exports.DEFAULT_APP_DATA = exports.DEFAULT_ADDRESS = exports.DEFAULT_SOURCE = exports.MIN_COWSWAP_VALUE_USD = exports.MAX_COWSWAP_TRADE = exports.MAX_ALLOWANCE = void 0;
4
4
  const constants_1 = require("@ethersproject/constants");
5
5
  exports.MAX_ALLOWANCE = '100000000000000000000000000';
6
6
  exports.MAX_COWSWAP_TRADE = '100000000000000000000000000';
@@ -16,3 +16,6 @@ exports.ORDER_KIND_BUY = 'buy';
16
16
  exports.SIGNING_SCHEME = 'ethsign';
17
17
  exports.ERC20_TOKEN_BALANCE = 'erc20';
18
18
  exports.ORDER_STATUS_FULFILLED = 'fulfilled';
19
+ // Address used by CowSwap to buy ETH
20
+ // See https://github.com/gnosis/gp-v2-contracts/commit/821b5a8da213297b0f7f1d8b17c893c5627020af#diff-12bbbe13cd5cf42d639e34a39d8795021ba40d3ee1e1a8282df652eb161a11d6R13
21
+ exports.COW_SWAP_ETH_MARKER_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "9.3.1",
3
+ "version": "9.3.2",
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",