@shapeshiftoss/swapper 11.5.1 → 11.5.3
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/swappers/thorchain/buildThorTrade/buildThorTrade.js +2 -1
- package/dist/swappers/thorchain/constants.d.ts +2 -0
- package/dist/swappers/thorchain/constants.js +7 -0
- package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.js +41 -26
- package/dist/swappers/thorchain/utils/estimateBuyAssetTradeFeeCrypto/estimateBuyAssetTradeFeeCrypto.d.ts +3 -0
- package/dist/swappers/thorchain/utils/{estimateTradeFee/estimateTradeFee.js → estimateBuyAssetTradeFeeCrypto/estimateBuyAssetTradeFeeCrypto.js} +13 -13
- package/dist/swappers/thorchain/utils/getLimit/getLimit.js +8 -6
- package/dist/swappers/thorchain/utils/txFeeHelpers/btcTxFees/getBtcTxFees.d.ts +5 -2
- package/dist/swappers/thorchain/utils/txFeeHelpers/btcTxFees/getBtcTxFees.js +2 -1
- package/package.json +1 -1
- package/dist/swappers/thorchain/utils/estimateTradeFee/estimateTradeFee.d.ts +0 -3
|
@@ -12,7 +12,7 @@ const makeTradeTx_1 = require("../utils/ethereum/makeTradeTx");
|
|
|
12
12
|
const buildTrade = async ({ deps, input, }) => {
|
|
13
13
|
var _a, _b, _c, _d;
|
|
14
14
|
try {
|
|
15
|
-
const { buyAsset, receiveAddress: destinationAddress, sellAmount, sellAsset, bip44Params, slippage: slippageTolerance = constants_1.DEFAULT_SLIPPAGE, wallet, } = input;
|
|
15
|
+
const { buyAsset, receiveAddress: destinationAddress, sellAmount, sellAsset, bip44Params, slippage: slippageTolerance = constants_1.DEFAULT_SLIPPAGE, wallet, sendMax, } = input;
|
|
16
16
|
const quote = await (0, getTradeQuote_1.getThorTradeQuote)({ deps, input });
|
|
17
17
|
const sellAdapter = deps.adapterManager.get(sellAsset.chainId);
|
|
18
18
|
if (!sellAdapter)
|
|
@@ -66,6 +66,7 @@ const buildTrade = async ({ deps, input, }) => {
|
|
|
66
66
|
.satsPerByte,
|
|
67
67
|
opReturnData,
|
|
68
68
|
},
|
|
69
|
+
sendMax,
|
|
69
70
|
});
|
|
70
71
|
return {
|
|
71
72
|
chainId: sellAsset.chainId,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MINIMUM_USD_TRADE_AMOUNT = exports.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN = void 0;
|
|
4
|
+
const bignumber_1 = require("../utils/bignumber");
|
|
5
|
+
// TODO: read from https://daemon.thorchain.shapeshift.com/lcd/thorchain/constants
|
|
6
|
+
exports.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN = (0, bignumber_1.bn)('0.02');
|
|
7
|
+
exports.MINIMUM_USD_TRADE_AMOUNT = (0, bignumber_1.bn)(1);
|
|
@@ -5,17 +5,18 @@ const caip_1 = require("@shapeshiftoss/caip");
|
|
|
5
5
|
const api_1 = require("../../../api");
|
|
6
6
|
const bignumber_1 = require("../../utils/bignumber");
|
|
7
7
|
const constants_1 = require("../../utils/constants");
|
|
8
|
+
const constants_2 = require("../constants");
|
|
8
9
|
const getThorTxData_1 = require("../utils/bitcoin/utils/getThorTxData");
|
|
9
|
-
const
|
|
10
|
-
const
|
|
10
|
+
const constants_3 = require("../utils/constants");
|
|
11
|
+
const estimateBuyAssetTradeFeeCrypto_1 = require("../utils/estimateBuyAssetTradeFeeCrypto/estimateBuyAssetTradeFeeCrypto");
|
|
11
12
|
const getThorTxData_2 = require("../utils/ethereum/utils/getThorTxData");
|
|
12
13
|
const getTradeRate_1 = require("../utils/getTradeRate/getTradeRate");
|
|
13
14
|
const getUsdRate_1 = require("../utils/getUsdRate/getUsdRate");
|
|
15
|
+
const isRune_1 = require("../utils/isRune/isRune");
|
|
14
16
|
const getBtcTxFees_1 = require("../utils/txFeeHelpers/btcTxFees/getBtcTxFees");
|
|
15
17
|
const getEthTxFees_1 = require("../utils/txFeeHelpers/ethTxFees/getEthTxFees");
|
|
16
|
-
const MINIMUM_USD_TRADE_AMOUNT = (0, bignumber_1.bn)(1);
|
|
17
18
|
const getThorTradeQuote = async ({ deps, input }) => {
|
|
18
|
-
const { sellAsset, buyAsset, sellAmount, bip44Params, chainId, receiveAddress } = input;
|
|
19
|
+
const { sellAsset, buyAsset, sellAmount: sellAmountCryptoPrecision, bip44Params, chainId, receiveAddress, sendMax, } = input;
|
|
19
20
|
if (!bip44Params) {
|
|
20
21
|
throw new Error('bip44Params required in getThorTradeQuote');
|
|
21
22
|
}
|
|
@@ -27,28 +28,41 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
27
28
|
code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
|
|
28
29
|
details: { chainId },
|
|
29
30
|
});
|
|
30
|
-
const rate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId,
|
|
31
|
-
const
|
|
32
|
-
const
|
|
31
|
+
const rate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId, sellAmountCryptoPrecision, deps);
|
|
32
|
+
const buyAmountCryptoPrecision = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)((0, bignumber_1.fromBaseUnit)(sellAmountCryptoPrecision, sellAsset.precision)).times(rate), buyAsset.precision);
|
|
33
|
+
const estimatedBuyAssetTradeFeeCryptoHuman = await (0, estimateBuyAssetTradeFeeCrypto_1.estimateBuyAssetTradeFeeCrypto)(deps, buyAsset);
|
|
33
34
|
const buyAssetUsdRate = await (0, getUsdRate_1.getUsdRate)({ deps, input: { assetId: buyAsset.assetId } });
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
35
|
+
const sellAssetUsdRate = await (0, getUsdRate_1.getUsdRate)({ deps, input: { assetId: sellAsset.assetId } });
|
|
36
|
+
const estimatedBuyAssetTradeFeeUsd = (0, bignumber_1.bn)(buyAssetUsdRate)
|
|
37
|
+
.times(estimatedBuyAssetTradeFeeCryptoHuman)
|
|
38
|
+
.toString();
|
|
39
|
+
const buyAssetTradeFeeUsdOrMinimum = constants_2.MINIMUM_USD_TRADE_AMOUNT.gt(estimatedBuyAssetTradeFeeUsd)
|
|
40
|
+
? constants_2.MINIMUM_USD_TRADE_AMOUNT.toString()
|
|
41
|
+
: estimatedBuyAssetTradeFeeUsd;
|
|
42
|
+
const sellAssetTradeFeeCryptoHuman = (() => {
|
|
43
|
+
// The 1$ minimum doesn't apply for swaps to RUNE, use OutboundTransactionFee in human value instead
|
|
44
|
+
if ((0, isRune_1.isRune)(buyAsset === null || buyAsset === void 0 ? void 0 : buyAsset.assetId))
|
|
45
|
+
return constants_2.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN;
|
|
46
|
+
return (0, bignumber_1.bnOrZero)(buyAssetTradeFeeUsdOrMinimum).div(sellAssetUsdRate);
|
|
47
|
+
})();
|
|
39
48
|
// minimum is tradeFee padded by an amount to be sure they get something back
|
|
40
49
|
// usually it will be slightly more than the amount because sellAssetTradeFee is already a high estimate
|
|
41
|
-
const
|
|
50
|
+
const minimumCryptoHuman = (0, bignumber_1.bnOrZero)(sellAssetTradeFeeCryptoHuman)
|
|
51
|
+
.times(constants_3.THOR_MINIMUM_PADDING)
|
|
52
|
+
.toString();
|
|
53
|
+
const buyAssetTradeFeeUsdOrDefault = (0, isRune_1.isRune)(buyAsset === null || buyAsset === void 0 ? void 0 : buyAsset.assetId)
|
|
54
|
+
? (0, bignumber_1.bn)(buyAssetUsdRate).times(constants_2.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN).toString()
|
|
55
|
+
: buyAssetTradeFeeUsdOrMinimum;
|
|
42
56
|
const commonQuoteFields = {
|
|
43
57
|
rate,
|
|
44
|
-
maximum:
|
|
45
|
-
sellAmount,
|
|
46
|
-
buyAmount,
|
|
58
|
+
maximum: constants_3.MAX_THORCHAIN_TRADE,
|
|
59
|
+
sellAmount: sellAmountCryptoPrecision,
|
|
60
|
+
buyAmount: buyAmountCryptoPrecision,
|
|
47
61
|
sources: [{ name: 'thorchain', proportion: '1' }],
|
|
48
62
|
buyAsset,
|
|
49
63
|
sellAsset,
|
|
50
64
|
bip44Params,
|
|
51
|
-
minimum,
|
|
65
|
+
minimum: minimumCryptoHuman,
|
|
52
66
|
};
|
|
53
67
|
const { chainNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
54
68
|
switch (chainNamespace) {
|
|
@@ -58,15 +72,15 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
58
72
|
deps,
|
|
59
73
|
sellAsset,
|
|
60
74
|
buyAsset,
|
|
61
|
-
sellAmount,
|
|
75
|
+
sellAmount: sellAmountCryptoPrecision,
|
|
62
76
|
slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
|
|
63
77
|
destinationAddress: receiveAddress,
|
|
64
|
-
buyAssetTradeFeeUsd,
|
|
78
|
+
buyAssetTradeFeeUsd: buyAssetTradeFeeUsdOrMinimum,
|
|
65
79
|
});
|
|
66
80
|
const feeData = await (0, getEthTxFees_1.getEthTxFees)({
|
|
67
81
|
adapterManager: deps.adapterManager,
|
|
68
82
|
sellAssetReference,
|
|
69
|
-
buyAssetTradeFeeUsd,
|
|
83
|
+
buyAssetTradeFeeUsd: buyAssetTradeFeeUsdOrMinimum,
|
|
70
84
|
});
|
|
71
85
|
return {
|
|
72
86
|
...commonQuoteFields,
|
|
@@ -80,19 +94,20 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
80
94
|
deps,
|
|
81
95
|
sellAsset,
|
|
82
96
|
buyAsset,
|
|
83
|
-
sellAmount,
|
|
97
|
+
sellAmount: sellAmountCryptoPrecision,
|
|
84
98
|
slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
|
|
85
99
|
destinationAddress: receiveAddress,
|
|
86
100
|
xpub: input.xpub,
|
|
87
|
-
buyAssetTradeFeeUsd,
|
|
101
|
+
buyAssetTradeFeeUsd: buyAssetTradeFeeUsdOrMinimum,
|
|
88
102
|
});
|
|
89
103
|
const feeData = await (0, getBtcTxFees_1.getBtcTxFees)({
|
|
90
|
-
sellAmount,
|
|
104
|
+
sellAmount: sellAmountCryptoPrecision,
|
|
91
105
|
vault,
|
|
92
106
|
opReturnData,
|
|
93
107
|
pubkey,
|
|
94
108
|
sellAdapter: sellAdapter,
|
|
95
|
-
buyAssetTradeFeeUsd,
|
|
109
|
+
buyAssetTradeFeeUsd: buyAssetTradeFeeUsdOrMinimum,
|
|
110
|
+
sendMax,
|
|
96
111
|
});
|
|
97
112
|
return {
|
|
98
113
|
...commonQuoteFields,
|
|
@@ -109,8 +124,8 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
109
124
|
feeData: {
|
|
110
125
|
fee: feeData.fast.txFee,
|
|
111
126
|
networkFee: feeData.fast.txFee,
|
|
112
|
-
tradeFee:
|
|
113
|
-
buyAssetTradeFeeUsd,
|
|
127
|
+
tradeFee: buyAssetTradeFeeUsdOrDefault,
|
|
128
|
+
buyAssetTradeFeeUsd: buyAssetTradeFeeUsdOrDefault,
|
|
114
129
|
sellAssetTradeFeeUsd: '0',
|
|
115
130
|
chainSpecific: { estimatedGas: feeData.fast.chainSpecific.gasLimit },
|
|
116
131
|
},
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.estimateBuyAssetTradeFeeCrypto = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const api_1 = require("../../../../api");
|
|
6
|
-
const constants_1 = require("
|
|
6
|
+
const constants_1 = require("../../constants");
|
|
7
|
+
const constants_2 = require("../constants");
|
|
7
8
|
const getPriceRatio_1 = require("../getPriceRatio/getPriceRatio");
|
|
8
9
|
const isRune_1 = require("../isRune/isRune");
|
|
9
10
|
const thorService_1 = require("../thorService");
|
|
10
|
-
|
|
11
|
-
const estimateTradeFee = async (deps, buyAsset) => {
|
|
11
|
+
const estimateBuyAssetTradeFeeCrypto = async (deps, buyAsset) => {
|
|
12
12
|
if ((0, isRune_1.isRune)(buyAsset.assetId)) {
|
|
13
|
-
return
|
|
13
|
+
return constants_1.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN.toString();
|
|
14
14
|
}
|
|
15
15
|
const thorId = caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAsset.assetId });
|
|
16
16
|
if (!thorId)
|
|
17
|
-
throw new api_1.SwapError('[
|
|
17
|
+
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - undefined thorId for given buyAssetId', {
|
|
18
18
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
19
19
|
details: { buyAssetId: buyAsset.assetId },
|
|
20
20
|
});
|
|
@@ -22,7 +22,7 @@ const estimateTradeFee = async (deps, buyAsset) => {
|
|
|
22
22
|
const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.daemonUrl}/lcd/thorchain/inbound_addresses`);
|
|
23
23
|
const inboundInfo = inboundAddresses.find((inbound) => inbound.chain === thorPoolChainId);
|
|
24
24
|
if (!inboundInfo)
|
|
25
|
-
throw new api_1.SwapError('[
|
|
25
|
+
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - unable to locate inbound pool info', {
|
|
26
26
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
27
27
|
details: { thorPoolChainId },
|
|
28
28
|
});
|
|
@@ -30,13 +30,13 @@ const estimateTradeFee = async (deps, buyAsset) => {
|
|
|
30
30
|
const { chainId: buyChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
31
31
|
const buyAdapter = deps.adapterManager.get(buyChainId);
|
|
32
32
|
if (!buyAdapter)
|
|
33
|
-
throw new api_1.SwapError('[
|
|
33
|
+
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - unable to get buy asset adapter', {
|
|
34
34
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
35
35
|
details: { buyChainId },
|
|
36
36
|
});
|
|
37
37
|
const buyFeeAssetId = buyAdapter.getFeeAssetId();
|
|
38
38
|
if (!buyFeeAssetId)
|
|
39
|
-
throw new api_1.SwapError('[
|
|
39
|
+
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - no fee assetId', {
|
|
40
40
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
41
41
|
details: { buyAssetId: buyAsset.assetId },
|
|
42
42
|
});
|
|
@@ -46,11 +46,11 @@ const estimateTradeFee = async (deps, buyAsset) => {
|
|
|
46
46
|
buyAssetId: buyFeeAssetId,
|
|
47
47
|
})
|
|
48
48
|
: '1';
|
|
49
|
-
if (!
|
|
50
|
-
throw new api_1.SwapError('[
|
|
49
|
+
if (!constants_2.THOR_TRADE_FEE_MULTIPLIERS[buyChainId])
|
|
50
|
+
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - no trade fee multiplier', {
|
|
51
51
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
52
52
|
details: { buyChainId },
|
|
53
53
|
});
|
|
54
|
-
return
|
|
54
|
+
return constants_2.THOR_TRADE_FEE_MULTIPLIERS[buyChainId].times(buyFeeAssetRatio).times(gasRate).toString();
|
|
55
55
|
};
|
|
56
|
-
exports.
|
|
56
|
+
exports.estimateBuyAssetTradeFeeCrypto = estimateBuyAssetTradeFeeCrypto;
|
|
@@ -5,19 +5,21 @@ const api_1 = require("../../../../api");
|
|
|
5
5
|
const bignumber_1 = require("../../../utils/bignumber");
|
|
6
6
|
const constants_1 = require("../constants");
|
|
7
7
|
const getTradeRate_1 = require("../getTradeRate/getTradeRate");
|
|
8
|
+
const getUsdRate_1 = require("../getUsdRate/getUsdRate");
|
|
8
9
|
const getLimit = async ({ sellAsset, buyAsset, sellAmount, deps, slippageTolerance, buyAssetTradeFeeUsd, }) => {
|
|
9
10
|
const tradeRate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId, sellAmount, deps);
|
|
10
|
-
const
|
|
11
|
+
const buyAssetUsdRate = await (0, getUsdRate_1.getUsdRate)({ deps, input: { assetId: buyAsset.assetId } });
|
|
12
|
+
const expectedBuyAmountCryptoPrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmount).times(tradeRate), sellAsset.precision), constants_1.THORCHAIN_FIXED_PRECISION);
|
|
11
13
|
const isValidSlippageRange = (0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1);
|
|
12
|
-
if ((0, bignumber_1.bnOrZero)(
|
|
14
|
+
if ((0, bignumber_1.bnOrZero)(expectedBuyAmountCryptoPrecision8).lt(0) || !isValidSlippageRange)
|
|
13
15
|
throw new api_1.SwapError('[getThorTxInfo]: bad expected buy amount or bad slippage tolerance', {
|
|
14
16
|
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
15
|
-
details: {
|
|
17
|
+
details: { expectedBuyAmountCryptoPrecision8, slippageTolerance },
|
|
16
18
|
});
|
|
17
|
-
const
|
|
18
|
-
return (0, bignumber_1.bnOrZero)(
|
|
19
|
+
const tradeFeeCryptoPrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)(buyAssetTradeFeeUsd).div(buyAssetUsdRate), constants_1.THORCHAIN_FIXED_PRECISION);
|
|
20
|
+
return (0, bignumber_1.bnOrZero)(expectedBuyAmountCryptoPrecision8)
|
|
19
21
|
.times((0, bignumber_1.bn)(1).minus(slippageTolerance))
|
|
20
|
-
.minus((0, bignumber_1.bnOrZero)(
|
|
22
|
+
.minus((0, bignumber_1.bnOrZero)(tradeFeeCryptoPrecision8))
|
|
21
23
|
.decimalPlaces(0)
|
|
22
24
|
.toString();
|
|
23
25
|
};
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { UtxoBaseAdapter } from '@shapeshiftoss/chain-adapters';
|
|
2
2
|
import { QuoteFeeData, UtxoSupportedChainIds } from '../../../../../api';
|
|
3
|
-
|
|
3
|
+
declare type GetBtcTxFeesInput = {
|
|
4
4
|
opReturnData: string;
|
|
5
5
|
vault: string;
|
|
6
6
|
sellAmount: string;
|
|
7
7
|
sellAdapter: UtxoBaseAdapter<UtxoSupportedChainIds>;
|
|
8
8
|
pubkey: string;
|
|
9
9
|
buyAssetTradeFeeUsd: string;
|
|
10
|
-
|
|
10
|
+
sendMax: boolean;
|
|
11
|
+
};
|
|
12
|
+
export declare const getBtcTxFees: ({ opReturnData, vault, sellAmount, sellAdapter, pubkey, buyAssetTradeFeeUsd, sendMax, }: GetBtcTxFeesInput) => Promise<QuoteFeeData<UtxoSupportedChainIds>>;
|
|
13
|
+
export {};
|
|
@@ -4,12 +4,13 @@ exports.getBtcTxFees = void 0;
|
|
|
4
4
|
const types_1 = require("@shapeshiftoss/types");
|
|
5
5
|
const api_1 = require("../../../../../api");
|
|
6
6
|
const bignumber_1 = require("../../../../utils/bignumber");
|
|
7
|
-
const getBtcTxFees = async ({ opReturnData, vault, sellAmount, sellAdapter, pubkey, buyAssetTradeFeeUsd, }) => {
|
|
7
|
+
const getBtcTxFees = async ({ opReturnData, vault, sellAmount, sellAdapter, pubkey, buyAssetTradeFeeUsd, sendMax, }) => {
|
|
8
8
|
try {
|
|
9
9
|
const feeDataOptions = await sellAdapter.getFeeData({
|
|
10
10
|
to: vault,
|
|
11
11
|
value: sellAmount,
|
|
12
12
|
chainSpecific: { pubkey, opReturnData },
|
|
13
|
+
sendMax,
|
|
13
14
|
});
|
|
14
15
|
const feeData = feeDataOptions['fast'];
|
|
15
16
|
// BCH specific hack:
|
package/package.json
CHANGED