@shapeshiftoss/swapper 2.0.1 → 2.1.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
@@ -1,6 +1,13 @@
1
1
  import { CAIP19 } from '@shapeshiftoss/caip';
2
2
  import { HDWallet } from '@shapeshiftoss/hdwallet-core';
3
3
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
4
+ export declare type BuyAssetBySellIdInput = {
5
+ sellAssetId: CAIP19;
6
+ buyAssetIds: CAIP19[];
7
+ };
8
+ export declare type SupportedSellAssetsInput = {
9
+ sellAssetIds: CAIP19[];
10
+ };
4
11
  export declare class SwapError extends Error {
5
12
  }
6
13
  export interface Swapper {
@@ -14,14 +21,6 @@ export interface Swapper {
14
21
  * Get a basic quote (rate) for a trading pair
15
22
  */
16
23
  getQuote(input: GetQuoteInput, wallet?: HDWallet): Promise<Quote<ChainTypes, SwapperType>>;
17
- /**
18
- * Get a list of available assets based on the array of assets you send it
19
- */
20
- getAvailableAssets(assets: Asset[]): Asset[];
21
- /**
22
- * Get a boolean if the trade pair will work
23
- */
24
- canTradePair(sellAsset: Asset, buyAsset: Asset): boolean;
25
24
  /**
26
25
  * Get the usd rate from either the assets symbol or tokenId
27
26
  */
@@ -50,4 +49,12 @@ export interface Swapper {
50
49
  * Get max swap balance (minus fees) for sell asset
51
50
  */
52
51
  getSendMaxAmount(args: SendMaxAmountInput): Promise<string>;
52
+ /**
53
+ * Get supported buyAssetId's by sellAssetId
54
+ */
55
+ filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): CAIP19[];
56
+ /**
57
+ * Get supported sell assetIds
58
+ */
59
+ filterAssetIdsBySellable(assetIds: CAIP19[]): CAIP19[];
53
60
  }
package/dist/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1,5 +1,6 @@
1
1
  import { GetQuoteInput, SwapperType } from '@shapeshiftoss/types';
2
2
  import { Swapper } from '..';
3
+ import { BuyAssetBySellIdInput, SupportedSellAssetsInput } from '../api';
3
4
  export declare class SwapperError extends Error {
4
5
  constructor(message: string);
5
6
  }
@@ -26,4 +27,6 @@ export declare class SwapperManager {
26
27
  */
27
28
  removeSwapper(swapperType: SwapperType): this;
28
29
  getBestSwapper(quoteParams: GetQuoteInput): Promise<SwapperType>;
30
+ getSupportedBuyAssetsFromSellId(args: BuyAssetBySellIdInput): string[];
31
+ getSupportedSellAssets(args: SupportedSellAssetsInput): string[];
29
32
  }
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.SwapperManager = exports.SwapperError = void 0;
4
7
  const types_1 = require("@shapeshiftoss/types");
8
+ const uniq_1 = __importDefault(require("lodash/uniq"));
5
9
  class SwapperError extends Error {
6
10
  constructor(message) {
7
11
  super(message);
@@ -59,5 +63,12 @@ class SwapperManager {
59
63
  quoteParams; // noop to shut up linter
60
64
  return types_1.SwapperType.Zrx; // TODO: implement getBestSwapper
61
65
  }
66
+ getSupportedBuyAssetsFromSellId(args) {
67
+ return (0, uniq_1.default)(Array.from(this.swappers.values()).flatMap((swapper) => swapper.filterBuyAssetsBySellAssetId(args)));
68
+ }
69
+ getSupportedSellAssets(args) {
70
+ const { sellAssetIds } = args;
71
+ return (0, uniq_1.default)(Array.from(this.swappers.values()).flatMap((swapper) => swapper.filterAssetIdsBySellable(sellAssetIds)));
72
+ }
62
73
  }
63
74
  exports.SwapperManager = SwapperManager;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -7,11 +7,11 @@ export declare class ThorchainSwapper implements Swapper {
7
7
  buildQuoteTx(): Promise<Quote<ChainTypes, SwapperType>>;
8
8
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
9
9
  getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
10
- getAvailableAssets(assets: Asset[]): Asset[];
11
- canTradePair(sellAsset: Asset, buyAsset: Asset): boolean;
12
10
  executeQuote(): Promise<ExecQuoteOutput>;
13
11
  getDefaultPair(): [CAIP19, CAIP19];
14
12
  approvalNeeded(): Promise<ApprovalNeededOutput>;
15
13
  approveInfinite(): Promise<string>;
16
14
  getSendMaxAmount(): Promise<string>;
15
+ filterBuyAssetsBySellAssetId(): CAIP19[];
16
+ filterAssetIdsBySellable(): CAIP19[];
17
17
  }
@@ -20,14 +20,6 @@ class ThorchainSwapper {
20
20
  console.info(input);
21
21
  throw new Error('ThorchainSwapper: getMinMax unimplemented');
22
22
  }
23
- getAvailableAssets(assets) {
24
- console.info(assets);
25
- throw new Error('ThorchainSwapper: getAvailableAssets unimplemented');
26
- }
27
- canTradePair(sellAsset, buyAsset) {
28
- console.info(sellAsset, buyAsset);
29
- throw new Error('ThorchainSwapper: canTradePair unimplemented');
30
- }
31
23
  async executeQuote() {
32
24
  throw new Error('ThorchainSwapper: executeQuote unimplemented');
33
25
  }
@@ -43,5 +35,11 @@ class ThorchainSwapper {
43
35
  async getSendMaxAmount() {
44
36
  throw new Error('ThorchainSwapper: getSendMaxAmount unimplemented');
45
37
  }
38
+ filterBuyAssetsBySellAssetId() {
39
+ throw new Error('ThorchainSwapper: filterBuyAssetsBySellAssetId unimplemented');
40
+ }
41
+ filterAssetIdsBySellable() {
42
+ throw new Error('ThorchainSwapper: filterAssetIdsBySellable unimplemented');
43
+ }
46
44
  }
47
45
  exports.ThorchainSwapper = ThorchainSwapper;
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -2,7 +2,7 @@ import { CAIP19 } from '@shapeshiftoss/caip';
2
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
3
  import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
4
4
  import Web3 from 'web3';
5
- import { Swapper } from '../../api';
5
+ import { BuyAssetBySellIdInput, Swapper } from '../../api';
6
6
  export declare type ZrxSwapperDeps = {
7
7
  adapterManager: ChainAdapterManager;
8
8
  web3: Web3;
@@ -19,11 +19,11 @@ export declare class ZrxSwapper implements Swapper {
19
19
  getQuote(input: GetQuoteInput): Promise<Quote<ChainTypes, SwapperType>>;
20
20
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
21
21
  getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
22
- getAvailableAssets(assets: Asset[]): Asset[];
23
- canTradePair(sellAsset: Asset, buyAsset: Asset): boolean;
24
22
  getDefaultPair(): [CAIP19, CAIP19];
25
23
  executeQuote(args: ExecQuoteInput<ChainTypes, SwapperType>): Promise<ExecQuoteOutput>;
26
24
  approvalNeeded(args: ApprovalNeededInput<ChainTypes, SwapperType>): Promise<ApprovalNeededOutput>;
27
25
  approveInfinite(args: ApproveInfiniteInput<ChainTypes, SwapperType>): Promise<string>;
28
26
  getSendMaxAmount(args: SendMaxAmountInput): Promise<string>;
27
+ filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): CAIP19[];
28
+ filterAssetIdsBySellable(assetIds: CAIP19[]): CAIP19[];
29
29
  }
@@ -36,13 +36,6 @@ class ZrxSwapper {
36
36
  async getMinMax(input) {
37
37
  return (0, getZrxMinMax_1.getZrxMinMax)(input);
38
38
  }
39
- getAvailableAssets(assets) {
40
- return assets.filter((asset) => asset.chain === types_1.ChainTypes.Ethereum);
41
- }
42
- canTradePair(sellAsset, buyAsset) {
43
- const availableAssets = this.getAvailableAssets([sellAsset, buyAsset]);
44
- return availableAssets.length === 2;
45
- }
46
39
  getDefaultPair() {
47
40
  const ETH = 'eip155:1/slip44:60';
48
41
  const FOX = 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d';
@@ -60,6 +53,15 @@ class ZrxSwapper {
60
53
  async getSendMaxAmount(args) {
61
54
  return (0, getZrxSendMaxAmount_1.getZrxSendMaxAmount)(this.deps, args);
62
55
  }
56
+ filterBuyAssetsBySellAssetId(args) {
57
+ const { buyAssetIds } = args;
58
+ // TODO: pending changes to caip lib, we may want to import caip2 value instead.
59
+ return buyAssetIds.filter((id) => id.startsWith('eip155:1'));
60
+ }
61
+ filterAssetIdsBySellable(assetIds) {
62
+ // reusing logic to avoid potential bugs from changing one and not the other
63
+ return this.filterBuyAssetsBySellAssetId({ buyAssetIds: assetIds, sellAssetId: '' });
64
+ }
63
65
  }
64
66
  exports.ZrxSwapper = ZrxSwapper;
65
67
  ZrxSwapper.swapperName = 'ZrxSwapper';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -23,6 +23,7 @@
23
23
  "dependencies": {
24
24
  "axios": "^0.26.0",
25
25
  "bignumber.js": "^9.0.1",
26
+ "lodash": "^4.17.21",
26
27
  "retry-axios": "^2.6.0",
27
28
  "web3": "^1.6.1"
28
29
  },
@@ -30,15 +31,15 @@
30
31
  "@shapeshiftoss/asset-service": "^2.0.0",
31
32
  "@shapeshiftoss/caip": "^2.0.0",
32
33
  "@shapeshiftoss/chain-adapters": "^2.0.0",
33
- "@shapeshiftoss/hdwallet-core": "^1.18.2",
34
- "@shapeshiftoss/types": "^2.0.0"
34
+ "@shapeshiftoss/hdwallet-core": "1.20.0",
35
+ "@shapeshiftoss/types": "^2.1.0"
35
36
  },
36
37
  "devDependencies": {
37
38
  "@shapeshiftoss/asset-service": "^2.0.0",
38
39
  "@shapeshiftoss/caip": "^2.0.0",
39
40
  "@shapeshiftoss/chain-adapters": "^2.0.0",
40
- "@shapeshiftoss/hdwallet-core": "^1.19.0",
41
- "@shapeshiftoss/types": "^2.0.0",
41
+ "@shapeshiftoss/hdwallet-core": "^1.20.0",
42
+ "@shapeshiftoss/types": "^2.1.0",
42
43
  "@types/readline-sync": "^1.4.4",
43
44
  "readline-sync": "^1.4.10",
44
45
  "web3-utils": "^1.5.2"