@shapeshiftoss/swapper 9.14.0 → 9.15.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
@@ -129,10 +129,14 @@ export declare type ExecuteTradeInput<C extends ChainId> = {
129
129
  export declare type TradeResult = {
130
130
  tradeId: string;
131
131
  };
132
- export declare type ApproveInfiniteInput<C extends ChainId> = {
132
+ export declare type ApproveInput<C extends ChainId> = {
133
133
  quote: TradeQuote<C>;
134
134
  wallet: HDWallet;
135
135
  };
136
+ export declare type ApproveInfiniteInput<C extends ChainId> = ApproveInput<C>;
137
+ export declare type ApproveAmountInput<C extends ChainId> = ApproveInput<C> & {
138
+ amount?: string;
139
+ };
136
140
  export declare type ApprovalNeededInput<C extends ChainId> = {
137
141
  quote: TradeQuote<C>;
138
142
  wallet: HDWallet;
@@ -163,6 +167,7 @@ export declare type TradeTxs = {
163
167
  export declare enum SwapErrorTypes {
164
168
  ALLOWANCE_REQUIRED_FAILED = "ALLOWANCE_REQUIRED_FAILED",
165
169
  APPROVE_INFINITE_FAILED = "APPROVE_INFINITE_FAILED",
170
+ APPROVE_AMOUNT_FAILED = "APPROVE_AMOUNT_FAILED",
166
171
  BUILD_TRADE_FAILED = "BUILD_TRADE_FAILED",
167
172
  CHECK_APPROVAL_FAILED = "CHECK_APPROVAL_FAILED",
168
173
  EXECUTE_TRADE_FAILED = "EXECUTE_TRADE_FAILED",
@@ -187,7 +192,7 @@ export declare enum SwapErrorTypes {
187
192
  TRADE_FAILED = "TRADE_FAILED"
188
193
  }
189
194
  export interface Swapper<T extends ChainId> {
190
- /** Human readable swapper name */
195
+ /** Human-readable swapper name */
191
196
  readonly name: string;
192
197
  /** perform any necessary async initialization */
193
198
  initialize?(): Promise<void>;
@@ -217,6 +222,11 @@ export interface Swapper<T extends ChainId> {
217
222
  * Get the txid of an approve infinite transaction
218
223
  */
219
224
  approveInfinite(args: ApproveInfiniteInput<T>): Promise<string>;
225
+ /**
226
+ * Get the txid of an approve amount transaction
227
+ * If no amount is specified the sell amount of the quote will be used
228
+ */
229
+ approveAmount(args: ApproveAmountInput<T>): Promise<string>;
220
230
  /**
221
231
  * Get supported buyAssetId's by sellAssetId
222
232
  */
package/dist/api.js CHANGED
@@ -18,6 +18,7 @@ var SwapErrorTypes;
18
18
  (function (SwapErrorTypes) {
19
19
  SwapErrorTypes["ALLOWANCE_REQUIRED_FAILED"] = "ALLOWANCE_REQUIRED_FAILED";
20
20
  SwapErrorTypes["APPROVE_INFINITE_FAILED"] = "APPROVE_INFINITE_FAILED";
21
+ SwapErrorTypes["APPROVE_AMOUNT_FAILED"] = "APPROVE_AMOUNT_FAILED";
21
22
  SwapErrorTypes["BUILD_TRADE_FAILED"] = "BUILD_TRADE_FAILED";
22
23
  SwapErrorTypes["CHECK_APPROVAL_FAILED"] = "CHECK_APPROVAL_FAILED";
23
24
  SwapErrorTypes["EXECUTE_TRADE_FAILED"] = "EXECUTE_TRADE_FAILED";
@@ -21,6 +21,7 @@ export declare class CowSwapper implements Swapper<KnownChainIds.EthereumMainnet
21
21
  executeTrade(args: ExecuteTradeInput<KnownChainIds.EthereumMainnet>): Promise<TradeResult>;
22
22
  approvalNeeded(args: ApprovalNeededInput<KnownChainIds.EthereumMainnet>): Promise<ApprovalNeededOutput>;
23
23
  approveInfinite(args: ApproveInfiniteInput<KnownChainIds.EthereumMainnet>): Promise<string>;
24
+ approveAmount(): Promise<string>;
24
25
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
25
26
  filterAssetIdsBySellable(assetIds: AssetId[]): AssetId[];
26
27
  getTradeTxs(args: TradeResult): Promise<TradeTxs>;
@@ -52,6 +52,11 @@ class CowSwapper {
52
52
  async approveInfinite(args) {
53
53
  return (0, cowApproveInfinite_1.cowApproveInfinite)(this.deps, args);
54
54
  }
55
+ async approveAmount() {
56
+ throw new api_1.SwapError('CowSwapper: approveAmount unimplemented', {
57
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR
58
+ });
59
+ }
55
60
  filterBuyAssetsBySellAssetId(args) {
56
61
  const { assetIds = [], sellAssetId } = args;
57
62
  if ((0, caip_1.fromAssetId)(sellAssetId).assetNamespace !== 'erc20' ||
@@ -15,6 +15,7 @@ export declare class OsmosisSwapper implements Swapper<ChainId> {
15
15
  }): Promise<MinMaxOutput>;
16
16
  approvalNeeded(): Promise<ApprovalNeededOutput>;
17
17
  approveInfinite(): Promise<string>;
18
+ approveAmount(): Promise<string>;
18
19
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): string[];
19
20
  filterAssetIdsBySellable(): AssetId[];
20
21
  buildTrade(args: BuildTradeInput): Promise<Trade<ChainId>>;
@@ -51,6 +51,11 @@ class OsmosisSwapper {
51
51
  code: api_1.SwapErrorTypes.RESPONSE_ERROR
52
52
  });
53
53
  }
54
+ async approveAmount() {
55
+ throw new api_1.SwapError('Osmosis: approveAmount unimplemented', {
56
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR
57
+ });
58
+ }
54
59
  filterBuyAssetsBySellAssetId(args) {
55
60
  const { assetIds = [], sellAssetId } = args;
56
61
  if (!this.supportedAssetIds.includes(sellAssetId))
@@ -14,6 +14,7 @@ export declare class TestSwapper implements Swapper<ChainId> {
14
14
  getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
15
15
  approvalNeeded(): Promise<ApprovalNeededOutput>;
16
16
  approveInfinite(): Promise<string>;
17
+ approveAmount(): Promise<string>;
17
18
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
18
19
  filterAssetIdsBySellable(): AssetId[];
19
20
  buildTrade(): Promise<Trade<ChainId>>;
@@ -31,6 +31,11 @@ class TestSwapper {
31
31
  async approveInfinite() {
32
32
  throw new Error('TestSwapper: approveInfinite unimplemented');
33
33
  }
34
+ async approveAmount() {
35
+ throw new api_1.SwapError('TestSwapper: approveAmount unimplemented', {
36
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR
37
+ });
38
+ }
34
39
  filterBuyAssetsBySellAssetId(args) {
35
40
  const { sellAssetId } = args;
36
41
  if (!this.supportAssets.includes(sellAssetId))
@@ -17,6 +17,7 @@ export declare class ThorchainSwapper implements Swapper<ChainId> {
17
17
  getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
18
18
  approvalNeeded(input: ApprovalNeededInput<KnownChainIds.EthereumMainnet>): Promise<ApprovalNeededOutput>;
19
19
  approveInfinite(input: ApproveInfiniteInput<KnownChainIds.EthereumMainnet>): Promise<string>;
20
+ approveAmount(): Promise<string>;
20
21
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
21
22
  filterAssetIdsBySellable(): AssetId[];
22
23
  buildTrade(input: BuildTradeInput): Promise<Trade<ChainId>>;
@@ -85,6 +85,11 @@ class ThorchainSwapper {
85
85
  async approveInfinite(input) {
86
86
  return (0, thorTradeApproveInfinite_1.thorTradeApproveInfinite)({ deps: this.deps, input });
87
87
  }
88
+ async approveAmount() {
89
+ throw new api_1.SwapError('ThorchainSwapper: approveAmount unimplemented', {
90
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR
91
+ });
92
+ }
88
93
  filterBuyAssetsBySellAssetId(args) {
89
94
  const { assetIds = [], sellAssetId } = args;
90
95
  if (!this.supportedSellAssetIds.includes(sellAssetId))
@@ -1,6 +1,6 @@
1
1
  import { Asset } from '@shapeshiftoss/asset-service';
2
2
  import { AssetId, ChainId } from '@shapeshiftoss/caip';
3
- import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, EvmSupportedChainIds, GetEvmTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs } from '../../api';
3
+ import { ApprovalNeededInput, ApprovalNeededOutput, ApproveAmountInput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, EvmSupportedChainIds, GetEvmTradeQuoteInput, Swapper, SwapperType, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
4
  import { ZrxExecuteTradeInput, ZrxSwapperDeps, ZrxTrade } from './types';
5
5
  export declare class ZrxSwapper<T extends EvmSupportedChainIds> implements Swapper<T> {
6
6
  readonly name = "0x";
@@ -14,6 +14,7 @@ export declare class ZrxSwapper<T extends EvmSupportedChainIds> implements Swapp
14
14
  executeTrade(args: ZrxExecuteTradeInput<T>): Promise<TradeResult>;
15
15
  approvalNeeded(args: ApprovalNeededInput<T>): Promise<ApprovalNeededOutput>;
16
16
  approveInfinite(args: ApproveInfiniteInput<T>): Promise<string>;
17
+ approveAmount(args: ApproveAmountInput<T>): Promise<string>;
17
18
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
18
19
  filterAssetIdsBySellable(assetIds?: AssetId[]): AssetId[];
19
20
  getTradeTxs(tradeResult: TradeResult): Promise<TradeTxs>;
@@ -7,7 +7,7 @@ const getZrxTradeQuote_1 = require("./getZrxTradeQuote/getZrxTradeQuote");
7
7
  const blacklist_1 = require("./utils/blacklist");
8
8
  const helpers_1 = require("./utils/helpers/helpers");
9
9
  const zrxApprovalNeeded_1 = require("./zrxApprovalNeeded/zrxApprovalNeeded");
10
- const zrxApproveInfinite_1 = require("./zrxApproveInfinite/zrxApproveInfinite");
10
+ const zrxApprove_1 = require("./zrxApprove/zrxApprove");
11
11
  const zrxBuildTrade_1 = require("./zrxBuildTrade/zrxBuildTrade");
12
12
  const zrxExecuteTrade_1 = require("./zrxExecuteTrade/zrxExecuteTrade");
13
13
  class ZrxSwapper {
@@ -44,7 +44,10 @@ class ZrxSwapper {
44
44
  return (0, zrxApprovalNeeded_1.zrxApprovalNeeded)(this.deps, args);
45
45
  }
46
46
  async approveInfinite(args) {
47
- return (0, zrxApproveInfinite_1.zrxApproveInfinite)(this.deps, args);
47
+ return (0, zrxApprove_1.zrxApproveInfinite)(this.deps, args);
48
+ }
49
+ async approveAmount(args) {
50
+ return (0, zrxApprove_1.zrxApproveAmount)(this.deps, args);
48
51
  }
49
52
  filterBuyAssetsBySellAssetId(args) {
50
53
  const { assetIds = [], sellAssetId } = args;
@@ -0,0 +1,4 @@
1
+ import { ApproveAmountInput, ApproveInfiniteInput, EvmSupportedChainIds } from '../../../api';
2
+ import { ZrxSwapperDeps } from '../types';
3
+ export declare function zrxApproveAmount<T extends EvmSupportedChainIds>(deps: ZrxSwapperDeps, args: ApproveAmountInput<T>): Promise<string>;
4
+ export declare function zrxApproveInfinite<T extends EvmSupportedChainIds>(deps: ZrxSwapperDeps, args: ApproveInfiniteInput<T>): Promise<string>;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.zrxApproveInfinite = exports.zrxApproveAmount = void 0;
4
+ const api_1 = require("../../../api");
5
+ const erc20_abi_1 = require("../../utils/abi/erc20-abi");
6
+ const constants_1 = require("../../utils/constants");
7
+ const helpers_1 = require("../../utils/helpers/helpers");
8
+ const constants_2 = require("../utils/constants");
9
+ const grantAllowanceForAmount = async ({ adapter, web3 }, { quote, wallet }, approvalAmount) => {
10
+ const approvalQuote = {
11
+ ...quote,
12
+ sellAmount: approvalAmount,
13
+ feeData: {
14
+ ...quote.feeData,
15
+ chainSpecific: {
16
+ ...quote.feeData.chainSpecific,
17
+ // 0x approvals are cheaper than trades, but we don't have dynamic quote data for them.
18
+ // Instead, we use a hardcoded gasLimit estimate in place of the estimatedGas in the 0x quote response.
19
+ estimatedGas: constants_1.APPROVAL_GAS_LIMIT
20
+ }
21
+ }
22
+ };
23
+ return await (0, helpers_1.grantAllowance)({
24
+ quote: approvalQuote,
25
+ wallet,
26
+ adapter,
27
+ erc20Abi: erc20_abi_1.erc20Abi,
28
+ web3
29
+ });
30
+ };
31
+ async function zrxApproveAmount(deps, args) {
32
+ var _a;
33
+ try {
34
+ const sellAmount = args.quote.sellAmount;
35
+ // If no amount is specified we use the quotes sell amount
36
+ const approvalAmount = (_a = args.amount) !== null && _a !== void 0 ? _a : sellAmount;
37
+ return grantAllowanceForAmount(deps, args, approvalAmount);
38
+ }
39
+ catch (e) {
40
+ if (e instanceof api_1.SwapError)
41
+ throw e;
42
+ throw new api_1.SwapError('[zrxApproveAmount]', {
43
+ cause: e,
44
+ code: api_1.SwapErrorTypes.APPROVE_AMOUNT_FAILED
45
+ });
46
+ }
47
+ }
48
+ exports.zrxApproveAmount = zrxApproveAmount;
49
+ async function zrxApproveInfinite(deps, args) {
50
+ try {
51
+ return grantAllowanceForAmount(deps, args, constants_2.MAX_ALLOWANCE);
52
+ }
53
+ catch (e) {
54
+ if (e instanceof api_1.SwapError)
55
+ throw e;
56
+ throw new api_1.SwapError('[zrxApproveInfinite]', {
57
+ cause: e,
58
+ code: api_1.SwapErrorTypes.APPROVE_INFINITE_FAILED
59
+ });
60
+ }
61
+ }
62
+ exports.zrxApproveInfinite = zrxApproveInfinite;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "9.14.0",
3
+ "version": "9.15.0",
4
4
  "description": "An interface and implementations to trade coins via various protocols",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",
@@ -1,3 +0,0 @@
1
- import { ApproveInfiniteInput, EvmSupportedChainIds } from '../../../api';
2
- import { ZrxSwapperDeps } from '../types';
3
- export declare function zrxApproveInfinite<T extends EvmSupportedChainIds>({ adapter, web3 }: ZrxSwapperDeps, { quote, wallet }: ApproveInfiniteInput<T>): Promise<string>;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.zrxApproveInfinite = void 0;
4
- const api_1 = require("../../../api");
5
- const erc20_abi_1 = require("../../utils/abi/erc20-abi");
6
- const constants_1 = require("../../utils/constants");
7
- const helpers_1 = require("../../utils/helpers/helpers");
8
- const constants_2 = require("../utils/constants");
9
- async function zrxApproveInfinite({ adapter, web3 }, { quote, wallet }) {
10
- try {
11
- const approvalQuote = {
12
- ...quote,
13
- sellAmount: constants_2.MAX_ALLOWANCE,
14
- feeData: {
15
- ...quote.feeData,
16
- chainSpecific: {
17
- ...quote.feeData.chainSpecific,
18
- // 0x approvals are cheaper than trades, but we don't have dynamic quote data for them.
19
- // Instead, we use a hardcoded gasLimit estimate in place of the estimatedGas in the 0x quote response.
20
- estimatedGas: constants_1.APPROVAL_GAS_LIMIT
21
- }
22
- }
23
- };
24
- const allowanceGrantRequired = await (0, helpers_1.grantAllowance)({
25
- quote: approvalQuote,
26
- wallet,
27
- adapter,
28
- erc20Abi: erc20_abi_1.erc20Abi,
29
- web3
30
- });
31
- return allowanceGrantRequired;
32
- }
33
- catch (e) {
34
- if (e instanceof api_1.SwapError)
35
- throw e;
36
- throw new api_1.SwapError('[zrxApproveInfinite]', {
37
- cause: e,
38
- code: api_1.SwapErrorTypes.APPROVE_INFINITE_FAILED
39
- });
40
- }
41
- }
42
- exports.zrxApproveInfinite = zrxApproveInfinite;