@shapeshiftoss/swapper 8.2.0 → 8.3.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
@@ -63,7 +63,8 @@ declare type CommonTradeInput = {
63
63
  sellAmount: string;
64
64
  sendMax: boolean;
65
65
  sellAssetAccountNumber: number;
66
- wallet?: HDWallet;
66
+ wallet: HDWallet;
67
+ receiveAddress: string;
67
68
  };
68
69
  export declare type EvmSupportedChainIds = KnownChainIds.EthereumMainnet | KnownChainIds.AvalancheMainnet;
69
70
  export declare type EvmSupportedChainAdapters = ethereum.ChainAdapter | avalanche.ChainAdapter;
@@ -114,7 +114,9 @@ const main = async () => {
114
114
  buyAsset,
115
115
  sellAmount: sellAmountBase,
116
116
  sellAssetAccountNumber: 0,
117
- sendMax: false
117
+ sendMax: false,
118
+ receiveAddress: '0xc770eefad204b5180df6a14ee197d99d808ee52d',
119
+ wallet
118
120
  });
119
121
  }
120
122
  catch (e) {
@@ -135,7 +137,8 @@ const main = async () => {
135
137
  sellAmount: sellAmountBase,
136
138
  sellAsset,
137
139
  sellAssetAccountNumber: 0,
138
- buyAssetAccountNumber: 0
140
+ buyAssetAccountNumber: 0,
141
+ receiveAddress: '0xc770eefad204b5180df6a14ee197d99d808ee52d'
139
142
  });
140
143
  const txid = await swapper.executeTrade({ trade, wallet });
141
144
  console.info('broadcast tx with id: ', txid);
@@ -1,7 +1,11 @@
1
1
  import { ChainId } from '@shapeshiftoss/caip';
2
2
  import { GetTradeQuoteInput, TradeQuote } from '../../../api';
3
3
  import { ThorchainSwapperDeps } from '../types';
4
- export declare const getThorTradeQuote: ({ deps, input }: {
4
+ declare type GetThorTradeQuoteInput = {
5
5
  deps: ThorchainSwapperDeps;
6
6
  input: GetTradeQuoteInput;
7
- }) => Promise<TradeQuote<ChainId>>;
7
+ };
8
+ declare type GetThorTradeQuoteReturn = Promise<TradeQuote<ChainId>>;
9
+ declare type GetThorTradeQuote = (args: GetThorTradeQuoteInput) => GetThorTradeQuoteReturn;
10
+ export declare const getThorTradeQuote: GetThorTradeQuote;
11
+ export {};
@@ -2,61 +2,32 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getThorTradeQuote = void 0;
4
4
  const caip_1 = require("@shapeshiftoss/caip");
5
+ const types_1 = require("@shapeshiftoss/types");
5
6
  const api_1 = require("../../../api");
6
7
  const bignumber_1 = require("../../utils/bignumber");
7
8
  const constants_1 = require("../../utils/constants");
8
9
  const helpers_1 = require("../../utils/helpers/helpers");
10
+ const getThorTxData_1 = require("../utils/bitcoin/utils/getThorTxData");
9
11
  const constants_2 = require("../utils/constants");
10
- const getThorTxData_1 = require("../utils/ethereum/utils/getThorTxData");
12
+ const estimateTradeFee_1 = require("../utils/estimateTradeFee/estimateTradeFee");
13
+ const getThorTxData_2 = require("../utils/ethereum/utils/getThorTxData");
11
14
  const getPriceRatio_1 = require("../utils/getPriceRatio/getPriceRatio");
15
+ const getBtcTxFees_1 = require("../utils/txFeeHelpers/btcTxFees/getBtcTxFees");
12
16
  const getEthTxFees_1 = require("../utils/txFeeHelpers/ethTxFees/getEthTxFees");
13
17
  const getThorTradeQuote = async ({ deps, input }) => {
14
- const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber, wallet } = input;
18
+ const { sellAsset, buyAsset, sellAmount, sellAssetAccountNumber, wallet, chainId, receiveAddress } = input;
15
19
  if (!wallet)
