@shapeshiftoss/swapper 2.1.0 → 2.2.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
@@ -1,12 +1,19 @@
1
1
  import { CAIP19 } from '@shapeshiftoss/caip';
2
2
  import { HDWallet } from '@shapeshiftoss/hdwallet-core';
3
- import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
3
+ import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, 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
+ };
4
11
  export declare type BuyAssetBySellIdInput = {
5
12
  sellAssetId: CAIP19;
6
- buyAssetIds: CAIP19[];
13
+ assetIds: CAIP19[];
7
14
  };
8
15
  export declare type SupportedSellAssetsInput = {
9
- sellAssetIds: CAIP19[];
16
+ assetIds: CAIP19[];
10
17
  };
11
18
  export declare class SwapError extends Error {
12
19
  }
@@ -45,10 +52,6 @@ export interface Swapper {
45
52
  * Get the txid of an approve infinite transaction
46
53
  */
47
54
  approveInfinite(args: ApproveInfiniteInput<ChainTypes, SwapperType>): Promise<string>;
48
- /**
49
- * Get max swap balance (minus fees) for sell asset
50
- */
51
- getSendMaxAmount(args: SendMaxAmountInput): Promise<string>;
52
55
  /**
53
56
  * Get supported buyAssetId's by sellAssetId
54
57
  */
@@ -1,6 +1,5 @@
1
- import { GetQuoteInput, SwapperType } from '@shapeshiftoss/types';
2
- import { Swapper } from '..';
3
- import { BuyAssetBySellIdInput, SupportedSellAssetsInput } from '../api';
1
+ import { SwapperType } from '@shapeshiftoss/types';
2
+ import { BuyAssetBySellIdInput, ByPairInput, SupportedSellAssetsInput, Swapper } from '..';
4
3
  export declare class SwapperError extends Error {
5
4
  constructor(message: string);
6
5
  }
@@ -26,7 +25,13 @@ export declare class SwapperManager {
26
25
  * @returns {SwapperManager}
27
26
  */
28
27
  removeSwapper(swapperType: SwapperType): this;
29
- getBestSwapper(quoteParams: GetQuoteInput): Promise<SwapperType>;
30
- getSupportedBuyAssetsFromSellId(args: BuyAssetBySellIdInput): string[];
31
- getSupportedSellAssets(args: SupportedSellAssetsInput): string[];
28
+ getBestSwapper(args: ByPairInput): Promise<Swapper>;
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[];
32
37
  }
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.SwapperManager = exports.SwapperError = void 0;
7
- const types_1 = require("@shapeshiftoss/types");
8
7
  const uniq_1 = __importDefault(require("lodash/uniq"));
9
8
  class SwapperError extends Error {
10
9
  constructor(message) {
@@ -59,16 +58,26 @@ class SwapperManager {
59
58
  this.swappers.delete(swapperType);
60
59
  return this;
61
60
  }
62
- async getBestSwapper(quoteParams) {
63
- quoteParams; // noop to shut up linter
64
- return types_1.SwapperType.Zrx; // TODO: implement getBestSwapper
61
+ async getBestSwapper(args) {
62
+ // TODO: This will eventually have logic to determine the best swapper.
63
+ // For now we return the first swapper we get from getSwappersByPair
64
+ return this.getSwappersByPair(args)[0];
65
65
  }
66
- getSupportedBuyAssetsFromSellId(args) {
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) {
67
76
  return (0, uniq_1.default)(Array.from(this.swappers.values()).flatMap((swapper) => swapper.filterBuyAssetsBySellAssetId(args)));
68
77
  }
69
- getSupportedSellAssets(args) {
70
- const { sellAssetIds } = args;
71
- return (0, uniq_1.default)(Array.from(this.swappers.values()).flatMap((swapper) => swapper.filterAssetIdsBySellable(sellAssetIds)));
78
+ getSupportedSellableAssetIds(args) {
79
+ const { assetIds } = args;
80
+ return (0, uniq_1.default)(Array.from(this.swappers.values()).flatMap((swapper) => swapper.filterAssetIdsBySellable(assetIds)));
72
81
  }
73
82
  }
74
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,22 @@
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
+ filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): CAIP19[];
21
+ filterAssetIdsBySellable(): CAIP19[];
22
+ }
@@ -0,0 +1,56 @@
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
+ filterBuyAssetsBySellAssetId(args) {
47
+ const { sellAssetId } = args;
48
+ if (!this.supportAssets.includes(sellAssetId))
49
+ return [];
50
+ return this.supportAssets;
51
+ }
52
+ filterAssetIdsBySellable() {
53
+ return this.supportAssets;
54
+ }
55
+ }
56
+ exports.TestSwapper = TestSwapper;
@@ -11,7 +11,6 @@ export declare class ThorchainSwapper implements Swapper {
11
11
  getDefaultPair(): [CAIP19, CAIP19];
12
12
  approvalNeeded(): Promise<ApprovalNeededOutput>;
13
13
  approveInfinite(): Promise<string>;
14
- getSendMaxAmount(): Promise<string>;
15
14
  filterBuyAssetsBySellAssetId(): CAIP19[];
16
15
  filterAssetIdsBySellable(): CAIP19[];
17
16
  }
@@ -32,14 +32,11 @@ class ThorchainSwapper {
32
32
  async approveInfinite() {
33
33
  throw new Error('ThorchainSwapper: approveInfinite unimplemented');
34
34
  }
35
- async getSendMaxAmount() {
36
- throw new Error('ThorchainSwapper: getSendMaxAmount unimplemented');
37
- }
38
35
  filterBuyAssetsBySellAssetId() {
39
- throw new Error('ThorchainSwapper: filterBuyAssetsBySellAssetId unimplemented');
36
+ return [];
40
37
  }
41
38
  filterAssetIdsBySellable() {
42
- throw new Error('ThorchainSwapper: filterAssetIdsBySellable unimplemented');
39
+ return [];
43
40
  }
44
41
  }
45
42
  exports.ThorchainSwapper = ThorchainSwapper;
@@ -1,6 +1,6 @@
1
1
  import { CAIP19 } from '@shapeshiftoss/caip';
2
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
- import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SendMaxAmountInput, SwapperType } from '@shapeshiftoss/types';
3
+ import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, Asset, BuildQuoteTxInput, ChainTypes, ExecQuoteInput, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SwapperType } from '@shapeshiftoss/types';
4
4
  import Web3 from 'web3';
