@shapeshiftoss/swapper 5.0.1 → 5.1.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.
package/dist/api.d.ts CHANGED
@@ -104,20 +104,20 @@ export declare type ApprovalNeededInput<C extends SupportedChainIds> = {
104
104
  };
105
105
  export declare enum SwapErrorTypes {
106
106
  ALLOWANCE_REQUIRED_FAILED = "ALLOWANCE_REQUIRED_FAILED",
107
- CHECK_APPROVAL_FAILED = "CHECK_APPROVAL_FAILED",
108
107
  APPROVE_INFINITE_FAILED = "APPROVE_INFINITE_FAILED",
109
108
  BUILD_TRADE_FAILED = "BUILD_TRADE_FAILED",
109
+ CHECK_APPROVAL_FAILED = "CHECK_APPROVAL_FAILED",
110
110
  EXECUTE_TRADE_FAILED = "EXECUTE_TRADE_FAILED",
111
111
  GRANT_ALLOWANCE_FAILED = "GRANT_ALLOWANCE_FAILED",
112
112
  MANAGER_ERROR = "MANAGER_ERROR",
113
113
  MIN_MAX_FAILED = "MIN_MAX_FAILED",
114
+ RESPONSE_ERROR = "RESPONSE_ERROR",
114
115
  SIGN_AND_BROADCAST_FAILED = "SIGN_AND_BROADCAST_FAILED",
115
116
  TRADE_QUOTE_FAILED = "TRADE_QUOTE_FAILED",
116
117
  UNSUPPORTED_PAIR = "UNSUPPORTED_PAIR",
117
118
  USD_RATE_FAILED = "USD_RATE_FAILED",
118
119
  UNSUPPORTED_CHAIN = "UNSUPPORTED_CHAIN",
119
- VALIDATION_FAILED = "VALIDATION_FAILED",
120
- RESPONSE_ERROR = "RESPONSE_ERROR"
120
+ VALIDATION_FAILED = "VALIDATION_FAILED"
121
121
  }