16
20
  throw new api_1.SwapError('[getTradeQuote] - wallet is required', {
17
21
  code: api_1.SwapErrorTypes.VALIDATION_FAILED
18
22
  });
19
23
  try {
20
- const { assetReference: sellAssetErc20Address, chainReference, chainNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
21
- const isErc20Trade = sellAssetErc20Address.startsWith('0x');
22
- const sellAssetChainId = (0, caip_1.toChainId)({ chainReference, chainNamespace });
23
- const adapter = deps.adapterManager.get(sellAssetChainId);
24
+ const { assetReference: sellAssetErc20Address } = (0, caip_1.fromAssetId)(sellAsset.assetId);
25
+ const adapter = deps.adapterManager.get(chainId);
24
26
  if (!adapter)
25
- throw new api_1.SwapError(`[getThorTradeQuote] - No chain adapter found for ${sellAssetChainId}.`, {
27
+ throw new api_1.SwapError(`[getThorTradeQuote] - No chain adapter found for ${chainId}.`, {
26
28
  code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
27
- details: { sellAssetChainId }
29
+ details: { chainId }
28
30
  });
29
- const bip44Params = adapter.buildBIP44Params({ accountNumber: Number(sellAssetAccountNumber) });
30
- const receiveAddress = await adapter.getAddress({ wallet, bip44Params });
31
- const { data, router } = await (0, getThorTxData_1.getThorTxInfo)({
32
- deps,
33
- sellAsset,
34
- buyAsset,
35
- sellAmount,
36
- slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
37
- destinationAddress: receiveAddress,
38
- isErc20Trade
39
- });
40
- const feeData = await (async () => {
41
- switch (sellAssetChainId) {
42
- case 'eip155:1':
43
- return await (0, getEthTxFees_1.getEthTxFees)({
44
- deps,
45
- data,
46
- router,
47
- buyAsset,
48
- sellAmount,
49
- adapterManager: deps.adapterManager,
50
- receiveAddress,
51
- sellAssetReference: sellAssetErc20Address
52
- });
53
- default:
54
- throw new api_1.SwapError('[getThorTradeQuote] - Asset chainId is not supported.', {
55
- code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
56
- details: { sellAssetChainId }
57
- });
58
- }
59
- })();
60
31
  const sellAssetId = sellAsset.assetId;
61
32
  const buyAssetId = buyAsset.assetId;
62
33
  const feeAssetId = (0, caip_1.getFeeAssetIdFromAssetId)(buyAssetId);
@@ -69,22 +40,82 @@ const getThorTradeQuote = async ({ deps, input }) => {
69
40
  const priceRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, { sellAssetId, buyAssetId });
70
41
  const rate = (0, bignumber_1.bnOrZero)(1).div(priceRatio).toString();
71
42
  const buyAmount = (0, helpers_1.normalizeAmount)((0, bignumber_1.bnOrZero)(sellAmount).times(rate));
72
- const sellAssetTradeFee = (0, bignumber_1.bnOrZero)(feeData.tradeFee).times((0, bignumber_1.bnOrZero)(feeAssetRatio));
43
+ const tradeFee = await (0, estimateTradeFee_1.estimateTradeFee)(deps, buyAsset.assetId);
44
+ const sellAssetTradeFee = (0, bignumber_1.bnOrZero)(tradeFee).times((0, bignumber_1.bnOrZero)(feeAssetRatio));
73
45
  // padding minimum by 1.5 the trade fee to avoid thorchain "not enough to cover fee" errors.
74
46
  const minimum = (0, bignumber_1.fromBaseUnit)(sellAssetTradeFee.times(1.5).toString(), sellAsset.precision);
75
- return {
47
+ const commonQuoteFields = {
76
48
  rate,
77
- minimum,
78
49
  maximum: constants_2.MAX_THORCHAIN_TRADE,
79
- feeData,
80
50
  sellAmount,
81
51
  buyAmount,
82
52
  sources: [{ name: 'thorchain', proportion: '1' }],
83
- allowanceContract: router,
84
53
  buyAsset,
85
54
  sellAsset,
86
- sellAssetAccountNumber
55
+ sellAssetAccountNumber,
56
+ minimum
87
57
  };
58
+ switch (chainId) {
59
+ case types_1.KnownChainIds.EthereumMainnet:
60
+ return (async () => {
61
+ const { data, router } = await (0, getThorTxData_2.getThorTxInfo)({
62
+ deps,
63
+ sellAsset,
64
+ buyAsset,
65
+ sellAmount,
66
+ slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
67
+ destinationAddress: receiveAddress
68
+ });
69
+ const feeData = await (0, getEthTxFees_1.getEthTxFees)({
70
+ deps,
71
+ data,
72
+ router,
73
+ buyAsset,
74
+ sellAmount,
75
+ adapterManager: deps.adapterManager,
76
+ receiveAddress,
77
+ sellAssetReference: sellAssetErc20Address
78
+ });
79
+ return {
80
+ ...commonQuoteFields,
81
+ allowanceContract: router,
82
+ feeData
83
+ };
84
+ })();
85
+ case types_1.KnownChainIds.BitcoinMainnet:
86
+ return (async () => {
87
+ const { vault, opReturnData, pubkey } = await (0, getThorTxData_1.getThorTxInfo)({
88
+ deps,
89
+ sellAsset,
90
+ buyAsset,
91
+ sellAmount,
92
+ slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
93
+ destinationAddress: receiveAddress,
94
+ wallet,
95
+ bip44Params: input.bip44Params,
96
+ accountType: input.accountType
97
+ });
98
+ const feeData = await (0, getBtcTxFees_1.getBtcTxFees)({
99
+ deps,
100
+ buyAsset,
101
+ sellAmount,
102
+ vault,
103
+ opReturnData,
104
+ pubkey,
105
+ adapterManager: deps.adapterManager
106
+ });
107
+ return {
108
+ ...commonQuoteFields,
109
+ allowanceContract: '0x0',
110
+ feeData
111
+ };
112
+ })();
113
+ default:
114
+ throw new api_1.SwapError('[getThorTradeQuote] - Asset chainId is not supported.', {
115
+ code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
116
+ details: { chainId }
117
+ });
118
+ }
88
119
  }
89
120
  catch (e) {
90
121
  if (e instanceof api_1.SwapError)
@@ -0,0 +1,22 @@
1
+ import { HDWallet } from '@shapeshiftoss/hdwallet-core';
2
+ import { Asset, BIP44Params, UtxoAccountType } from '@shapeshiftoss/types';
3
+ import { ThorchainSwapperDeps } from '../../../types';
4
+ declare type GetBtcThorTxInfoArgs = {
5
+ deps: ThorchainSwapperDeps;
6
+ sellAsset: Asset;
7
+ buyAsset: Asset;
8
+ sellAmount: string;
9
+ slippageTolerance: string;
10
+ destinationAddress: string;
11
+ wallet: HDWallet;
12
+ bip44Params: BIP44Params;
13
+ accountType: UtxoAccountType;
14
+ };
15
+ declare type GetBtcThorTxInfoReturn = Promise<{
16
+ opReturnData: string;
17
+ vault: string;
18
+ pubkey: string;
19
+ }>;
20
+ declare type GetBtcThorTxInfo = (args: GetBtcThorTxInfoArgs) => GetBtcThorTxInfoReturn;
21
+ export declare const getThorTxInfo: GetBtcThorTxInfo;
22
+ export {};
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getThorTxInfo = void 0;
4
+ const caip_1 = require("@shapeshiftoss/caip");
5
+ const api_1 = require("../../../../../api");
6
+ const bignumber_1 = require("../../../../utils/bignumber");
7
+ const getPriceRatio_1 = require("../../getPriceRatio/getPriceRatio");
8
+ const makeSwapMemo_1 = require("../../makeSwapMemo/makeSwapMemo");
9
+ const thorService_1 = require("../../thorService");
10
+ const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTolerance, destinationAddress, wallet, bip44Params, accountType }) => {
11
+ try {
12
+ const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.midgardUrl}/thorchain/inbound_addresses`);
13
+ const btcInboundAddresses = inboundAddresses.find((inbound) => inbound.chain === 'BTC');
14
+ const vault = btcInboundAddresses === null || btcInboundAddresses === void 0 ? void 0 : btcInboundAddresses.address;
15
+ if (!vault)
16
+ throw new api_1.SwapError(`[getThorTxInfo]: vault not found for BTC`, {
17
+ code: api_1.SwapErrorTypes.RESPONSE_ERROR,
18
+ details: { inboundAddresses }
19
+ });
20
+ const priceRatio = await (0, getPriceRatio_1.getPriceRatio)(deps, {
21
+ buyAssetId: buyAsset.assetId,
22
+ sellAssetId: sellAsset.assetId
23
+ });
24
+ const expectedBuyAmount = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).dividedBy(priceRatio), sellAsset.precision), buyAsset.precision);
25
+ const isValidSlippageRange = (0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1);
26
+ if ((0, bignumber_1.bnOrZero)(expectedBuyAmount).lt(0) || !isValidSlippageRange)
27
+ throw new api_1.SwapError('[getThorTxInfo]: bad expected buy amount or bad slippage tolerance', {
28
+ code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
29
+ details: { expectedBuyAmount, slippageTolerance }
30
+ });
31
+ const limit = (0, bignumber_1.bnOrZero)(expectedBuyAmount)
32
+ .times((0, bignumber_1.bn)(1).minus(slippageTolerance))
33
+ .decimalPlaces(0)
34
+ .toString();
35
+ const memo = (0, makeSwapMemo_1.makeSwapMemo)({
36
+ buyAssetId: buyAsset.assetId,
37
+ destinationAddress,
38
+ limit
39
+ });
40
+ const adapter = deps.adapterManager.get(caip_1.btcChainId);
41
+ const pubkey = await adapter.getPublicKey(wallet, bip44Params, accountType);
42
+ return {
43
+ opReturnData: memo,
44
+ vault,
45
+ pubkey: pubkey.xpub
46
+ };
47
+ }
48
+ catch (e) {
49
+ if (e instanceof api_1.SwapError)
50
+ throw e;
51
+ throw new api_1.SwapError('[getThorTxInfo]', { cause: e, code: api_1.SwapErrorTypes.TRADE_QUOTE_FAILED });
52
+ }
53
+ };
54
+ exports.getThorTxInfo = getThorTxInfo;
@@ -14,8 +14,7 @@ const makeTradeTx = async ({ wallet, bip44Params, sellAmount, buyAsset, sellAsse
14
14
  buyAsset,
15
15
  sellAmount,
16
16
  slippageTolerance,
17
- destinationAddress,
18
- isErc20Trade
17
+ destinationAddress
19
18
  });
20
19
  return adapter.buildCustomTx({
21
20
  wallet,
@@ -1,15 +1,18 @@
1
1
  import { Asset } from '@shapeshiftoss/types';
2
2
  import { ThorchainSwapperDeps } from '../../../types';
3
- export declare const getThorTxInfo: ({ deps, sellAsset, buyAsset, sellAmount, slippageTolerance, destinationAddress, isErc20Trade }: {
3
+ declare type GetBtcThorTxInfoArgs = {
4
4
  deps: ThorchainSwapperDeps;
5
5
  sellAsset: Asset;
6
6
  buyAsset: Asset;
7
7
  sellAmount: string;
8
8
  slippageTolerance: string;
9
9
  destinationAddress: string;
10
- isErc20Trade: boolean;
11
- }) => Promise<{
12
- data: any;
10
+ };
11
+ declare type GetBtcThorTxInfoReturn = Promise<{
12
+ data: string;
13
13
  memo: string;
14
14
  router: string;
15
15
  }>;
16
+ declare type GetBtcThorTxInfo = (args: GetBtcThorTxInfoArgs) => GetBtcThorTxInfoReturn;
17
+ export declare const getThorTxInfo: GetBtcThorTxInfo;
18
+ export {};
@@ -8,9 +8,10 @@ const getPriceRatio_1 = require("../../getPriceRatio/getPriceRatio");
8
8
  const makeSwapMemo_1 = require("../../makeSwapMemo/makeSwapMemo");
9
9
  const thorService_1 = require("../../thorService");
10
10
  const routerCalldata_1 = require("../routerCalldata");
11
- const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTolerance, destinationAddress, isErc20Trade }) => {
11
+ const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTolerance, destinationAddress }) => {
12
12
  try {
13
- const { assetReference } = (0, caip_1.fromAssetId)(sellAsset.assetId);
13
+ const { assetReference, assetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
14
+ const isErc20Trade = assetNamespace === 'erc20';
14
15
  const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.midgardUrl}/thorchain/inbound_addresses`);
15
16
  const ethInboundAddresses = inboundAddresses.find((inbound) => inbound.chain === 'ETH');
16
17
  const vault = ethInboundAddresses === null || ethInboundAddresses === void 0 ? void 0 : ethInboundAddresses.address;
@@ -25,10 +26,11 @@ const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmount, slippageTo
25
26
  sellAssetId: sellAsset.assetId
26
27
  });
27
28
  const expectedBuyAmount = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).dividedBy(priceRatio), sellAsset.precision), buyAsset.precision);