5
5
  import { BuyAssetBySellIdInput, Swapper } from '../../api';
6
6
  export declare type ZrxSwapperDeps = {
@@ -23,7 +23,6 @@ export declare class ZrxSwapper implements Swapper {
23
23
  executeQuote(args: ExecQuoteInput<ChainTypes, SwapperType>): Promise<ExecQuoteOutput>;
24
24
  approvalNeeded(args: ApprovalNeededInput<ChainTypes, SwapperType>): Promise<ApprovalNeededOutput>;
25
25
  approveInfinite(args: ApproveInfiniteInput<ChainTypes, SwapperType>): Promise<string>;
26
- getSendMaxAmount(args: SendMaxAmountInput): Promise<string>;
27
26
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): CAIP19[];
28
27
  filterAssetIdsBySellable(assetIds: CAIP19[]): CAIP19[];
29
28
  }
@@ -4,7 +4,6 @@ exports.ZrxSwapper = exports.ZrxError = void 0;
4
4
  const types_1 = require("@shapeshiftoss/types");
5
5
  const getZrxMinMax_1 = require("./getZrxMinMax/getZrxMinMax");
6
6
  const getZrxQuote_1 = require("./getZrxQuote/getZrxQuote");
7
- const getZrxSendMaxAmount_1 = require("./getZrxSendMaxAmount/getZrxSendMaxAmount");
8
7
  const helpers_1 = require("./utils/helpers/helpers");
9
8
  const ZrxApprovalNeeded_1 = require("./ZrxApprovalNeeded/ZrxApprovalNeeded");
10
9
  const ZrxApproveInfinite_1 = require("./ZrxApproveInfinite/ZrxApproveInfinite");
@@ -50,17 +49,13 @@ class ZrxSwapper {
50
49
  async approveInfinite(args) {
51
50
  return (0, ZrxApproveInfinite_1.ZrxApproveInfinite)(this.deps, args);
52
51
  }
53
- async getSendMaxAmount(args) {
54
- return (0, getZrxSendMaxAmount_1.getZrxSendMaxAmount)(this.deps, args);
55
- }
56
52
  filterBuyAssetsBySellAssetId(args) {
57
- const { buyAssetIds } = args;
53
+ const { assetIds, sellAssetId } = args;
58
54
  // TODO: pending changes to caip lib, we may want to import caip2 value instead.
59
- return buyAssetIds.filter((id) => id.startsWith('eip155:1'));
55
+ return assetIds.filter((id) => id.startsWith('eip155:1') && sellAssetId.startsWith('eip155:1'));
60
56
  }
61
57
  filterAssetIdsBySellable(assetIds) {
62
- // reusing logic to avoid potential bugs from changing one and not the other
63
- return this.filterBuyAssetsBySellAssetId({ buyAssetIds: assetIds, sellAssetId: '' });
58
+ return assetIds.filter((id) => id.startsWith('eip155:1'));
64
59
  }
65
60
  }
