@shapeshiftoss/swapper 4.0.2 → 4.0.3

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,6 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
2
  import { HDWallet } from '@shapeshiftoss/hdwallet-core';
3
- import { ApprovalNeededOutput, Asset, chainAdapters, ChainTypes, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, Quote, SwapperType } from '@shapeshiftoss/types';
3
+ import { ApprovalNeededOutput, Asset, chainAdapters, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
4
4
  export declare type SupportedAssetInput = {
5
5
  assetIds: AssetId[];
6
6
  };
@@ -18,8 +18,7 @@ export declare type SupportedSellAssetsInput = {
18
18
  export declare type CommonTradeInput = {
19
19
  sellAsset: Asset;
20
20
  buyAsset: Asset;
21
- sellAmount?: string;
22
- buyAmount?: string;
21
+ sellAmount: string;
23
22
  sendMax: boolean;
24
23
  sellAssetAccountId: string;
25
24
  };
@@ -63,11 +62,11 @@ export declare type SwapSource = {
63
62
  proportion: string;
64
63
  };
65
64
  export declare type ApproveInfiniteInput<C extends ChainTypes> = {
66
- quote: Quote<C> | TradeQuote<C>;
65
+ quote: TradeQuote<C>;
67
66
  wallet: HDWallet;
68
67
  };
69
68
  export declare type ApprovalNeededInput<C extends ChainTypes> = {
70
- quote: Quote<C> | TradeQuote<C>;
69
+ quote: TradeQuote<C>;
71
70
  wallet: HDWallet;
72
71
  };
73
72
  export declare class SwapError extends Error {
@@ -90,7 +89,7 @@ export interface Swapper {
90
89
  /**
91
90
  * Get the minimum and maximum trade value of the sellAsset and buyAsset
92
91
  */
93
- getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
92
+ getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
94
93
  /**
95
94
  * Execute a trade built with buildTrade by signing and broadcasting
96
95
  */
@@ -1,5 +1,5 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
- import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
2
+ import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
3
3
  import { BuyAssetBySellIdInput, Swapper, Trade, TradeQuote } from '../../api';
4
4
  /**
5
5
  * Playground for testing different scenarios of multiple swappers in the manager.
@@ -10,7 +10,7 @@ export declare class TestSwapper implements Swapper {
10
10
  getType(): SwapperType;
11
11
  constructor();
12
12
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
13
- getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
13
+ getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
14
14
  approvalNeeded(): Promise<ApprovalNeededOutput>;
15
15
  approveInfinite(): Promise<string>;
16
16
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
@@ -1,10 +1,10 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
- import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
2
+ import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
3
3
  import { Swapper, Trade, TradeQuote } from '../../api';
4
4
  export declare class ThorchainSwapper implements Swapper {
5
5
  getType(): SwapperType;
6
6
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
7
- getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
7
+ getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
8
8
  approvalNeeded(): Promise<ApprovalNeededOutput>;
9
9
  approveInfinite(): Promise<string>;
10
10
  filterBuyAssetsBySellAssetId(): AssetId[];
@@ -1,6 +1,6 @@
1
1
  import { AssetId } from '@shapeshiftoss/caip';
2
2
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
3
- import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetQuoteInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
3
+ import { ApprovalNeededOutput, Asset, ChainTypes, ExecQuoteOutput, GetMinMaxInput, MinMaxOutput, SwapperType } from '@shapeshiftoss/types';
4
4
  import Web3 from 'web3';
5
5
  import { ApprovalNeededInput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, Trade, TradeQuote } from '../../api';
6
6
  export declare type ZrxSwapperDeps = {
@@ -18,7 +18,7 @@ export declare class ZrxSwapper implements Swapper {
18
18
  buildTrade(args: BuildTradeInput): Promise<Trade<ChainTypes>>;
19
19
  getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<ChainTypes>>;
20
20
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
21
- getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
21
+ getMinMax(input: GetMinMaxInput): Promise<MinMaxOutput>;
22
22
  executeTrade(args: ExecuteTradeInput<ChainTypes>): Promise<ExecQuoteOutput>;
23
23
  approvalNeeded(args: ApprovalNeededInput<ChainTypes>): Promise<ApprovalNeededOutput>;
24
24
  approveInfinite(args: ApproveInfiniteInput<ChainTypes>): Promise<string>;
@@ -33,7 +33,7 @@ class ZrxSwapper {
33
33
  return (0, helpers_1.getUsdRate)(input);
34
34
  }
35
35
  async getMinMax(input) {
36
- return (0, getZrxMinMax_1.getZrxMinMax)(input);
36
+ return (0, getZrxMinMax_1.getZrxMinMax)(input.sellAsset, input.buyAsset);
37
37
  }
38
38
  async executeTrade(args) {
39
39
  return (0, zrxExecuteTrade_1.zrxExecuteTrade)(this.deps, args);
@@ -1,2 +1,2 @@
1
- import { GetQuoteInput, MinMaxOutput } from '@shapeshiftoss/types';
2
- export declare const getZrxMinMax: (input: Pick<GetQuoteInput, 'sellAsset' | 'buyAsset'>) => Promise<MinMaxOutput>;
1
+ import { Asset, MinMaxOutput } from '@shapeshiftoss/types';
2
+ export declare const getZrxMinMax: (sellAsset: Asset, buyAsset: Asset) => Promise<MinMaxOutput>;
@@ -6,8 +6,7 @@ const bignumber_1 = require("../utils/bignumber");
6
6
  const constants_1 = require("../utils/constants");
7
7
  const helpers_1 = require("../utils/helpers/helpers");
8
8
  const ZrxSwapper_1 = require("../ZrxSwapper");
9
- const getZrxMinMax = async (input) => {
10
- const { sellAsset, buyAsset } = input;
9
+ const getZrxMinMax = async (sellAsset, buyAsset) => {
11
10
  if (sellAsset.chain !== types_1.ChainTypes.Ethereum || buyAsset.chain !== types_1.ChainTypes.Ethereum) {
12
11
  throw new ZrxSwapper_1.ZrxError('getZrxMinMax - must be eth assets');
13
12
  }
@@ -15,7 +14,7 @@ const getZrxMinMax = async (input) => {
15
14
  symbol: sellAsset.symbol,
16
15
  tokenId: sellAsset.tokenId
17
16
  });
18
- const minimum = (0, bignumber_1.bnOrZero)(1).dividedBy((0, bignumber_1.bnOrZero)(usdRate)).toString();
17
+ const minimum = (0, bignumber_1.bn)(1).dividedBy((0, bignumber_1.bnOrZero)(usdRate)).toString();
19
18
  return {
20
19
  minimum,
21
20
  maximum: constants_1.MAX_ZRX_TRADE // Arbitrarily large value. 10e+28 here.
@@ -10,7 +10,7 @@ const zrxService_1 = require("../utils/zrxService");
10
10
  const ZrxSwapper_1 = require("../ZrxSwapper");
11
11
  async function getZrxTradeQuote(input) {
12
12
  var _a, _b, _c, _d, _e, _f, _g;
13
- const { sellAsset, buyAsset, sellAmount, buyAmount, sellAssetAccountId } = input;
13
+ const { sellAsset, buyAsset, sellAmount, sellAssetAccountId } = input;
14
14
  if (!buyAsset) {
15
15
  throw new ZrxSwapper_1.ZrxError('getQuote - Missing buyAsset');
16
16
  }
@@ -20,24 +20,12 @@ async function getZrxTradeQuote(input) {
20
20
  if (buyAsset.chain !== types_1.ChainTypes.Ethereum || sellAsset.chain !== types_1.ChainTypes.Ethereum) {
21
21
  throw new ZrxSwapper_1.ZrxError('getQuote - Both assets need to be on the Ethereum chain to use Zrx');
22
22
  }
23
- if (!sellAmount && !buyAmount) {
24
- throw new ZrxSwapper_1.ZrxError('getQuote - sellAmount or buyAmount amount is required');
25
- }
26
23
  const useSellAmount = !!sellAmount;
27
24
  const buyToken = buyAsset.tokenId || buyAsset.symbol;
28
25
  const sellToken = sellAsset.tokenId || sellAsset.symbol;
29
- const { minimum: minQuoteSellAmount, maximum: maxSellAmount } = await (0, getZrxMinMax_1.getZrxMinMax)(input);
30
- const minQuoteSellAmountWei = (0, bignumber_1.bnOrZero)(minQuoteSellAmount).times((0, bignumber_1.bnOrZero)(10).exponentiatedBy(sellAsset.precision));
31
- const amount = useSellAmount ? { sellAmount } : { buyAmount };
32
- const amountKey = Object.keys(amount)[0];
33
- const amountValue = Object.values(amount)[0];
34
- let normalizedAmount = (0, helpers_1.normalizeAmount)(amountValue);
35
- if (!normalizedAmount || normalizedAmount === '0') {
36
- normalizedAmount = (0, helpers_1.normalizeAmount)(minQuoteSellAmountWei === null || minQuoteSellAmountWei === void 0 ? void 0 : minQuoteSellAmountWei.toString());
37
- }
38
- if (!normalizedAmount || normalizedAmount === '0') {
39
- throw new ZrxSwapper_1.ZrxError('getQuote - Must have valid sellAmount, buyAmount or minimum amount');
40
- }
26
+ const { minimum, maximum } = await (0, getZrxMinMax_1.getZrxMinMax)(buyAsset, sellAsset);
27
+ const minQuoteSellAmount = (0, bignumber_1.bnOrZero)(minimum).times((0, bignumber_1.bn)(10).exponentiatedBy(sellAsset.precision));
28
+ const normalizedSellAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(minimum).lt(minQuoteSellAmount) ? minQuoteSellAmount.toString() : sellAmount);
41
29
  try {
42
30
  /**
43
31
  * /swap/v1/price
@@ -52,7 +40,7 @@ async function getZrxTradeQuote(input) {
52
40
  params: {
53
41
  sellToken,
54
42
  buyToken,
55
- [amountKey]: normalizedAmount
43
+ sellAmount: normalizedSellAmount
56
44
  }
57
45
  });
58
46
  const { data } = quoteResponse;
@@ -62,8 +50,8 @@ async function getZrxTradeQuote(input) {
62
50
  success: true,
63
51
  statusReason: '',
64
52
  rate,
65
- minimum: minQuoteSellAmount,
66
- maximum: maxSellAmount,
53
+ minimum,
54
+ maximum,
67
55
  feeData: {
68
56
  fee: (0, bignumber_1.bnOrZero)(estimatedGas).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString(),
69
57
  chainSpecific: {
@@ -1,9 +1,10 @@
1
1
  import { ChainAdapter } from '@shapeshiftoss/chain-adapters';
2
2
  import { HDWallet } from '@shapeshiftoss/hdwallet-core';
3
- import { Asset, ChainTypes, Quote } from '@shapeshiftoss/types';
3
+ import { Asset, ChainTypes } from '@shapeshiftoss/types';
4
4
  import BigNumber from 'bignumber.js';
5
5
  import Web3 from 'web3';
6
6
  import { AbiItem } from 'web3-utils';
7
+ import { TradeQuote } from '../../../../api';
7
8
  export declare type GetAllowanceRequiredArgs = {
8
9
  receiveAddress: string;
9
10
  allowanceContract: string;
@@ -20,7 +21,7 @@ export declare type GetERC20AllowanceArgs = {
20
21
  spenderAddress: string;
21
22
  };
22
23
  declare type GrantAllowanceArgs = {
23
- quote: Quote<ChainTypes>;
24
+ quote: TradeQuote<ChainTypes>;
24
25
  wallet: HDWallet;
25
26
  adapter: ChainAdapter<ChainTypes.Ethereum>;
26
27
  erc20Abi: AbiItem[];
@@ -12,6 +12,15 @@ export declare const setupQuote: () => {
12
12
  sellAssetAccountId: string;
13
13
  buyAssetAccountId: string;
14
14
  sendMax: boolean;
15
+ minimum: string;
16
+ maximum: string;
17
+ statusReason: string;
18
+ buyAmount: string;
19
+ feeData: {
20
+ fee: string;
21
+ };
22
+ rate: string;
23
+ sources: never[];
15
24
  };
16
25
  buyAsset: Asset;
17
26
  sellAsset: Asset;
@@ -15,7 +15,14 @@ const setupQuote = () => {
15
15
  receiveAddress: 'receiveAddress',
16
16
  sellAssetAccountId: '0',
17
17
  buyAssetAccountId: '0',
18
- sendMax: false
18
+ sendMax: false,
19
+ minimum: '0',
20
+ maximum: '999999999999',
21
+ statusReason: '',
22
+ buyAmount: '',
23
+ feeData: { fee: '0' },
24
+ rate: '1',
25
+ sources: []
19
26
  };
20
27
  return { quoteInput, buyAsset, sellAsset };
21
28
  };
@@ -35,10 +35,7 @@ const helpers_1 = require("../utils/helpers/helpers");
35
35
  const zrxService_1 = require("../utils/zrxService");
36
36
  async function zrxBuildTrade({ adapterManager, web3 }, input) {
37
37
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
38
- const { sellAsset, buyAsset, sellAmount, buyAmount, slippage, sellAssetAccountId, buyAssetAccountId, wallet } = input;
39
- if ((buyAmount && sellAmount) || (!buyAmount && !sellAmount)) {
40
- throw new __1.SwapError('ZrxSwapper:ZrxBuildTrade Exactly one of buyAmount or sellAmount is required');
41
- }
38
+ const { sellAsset, buyAsset, sellAmount, slippage, sellAssetAccountId, buyAssetAccountId, wallet } = input;
42
39
  if (!sellAssetAccountId || !buyAssetAccountId) {
43
40
  throw new __1.SwapError('ZrxSwapper:ZrxBuildTrade Both sellAssetAccountId and buyAssetAccountId are required');
44
41
  }
@@ -67,7 +64,6 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
67
64
  * sellToken: contract address (or symbol) of token to sell
68
65
  * buyToken: contractAddress (or symbol) of token to buy
69
66
  * sellAmount?: integer string value of the smallest increment of the sell token
70
- * buyAmount?: integer string value of the smallest incremtent of the buy token
71
67
  * }
72
68
  */
73
69
  const zrxRetry = (0, applyAxiosRetry_1.applyAxiosRetry)(zrxService_1.zrxService, {
@@ -92,7 +88,6 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
92
88
  buyToken,
93
89
  sellToken,
94
90
  sellAmount: (0, helpers_1.normalizeAmount)(sellAmount === null || sellAmount === void 0 ? void 0 : sellAmount.toString()),
95
- buyAmount: (0, helpers_1.normalizeAmount)(buyAmount === null || buyAmount === void 0 ? void 0 : buyAmount.toString()),
96
91
  takerAddress: receiveAddress,
97
92
  slippagePercentage,
98
93
  skipValidation: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",