@shapeshiftoss/swapper 5.10.0 → 5.11.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
@@ -33,9 +33,14 @@ declare type ChainSpecificQuoteFeeData<T1> = ChainSpecific<T1, {
33
33
  approvalFee?: string;
34
34
  totalFee?: string;
35
35
  };
36
+ 'bip122:000000000019d6689c085ae165831e93': {
37
+ byteCount: string;
38
+ satsPerByte: string;
39
+ };
36
40
  }>;
37
41
  export declare type QuoteFeeData<T1 extends SupportedChainIds> = {
38
42
  fee: string;
43
+ tradeFee: string;
39
44
  } & ChainSpecificQuoteFeeData<T1>;
40
45
  export declare type ByPairInput = {
41
46
  sellAssetId: AssetId;
@@ -62,8 +67,6 @@ export declare type BuildTradeInput = CommonTradeInput & {
62
67
  wallet: HDWallet;
63
68
  };
64
69
  interface TradeBase<C extends SupportedChainIds> {
65
- success: boolean;
66
- statusReason: string;
67
70
  buyAmount: string;
68
71
  sellAmount: string;
69
72
  feeData: QuoteFeeData<C>;
@@ -106,10 +106,6 @@ const main = async () => {
106
106
  return;
107
107
  }
108
108
  console.info('quote = ', JSON.stringify(quote));
109
- if (!quote.success) {
110
- console.error('Obtaining the quote failed: ', quote.statusReason);
111
- return;
112
- }
113
109
  const buyAmount = fromBaseUnit(quote.buyAmount || '0', buyAsset.precision);
114
110
  const answer = readline_sync_1.default.question(`Swap ${sellAmount} ${sellAsset.symbol} for ${buyAmount} ${buyAsset.symbol} on ${swapper.getType()}? (y/n): `);
115
111
  if (answer === 'y') {
@@ -3,6 +3,7 @@ import { Asset, SupportedChainIds } from '@shapeshiftoss/types';
3
3
  import { ApprovalNeededOutput, BuyAssetBySellIdInput, Swapper, SwapperType, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
4
4
  import { ThorchainSwapperDeps } from './types';
5
5
  export declare class ThorchainSwapper implements Swapper {
6
+ private swapSupportedChainIds;
6
7
  private supportedAssetIds;
7
8
  deps: ThorchainSwapperDeps;
8
9
  constructor(deps: ThorchainSwapperDeps);
@@ -7,6 +7,10 @@ const getUsdRate_1 = require("./utils/getUsdRate/getUsdRate");
7
7
  const thorService_1 = require("./utils/thorService");
8
8
  class ThorchainSwapper {
9
9
  constructor(deps) {
10
+ this.swapSupportedChainIds = {
11
+ 'eip155:1': true,
12
+ 'bip122:000000000019d6689c085ae165831e93': true
13
+ };
10
14
  this.supportedAssetIds = [];
11
15
  this.deps = deps;
12
16
  }
@@ -15,7 +19,7 @@ class ThorchainSwapper {
15
19
  const { data: responseData } = await thorService_1.thorService.get(`${this.deps.midgardUrl}/pools`);
16
20
  const supportedAssetIds = responseData.reduce((acc, midgardPool) => {
17
21
  const assetId = caip_1.adapters.poolAssetIdToAssetId(midgardPool.asset);
18
- if (!assetId)
22
+ if (!assetId || !this.swapSupportedChainIds[(0, caip_1.fromAssetId)(assetId).chainId])
19
23
  return acc;
20
24
  acc.push(assetId);
21
25
  return acc;
@@ -1,5 +1,5 @@
1
1
  import { ChainAdapterManager } from '@shapeshiftoss/chain-adapters';
2
- export declare type MidgardResponse = {
2
+ export declare type PoolResponse = {
3
3
  asset: string;
4
4
  assetDepth: string;
5
5
  assetPrice: string;
@@ -0,0 +1,3 @@
1
+ import { AssetId } from '@shapeshiftoss/caip';
2
+ import { ThorchainSwapperDeps } from '../../types';
3
+ export declare const estimateTradeFee: (deps: ThorchainSwapperDeps, buyAssetId: AssetId) => Promise<string>;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.estimateTradeFee = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const api_1 = require("../../../../api");
6
+ const bignumber_1 = require("../../../zrx/utils/bignumber");
7
+ const thorService_1 = require("../thorService");
8
+ const gweiGasPrecision = 9;
9
+ // https://docs.thorchain.org/how-it-works/fees#outbound-fee
10
+ // estimatedTradeFee = `3 * estimatedFeeOutboundFee`
11
+ // eth txs from thorchain use approx 38383 gas
12
+ const ethEstimate = (gasRate) => (0, bignumber_1.bnOrZero)(gasRate).times(38383).times(3).times((0, bignumber_1.bn)(10).exponentiatedBy(gweiGasPrecision)).toString();
13
+ // erc20 txs from thorchain use approx 70996 gas
14
+ const erc20Estimate = (gasRate) => (0, bignumber_1.bnOrZero)(gasRate).times(70996).times(3).times((0, bignumber_1.bn)(10).exponentiatedBy(gweiGasPrecision)).toString();
15
+ // BTC utxo transactions from thorchain are approx 303 bytes long
16
+ const btcEstimate = (gasRate) => (0, bignumber_1.bnOrZero)(gasRate).times(303).times(3).toString();
17
+ const estimateTradeFee = async (deps, buyAssetId) => {
18
+ const thorId = caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAssetId });
19
+ if (!thorId)
20
+ throw new api_1.SwapError('[estimateTradeFee] - undefined thorId for given buyAssetId', {
21
+ code: api_1.SwapErrorTypes.VALIDATION_FAILED,
22
+ details: { buyAssetId }
23
+ });
24
+ const thorPoolChainId = thorId.slice(0, thorId.indexOf('.'));
25
+ const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.midgardUrl}/thorchain/inbound_addresses`);
26
+ const inboundInfo = inboundAddresses.find((inbound) => inbound.chain === thorPoolChainId);
27
+ if (!inboundInfo)
28
+ throw new api_1.SwapError('[estimateTradeFee] - unable to locate inbound pool info', {
29
+ code: api_1.SwapErrorTypes.VALIDATION_FAILED,
30
+ details: { thorPoolChainId }
31
+ });
32
+ const gasRate = inboundInfo.gas_rate;
33
+ const { chainId, assetNamespace } = (0, caip_1.fromAssetId)(buyAssetId);
34
+ switch (chainId) {
35
+ case 'bip122:000000000019d6689c085ae165831e93':
36
+ return btcEstimate(gasRate);
37
+ case 'eip155:1':
38
+ switch (assetNamespace) {
39
+ case 'slip44':
40
+ return ethEstimate(gasRate);
41
+ case 'erc20':
42
+ return erc20Estimate(gasRate);
43
+ default:
44
+ throw new api_1.SwapError('[estimateTradeFee] - unsupported asset namespace', {
45
+ code: api_1.SwapErrorTypes.VALIDATION_FAILED,
46
+ details: { assetNamespace }
47
+ });
48
+ }
49
+ default:
50
+ throw new api_1.SwapError('[estimateTradeFee] - unsupported chain id', {
51
+ code: api_1.SwapErrorTypes.VALIDATION_FAILED,
52
+ details: { chainId }
53
+ });
54
+ }
55
+ };
56
+ exports.estimateTradeFee = estimateTradeFee;
@@ -1,5 +1,6 @@
1
- import { MidgardResponse } from '../../types';
2
- export declare const btcMidgardPool: MidgardResponse;
3
- export declare const ethMidgardPool: MidgardResponse;
4
- export declare const foxMidgardPool: MidgardResponse;
5
- export declare const midgardResponse: MidgardResponse[];
1
+ import { InboundResponse, PoolResponse } from '../../types';
2
+ export declare const btcMidgardPool: PoolResponse;
3
+ export declare const ethMidgardPool: PoolResponse;
4
+ export declare const foxMidgardPool: PoolResponse;
5
+ export declare const mockInboundAdresses: InboundResponse[];
6
+ export declare const poolResponse: PoolResponse[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.midgardResponse = exports.foxMidgardPool = exports.ethMidgardPool = exports.btcMidgardPool = void 0;
3
+ exports.poolResponse = exports.mockInboundAdresses = exports.foxMidgardPool = exports.ethMidgardPool = exports.btcMidgardPool = void 0;
4
4
  exports.btcMidgardPool = {
5
5
  asset: 'BTC.BTC',
6
6
  assetDepth: '91027798705',
@@ -43,4 +43,56 @@ exports.foxMidgardPool = {
43
43
  units: '10568061707512',
44
44
  volume24h: '251596933033'
45
45
  };
46
- exports.midgardResponse = [exports.btcMidgardPool, exports.ethMidgardPool, exports.foxMidgardPool];
46
+ exports.mockInboundAdresses = [
47
+ {
48
+ chain: 'BCH',
49
+ pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
50
+ address: 'qp5a6rn8zmamgmsyspqsl0p6ktk4hrheggdjdvntv0',
51
+ halted: false,
52
+ gas_rate: '3'
53
+ },
54
+ {
55
+ chain: 'BNB',
56
+ pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
57
+ address: 'bnb1d8wsuecklw6xupyqgy8mcw4ja4dca72z7nvqzw',
58
+ halted: false,
59
+ gas_rate: '11250'
60
+ },
61
+ {
62
+ chain: 'BTC',
63
+ pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
64
+ address: 'bc1qd8wsuecklw6xupyqgy8mcw4ja4dca72z2dd88d',
65
+ halted: false,
66
+ gas_rate: '18'
67
+ },
68
+ {
69
+ chain: 'DOGE',
70
+ pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
71
+ address: 'DEnrJfAjC4KMsLsGtoTGSmYaFZLPunWw9q',
72
+ halted: false,
73
+ gas_rate: '550384'
74
+ },
75
+ {
76
+ chain: 'ETH',
77
+ pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
78
+ address: '0x78e4e10dcacb0a8261eb3d5e57ffb98ae8d4dff1',
79
+ router: '0x3624525075b88B24ecc29CE226b0CEc1fFcB6976',
80
+ halted: false,
81
+ gas_rate: '280'
82
+ },
83
+ {
84
+ chain: 'LTC',
85
+ pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
86
+ address: 'ltc1qd8wsuecklw6xupyqgy8mcw4ja4dca72zw3hrla',
87
+ halted: false,
88
+ gas_rate: '60'
89
+ },
90
+ {
91
+ chain: 'TERRA',
92
+ pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
93
+ address: 'terra1d8wsuecklw6xupyqgy8mcw4ja4dca72z6nfvpq',
94
+ halted: true,
95
+ gas_rate: '182523800'
96
+ }
97
+ ];
98
+ exports.poolResponse = [exports.btcMidgardPool, exports.ethMidgardPool, exports.foxMidgardPool];
@@ -1,4 +1,3 @@
1
- import { SupportedChainIds } from '@shapeshiftoss/types';
2
1
  import { ApprovalNeededInput, ApprovalNeededOutput } from '../../../api';
3
2
  import { ZrxSwapperDeps } from '../ZrxSwapper';
4
- export declare function ZrxApprovalNeeded({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
3
+ export declare function ZrxApprovalNeeded({ adapterManager, web3 }: ZrxSwapperDeps, { quote, wallet }: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
@@ -16,8 +16,8 @@ export declare class ZrxSwapper implements Swapper {
16
16
  buildTrade(args: BuildTradeInput): Promise<ZrxTrade<SupportedChainIds>>;
17
17
  getTradeQuote(input: GetTradeQuoteInput): Promise<TradeQuote<SupportedChainIds>>;
18
18
  getUsdRate(input: Asset): Promise<string>;
19
- executeTrade(args: ExecuteTradeInput<SupportedChainIds>): Promise<TradeResult>;
20
- approvalNeeded(args: ApprovalNeededInput<SupportedChainIds>): Promise<ApprovalNeededOutput>;
19
+ executeTrade(args: ExecuteTradeInput<'eip155:1'>): Promise<TradeResult>;
20
+ approvalNeeded(args: ApprovalNeededInput<'eip155:1'>): Promise<ApprovalNeededOutput>;
21
21
  approveInfinite(args: ApproveInfiniteInput<SupportedChainIds>): Promise<string>;
22
22
  filterBuyAssetsBySellAssetId(args: BuyAssetBySellIdInput): AssetId[];
23
23
  filterAssetIdsBySellable(assetIds?: AssetId[]): AssetId[];
@@ -46,8 +46,6 @@ async function getZrxTradeQuote(input) {
46
46
  const estimatedGas = (0, bignumber_1.bnOrZero)(data.estimatedGas).times(1.5);
47
47
  const rate = useSellAmount ? data.price : (0, bignumber_1.bn)(1).div(data.price).toString();
48
48
  return {
49
- success: true,
50
- statusReason: '',
51
49
  rate,
52
50
  minimum,
53
51
  maximum,
@@ -58,7 +56,8 @@ async function getZrxTradeQuote(input) {
58
56
  gasPrice: data.gasPrice,
59
57
  approvalFee: sellAssetErc20Address &&
60
58
  (0, bignumber_1.bnOrZero)(constants_1.APPROVAL_GAS_LIMIT).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString()
61
- }
59
+ },
60
+ tradeFee: '0'
62
61
  },
63
62
  sellAmount: data.sellAmount,
64
63
  buyAmount: data.buyAmount,
@@ -3,7 +3,7 @@ import { TradeQuote } from '../../../../api';
3
3
  import { ZrxQuoteResponse } from '../../types';
4
4
  export declare const setupQuote: () => {
5
5
  quoteInput: import("../../../../api").CommonTradeInput;
6
- tradeQuote: TradeQuote<"eip155:1" | "bip122:000000000019d6689c085ae165831e93" | "cosmos:cosmoshub-4" | "cosmos:osmosis-1">;
6
+ tradeQuote: TradeQuote<"eip155:1">;
7
7
  buyAsset: Asset;
8
8
  sellAsset: Asset;
9
9
  };
@@ -44,11 +44,10 @@ export declare const setupExecuteTrade: () => {
44
44
  txData: string;
45
45
  depositAddress: string;
46
46
  receiveAddress: string;
47
- success: boolean;
48
- statusReason: string;
49
47
  feeData: {
50
48
  fee: string;
51
49
  chainSpecific: {};
50
+ tradeFee: string;
52
51
  };
53
52
  rate: string;
54
53
  allowanceContract: string;
@@ -6,8 +6,6 @@ const setupQuote = () => {
6
6
  const sellAsset = { ...assets_1.FOX };
7
7
  const buyAsset = { ...assets_1.WETH };
8
8
  const tradeQuote = {
9
- success: true,
10
- statusReason: '',
11
9
  buyAmount: '',
12
10
  sellAmount: '1000000000000000000',
13
11
  sellAsset,
@@ -16,7 +14,7 @@ const setupQuote = () => {
16
14
  sellAssetAccountId: '0',
17
15
  minimum: '0',
18
16
  maximum: '999999999999',
19
- feeData: { fee: '0' },
17
+ feeData: { fee: '0', tradeFee: '0', chainSpecific: {} },
20
18
  rate: '1',
21
19
  sources: []
22
20
  };
@@ -83,9 +81,7 @@ const setupExecuteTrade = () => {
83
81
  txData: '0x0',
84
82
  depositAddress: '0x0',
85
83
  receiveAddress: '0x0',
86
- success: true,
87
- statusReason: '',
88
- feeData: { fee: '0', chainSpecific: {} },
84
+ feeData: { fee: '0', chainSpecific: {}, tradeFee: '0' },
89
85
  rate: '0',
90
86
  allowanceContract: '0x0',
91
87
  sources: []
@@ -92,8 +92,6 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
92
92
  const trade = {
93
93
  sellAsset,
94
94
  buyAsset,
95
- success: true,
96
- statusReason: '',
97
95
  sellAssetAccountId,
98
96
  receiveAddress,
99
97
  rate: data.price,
@@ -103,7 +101,8 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
103
101
  chainSpecific: {
104
102
  estimatedGas: estimatedGas.toString(),
105
103
  gasPrice: data.gasPrice
106
- }
104
+ },
105
+ tradeFee: '0'
107
106
  },
108
107
  txData: data.data,
109
108
  sellAmount: data.sellAmount,
@@ -125,7 +124,8 @@ async function zrxBuildTrade({ adapterManager, web3 }, input) {
125
124
  chainSpecific: {
126
125
  ...(_c = trade.feeData) === null || _c === void 0 ? void 0 : _c.chainSpecific,
127
126
  approvalFee: (0, bignumber_1.bnOrZero)(constants_1.APPROVAL_GAS_LIMIT).multipliedBy((0, bignumber_1.bnOrZero)(data.gasPrice)).toString()
128
- }
127
+ },
128
+ tradeFee: '0'
129
129
  };
130
130
  }
131
131
  return trade;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "5.10.0",
3
+ "version": "5.11.0",
4
4
  "description": "TODO: description",
5
5
  "repository": "https://github.com/shapeshift/lib",
6
6
  "license": "MIT",