66
61
  exports.ZrxSwapper = ZrxSwapper;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "2.1.0",
3
+ "version": "2.2.2",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -1,3 +0,0 @@
1
- import { SendMaxAmountInput } from '@shapeshiftoss/types';
2
- import { ZrxSwapperDeps } from '../ZrxSwapper';
3
- export declare function getZrxSendMaxAmount({ adapterManager }: ZrxSwapperDeps, { wallet, quote, sellAssetAccountId, feeEstimateKey }: SendMaxAmountInput): Promise<string>;
@@ -1,63 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getZrxSendMaxAmount = void 0;
7
- const caip_1 = require("@shapeshiftoss/caip");
8
- const types_1 = require("@shapeshiftoss/types");
9
- const bignumber_js_1 = __importDefault(require("bignumber.js"));
10
- const api_1 = require("../../../api");
11
- const bignumber_1 = require("../utils/bignumber");
12
- const constants_1 = require("../utils/constants");
13
- async function getZrxSendMaxAmount({ adapterManager }, { wallet, quote, sellAssetAccountId, feeEstimateKey = types_1.chainAdapters.FeeDataKey.Average }) {
14
- var _a, _b, _c;
15
- const adapter = adapterManager.byChain(types_1.ChainTypes.Ethereum);
16
- const bip44Params = adapter.buildBIP44Params({
17
- accountNumber: (0, bignumber_1.bnOrZero)(sellAssetAccountId).toNumber()
18
- });
19
- const ethAddress = await adapter.getAddress({ wallet, bip44Params });
20
- const account = await adapter.getAccount(ethAddress);
21
- if (!quote.sellAsset) {
22
- throw new api_1.SwapError('quote.sellAsset is required');
23
- }
24
- const tokenId = (_a = quote.sellAsset) === null || _a === void 0 ? void 0 : _a.tokenId;
25
- let balance;
26
- if (tokenId) {
27
- balance = (_c = (_b = account.chainSpecific.tokens) === null || _b === void 0 ? void 0 : _b.find((token) => {
28
- return caip_1.caip19.fromCAIP19(token.caip19).assetReference === tokenId.toLowerCase();
29
- })) === null || _c === void 0 ? void 0 : _c.balance;
30
- }
31
- else {
32
- balance = account.balance;
33
- }
34
- if (!balance) {
35
- throw new api_1.SwapError(`No balance found for ${tokenId ? quote.sellAsset.symbol : 'ETH'}`);
36
- }
37
- // return the erc20 token balance. No need to subtract the fee.
38
- if (tokenId && new bignumber_js_1.default(balance).gt(0)) {
39
- return balance;
40
- }
41
- if (!quote.txData) {
42
- throw new api_1.SwapError('quote.txData is required to get correct fee estimate');
43
- }
44
- const feeEstimates = await adapter.getFeeData({
45
- to: quote.depositAddress,
46
- value: (0, bignumber_1.bnOrZero)(quote.sellAmount).toString(),
47
- chainSpecific: {
48
- from: ethAddress,
49
- contractData: quote.txData
50
- }
51
- });
52
- const estimatedFee = feeEstimates[feeEstimateKey].txFee;
53
- // (ryankk) We need to pad the fee for ETH max sends because the fee estimate is based off
54
- // of a minimum quote value (quote.sellAmount) and not the users full ETH balance.
55
- const paddedFee = new bignumber_js_1.default(estimatedFee).times(constants_1.ETH_FEE_ESTIMATE_PADDING);
56
- const sendMaxAmount = new bignumber_js_1.default(balance).minus(paddedFee);
57
- // gte covers if sendMaxAmount is NaN
58
- if (!sendMaxAmount.gte(0)) {
59
- throw new api_1.SwapError('ETH balance is less than estimated fee');
60
- }
61
- return sendMaxAmount.toString();
62
- }
63
- exports.getZrxSendMaxAmount = getZrxSendMaxAmount;