122
122
  export interface Swapper {
123
123
  /** Returns the swapper type */
package/dist/api.js CHANGED
@@ -7,18 +7,18 @@ exports.SwapError = (0, errors_1.createErrorClass)('SwapError');
7
7
  var SwapErrorTypes;
8
8
  (function (SwapErrorTypes) {
9
9
  SwapErrorTypes["ALLOWANCE_REQUIRED_FAILED"] = "ALLOWANCE_REQUIRED_FAILED";
10
- SwapErrorTypes["CHECK_APPROVAL_FAILED"] = "CHECK_APPROVAL_FAILED";
11
10
  SwapErrorTypes["APPROVE_INFINITE_FAILED"] = "APPROVE_INFINITE_FAILED";
12
11
  SwapErrorTypes["BUILD_TRADE_FAILED"] = "BUILD_TRADE_FAILED";
12
+ SwapErrorTypes["CHECK_APPROVAL_FAILED"] = "CHECK_APPROVAL_FAILED";
13
13
  SwapErrorTypes["EXECUTE_TRADE_FAILED"] = "EXECUTE_TRADE_FAILED";
14
14
  SwapErrorTypes["GRANT_ALLOWANCE_FAILED"] = "GRANT_ALLOWANCE_FAILED";
15
15
  SwapErrorTypes["MANAGER_ERROR"] = "MANAGER_ERROR";
16
16
  SwapErrorTypes["MIN_MAX_FAILED"] = "MIN_MAX_FAILED";
17
+ SwapErrorTypes["RESPONSE_ERROR"] = "RESPONSE_ERROR";
17
18
  SwapErrorTypes["SIGN_AND_BROADCAST_FAILED"] = "SIGN_AND_BROADCAST_FAILED";
18
19
  SwapErrorTypes["TRADE_QUOTE_FAILED"] = "TRADE_QUOTE_FAILED";
19
20
  SwapErrorTypes["UNSUPPORTED_PAIR"] = "UNSUPPORTED_PAIR";
20
21
  SwapErrorTypes["USD_RATE_FAILED"] = "USD_RATE_FAILED";
21
22
  SwapErrorTypes["UNSUPPORTED_CHAIN"] = "UNSUPPORTED_CHAIN";
22
23
  SwapErrorTypes["VALIDATION_FAILED"] = "VALIDATION_FAILED";
23
- SwapErrorTypes["RESPONSE_ERROR"] = "RESPONSE_ERROR";
24
24
  })(SwapErrorTypes = exports.SwapErrorTypes || (exports.SwapErrorTypes = {}));
@@ -14,6 +14,7 @@ export declare class SwapperManager {
14
14
  *
15
15
  * @param swapperType swapper type {SwapperType|string}
16
16
  * @returns {Swapper}
17
+ * @deprecated this will be removed, currently used in swapper tests
17
18
  */
18
19
  getSwapper(swapperType: SwapperType): Swapper;
19
20
  /**
@@ -22,7 +23,7 @@ export declare class SwapperManager {
22
23
  * @returns {SwapperManager}
23
24
  */
24
25
  removeSwapper(swapperType: SwapperType): this;
25
- getBestSwapper(args: ByPairInput): Promise<Swapper>;
26
+ getBestSwapper(args: ByPairInput): Promise<Swapper | undefined>;
26
27
  /**
27
28
  *
28
29
  * @param pair type {GetQuoteInput}
@@ -38,6 +38,7 @@ class SwapperManager {
38
38
  *
39
39
  * @param swapperType swapper type {SwapperType|string}
40
40
  * @returns {Swapper}
41
+ * @deprecated this will be removed, currently used in swapper tests
41
42
  */
42
43
  getSwapper(swapperType) {
43
44
  const swapper = this.swappers.get(swapperType);
@@ -84,7 +84,12 @@ const main = async () => {
84
84
  const manager = new SwapperManager_1.SwapperManager();
85
85
  const zrxSwapper = new ZrxSwapper_1.ZrxSwapper(zrxSwapperDeps);
86
86
  manager.addSwapper(types_1.SwapperType.Zrx, zrxSwapper);
87
- const swapper = manager.getSwapper(types_1.SwapperType.Zrx);
87
+ const swapper = await manager.getBestSwapper({
88
+ sellAssetId: 'eip155:1/slip44:60',
89
+ buyAssetId: 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'
90
+ });
91
+ if (!swapper)
92
+ return;
88
93
  const sellAmountBase = toBaseUnit(sellAmount, sellAsset.precision);
89
94
  let quote;
90
95
  try {
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZrxApprovalNeeded = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
4
5
  const api_1 = require("../../../api");
5
6
  const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
6
7
  const bignumber_1 = require("../utils/bignumber");
@@ -16,8 +17,8 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
16
17
  details: { chainId: sellAsset.chainId }
17
18
  });
18
19
  }
19
- // No approval needed for trading eth itself
20
- if (sellAsset.assetId === 'eip155:1/slip44:60') {
20
+ // No approval needed for selling a fee asset
21
+ if (sellAsset.assetId === (0, caip_1.getFeeAssetIdFromAssetId)(sellAsset.assetId)) {
21
22
  return { approvalNeeded: false };
22
23
  }
23
24
  const accountNumber = (0, bignumber_1.bnOrZero)(quote.sellAssetAccountId).toNumber();
@@ -4,6 +4,7 @@ exports.ZrxSwapper = void 0;
4
4
  const types_1 = require("@shapeshiftoss/types");
5
5
  const getZrxMinMax_1 = require("./getZrxMinMax/getZrxMinMax");
6
6
  const getZrxTradeQuote_1 = require("./getZrxTradeQuote/getZrxTradeQuote");
7
+ const blacklist_1 = require("./utils/blacklist");
7
8
  const helpers_1 = require("./utils/helpers/helpers");
8
9
  const ZrxApprovalNeeded_1 = require("./ZrxApprovalNeeded/ZrxApprovalNeeded");
9
10
  const ZrxApproveInfinite_1 = require("./ZrxApproveInfinite/ZrxApproveInfinite");
@@ -39,10 +40,12 @@ class ZrxSwapper {
39
40
  }
40
41
  filterBuyAssetsBySellAssetId(args) {
41
42
  const { assetIds = [], sellAssetId } = args;
42
- return assetIds.filter((id) => id.startsWith('eip155:1') && (sellAssetId === null || sellAssetId === void 0 ? void 0 : sellAssetId.startsWith('eip155:1')));
43
+ return assetIds.filter((id) => id.startsWith('eip155:1') &&
44
+ (sellAssetId === null || sellAssetId === void 0 ? void 0 : sellAssetId.startsWith('eip155:1')) &&
45
+ !blacklist_1.UNSUPPORTED_ASSETS.includes(id));
43
46
  }
44
47
  filterAssetIdsBySellable(assetIds = []) {
45
- return assetIds.filter((id) => id.startsWith('eip155:1'));
48
+ return assetIds.filter((id) => id.startsWith('eip155:1') && !blacklist_1.UNSUPPORTED_ASSETS.includes(id));
46
49
  }
47
50
  }
48
51
  exports.ZrxSwapper = ZrxSwapper;
@@ -22,7 +22,7 @@ async function getZrxTradeQuote(input) {
22
22
  const sellToken = sellAsset.tokenId || sellAsset.symbol;
23
23
  const { minimum, maximum } = await (0, getZrxMinMax_1.getZrxMinMax)(sellAsset, buyAsset);
24
24
  const minQuoteSellAmount = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
25
- const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).eq(0) ? minQuoteSellAmount.toString() : sellAmount);
25
+ const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).eq(0) ? minQuoteSellAmount : sellAmount);
26
26
  /**
27
27
  * /swap/v1/price
28
28
  * params: {
@@ -0,0 +1 @@
1
+ export declare const UNSUPPORTED_ASSETS: readonly string[];
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UNSUPPORTED_ASSETS = void 0;
4
+ // Zrx doesnt have an easily accessible master assets list.
5
+ // We assume all erc20's are supported and remove these explicitely unsupported assets
6
+ exports.UNSUPPORTED_ASSETS = Object.freeze([
7
+ // Foxy token unsupported by zrx
8
+ 'eip155:1/erc20:0xdc49108ce5c57bc3408c3a5e95f3d864ec386ed3'
9
+ ]);
@@ -35,7 +35,7 @@ declare type GrantAllowanceArgs = {
35
35
  * ETH or ERC20, we still keep 7 decimal places.
36
36
  * @param amount
37
37
  */
