@shapeshiftoss/swapper 7.5.1 → 7.7.1

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, ChainId } from '@shapeshiftoss/caip';
2
2
  import { BTCSignTx, ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core';
3
- import { Asset, ChainSpecific, KnownChainIds } from '@shapeshiftoss/types';
3
+ import { Asset, BIP44Params, ChainSpecific, KnownChainIds, UtxoAccountType } from '@shapeshiftoss/types';
4
4
  export declare const SwapError: {
5
5
  new <V extends {
6
6
  cause?: unknown;
@@ -58,8 +58,17 @@ export declare type CommonTradeInput = {
58
58
  sellAssetAccountNumber: number;
59
59
  wallet?: HDWallet;
60
60
  };
61
- export declare type GetTradeQuoteInput = CommonTradeInput;
62
- export declare type BuildTradeInput = CommonTradeInput & {
61
+ export declare type GetEthTradeQuoteInput = CommonTradeInput & {
62
+ chainId: 'eip155:1';
63
+ };
64
+ export declare type GetBtcTradeQuoteInput = CommonTradeInput & {
65
+ chainId: 'bip122:000000000019d6689c085ae165831e93';
66
+ accountType: UtxoAccountType;
67
+ bip44Params: BIP44Params;
68
+ wallet: HDWallet;
69
+ };
70
+ export declare type GetTradeQuoteInput = GetEthTradeQuoteInput | GetBtcTradeQuoteInput;
71
+ export declare type BuildTradeInput = GetTradeQuoteInput & {
63
72
  buyAssetAccountNumber: number;
64
73
  slippage?: string;
65
74
  wallet: HDWallet;
@@ -109,11 +109,11 @@ const main = async () => {
109
109
  let quote;
110
110
  try {
111
111
  quote = await swapper.getTradeQuote({
112
+ chainId: 'eip155:1',
112
113
  sellAsset,
113
114
  buyAsset,
114
115
  sellAmount: sellAmountBase,
115
116
  sellAssetAccountNumber: 0,
116
- wallet,
117
117
  sendMax: false
118
118
  });
119
119
  }
@@ -128,6 +128,7 @@ const main = async () => {
128
128
  const answer = readline_sync_1.default.question(`Swap ${sellAmount} ${sellAsset.symbol} for ${buyAmount} ${buyAsset.symbol} on ${swapper.getType()}? (y/n): `);
129
129
  if (answer === 'y') {
130
130
  const trade = await swapper.buildTrade({
131
+ chainId: 'eip155:1',
131
132
  wallet,
132
133
  buyAsset,
133
134
  sendMax: false,
@@ -1,6 +1,6 @@
1
1
  import { AssetId, ChainId } from '@shapeshiftoss/caip';
2
2
  import type { Asset } from '@shapeshiftoss/types';
3
- import { ApprovalNeededInput, ApprovalNeededOutput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
3
+ import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
4
  import { ThorchainSwapperDeps } from './types';
5
5
  export declare class ThorchainSwapper implements Swapper<ChainId> {
6
6
  private swapSupportedChainIds;
@@ -11,7 +11,7 @@ export declare class ThorchainSwapper implements Swapper<ChainId> {
11
11
  getType(): SwapperType;
12
12
  getUsdRate(input: Pick<Asset, 'symbol' | 'assetId'>): Promise<string>;
13
13
  approvalNeeded(input: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
14
- approveInfinite(): Promise<string>;
14
+ approveInfinite(input: ApproveInfiniteInput<'eip155:1'>): Promise<string>;
15
15
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
16
16
  filterAssetIdsBySellable(): AssetId[];
17
17
  buildTrade(): Promise<Trade<ChainId>>;
@@ -5,6 +5,7 @@ const caip_1 = require("@shapeshiftoss/caip");
5
5
  const api_1 = require("../../api");
6
6
  const getTradeQuote_1 = require("./getThorTradeQuote/getTradeQuote");
7
7
  const thorTradeApprovalNeeded_1 = require("./thorTradeApprovalNeeded/thorTradeApprovalNeeded");
8
+ const thorTradeApproveInfinite_1 = require("./thorTradeApproveInfinite/thorTradeApproveInfinite");
8
9
  const getUsdRate_1 = require("./utils/getUsdRate/getUsdRate");
9
10
  const thorService_1 = require("./utils/thorService");
10
11
  class ThorchainSwapper {
@@ -44,8 +45,8 @@ class ThorchainSwapper {
44
45
  async approvalNeeded(input) {
45
46
  return (0, thorTradeApprovalNeeded_1.thorTradeApprovalNeeded)({ deps: this.deps, input });
46
47
  }
47
- async approveInfinite() {
48
- throw new Error('ThorchainSwapper: approveInfinite unimplemented');
48
+ async approveInfinite(input) {
49
+ return (0, thorTradeApproveInfinite_1.thorTradeApproveInfinite)({ deps: this.deps, input });
49
50
  }
50
51
  filterBuyAssetsBySellAssetId(args) {
51
52
  const { assetIds = [], sellAssetId } = args;
@@ -0,0 +1,6 @@
1
+ import { ApproveInfiniteInput } from '../../../api';
2
+ import { ThorchainSwapperDeps } from '../types';
3
+ export declare const thorTradeApproveInfinite: ({ deps, input }: {
4
+ deps: ThorchainSwapperDeps;
5
+ input: ApproveInfiniteInput<'eip155:1'>;
6
+ }) => Promise<string>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.thorTradeApproveInfinite = void 0;
4
+ const types_1 = require("@shapeshiftoss/types");
5
+ const api_1 = require("../../../api");
6
+ const erc20_abi_1 = require("../../utils/abi/erc20-abi");
7
+ const helpers_1 = require("../../utils/helpers/helpers");
8
+ const constants_1 = require("../utils/constants");
9
+ const thorTradeApproveInfinite = async ({ deps, input }) => {
10
+ try {
11
+ const { adapterManager, web3 } = deps;
12
+ const { quote, wallet } = input;
13
+ const sellAssetChainId = quote.sellAsset.chainId;
14
+ const adapter = adapterManager.get(types_1.KnownChainIds.EthereumMainnet);
15
+ if (!adapter)
16
+ throw new api_1.SwapError(`[thorTradeApproveInfinite] - No chain adapter found for ${sellAssetChainId}.`, {
17
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
18
+ details: { sellAssetChainId }
19
+ });
20
+ const allowanceGrantRequired = await (0, helpers_1.grantAllowance)({
21
+ quote: {
22
+ ...quote,
23
+ sellAmount: constants_1.MAX_ALLOWANCE
24
+ },
25
+ wallet,
26
+ adapter,
27
+ erc20Abi: erc20_abi_1.erc20Abi,
28
+ web3
29
+ });
30
+ return allowanceGrantRequired;
31
+ }
32
+ catch (e) {
33
+ if (e instanceof api_1.SwapError)
34
+ throw e;
35
+ throw new api_1.SwapError('[ZrxApproveInfinite]', {
36
+ cause: e,
37
+ code: api_1.SwapErrorTypes.APPROVE_INFINITE_FAILED
38
+ });
39
+ }
40
+ };
41
+ exports.thorTradeApproveInfinite = thorTradeApproveInfinite;
@@ -1 +1,2 @@
1
1
  export declare const MAX_THORCHAIN_TRADE = "100000000000000000000000000";
2
+ export declare const MAX_ALLOWANCE = "100000000000000000000000000";
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MAX_THORCHAIN_TRADE = void 0;
3
+ exports.MAX_ALLOWANCE = exports.MAX_THORCHAIN_TRADE = void 0;
4
4
  exports.MAX_THORCHAIN_TRADE = '100000000000000000000000000';
5
+ exports.MAX_ALLOWANCE = '100000000000000000000000000';
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getEthTxFees = void 0;
4
+ const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
4
5
  const types_1 = require("@shapeshiftoss/types");
5
6
  const api_1 = require("../../../../../api");
6
7
  const bignumber_1 = require("../../../../utils/bignumber");
@@ -9,17 +10,36 @@ const estimateTradeFee_1 = require("../../estimateTradeFee/estimateTradeFee");
9
10
  const getEthTxFees = async ({ deps, data, router, buyAsset, sellAmount, adapterManager, receiveAddress, sellAssetReference }) => {
10
11
  try {
11
12
  const adapter = adapterManager.get(types_1.KnownChainIds.EthereumMainnet);
12
- if (!adapter)
13
+ if (!adapter) {
13
14
  throw new api_1.SwapError(`[getThorTxInfo] - No chain adapter found for ${types_1.KnownChainIds.EthereumMainnet}.`, {
14
15
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
15
16
  details: { chainId: types_1.KnownChainIds.EthereumMainnet }
16
17
  });
17
- // TODO(ryankk): add falling back on hard coded gas Limit * new chain adapter method if it fails
18
- const feeDataOptions = await adapter.getFeeData({
19
- to: router,
20
- value: sellAmount,
21
- chainSpecific: { from: receiveAddress, contractData: data }
22
- });
18
+ }
19
+ let feeDataOptions;
20
+ try {
21
+ feeDataOptions = await adapter.getFeeData({
22
+ to: router,
23
+ value: sellAmount,
24
+ chainSpecific: { from: receiveAddress, contractData: data }
25
+ });
26
+ }
27
+ catch (e) {
28
+ // Fallback to fixed fee amount in case of failure so that quote will not fail
29
+ // eslint-disable-next-line no-console
30
+ console.debug('[ThorSwapper:getEthTxFees] precise gas estimate failed, falling back on hard coded limit');
31
+ const gasFeeData = await adapter.getGasFeeData();
32
+ const gasLimit = '100000'; // good value to cover all thortrades out of eth/erc20
33
+ feeDataOptions = {
34
+ fast: {
35
+ txFee: (0, bignumber_1.bn)(gasLimit).times(gasFeeData[chain_adapters_1.FeeDataKey.Fast].gasPrice).toString(),
36
+ chainSpecific: {
37
+ gasPrice: gasFeeData[chain_adapters_1.FeeDataKey.Fast].gasPrice,
38
+ gasLimit
39
+ }
40
+ }
41
+ };
42
+ }
23
43
  const feeData = feeDataOptions['fast'];
24
44
  const tradeFee = await (0, estimateTradeFee_1.estimateTradeFee)(deps, buyAsset.assetId);
25
45
  return {
@@ -1,7 +1,7 @@
1
1
  import { Asset } from '@shapeshiftoss/types';
2
2
  import { TradeQuote } from '../../../api';
3
3
  export declare const setupQuote: () => {
4
- quoteInput: import("../../../api").CommonTradeInput;
4
+ quoteInput: import("../../../api").GetEthTradeQuoteInput;
5
5
  tradeQuote: TradeQuote<"eip155:1">;
6
6
  buyAsset: Asset;
7
7
  sellAsset: Asset;
@@ -19,6 +19,7 @@ const setupQuote = () => {
19
19
  sources: []
20
20
  };
21
21
  const quoteInput = {
22
+ chainId: 'eip155:1',
22
23
  sellAmount: '1000000000000000000',
23
24
  sellAsset,
24
25
  buyAsset,
@@ -1,3 +1,4 @@
1
+ import { HDWallet } from '@shapeshiftoss/hdwallet-core';
1
2
  import { Asset } from '@shapeshiftoss/types';
2
3
  import { ZrxQuoteResponse } from '../../types';
3
4
  export declare const setupZrxTradeQuoteResponse: () => {
@@ -6,17 +7,12 @@ export declare const setupZrxTradeQuoteResponse: () => {
6
7
  sellAsset: Asset;
7
8
  };
8
9
  export declare const setupBuildTrade: () => {
9
- buildTradeInput: {
10
- sellAmount: string;
11
- allowanceTarget: string;
12
- price: string;
13
- to: string;
14
- buyAmount: string;
15
- buyAsset: Asset;
16
- sendMax: boolean;
17
- sellAssetAccountNumber: number;
10
+ buildTradeInput: import("../../../../api").CommonTradeInput & {
11
+ chainId: "eip155:1";
12
+ } & {
18
13
  buyAssetAccountNumber: number;
19
- sellAsset: Asset;
14
+ slippage?: string | undefined;
15
+ wallet: HDWallet;
20
16
  };
21
17
  buyAsset: Asset;
22
18
  sellAsset: Asset;
@@ -23,16 +23,14 @@ const setupBuildTrade = () => {
23
23
  const sellAsset = { ...assets_1.FOX };
24
24
  const buyAsset = { ...assets_1.WETH };
25
25
  const buildTradeInput = {
26
+ chainId: 'eip155:1',
26
27
  sellAmount: '1000000000000000000',
27
- allowanceTarget: 'allowanceTargetAddress',
28
- price: '1',
29
- to: '0x123',
30
- buyAmount: '',
31
28
  buyAsset,
32
29
  sendMax: false,
33
30
  sellAssetAccountNumber: 0,
34
31
  buyAssetAccountNumber: 0,
35
- sellAsset
32
+ sellAsset,
33
+ wallet: {}
36
34
  };
37
35
  return { buildTradeInput, buyAsset, sellAsset };
38
36
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "7.5.1",
3
+ "version": "7.7.1",
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",