@shapeshiftoss/swapper 3.0.1 → 3.0.4

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
@@ -32,10 +32,6 @@ export interface Swapper {
32
32
  * Get the usd rate from either the assets symbol or tokenId
33
33
  */
34
34
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
35
- /**
36
- * Get the default pair of the swapper
37
- */
38
- getDefaultPair(): [CAIP19, CAIP19];
39
35
  /**
40
36
  * Get the minimum and maximum trade value of the sellAsset and buyAsset
41
37
  */
@@ -14,7 +14,6 @@ export declare class TestSwapper implements Swapper {
14
14
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
15
15
  getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
16
16
  executeQuote(): Promise<ExecQuoteOutput>;
17
- getDefaultPair(): [CAIP19, CAIP19];
18
17
  approvalNeeded(): Promise<ApprovalNeededOutput>;
19
18
  approveInfinite(): Promise<string>;
20
19
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): CAIP19[];
@@ -34,9 +34,6 @@ class TestSwapper {
34
34
  async executeQuote() {
35
35
  throw new Error('TestSwapper: executeQuote unimplemented');
36
36
  }
37
- getDefaultPair() {
38
- throw new Error('TestSwapper: getDefaultPair unimplemented');
39
- }
40
37
  async approvalNeeded() {
41
38
  throw new Error('TestSwapper: approvalNeeded unimplemented');
42
39
  }
@@ -8,7 +8,6 @@ export declare class ThorchainSwapper implements Swapper {
8
8
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
9
9
  getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
10
10
  executeQuote(): Promise<ExecQuoteOutput>;
11
- getDefaultPair(): [CAIP19, CAIP19];
12
11
  approvalNeeded(): Promise<ApprovalNeededOutput>;
13
12
  approveInfinite(): Promise<string>;
14
13
  filterBuyAssetsBySellAssetId(): CAIP19[];
@@ -23,9 +23,6 @@ class ThorchainSwapper {
23
23
  async executeQuote() {
24
24
  throw new Error('ThorchainSwapper: executeQuote unimplemented');
25
25
  }
26
- getDefaultPair() {
27
- throw new Error('ThorchainSwapper: getDefaultPair unimplemented');
28
- }
29
26
  async approvalNeeded() {
30
27
  throw new Error('ThorchainSwapper: approvalNeeded unimplemented');
31
28
  }
@@ -7,8 +7,8 @@ const api_1 = require("../../../api");
7
7
  const erc20Allowance_abi_1 = require("../utils/abi/erc20Allowance-abi");
8
8
  const constants_1 = require("../utils/constants");
9
9
  const helpers_1 = require("../utils/helpers/helpers");
10
- const zrxService_1 = require("../utils/zrxService");
11
10
  async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
11
+ var _a, _b;
12
12
  const { sellAsset } = quote;
13
13
  if (sellAsset.symbol === 'ETH') {
14
14
  return { approvalNeeded: false };
@@ -20,42 +20,21 @@ async function ZrxApprovalNeeded({ adapterManager, web3 }, { quote, wallet }) {
20
20
  const adapter = adapterManager.byChain(sellAsset.chain);
21
21
  const bip44Params = adapter.buildBIP44Params({ accountNumber });
22
22
  const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
23
- /**
24
- * /swap/v1/quote
25
- * params: {
26
- * sellToken: contract address (or symbol) of token to sell
27
- * buyToken: contractAddress (or symbol) of token to buy
28
- * sellAmount?: integer string value of the smallest increment of the sell token
29
- * buyAmount?: integer string value of the smallest incremtent of the buy token
30
- * }
31
- */
32
- const quoteResponse = await zrxService_1.zrxService.get('/swap/v1/quote', {
33
- params: {
34
- buyToken: 'ETH',
35
- sellToken: quote.sellAsset.tokenId || quote.sellAsset.symbol || quote.sellAsset.chain,
36
- buyAmount: constants_1.APPROVAL_BUY_AMOUNT,
37
- takerAddress: receiveAddress,
38
- slippagePercentage: constants_1.DEFAULT_SLIPPAGE,
39
- skipValidation: true,
40
- affiliateAddress: constants_1.AFFILIATE_ADDRESS
41
- }
42
- });
43
- const { data } = quoteResponse;
44
- if (!quote.sellAsset.tokenId || !data.allowanceTarget) {
23
+ if (!quote.sellAsset.tokenId || !quote.allowanceContract) {
45
24
  throw new api_1.SwapError('ZrxApprovalNeeded - tokenId and allowanceTarget are required');
46
25
  }
47
26
  const allowanceResult = await (0, helpers_1.getERC20Allowance)({
48
27
  web3,
49
28
  erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi,
50
29
  tokenId: quote.sellAsset.tokenId,
51
- spenderAddress: data.allowanceTarget,
30
+ spenderAddress: quote.allowanceContract,
52
31
  ownerAddress: receiveAddress
53
32
  });
54
33
  const allowanceOnChain = new bignumber_js_1.BigNumber(allowanceResult || '0');
55
34
  return {
56
35
  approvalNeeded: allowanceOnChain.lt(new bignumber_js_1.BigNumber(quote.sellAmount || 1)),
57
36
  gas: constants_1.APPROVAL_GAS_LIMIT,
58
- gasPrice: data.gasPrice
37
+ gasPrice: (_b = (_a = quote === null || quote === void 0 ? void 0 : quote.feeData) === null || _a === void 0 ? void 0 : _a.chainSpecific) === null || _b === void 0 ? void 0 : _b.gasPrice
59
38
  };
60
39
  }
61
40
  exports.ZrxApprovalNeeded = ZrxApprovalNeeded;
@@ -2,46 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ZrxApproveInfinite = void 0;
4
4
  const types_1 = require("@shapeshiftoss/types");
5
- const api_1 = require("../../../api");
6
5
  const erc20_abi_1 = require("../utils/abi/erc20-abi");
7
- const bignumber_1 = require("../utils/bignumber");
8
6
  const constants_1 = require("../utils/constants");
9
7
  const helpers_1 = require("../utils/helpers/helpers");
10
- const zrxService_1 = require("../utils/zrxService");
11
8
  async function ZrxApproveInfinite({ adapterManager, web3 }, { quote, wallet }) {
12
9
  const adapter = adapterManager.byChain(types_1.ChainTypes.Ethereum);
13
- const bip44Params = adapter.buildBIP44Params({
14
- accountNumber: (0, bignumber_1.bnOrZero)(quote.sellAssetAccountId).toNumber()
15
- }); // TODO: Add account number
16
- const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
17
- /**
18
- * /swap/v1/quote
19
- * params: {
20
- * sellToken: contract address (or symbol) of token to sell
21
- * buyToken: contractAddress (or symbol) of token to buy
22
- * sellAmount?: integer string value of the smallest increment of the sell token
23
- * buyAmount?: integer string value of the smallest incremtent of the buy token
24
- * }
25
- */
26
- const quoteResponse = await zrxService_1.zrxService.get('/swap/v1/quote', {
27
- params: {
28
- buyToken: 'ETH',
29
- sellToken: quote.sellAsset.tokenId || quote.sellAsset.symbol || quote.sellAsset.network,
30
- buyAmount: '100000000000000000',
31
- takerAddress: receiveAddress,
32
- slippagePercentage: constants_1.DEFAULT_SLIPPAGE,
33
- skipValidation: true,
34
- affiliateAddress: constants_1.AFFILIATE_ADDRESS
35
- }
36
- });
37
- const { data } = quoteResponse;
38
- if (!data.allowanceTarget) {
39
- throw new api_1.SwapError('approveInfinite - allowanceTarget is required');
40
- }
41
10
  const allowanceGrantRequired = await (0, helpers_1.grantAllowance)({
42
11
  quote: {
43
12
  ...quote,
44
- allowanceContract: data.allowanceTarget,
13
+ allowanceContract: quote.allowanceContract,
45
14
  sellAmount: constants_1.MAX_ALLOWANCE
46
15
  },
47
16
  wallet,
@@ -37,8 +37,8 @@ const constants_1 = require("../utils/constants");
37
37
  const helpers_1 = require("../utils/helpers/helpers");
38
38
  const zrxService_1 = require("../utils/zrxService");
39
39
  async function ZrxBuildQuoteTx({ adapterManager, web3 }, { input, wallet }) {
40
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
41
- const { sellAsset, buyAsset, sellAmount, buyAmount, slippage, sellAssetAccountId, buyAssetAccountId, priceImpact } = input;
40
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
41
+ const { sellAsset, buyAsset, sellAmount, buyAmount, slippage, sellAssetAccountId, buyAssetAccountId } = input;
42
42
  if ((buyAmount && sellAmount) || (!buyAmount && !sellAmount)) {
43
43
  throw new __1.SwapError('ZrxSwapper:ZrxBuildQuoteTx Exactly one of buyAmount or sellAmount is required');
44
44
  }
@@ -114,7 +114,6 @@ async function ZrxBuildQuoteTx({ adapterManager, web3 }, { input, wallet }) {
114
114
  receiveAddress,
115
115
  slippage,
116
116
  success: true,
117
- statusCode: 0,
118
117
  rate: data.price,
119
118
  depositAddress: data.to,
120
119
  feeData: {
@@ -129,18 +128,15 @@ async function ZrxBuildQuoteTx({ adapterManager, web3 }, { input, wallet }) {
129
128
  txData: data.data,
130
129
  sellAmount: data.sellAmount,
131
130
  buyAmount: data.buyAmount,
132
- guaranteedPrice: data.guaranteedPrice,
133
131
  allowanceContract: data.allowanceTarget,
134
- sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_1.DEFAULT_SOURCE,
135
- priceImpact
132
+ sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_1.DEFAULT_SOURCE
136
133
  };
137
134
  const allowanceRequired = await (0, helpers_1.getAllowanceRequired)({
138
135
  quote,
139
136
  web3,
140
137
  erc20AllowanceAbi: erc20Allowance_abi_1.erc20AllowanceAbi
141
138
  });
142
- quote.allowanceGrantRequired = allowanceRequired.gt(0);
143
- if (quote.allowanceGrantRequired) {
139
+ if (allowanceRequired) {
144
140
  quote.feeData = {
145
141
  fee: ((_b = quote.feeData) === null || _b === void 0 ? void 0 : _b.fee) || '0',
146
142
  chainSpecific: {
@@ -152,15 +148,13 @@ async function ZrxBuildQuoteTx({ adapterManager, web3 }, { input, wallet }) {
152
148
  return quote;
153
149
  }
154
150
  catch (e) {
155
- const statusCode = ((_g = (_f = (_e = (_d = e === null || e === void 0 ? void 0 : e.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.validationErrors) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.code) || ((_j = (_h = e === null || e === void 0 ? void 0 : e.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.code) || -1;
156
- const statusReason = ((_o = (_m = (_l = (_k = e === null || e === void 0 ? void 0 : e.response) === null || _k === void 0 ? void 0 : _k.data) === null || _l === void 0 ? void 0 : _l.validationErrors) === null || _m === void 0 ? void 0 : _m[0]) === null || _o === void 0 ? void 0 : _o.reason) ||
157
- ((_q = (_p = e === null || e === void 0 ? void 0 : e.response) === null || _p === void 0 ? void 0 : _p.data) === null || _q === void 0 ? void 0 : _q.reason) ||
151
+ const statusReason = ((_g = (_f = (_e = (_d = e === null || e === void 0 ? void 0 : e.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.validationErrors) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.reason) ||
152
+ ((_j = (_h = e === null || e === void 0 ? void 0 : e.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.reason) ||
158
153
  'Unknown Error';
159
154
  return {
160
155
  sellAsset,
161
156
  buyAsset,
162
157
  success: false,
163
- statusCode,
164
158
  statusReason
165
159
  };
166
160
  }
@@ -19,7 +19,6 @@ export declare class ZrxSwapper implements Swapper {
19
19
  getQuote(input: GetQuoteInput): Promise<Quote<ChainTypes>>;
20
20
  getUsdRate(input: Pick<Asset, 'symbol' | 'tokenId'>): Promise<string>;
21
21
  getMinMax(input: GetQuoteInput): Promise<MinMaxOutput>;
22
- getDefaultPair(): [CAIP19, CAIP19];
23
22
  executeQuote(args: ExecQuoteInput<ChainTypes>): Promise<ExecQuoteOutput>;
24
23
  approvalNeeded(args: ApprovalNeededInput<ChainTypes>): Promise<ApprovalNeededOutput>;
25
24
  approveInfinite(args: ApproveInfiniteInput<ChainTypes>): Promise<string>;
@@ -35,11 +35,6 @@ class ZrxSwapper {
35
35
  async getMinMax(input) {
36
36
  return (0, getZrxMinMax_1.getZrxMinMax)(input);
37
37
  }
38
- getDefaultPair() {
39
- const ETH = 'eip155:1/slip44:60';
40
- const FOX = 'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d';
41
- return [ETH, FOX];
42
- }
43
38
  async executeQuote(args) {
44
39
  return (0, ZrxExecuteQuote_1.ZrxExecuteQuote)(this.deps, args);
45
40
  }
@@ -8,35 +8,19 @@ const types_1 = require("@shapeshiftoss/types");
8
8
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
9
9
  const constants_1 = require("../utils/constants");
10
10
  const helpers_1 = require("../utils/helpers/helpers");
11
- const zrxService_1 = require("../utils/zrxService");
12
11
  const ZrxSwapper_1 = require("../ZrxSwapper");
13
12
  const getZrxMinMax = async (input) => {
14
- var _a;
15
13
  const { sellAsset, buyAsset } = input;
16
14
  if (sellAsset.chain !== types_1.ChainTypes.Ethereum || buyAsset.chain !== types_1.ChainTypes.Ethereum) {
17
15
  throw new ZrxSwapper_1.ZrxError('getZrxMinMax - must be eth assets');
18
16
  }
19
- const buyToken = buyAsset.tokenId || buyAsset.symbol;
20
- const sellToken = sellAsset.tokenId || sellAsset.symbol;
21
17
  const usdRate = await (0, helpers_1.getUsdRate)({
22
18
  symbol: sellAsset.symbol,
23
19
  tokenId: sellAsset.tokenId
24
20
  });
25
- const minimumWeiAmount = new bignumber_js_1.default(1)
26
- .dividedBy(new bignumber_js_1.default(usdRate))
27
- .times(new bignumber_js_1.default(10).exponentiatedBy(sellAsset.precision));
28
21
  const minimum = new bignumber_js_1.default(1).dividedBy(new bignumber_js_1.default(usdRate)).toString();
29
- const minimumPriceResult = await zrxService_1.zrxService.get('/swap/v1/price', {
30
- params: {
31
- sellToken,
32
- buyToken,
33
- sellAmount: (0, helpers_1.normalizeAmount)(minimumWeiAmount.toString())
34
- }
35
- });
36
- const minimumPrice = new bignumber_js_1.default((_a = minimumPriceResult === null || minimumPriceResult === void 0 ? void 0 : minimumPriceResult.data) === null || _a === void 0 ? void 0 : _a.price).toString();
37
22
  return {
38
23
  minimum,
39
- minimumPrice,
40
24
  maximum: constants_1.MAX_ZRX_TRADE
41
25
  };
42
26
  };
@@ -11,8 +11,8 @@ const helpers_1 = require("../utils/helpers/helpers");
11
11
  const zrxService_1 = require("../utils/zrxService");
12
12
  const ZrxSwapper_1 = require("../ZrxSwapper");
13
13
  async function getZrxQuote(input) {
14
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
15
- const { sellAsset, buyAsset, sellAmount, buyAmount, minimumPrice, minimum: minQuoteSellAmount, slippage } = input;
14
+ var _a, _b, _c, _d, _e, _f, _g;
15
+ const { sellAsset, buyAsset, sellAmount, buyAmount, minimum: minQuoteSellAmount, slippage } = input;
16
16
  if (!buyAsset) {
17
17
  throw new ZrxSwapper_1.ZrxError('getQuote - Missing buyAsset');
18
18
  }
@@ -62,12 +62,6 @@ async function getZrxQuote(input) {
62
62
  }
63
63
  });
64
64
  const { data } = quoteResponse;
65
- const quotePrice = new bignumber_js_1.default(data.price);
66
- const priceDifference = quotePrice.minus(minimumPrice);
67
- const priceImpact = priceDifference
68
- .dividedBy(minimumPrice)
69
- .abs()
70
- .valueOf();
71
65
  const estimatedGas = data.estimatedGas
72
66
  ? new bignumber_js_1.default(data.estimatedGas).times(1.5)
73
67
  : new bignumber_js_1.default(0);
@@ -75,10 +69,8 @@ async function getZrxQuote(input) {
75
69
  return {
76
70
  sellAsset,
77
71
  buyAsset,
78
- priceImpact,
79
72
  slippage,
80
73
  success: true,
81
- statusCode: 0,
82
74
  rate,
83
75
  minimum: minQuoteSellAmount,
84
76
  maximum: constants_1.MAX_ZRX_TRADE,
@@ -95,14 +87,13 @@ async function getZrxQuote(input) {
95
87
  },
96
88
  sellAmount: data.sellAmount,
97
89
  buyAmount: data.buyAmount,
98
- guaranteedPrice: data.guaranteedPrice,
99
- sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_1.DEFAULT_SOURCE
90
+ sources: ((_a = data.sources) === null || _a === void 0 ? void 0 : _a.filter((s) => parseFloat(s.proportion) > 0)) || constants_1.DEFAULT_SOURCE,
91
+ allowanceContract: data.allowanceTarget
100
92
  };
101
93
  }
102
94
  catch (e) {
103
- const statusCode = ((_e = (_d = (_c = (_b = e === null || e === void 0 ? void 0 : e.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.validationErrors) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.code) || ((_g = (_f = e === null || e === void 0 ? void 0 : e.response) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.code) || -1;
104
- const statusReason = ((_l = (_k = (_j = (_h = e === null || e === void 0 ? void 0 : e.response) === null || _h === void 0 ? void 0 : _h.data) === null || _j === void 0 ? void 0 : _j.validationErrors) === null || _k === void 0 ? void 0 : _k[0]) === null || _l === void 0 ? void 0 : _l.reason) ||
105
- ((_o = (_m = e === null || e === void 0 ? void 0 : e.response) === null || _m === void 0 ? void 0 : _m.data) === null || _o === void 0 ? void 0 : _o.reason) ||
95
+ const statusReason = ((_e = (_d = (_c = (_b = e === null || e === void 0 ? void 0 : e.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.validationErrors) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.reason) ||
96
+ ((_g = (_f = e === null || e === void 0 ? void 0 : e.response) === null || _f === void 0 ? void 0 : _f.data) === null || _g === void 0 ? void 0 : _g.reason) ||
106
97
  'Unknown Error';
107
98
  return {
108
99
  sellAsset,
@@ -110,7 +101,6 @@ async function getZrxQuote(input) {
110
101
  minimum: minQuoteSellAmount,
111
102
  maximum: constants_1.MAX_ZRX_TRADE,
112
103
  success: false,
113
- statusCode,
114
104
  statusReason
115
105
  };
116
106
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "3.0.1",
3
+ "version": "3.0.4",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -34,7 +34,7 @@
34
34
  "@shapeshiftoss/caip": "^2.0.0",
35
35
  "@shapeshiftoss/chain-adapters": "^2.0.0",
36
36
  "@shapeshiftoss/hdwallet-core": "1.20.0",
37
- "@shapeshiftoss/types": "^2.1.0"
37
+ "@shapeshiftoss/types": "^3.0.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@shapeshiftoss/asset-service": "^2.0.0",