38
- export declare const normalizeAmount: (amount: string | undefined) => string | undefined;
38
+ export declare const normalizeAmount: (amount: string | number | BigNumber) => string;
39
39
  export declare const getERC20Allowance: ({ erc20AllowanceAbi, web3, tokenId, ownerAddress, spenderAddress }: GetERC20AllowanceArgs) => Promise<any>;
40
40
  export declare const getAllowanceRequired: ({ receiveAddress, allowanceContract, sellAsset, sellAmount, web3, erc20AllowanceAbi }: GetAllowanceRequiredArgs) => Promise<BigNumber>;
41
41
  export declare const getUsdRate: (input: Pick<Asset, 'symbol' | 'tokenId'>) => Promise<string>;
@@ -14,8 +14,6 @@ const zrxService_1 = require("../zrxService");
14
14
  * @param amount
15
15
  */
16
16
  const normalizeAmount = (amount) => {
17
- if (!amount)
18
- return;
19
17
  return (0, bignumber_1.bnOrZero)(amount).toNumber().toLocaleString('fullwide', { useGrouping: false });
20
18
  };
21
19
  exports.normalizeAmount = normalizeAmount;
@@ -77,7 +77,7 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
77
77
  params: {
78
78
  buyToken,
79
79
  sellToken,
80
- sellAmount: (0, helpers_1.normalizeAmount)(sellAmount === null || sellAmount === void 0 ? void 0 : sellAmount.toString()),
80
+ sellAmount: (0, helpers_1.normalizeAmount)(sellAmount),
81
81
  takerAddress: receiveAddress,
82
82
  slippagePercentage,
83
83
  skipValidation: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "5.0.1",
3
+ "version": "5.1.2",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "peerDependencies": {
33
33
  "@shapeshiftoss/asset-service": "^3.0.0",
34
- "@shapeshiftoss/caip": "^4.0.0",
34
+ "@shapeshiftoss/caip": "^4.0.1",
35
35
  "@shapeshiftoss/chain-adapters": "^4.0.0",
36
36
  "@shapeshiftoss/hdwallet-core": "1.20.0",
37
37
  "@shapeshiftoss/types": "^4.0.0",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "devDependencies": {
41
41
  "@shapeshiftoss/asset-service": "^3.0.0",
42
- "@shapeshiftoss/caip": "^4.0.0",
42
+ "@shapeshiftoss/caip": "^4.0.1",
43
43
  "@shapeshiftoss/chain-adapters": "^4.0.0",
44
44
  "@shapeshiftoss/hdwallet-core": "^1.20.0",
45
45
  "@shapeshiftoss/types": "^4.1.0",