28
- if (!(0, bignumber_1.bnOrZero)(expectedBuyAmount).gte(0) ||
29
- !((0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1)))
29
+ const isValidSlippageRange = (0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1);
30
+ if ((0, bignumber_1.bnOrZero)(expectedBuyAmount).lt(0) || !isValidSlippageRange)
30
31
  throw new api_1.SwapError('[makeTradeTx]: bad expected buy amount or bad slippage tolerance', {
31
- code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED
32
+ code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
33
+ details: { expectedBuyAmount, slippageTolerance }
32
34
  });
33
35
  const limit = (0, bignumber_1.bnOrZero)(expectedBuyAmount)
34
36
  .times((0, bignumber_1.bn)(1).minus(slippageTolerance))
@@ -14,7 +14,8 @@ export declare const setupBuildTrade: () => {
14
14
  sellAmount: string;
15
15
  sendMax: boolean;
16
16
  sellAssetAccountNumber: number;
17
- wallet?: HDWallet | undefined;
17
+ wallet: HDWallet;
18
+ receiveAddress: string;
18
19
  } & {
19
20
  chainId: import("../../../api").EvmSupportedChainIds;
20
21
  } & {
@@ -25,7 +25,9 @@ const setupQuote = () => {
25
25
  sellAsset,
26
26
  buyAsset,
27
27
  sellAssetAccountNumber: 0,
28
- sendMax: false
28
+ sendMax: false,
29
+ receiveAddress: '0xc770eefad204b5180df6a14ee197d99d808ee52d',
30
+ wallet: {}
29
31
  };
30
32
  return { quoteInput, tradeQuote, buyAsset, sellAsset };
31
33
  };
@@ -41,7 +43,8 @@ const setupBuildTrade = () => {
41
43
  sellAssetAccountNumber: 0,
42
44
  buyAssetAccountNumber: 0,
43
45
  sellAsset,
44
- wallet: {}
46
+ wallet: {},
47
+ receiveAddress: ''
45
48
  };
46
49
  return { buildTradeInput, buyAsset, sellAsset };
47
50
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shapeshiftoss/swapper",
3
- "version": "8.2.0",
3
+ "version": "8.3.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",