@shapeshiftoss/swapper 2.0.2 → 2.2.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,20 @@
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 SupportedAssetInput = {
5
+ assetIds: CAIP19[];
6
+ };
7
+ export declare type ByPairInput = {
8
+ sellAssetId: CAIP19;
9
+ buyAssetId: CAIP19;
10
+ };
11
+ export declare type BuyAssetBySellIdInput = {
12
+ sellAssetId: CAIP19;
13
+ assetIds: CAIP19[];
14
+ };
15
+ export declare type SupportedSellAssetsInput = {
16
+ assetIds: CAIP19[];
17
+ };
4
18
  export declare class SwapError extends Error {
5
19
  }
6
20
  export interface Swapper {
@@ -14,14 +28,6 @@ export interface Swapper {
14
28
  * Get a basic quote (rate) for a trading pair
15
29
  */
16
30
  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
31
  /**
26
32
  * Get the usd rate from either the assets symbol or tokenId
27
33
  */
@@ -50,4 +56,12 @@ export interface Swapper {
50
56
  * Get max swap balance (minus fees) for sell asset
51
57
  */
52
58
  getSendMaxAmount(args: SendMaxAmountInput): Promise<string>;
59
+ /**
60
+ * Get supported buyAssetId's by sellAssetId
61
+ */
62
+ filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): CAIP19[];
63
+ /**
64
+ * Get supported sell assetIds
65
+ */
66
+ filterAssetIdsBySellable(assetIds: CAIP19[]): CAIP19[];
53
67
  }
@@ -1,5 +1,5 @@
1
1
  import { GetQuoteInput, SwapperType } from '@shapeshiftoss/types';
2
- import { Swapper } from '..';
2
+ import { BuyAssetBySellIdInput, ByPairInput, SupportedSellAssetsInput, Swapper } from '..';
3
3
  export declare class SwapperError extends Error {
4
4
  constructor(message: string);
5
5
  }
@@ -26,4 +26,12 @@ export declare class SwapperManager {
26
26
  */
27
27
  removeSwapper(swapperType: SwapperType): this;
28
28
  getBestSwapper(quoteParams: GetQuoteInput): Promise<SwapperType>;
29
+ /**
30
+ *
31
+ * @param pair type {GetQuoteInput}
32
+ * @returns {SwapperType}
33
+ */
34
+ getSwappersByPair(pair: ByPairInput): Swapper[];
35
+ getSupportedBuyAssetIdsFromSellId(args: BuyAssetBySellIdInput): string[];
36
+ getSupportedSellableAssetIds(args: SupportedSellAssetsInput): string[];
29
37
  }
@@ -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,21 @@ class SwapperManager {
59
63
  quoteParams; // noop to shut up linter
60
64
  return types_1.SwapperType.Zrx; // TODO: implement getBestSwapper
61
65
  }
66
+ /**
67
+ *
68
+ * @param pair type {GetQuoteInput}
69
+ * @returns {SwapperType}
70
+ */
71
+ getSwappersByPair(pair) {
72
+ const { sellAssetId, buyAssetId } = pair;
73
+ return Array.from(this.swappers.values()).filter((swapper) => swapper.filterBuyAssetsBySellAssetId({ sellAssetId, assetIds: [buyAssetId] }).length);
74
+ }
75
+ getSupportedBuyAssetIdsFromSellId(args) {
76
+ return (0, uniq_1.default)(Array.from(this.swappers.values()).flatMap((swapper) => swapper.filterBuyAssetsBySellAssetId(args)));
77
+ }
78
+ getSupportedSellableAssetIds(args) {
79
+ const { assetIds } = args;
80
+ return (0, uniq_1.default)(Array.from(this.swappers.values()).flatMap((swapper) => swapper.filterAssetIdsBySellable(assetIds)));
81
+ }
62
82
  }
63
83
  exports.SwapperManager = SwapperManager;
@@ -1,2 +1,3 @@
1
1
  export * from './zrx/ZrxSwapper';
2
2
  export * from './thorchain/ThorchainSwapper';
3
+ export * from './test/TestSwapper';
@@ -16,3 +16,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./zrx/ZrxSwapper"), exports);
18
18
  __exportStar(require("./thorchain/ThorchainSwapper"), exports);
19
+ __exportStar(require("./test/TestSwapper"), exports);
@@ -0,0 +1,23 @@
1
+ import { CAIP19 } from '@shapeshiftoss/caip';
2
+ import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SwapperType } from '@shapeshiftoss/types';
3
+ import { BuyAssetBySellIdInput, Swapper } from '../../api';
4
+ /**
5
+ * Playground for testing different scenarios of multiple swappers in the manager.
6
+ * Meant for local testing only
7
+ */
8
+ export declare class TestSwapper implements Swapper {
9
+ supportAssets: string[];
10
+ getType(): SwapperType;
11
+ constructor();
12
+ getQuote(): Promise<Quote<ChainTypes, SwapperType>>;
13
+ buildQuoteTx(): Promise<Quote<ChainTypes, SwapperType>>;
14
+ getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
15
+ getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
16
+ executeQuote(): Promise<ExecQuoteOutput>;
17
+ getDefaultPair(): [CAIP19, CAIP19];
18
+ approvalNeeded(): Promise<ApprovalNeededOutput>;
19
+ approveInfinite(): Promise<string>;
20
+ getSendMaxAmount(): Promise<string>;
21
+ filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): CAIP19[];
22
+ filterAssetIdsBySellable(): CAIP19[];
23
+ }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TestSwapper = void 0;
4
+ const types_1 = require("@shapeshiftoss/types");
5
+ /**
6
+ * Playground for testing different scenarios of multiple swappers in the manager.
7
+ * Meant for local testing only
8
+ */
9
+ class TestSwapper {
10
+ constructor() {
11
+ this.supportAssets = [
12
+ 'bip122:000000000933ea01ad0ee984209779ba/slip44:0',
13
+ 'cosmos:cosmoshub-4/slip44:118',
14
+ 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d'
15
+ ];
16
+ }
17
+ getType() {
18
+ return types_1.SwapperType.Test;
19
+ }
20
+ async getQuote() {
21
+ throw new Error('TestSwapper: getQuote unimplemented');
22
+ }
23
+ async buildQuoteTx() {
24
+ throw new Error('TestSwapper: getQuote unimplemented');
25
+ }
26
+ getUsdRate(input) {
27
+ console.info(input);
28
+ throw new Error('TestSwapper: getUsdRate unimplemented');
29
+ }
30
+ getMinMax(input) {
31
+ console.info(input);
32
+ throw new Error('TestSwapper: getMinMax unimplemented');
33
+ }
34
+ async executeQuote() {
35
+ throw new Error('TestSwapper: executeQuote unimplemented');
36
+ }
37
+ getDefaultPair() {
38
+ throw new Error('TestSwapper: getDefaultPair unimplemented');
39
+ }
40
+ async approvalNeeded() {
41
+ throw new Error('TestSwapper: approvalNeeded unimplemented');
42
+ }
43
+ async approveInfinite() {
44
+ throw new Error('TestSwapper: approveInfinite unimplemented');
45
+ }
46
+ async getSendMaxAmount() {
47
+ throw new Error('TestSwapper: getSendMaxAmount unimplemented');
48
+ }
49
+ filterBuyAssetsBySellAssetId(args) {
50
+ const { sellAssetId } = args;
51
+ if (!this.supportAssets.includes(sellAssetId))
52
+ return [];
53
+ return this.supportAssets;
54
+ }
55
+ filterAssetIdsBySellable() {
56
+ return this.supportAssets;
57
+ }
58
+ }
59
+ exports.TestSwapper = TestSwapper;
@@ -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
+ return [];
40
+ }
41
+ filterAssetIdsBySellable() {
42
+ return [];
43
+ }
46
44
  }
47
45
  exports.ThorchainSwapper = ThorchainSwapper;
@@ -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,14 @@ class ZrxSwapper {
60
53
  async getSendMaxAmount(args) {
61
54
  return (0, getZrxSendMaxAmount_1.getZrxSendMaxAmount)(this.deps, args);
62
55
  }
56
+ filterBuyAssetsBySellAssetId(args) {
57
+ const { assetIds, sellAssetId } = args;
58
+ // TODO: pending changes to caip lib, we may want to import caip2 value instead.
59
+ return assetIds.filter((id) => id.startsWith('eip155:1') && sellAssetId.startsWith('eip155:1'));
60
+ }
61
+ filterAssetIdsBySellable(assetIds) {
62
+ return assetIds.filter((id) => id.startsWith('eip155:1'));
63
+ }
63
64
  }
64
65
  exports.ZrxSwapper = ZrxSwapper;
65
66
  ZrxSwapper.swapperName = 'ZrxSwapper';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "2.0.2",
3
+ "version": "2.2.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,14 +31,14 @@
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/hdwallet-core": "1.20.0",
34
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/hdwallet-core": "^1.20.0",
41
42
  "@shapeshiftoss/types": "^2.1.0",
42
43
  "@types/readline-sync": "^1.4.4",
43
44
  "readline-sync": "^1.4.10",