@shapeshiftoss/swapper 14.0.0 → 14.2.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/swappers/thorchain/ThorchainSwapper.d.ts +2 -1
- package/dist/swappers/thorchain/ThorchainSwapper.js +4 -4
- package/dist/swappers/thorchain/buildThorTrade/buildThorTrade.js +2 -2
- package/dist/swappers/thorchain/constants.d.ts +0 -2
- package/dist/swappers/thorchain/constants.js +1 -3
- package/dist/swappers/thorchain/getThorTradeQuote/getTradeQuote.js +28 -38
- package/dist/swappers/thorchain/types.d.ts +7 -1
- package/dist/swappers/thorchain/utils/bitcoin/utils/getThorTxData.d.ts +1 -1
- package/dist/swappers/thorchain/utils/bitcoin/utils/getThorTxData.js +4 -7
- package/dist/swappers/thorchain/utils/constants.d.ts +0 -3
- package/dist/swappers/thorchain/utils/constants.js +1 -17
- package/dist/swappers/thorchain/utils/cosmos/{cosmosTxData.d.ts → getCosmosTxData.d.ts} +5 -3
- package/dist/swappers/thorchain/utils/cosmos/getCosmosTxData.js +70 -0
- package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.d.ts +8 -6
- package/dist/swappers/thorchain/utils/ethereum/makeTradeTx.js +2 -9
- package/dist/swappers/thorchain/utils/ethereum/utils/getThorTxData.d.ts +1 -2
- package/dist/swappers/thorchain/utils/ethereum/utils/getThorTxData.js +8 -15
- package/dist/swappers/thorchain/utils/getInboundAddressDataForChain.d.ts +3 -0
- package/dist/swappers/thorchain/utils/getInboundAddressDataForChain.js +15 -0
- package/dist/swappers/thorchain/utils/getLimit/getLimit.d.ts +3 -4
- package/dist/swappers/thorchain/utils/getLimit/getLimit.js +50 -8
- package/dist/swappers/thorchain/utils/test-data/responses.d.ts +2 -2
- package/dist/swappers/thorchain/utils/test-data/responses.js +64 -8
- package/dist/swappers/thorchain/utils/test-data/setupThorswapDeps.js +2 -1
- package/dist/swappers/utils/test-data/assets.d.ts +1 -0
- package/dist/swappers/utils/test-data/assets.js +14 -2
- package/package.json +1 -1
- package/dist/swappers/thorchain/utils/cosmos/cosmosTxData.js +0 -66
- package/dist/swappers/thorchain/utils/estimateBuyAssetTradeFeeCrypto/estimateBuyAssetTradeFeeCrypto.d.ts +0 -3
- package/dist/swappers/thorchain/utils/estimateBuyAssetTradeFeeCrypto/estimateBuyAssetTradeFeeCrypto.js +0 -56
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/asset-service';
|
|
2
2
|
import { AssetId, ChainId } from '@shapeshiftoss/caip';
|
|
3
3
|
import { KnownChainIds } from '@shapeshiftoss/types';
|
|
4
|
-
import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper,
|
|
4
|
+
import type { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildTradeInput, BuyAssetBySellIdInput, ExecuteTradeInput, GetTradeQuoteInput, Swapper, Trade, TradeQuote, TradeResult, TradeTxs } from '../../api';
|
|
5
|
+
import { SwapperName, SwapperType } from '../../api';
|
|
5
6
|
import { ThorchainSwapperDeps } from './types';
|
|
6
7
|
export * from './types';
|
|
7
8
|
export declare class ThorchainSwapper implements Swapper<ChainId> {
|
|
@@ -52,9 +52,9 @@ class ThorchainSwapper {
|
|
|
52
52
|
}
|
|
53
53
|
async initialize() {
|
|
54
54
|
try {
|
|
55
|
-
const { data } = await thorService_1.thorService.get(`${this.deps.daemonUrl}/lcd/thorchain/pools`);
|
|
56
|
-
const
|
|
57
|
-
this.supportedSellAssetIds =
|
|
55
|
+
const { data: allPools } = await thorService_1.thorService.get(`${this.deps.daemonUrl}/lcd/thorchain/pools`);
|
|
56
|
+
const availablePools = allPools.filter((pool) => pool.status === 'Available');
|
|
57
|
+
this.supportedSellAssetIds = availablePools.reduce((acc, pool) => {
|
|
58
58
|
const assetId = caip_1.adapters.poolAssetIdToAssetId(pool.asset);
|
|
59
59
|
if (!assetId || !this.sellSupportedChainIds[(0, caip_1.fromAssetId)(assetId).chainId])
|
|
60
60
|
return acc;
|
|
@@ -62,7 +62,7 @@ class ThorchainSwapper {
|
|
|
62
62
|
return acc;
|
|
63
63
|
}, []);
|
|
64
64
|
this.supportedSellAssetIds.push(caip_1.thorchainAssetId);
|
|
65
|
-
this.supportedBuyAssetIds =
|
|
65
|
+
this.supportedBuyAssetIds = availablePools.reduce((acc, pool) => {
|
|
66
66
|
const assetId = caip_1.adapters.poolAssetIdToAssetId(pool.asset);
|
|
67
67
|
if (!assetId || !this.buySupportedChainIds[(0, caip_1.fromAssetId)(assetId).chainId])
|
|
68
68
|
return acc;
|
|
@@ -7,7 +7,7 @@ const api_1 = require("../../../api");
|
|
|
7
7
|
const constants_1 = require("../../utils/constants");
|
|
8
8
|
const getTradeQuote_1 = require("../getThorTradeQuote/getTradeQuote");
|
|
9
9
|
const getThorTxData_1 = require("../utils/bitcoin/utils/getThorTxData");
|
|
10
|
-
const
|
|
10
|
+
const getCosmosTxData_1 = require("../utils/cosmos/getCosmosTxData");
|
|
11
11
|
const makeTradeTx_1 = require("../utils/ethereum/makeTradeTx");
|
|
12
12
|
const buildTrade = async ({ deps, input, }) => {
|
|
13
13
|
var _a, _b, _c, _d;
|
|
@@ -79,7 +79,7 @@ const buildTrade = async ({ deps, input, }) => {
|
|
|
79
79
|
if (!bip44Params) {
|
|
80
80
|
throw new Error('bip44Params required as input');
|
|
81
81
|
}
|
|
82
|
-
const txData = await (0,
|
|
82
|
+
const txData = await (0, getCosmosTxData_1.getCosmosTxData)({
|
|
83
83
|
bip44Params,
|
|
84
84
|
deps,
|
|
85
85
|
sellAdapter: sellAdapter,
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN = void 0;
|
|
4
4
|
const bignumber_1 = require("../utils/bignumber");
|
|
5
5
|
// TODO: read from https://daemon.thorchain.shapeshift.com/lcd/thorchain/constants
|
|
6
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);
|
|
8
|
-
exports.MINIMUM_USD_TRADE_AMOUNT_ETHEREUM_NETWORK = (0, bignumber_1.bn)(42);
|
|
@@ -2,15 +2,13 @@
|
|
|
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");
|
|
6
5
|
const api_1 = require("../../../api");
|
|
7
6
|
const bignumber_1 = require("../../utils/bignumber");
|
|
8
7
|
const constants_1 = require("../../utils/constants");
|
|
9
8
|
const constants_2 = require("../constants");
|
|
10
9
|
const getThorTxData_1 = require("../utils/bitcoin/utils/getThorTxData");
|
|
11
10
|
const constants_3 = require("../utils/constants");
|
|
12
|
-
const
|
|
13
|
-
const getThorTxData_2 = require("../utils/ethereum/utils/getThorTxData");
|
|
11
|
+
const getInboundAddressDataForChain_1 = require("../utils/getInboundAddressDataForChain");
|
|
14
12
|
const getTradeRate_1 = require("../utils/getTradeRate/getTradeRate");
|
|
15
13
|
const getUsdRate_1 = require("../utils/getUsdRate/getUsdRate");
|
|
16
14
|
const isRune_1 = require("../utils/isRune/isRune");
|
|
@@ -23,40 +21,37 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
23
21
|
}
|
|
24
22
|
try {
|
|
25
23
|
const { assetReference: sellAssetReference } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
24
|
+
const { chainId: buyAssetChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
26
25
|
const sellAdapter = deps.adapterManager.get(chainId);
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const buyAdapter = deps.adapterManager.get(buyAssetChainId);
|
|
27
|
+
if (!sellAdapter || !buyAdapter)
|
|
28
|
+
throw new api_1.SwapError(`[getThorTradeQuote] - No chain adapter found for ${chainId} or ${buyAssetChainId}.`, {
|
|
29
29
|
code: api_1.SwapErrorTypes.UNSUPPORTED_CHAIN,
|
|
30
|
-
details: { chainId },
|
|
30
|
+
details: { sellAssetChainId: chainId, buyAssetChainId },
|
|
31
31
|
});
|
|
32
32
|
const rate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAsset.assetId, sellAmountCryptoPrecision, deps);
|
|
33
33
|
const buyAmountCryptoPrecision = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)((0, bignumber_1.fromBaseUnit)(sellAmountCryptoPrecision, sellAsset.precision)).times(rate), buyAsset.precision);
|
|
34
|
-
const
|
|
35
|
-
const
|
|
34
|
+
const buyAssetAddressData = await (0, getInboundAddressDataForChain_1.getInboundAddressDataForChain)(deps.daemonUrl, buyAsset.assetId);
|
|
35
|
+
const estimatedBuyAssetTradeFeeFeeAssetCryptoHuman = (0, isRune_1.isRune)(buyAsset.assetId)
|
|
36
|
+
? constants_2.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN.toString()
|
|
37
|
+
: (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(buyAssetAddressData === null || buyAssetAddressData === void 0 ? void 0 : buyAssetAddressData.outbound_fee), constants_3.THORCHAIN_FIXED_PRECISION);
|
|
38
|
+
const buyAssetChainFeeAssetId = buyAdapter.getFeeAssetId();
|
|
36
39
|
const sellAssetUsdRate = await (0, getUsdRate_1.getUsdRate)({ deps, input: { assetId: sellAsset.assetId } });
|
|
37
|
-
const
|
|
38
|
-
|
|
40
|
+
const buyFeeAssetUsdRate = await (0, getUsdRate_1.getUsdRate)({
|
|
41
|
+
deps,
|
|
42
|
+
input: { assetId: buyAssetChainFeeAssetId },
|
|
43
|
+
});
|
|
44
|
+
const buyAssetTradeFeeUsd = (0, bignumber_1.bn)(buyFeeAssetUsdRate)
|
|
45
|
+
.times(estimatedBuyAssetTradeFeeFeeAssetCryptoHuman)
|
|
39
46
|
.toString();
|
|
40
|
-
const
|
|
41
|
-
?
|
|
42
|
-
:
|
|
43
|
-
const buyAssetTradeFeeUsdOrMinimum = minimumUsdAmount.gt(estimatedBuyAssetTradeFeeUsd)
|
|
44
|
-
? minimumUsdAmount.toString()
|
|
45
|
-
: estimatedBuyAssetTradeFeeUsd;
|
|
46
|
-
const minimumSellAssetAmountCryptoHuman = (() => {
|
|
47
|
-
// The 1$ minimum doesn't apply for swaps to RUNE, use OutboundTransactionFee in human value instead
|
|
48
|
-
if ((0, isRune_1.isRune)(buyAsset === null || buyAsset === void 0 ? void 0 : buyAsset.assetId))
|
|
49
|
-
return constants_2.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN;
|
|
50
|
-
return (0, bignumber_1.bnOrZero)(buyAssetTradeFeeUsdOrMinimum).div(sellAssetUsdRate);
|
|
51
|
-
})();
|
|
47
|
+
const minimumSellAssetAmountCryptoHuman = (0, bignumber_1.bn)(sellAssetUsdRate).isGreaterThan(0)
|
|
48
|
+
? (0, bignumber_1.bnOrZero)(buyAssetTradeFeeUsd).div(sellAssetUsdRate)
|
|
49
|
+
: 0; // We don't have a valid rate for the sell asset, there is no sane minimum
|
|
52
50
|
// minimum is tradeFee padded by an amount to be sure they get something back
|
|
53
51
|
// usually it will be slightly more than the amount because sellAssetTradeFee is already a high estimate
|
|
54
52
|
const minimumSellAssetAmountPaddedCryptoHuman = (0, bignumber_1.bnOrZero)(minimumSellAssetAmountCryptoHuman)
|
|
55
53
|
.times(constants_3.THOR_MINIMUM_PADDING)
|
|
56
54
|
.toString();
|
|
57
|
-
const buyAssetTradeFeeUsdOrDefault = (0, isRune_1.isRune)(buyAsset === null || buyAsset === void 0 ? void 0 : buyAsset.assetId)
|
|
58
|
-
? (0, bignumber_1.bn)(buyAssetUsdRate).times(constants_2.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN).toString()
|
|
59
|
-
: estimatedBuyAssetTradeFeeUsd;
|
|
60
55
|
const commonQuoteFields = {
|
|
61
56
|
rate,
|
|
62
57
|
maximum: constants_3.MAX_THORCHAIN_TRADE,
|
|
@@ -72,19 +67,14 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
72
67
|
switch (chainNamespace) {
|
|
73
68
|
case caip_1.CHAIN_NAMESPACE.Evm:
|
|
74
69
|
return (async () => {
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
sellAmountCryptoPrecision,
|
|
80
|
-
slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
|
|
81
|
-
destinationAddress: receiveAddress,
|
|
82
|
-
buyAssetTradeFeeUsd: estimatedBuyAssetTradeFeeUsd,
|
|
83
|
-
});
|
|
70
|
+
const ethAddressData = await (0, getInboundAddressDataForChain_1.getInboundAddressDataForChain)(deps.daemonUrl, caip_1.ethAssetId);
|
|
71
|
+
const router = ethAddressData === null || ethAddressData === void 0 ? void 0 : ethAddressData.router;
|
|
72
|
+
if (!router)
|
|
73
|
+
throw new api_1.SwapError('[getThorTradeQuote] No router address found for ETH');
|
|
84
74
|
const feeData = await (0, getEthTxFees_1.getEthTxFees)({
|
|
85
75
|
adapterManager: deps.adapterManager,
|
|
86
76
|
sellAssetReference,
|
|
87
|
-
buyAssetTradeFeeUsd
|
|
77
|
+
buyAssetTradeFeeUsd,
|
|
88
78
|
});
|
|
89
79
|
return {
|
|
90
80
|
...commonQuoteFields,
|
|
@@ -102,7 +92,7 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
102
92
|
slippageTolerance: constants_1.DEFAULT_SLIPPAGE,
|
|
103
93
|
destinationAddress: receiveAddress,
|
|
104
94
|
xpub: input.xpub,
|
|
105
|
-
buyAssetTradeFeeUsd
|
|
95
|
+
buyAssetTradeFeeUsd,
|
|
106
96
|
});
|
|
107
97
|
const feeData = await (0, getBtcTxFees_1.getBtcTxFees)({
|
|
108
98
|
sellAmountCryptoPrecision,
|
|
@@ -110,7 +100,7 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
110
100
|
opReturnData,
|
|
111
101
|
pubkey,
|
|
112
102
|
sellAdapter: sellAdapter,
|
|
113
|
-
buyAssetTradeFeeUsd
|
|
103
|
+
buyAssetTradeFeeUsd,
|
|
114
104
|
sendMax,
|
|
115
105
|
});
|
|
116
106
|
return {
|
|
@@ -127,7 +117,7 @@ const getThorTradeQuote = async ({ deps, input }) => {
|
|
|
127
117
|
allowanceContract: '0x0',
|
|
128
118
|
feeData: {
|
|
129
119
|
networkFee: feeData.fast.txFee,
|
|
130
|
-
buyAssetTradeFeeUsd
|
|
120
|
+
buyAssetTradeFeeUsd,
|
|
131
121
|
sellAssetTradeFeeUsd: '0',
|
|
132
122
|
chainSpecific: { estimatedGas: feeData.fast.chainSpecific.gasLimit },
|
|
133
123
|
},
|
|
@@ -47,13 +47,19 @@ declare type MidgardAction = {
|
|
|
47
47
|
export declare type MidgardActionsResponse = {
|
|
48
48
|
actions: MidgardAction[];
|
|
49
49
|
};
|
|
50
|
-
export declare type
|
|
50
|
+
export declare type InboundAddressResponse = {
|
|
51
51
|
chain: string;
|
|
52
52
|
pub_key: string;
|
|
53
53
|
address: string;
|
|
54
54
|
halted: boolean;
|
|
55
55
|
gas_rate: string;
|
|
56
|
+
gas_rate_units: string;
|
|
56
57
|
router?: string;
|
|
58
|
+
global_trading_paused: boolean;
|
|
59
|
+
chain_trading_paused: boolean;
|
|
60
|
+
chain_lp_actions_paused: boolean;
|
|
61
|
+
outbound_tx_size: string;
|
|
62
|
+
outbound_fee: string;
|
|
57
63
|
};
|
|
58
64
|
export declare type ThorchainSwapperDeps = {
|
|
59
65
|
daemonUrl: string;
|
|
@@ -2,25 +2,22 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getThorTxInfo = void 0;
|
|
4
4
|
const api_1 = require("../../../../../api");
|
|
5
|
+
const getInboundAddressDataForChain_1 = require("../../getInboundAddressDataForChain");
|
|
5
6
|
const getLimit_1 = require("../../getLimit/getLimit");
|
|
6
7
|
const makeSwapMemo_1 = require("../../makeSwapMemo/makeSwapMemo");
|
|
7
|
-
const thorService_1 = require("../../thorService");
|
|
8
8
|
const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmountCryptoPrecision, slippageTolerance, destinationAddress, xpub, buyAssetTradeFeeUsd, }) => {
|
|
9
9
|
try {
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const vault = sellAssetInboundAddresses === null || sellAssetInboundAddresses === void 0 ? void 0 : sellAssetInboundAddresses.address;
|
|
10
|
+
const inboundAddress = await (0, getInboundAddressDataForChain_1.getInboundAddressDataForChain)(deps.daemonUrl, sellAsset.assetId);
|
|
11
|
+
const vault = inboundAddress === null || inboundAddress === void 0 ? void 0 : inboundAddress.address;
|
|
13
12
|
if (!vault)
|
|
14
13
|
throw new api_1.SwapError(`[getThorTxInfo]: vault not found for asset`, {
|
|
15
14
|
code: api_1.SwapErrorTypes.RESPONSE_ERROR,
|
|
16
|
-
details: {
|
|
15
|
+
details: { inboundAddress, sellAsset },
|
|
17
16
|
});
|
|
18
17
|
const limit = await (0, getLimit_1.getLimit)({
|
|
19
18
|
buyAssetId: buyAsset.assetId,
|
|
20
|
-
destinationAddress,
|
|
21
19
|
sellAmountCryptoPrecision,
|
|
22
20
|
sellAsset,
|
|
23
|
-
buyAsset,
|
|
24
21
|
slippageTolerance,
|
|
25
22
|
deps,
|
|
26
23
|
buyAssetTradeFeeUsd,
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { ChainId } from '@shapeshiftoss/caip';
|
|
2
|
-
import BigNumber from 'bignumber.js';
|
|
3
1
|
export declare const MAX_THORCHAIN_TRADE = "100000000000000000000000000";
|
|
4
2
|
export declare const MAX_ALLOWANCE = "100000000000000000000000000";
|
|
5
3
|
export declare const THOR_MINIMUM_PADDING = 1.2;
|
|
@@ -7,4 +5,3 @@ export declare const THOR_ETH_GAS_LIMIT = "100000";
|
|
|
7
5
|
export declare const THORCHAIN_FIXED_PRECISION = 8;
|
|
8
6
|
export declare const THORCHAIN_AFFILIATE_NAME = "ss";
|
|
9
7
|
export declare const THORCHAIN_AFFILIATE_BIPS = "0";
|
|
10
|
-
export declare const THOR_TRADE_FEE_MULTIPLIERS: Record<ChainId, BigNumber>;
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
|
-
const bignumber_1 = require("../../utils/bignumber");
|
|
3
|
+
exports.THORCHAIN_AFFILIATE_BIPS = exports.THORCHAIN_AFFILIATE_NAME = exports.THORCHAIN_FIXED_PRECISION = exports.THOR_ETH_GAS_LIMIT = exports.THOR_MINIMUM_PADDING = exports.MAX_ALLOWANCE = exports.MAX_THORCHAIN_TRADE = void 0;
|
|
6
4
|
exports.MAX_THORCHAIN_TRADE = '100000000000000000000000000';
|
|
7
5
|
exports.MAX_ALLOWANCE = '100000000000000000000000000';
|
|
8
6
|
exports.THOR_MINIMUM_PADDING = 1.2;
|
|
@@ -10,17 +8,3 @@ exports.THOR_ETH_GAS_LIMIT = '100000'; // for sends of eth / erc20 into thorchai
|
|
|
10
8
|
exports.THORCHAIN_FIXED_PRECISION = 8; // limit values are precision 8 regardless of the chain
|
|
11
9
|
exports.THORCHAIN_AFFILIATE_NAME = 'ss';
|
|
12
10
|
exports.THORCHAIN_AFFILIATE_BIPS = '0'; // affiliate fee in basis points (100 = 1%)
|
|
13
|
-
// Used to estimate the fee thorchain will take out of the buyAsset
|
|
14
|
-
// Official docs on fees are incorrect
|
|
15
|
-
// https://discord.com/channels/838986635756044328/997675038675316776/998552541170253834
|
|
16
|
-
// This is still not "perfect" and tends to overestimate by a small randomish amount
|
|
17
|
-
// TODO figure out if its possible to accurately estimate the outbound fee.
|
|
18
|
-
// Neither the discord nor official docs are correct
|
|
19
|
-
exports.THOR_TRADE_FEE_MULTIPLIERS = {
|
|
20
|
-
[types_1.KnownChainIds.BitcoinMainnet]: (0, bignumber_1.bn)(0.00002),
|
|
21
|
-
[types_1.KnownChainIds.DogecoinMainnet]: (0, bignumber_1.bn)(0.00002),
|
|
22
|
-
[types_1.KnownChainIds.LitecoinMainnet]: (0, bignumber_1.bn)(0.000005),
|
|
23
|
-
[types_1.KnownChainIds.BitcoinCashMainnet]: (0, bignumber_1.bn)(0.00003),
|
|
24
|
-
[types_1.KnownChainIds.CosmosMainnet]: (0, bignumber_1.bn)(0.00000002),
|
|
25
|
-
[types_1.KnownChainIds.EthereumMainnet]: (0, bignumber_1.bn)(0.00024), // A value that "works". Discord recommended value (80,000 gas * 2 or 160k) is often too low
|
|
26
|
-
};
|
|
@@ -4,8 +4,8 @@ import { ChainAdapter } from '@shapeshiftoss/chain-adapters';
|
|
|
4
4
|
import { HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
5
5
|
import { BIP44Params, KnownChainIds } from '@shapeshiftoss/types';
|
|
6
6
|
import { TradeQuote } from '../../../../api';
|
|
7
|
-
import { ThorchainSwapperDeps } from '../../types';
|
|
8
|
-
|
|
7
|
+
import type { ThorchainSwapperDeps } from '../../types';
|
|
8
|
+
declare type GetCosmosTxDataInput = {
|
|
9
9
|
bip44Params: BIP44Params;
|
|
10
10
|
destinationAddress: string;
|
|
11
11
|
deps: ThorchainSwapperDeps;
|
|
@@ -17,4 +17,6 @@ export declare const cosmosTxData: (input: {
|
|
|
17
17
|
quote: TradeQuote<KnownChainIds.CosmosMainnet>;
|
|
18
18
|
chainId: ChainId;
|
|
19
19
|
sellAdapter: ChainAdapter<KnownChainIds.CosmosMainnet>;
|
|
20
|
-
}
|
|
20
|
+
};
|
|
21
|
+
export declare const getCosmosTxData: (input: GetCosmosTxDataInput) => Promise<import("@shapeshiftoss/hdwallet-core").CosmosSignTx>;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getCosmosTxData = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const types_1 = require("@shapeshiftoss/types");
|
|
6
|
+
const api_1 = require("../../../../api");
|
|
7
|
+
const getInboundAddressDataForChain_1 = require("../getInboundAddressDataForChain");
|
|
8
|
+
const getLimit_1 = require("../getLimit/getLimit");
|
|
9
|
+
const makeSwapMemo_1 = require("../makeSwapMemo/makeSwapMemo");
|
|
10
|
+
const getCosmosTxData = async (input) => {
|
|
11
|
+
const { bip44Params, deps, destinationAddress, sellAmountCryptoPrecision, sellAsset, buyAsset, slippageTolerance, quote, wallet, sellAdapter, } = input;
|
|
12
|
+
const fromThorAsset = sellAsset.chainId == types_1.KnownChainIds.ThorchainMainnet;
|
|
13
|
+
const gaiaAddressData = await (0, getInboundAddressDataForChain_1.getInboundAddressDataForChain)(deps.daemonUrl, caip_1.cosmosAssetId);
|
|
14
|
+
const vault = gaiaAddressData === null || gaiaAddressData === void 0 ? void 0 : gaiaAddressData.address;
|
|
15
|
+
if (!vault && !fromThorAsset)
|
|
16
|
+
throw new api_1.SwapError('[buildTrade]: no vault for chain', {
|
|
17
|
+
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
18
|
+
fn: 'buildTrade',
|
|
19
|
+
details: { chainId: input.chainId },
|
|
20
|
+
});
|
|
21
|
+
const limit = await (0, getLimit_1.getLimit)({
|
|
22
|
+
buyAssetId: buyAsset.assetId,
|
|
23
|
+
sellAmountCryptoPrecision,
|
|
24
|
+
sellAsset,
|
|
25
|
+
slippageTolerance,
|
|
26
|
+
deps,
|
|
27
|
+
buyAssetTradeFeeUsd: quote.feeData.buyAssetTradeFeeUsd,
|
|
28
|
+
});
|
|
29
|
+
const memo = (0, makeSwapMemo_1.makeSwapMemo)({
|
|
30
|
+
buyAssetId: buyAsset.assetId,
|
|
31
|
+
destinationAddress,
|
|
32
|
+
limit,
|
|
33
|
+
});
|
|
34
|
+
const builtTxResponse = await (async () => {
|
|
35
|
+
switch (true) {
|
|
36
|
+
case fromThorAsset:
|
|
37
|
+
return await sellAdapter.buildDepositTransaction({
|
|
38
|
+
bip44Params,
|
|
39
|
+
value: sellAmountCryptoPrecision,
|
|
40
|
+
wallet,
|
|
41
|
+
memo,
|
|
42
|
+
chainSpecific: {
|
|
43
|
+
gas: quote.feeData.chainSpecific.estimatedGas,
|
|
44
|
+
fee: quote.feeData.networkFee,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
default:
|
|
48
|
+
if (!vault)
|
|
49
|
+
throw new api_1.SwapError('[buildTrade]: no vault for chain', {
|
|
50
|
+
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
51
|
+
fn: 'buildTrade',
|
|
52
|
+
details: { chainId: input.chainId },
|
|
53
|
+
});
|
|
54
|
+
return await sellAdapter.buildSendTransaction({
|
|
55
|
+
bip44Params,
|
|
56
|
+
value: sellAmountCryptoPrecision,
|
|
57
|
+
wallet,
|
|
58
|
+
to: vault,
|
|
59
|
+
memo,
|
|
60
|
+
chainSpecific: {
|
|
61
|
+
gas: quote.feeData.chainSpecific
|
|
62
|
+
.estimatedGas,
|
|
63
|
+
fee: quote.feeData.networkFee,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
})();
|
|
68
|
+
return builtTxResponse.txToSign;
|
|
69
|
+
};
|
|
70
|
+
exports.getCosmosTxData = getCosmosTxData;
|
|
@@ -2,8 +2,8 @@ import { Asset } from '@shapeshiftoss/asset-service';
|
|
|
2
2
|
import { ethereum } from '@shapeshiftoss/chain-adapters';
|
|
3
3
|
import { ETHSignTx, HDWallet } from '@shapeshiftoss/hdwallet-core';
|
|
4
4
|
import { BIP44Params } from '@shapeshiftoss/types';
|
|
5
|
-
import { ThorchainSwapperDeps } from '../../types';
|
|
6
|
-
|
|
5
|
+
import type { ThorchainSwapperDeps } from '../../types';
|
|
6
|
+
declare type MakeTradeTxArgs = {
|
|
7
7
|
wallet: HDWallet;
|
|
8
8
|
bip44Params: BIP44Params;
|
|
9
9
|
sellAmountCryptoPrecision: string;
|
|
@@ -17,12 +17,14 @@ export declare const makeTradeTx: ({ wallet, bip44Params, sellAmountCryptoPrecis
|
|
|
17
17
|
buyAssetTradeFeeUsd: string;
|
|
18
18
|
} & ({
|
|
19
19
|
gasPrice: string;
|
|
20
|
-
maxFeePerGas?:
|
|
21
|
-
maxPriorityFeePerGas?:
|
|
20
|
+
maxFeePerGas?: never;
|
|
21
|
+
maxPriorityFeePerGas?: never;
|
|
22
22
|
} | {
|
|
23
|
-
gasPrice?:
|
|
23
|
+
gasPrice?: never;
|
|
24
24
|
maxFeePerGas: string;
|
|
25
25
|
maxPriorityFeePerGas: string;
|
|
26
|
-
})
|
|
26
|
+
});
|
|
27
|
+
export declare const makeTradeTx: ({ wallet, bip44Params, sellAmountCryptoPrecision, buyAsset, sellAsset, destinationAddress, adapter, maxFeePerGas, maxPriorityFeePerGas, gasPrice, slippageTolerance, deps, gasLimit, buyAssetTradeFeeUsd, }: MakeTradeTxArgs) => Promise<{
|
|
27
28
|
txToSign: ETHSignTx;
|
|
28
29
|
}>;
|
|
30
|
+
export {};
|
|
@@ -4,7 +4,7 @@ exports.makeTradeTx = void 0;
|
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const web3_utils_1 = require("web3-utils");
|
|
6
6
|
const api_1 = require("../../../../api");
|
|
7
|
-
const getThorTxData_1 = require("
|
|
7
|
+
const getThorTxData_1 = require("./utils/getThorTxData");
|
|
8
8
|
const makeTradeTx = async ({ wallet, bip44Params, sellAmountCryptoPrecision, buyAsset, sellAsset, destinationAddress, adapter, maxFeePerGas, maxPriorityFeePerGas, gasPrice, slippageTolerance, deps, gasLimit, buyAssetTradeFeeUsd, }) => {
|
|
9
9
|
try {
|
|
10
10
|
const { assetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
@@ -23,14 +23,7 @@ const makeTradeTx = async ({ wallet, bip44Params, sellAmountCryptoPrecision, buy
|
|
|
23
23
|
bip44Params,
|
|
24
24
|
to: router,
|
|
25
25
|
gasLimit,
|
|
26
|
-
...(gasPrice !== undefined
|
|
27
|
-
? {
|
|
28
|
-
gasPrice,
|
|
29
|
-
}
|
|
30
|
-
: {
|
|
31
|
-
maxFeePerGas,
|
|
32
|
-
maxPriorityFeePerGas,
|
|
33
|
-
}),
|
|
26
|
+
...(gasPrice !== undefined ? { gasPrice } : { maxFeePerGas, maxPriorityFeePerGas }),
|
|
34
27
|
value: isErc20Trade ? '0x0' : (0, web3_utils_1.numberToHex)(sellAmountCryptoPrecision),
|
|
35
28
|
data,
|
|
36
29
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/asset-service';
|
|
2
|
-
import { ThorchainSwapperDeps } from '../../../types';
|
|
2
|
+
import type { ThorchainSwapperDeps } from '../../../types';
|
|
3
3
|
declare type GetBtcThorTxInfoArgs = {
|
|
4
4
|
deps: ThorchainSwapperDeps;
|
|
5
5
|
sellAsset: Asset;
|
|
@@ -11,7 +11,6 @@ declare type GetBtcThorTxInfoArgs = {
|
|
|
11
11
|
};
|
|
12
12
|
declare type GetBtcThorTxInfoReturn = Promise<{
|
|
13
13
|
data: string;
|
|
14
|
-
memo: string;
|
|
15
14
|
router: string;
|
|
16
15
|
}>;
|
|
17
16
|
declare type GetBtcThorTxInfo = (args: GetBtcThorTxInfoArgs) => GetBtcThorTxInfoReturn;
|
|
@@ -3,29 +3,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getThorTxInfo = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
const api_1 = require("../../../../../api");
|
|
6
|
+
const getInboundAddressDataForChain_1 = require("../../getInboundAddressDataForChain");
|
|
6
7
|
const getLimit_1 = require("../../getLimit/getLimit");
|
|
7
8
|
const makeSwapMemo_1 = require("../../makeSwapMemo/makeSwapMemo");
|
|
8
|
-
const thorService_1 = require("../../thorService");
|
|
9
9
|
const routerCalldata_1 = require("../routerCalldata");
|
|
10
10
|
const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmountCryptoPrecision, slippageTolerance, destinationAddress, buyAssetTradeFeeUsd, }) => {
|
|
11
11
|
try {
|
|
12
12
|
const { assetReference, assetNamespace } = (0, caip_1.fromAssetId)(sellAsset.assetId);
|
|
13
13
|
const isErc20Trade = assetNamespace === 'erc20';
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const vault =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
throw new api_1.SwapError(`[getPriceRatio]: router or vault found for ETH`, {
|
|
14
|
+
const inboundAddress = await (0, getInboundAddressDataForChain_1.getInboundAddressDataForChain)(deps.daemonUrl, caip_1.ethAssetId);
|
|
15
|
+
const router = inboundAddress === null || inboundAddress === void 0 ? void 0 : inboundAddress.router;
|
|
16
|
+
const vault = inboundAddress === null || inboundAddress === void 0 ? void 0 : inboundAddress.address;
|
|
17
|
+
if (!inboundAddress || !router || !vault)
|
|
18
|
+
throw new api_1.SwapError(`[getPriceRatio]: inboundAddress not found for ETH`, {
|
|
20
19
|
code: api_1.SwapErrorTypes.RESPONSE_ERROR,
|
|
21
|
-
details: {
|
|
20
|
+
details: { inboundAddress },
|
|
22
21
|
});
|
|
23
22
|
const limit = await (0, getLimit_1.getLimit)({
|
|
24
23
|
buyAssetId: buyAsset.assetId,
|
|
25
|
-
destinationAddress,
|
|
26
24
|
sellAmountCryptoPrecision,
|
|
27
25
|
sellAsset,
|
|
28
|
-
buyAsset,
|
|
29
26
|
slippageTolerance,
|
|
30
27
|
deps,
|
|
31
28
|
buyAssetTradeFeeUsd,
|
|
@@ -36,11 +33,7 @@ const getThorTxInfo = async ({ deps, sellAsset, buyAsset, sellAmountCryptoPrecis
|
|
|
36
33
|
limit,
|
|
37
34
|
});
|
|
38
35
|
const data = await (0, routerCalldata_1.deposit)(router, vault, isErc20Trade ? assetReference : '0x0000000000000000000000000000000000000000', sellAmountCryptoPrecision, memo);
|
|
39
|
-
return {
|
|
40
|
-
data,
|
|
41
|
-
memo,
|
|
42
|
-
router,
|
|
43
|
-
};
|
|
36
|
+
return { data, router };
|
|
44
37
|
}
|
|
45
38
|
catch (e) {
|
|
46
39
|
if (e instanceof api_1.SwapError)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInboundAddressDataForChain = void 0;
|
|
4
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
+
const thorService_1 = require("./thorService");
|
|
6
|
+
const getInboundAddressDataForChain = async (daemonUrl, assetId) => {
|
|
7
|
+
if (!assetId)
|
|
8
|
+
return undefined;
|
|
9
|
+
const assetPoolId = caip_1.adapters.assetIdToPoolAssetId({ assetId });
|
|
10
|
+
const assetChainSymbol = assetPoolId === null || assetPoolId === void 0 ? void 0 : assetPoolId.slice(0, assetPoolId.indexOf('.'));
|
|
11
|
+
const { data: inboundAddresses } = await thorService_1.thorService.get(`${daemonUrl}/lcd/thorchain/inbound_addresses`);
|
|
12
|
+
const activeInboundAddresses = inboundAddresses.filter((a) => !a.halted);
|
|
13
|
+
return activeInboundAddresses.find((inbound) => inbound.chain === assetChainSymbol);
|
|
14
|
+
};
|
|
15
|
+
exports.getInboundAddressDataForChain = getInboundAddressDataForChain;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { Asset } from '@shapeshiftoss/asset-service';
|
|
2
2
|
import { ThorchainSwapperDeps } from '../../types';
|
|
3
|
-
export declare
|
|
3
|
+
export declare type GetLimitArgs = {
|
|
4
4
|
buyAssetId: string;
|
|
5
|
-
destinationAddress: string;
|
|
6
5
|
sellAsset: Asset;
|
|
7
|
-
buyAsset: Asset;
|
|
8
6
|
sellAmountCryptoPrecision: string;
|
|
9
7
|
deps: ThorchainSwapperDeps;
|
|
10
8
|
slippageTolerance: string;
|
|
11
9
|
buyAssetTradeFeeUsd: string;
|
|
12
|
-
}
|
|
10
|
+
};
|
|
11
|
+
export declare const getLimit: ({ sellAsset, buyAssetId, sellAmountCryptoPrecision, deps, slippageTolerance, buyAssetTradeFeeUsd, }: GetLimitArgs) => Promise<string>;
|
|
@@ -1,25 +1,67 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.getLimit = void 0;
|
|
7
|
+
const caip_1 = require("@shapeshiftoss/caip");
|
|
8
|
+
const max_1 = __importDefault(require("lodash/max"));
|
|
4
9
|
const api_1 = require("../../../../api");
|
|
5
10
|
const bignumber_1 = require("../../../utils/bignumber");
|
|
6
|
-
const constants_1 = require("
|
|
11
|
+
const constants_1 = require("../../constants");
|
|
12
|
+
const constants_2 = require("../constants");
|
|
13
|
+
const getInboundAddressDataForChain_1 = require("../getInboundAddressDataForChain");
|
|
7
14
|
const getTradeRate_1 = require("../getTradeRate/getTradeRate");
|
|
8
15
|
const getUsdRate_1 = require("../getUsdRate/getUsdRate");
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
16
|
+
const isRune_1 = require("../isRune/isRune");
|
|
17
|
+
const getLimit = async ({ sellAsset, buyAssetId, sellAmountCryptoPrecision, deps, slippageTolerance, buyAssetTradeFeeUsd, }) => {
|
|
18
|
+
var _a, _b;
|
|
19
|
+
const tradeRate = await (0, getTradeRate_1.getTradeRate)(sellAsset, buyAssetId, sellAmountCryptoPrecision, deps);
|
|
20
|
+
const sellAssetChainFeeAssetId = (_a = deps.adapterManager.get(sellAsset.chainId)) === null || _a === void 0 ? void 0 : _a.getFeeAssetId();
|
|
21
|
+
const buyAssetChainFeeAssetId = (_b = deps.adapterManager
|
|
22
|
+
.get((0, caip_1.fromAssetId)(buyAssetId).chainId)) === null || _b === void 0 ? void 0 : _b.getFeeAssetId();
|
|
23
|
+
if (!sellAssetChainFeeAssetId || !buyAssetChainFeeAssetId) {
|
|
24
|
+
throw new api_1.SwapError('[getLimit]: no sellAssetChainFeeAsset or buyAssetChainFeeAssetId', {
|
|
25
|
+
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
26
|
+
details: { sellAssetChainFeeAssetId, buyAssetChainFeeAssetId },
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const sellFeeAssetUsdRate = await (0, getUsdRate_1.getUsdRate)({
|
|
30
|
+
deps,
|
|
31
|
+
input: { assetId: sellAssetChainFeeAssetId },
|
|
32
|
+
});
|
|
33
|
+
const buyAssetUsdRate = await (0, getUsdRate_1.getUsdRate)({ deps, input: { assetId: buyAssetId } });
|
|
34
|
+
const expectedBuyAmountCryptoPrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAmountCryptoPrecision).times(tradeRate), sellAsset.precision), constants_2.THORCHAIN_FIXED_PRECISION);
|
|
13
35
|
const isValidSlippageRange = (0, bignumber_1.bnOrZero)(slippageTolerance).gte(0) && (0, bignumber_1.bnOrZero)(slippageTolerance).lte(1);
|
|
14
36
|
if ((0, bignumber_1.bnOrZero)(expectedBuyAmountCryptoPrecision8).lt(0) || !isValidSlippageRange)
|
|
15
|
-
throw new api_1.SwapError('[
|
|
37
|
+
throw new api_1.SwapError('[getLimit]: bad expected buy amount or bad slippage tolerance', {
|
|
16
38
|
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
17
39
|
details: { expectedBuyAmountCryptoPrecision8, slippageTolerance },
|
|
18
40
|
});
|
|
19
|
-
const
|
|
41
|
+
const buyAssetTradeFeeCryptoPrecision8 = (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)(buyAssetTradeFeeUsd).div(buyAssetUsdRate), constants_2.THORCHAIN_FIXED_PRECISION);
|
|
42
|
+
const sellAssetAddressData = await (0, getInboundAddressDataForChain_1.getInboundAddressDataForChain)(deps.daemonUrl, sellAsset.assetId);
|
|
43
|
+
const refundFeeBuyAssetCryptoPrecision8 = (() => {
|
|
44
|
+
switch (true) {
|
|
45
|
+
// If the sell asset is on THOR the return fee is fixed at 0.02 RUNE
|
|
46
|
+
case (0, isRune_1.isRune)(sellAsset.assetId): {
|
|
47
|
+
const runeFeeUsd = constants_1.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN.times(sellFeeAssetUsdRate);
|
|
48
|
+
return (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)(runeFeeUsd).div(buyAssetUsdRate), constants_2.THORCHAIN_FIXED_PRECISION);
|
|
49
|
+
}
|
|
50
|
+
// Else the return fee is the outbound fee of the sell asset's chain
|
|
51
|
+
default: {
|
|
52
|
+
const sellAssetTradeFeeCryptoHuman = (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(sellAssetAddressData === null || sellAssetAddressData === void 0 ? void 0 : sellAssetAddressData.outbound_fee), constants_2.THORCHAIN_FIXED_PRECISION);
|
|
53
|
+
const sellAssetTradeFeeUsd = (0, bignumber_1.bnOrZero)(sellAssetTradeFeeCryptoHuman).times(sellFeeAssetUsdRate);
|
|
54
|
+
return (0, bignumber_1.toBaseUnit)((0, bignumber_1.bnOrZero)(sellAssetTradeFeeUsd).div(buyAssetUsdRate), constants_2.THORCHAIN_FIXED_PRECISION);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
})();
|
|
58
|
+
const highestPossibleFeeCryptoPrecision8 = (0, max_1.default)([
|
|
59
|
+
buyAssetTradeFeeCryptoPrecision8,
|
|
60
|
+
refundFeeBuyAssetCryptoPrecision8,
|
|
61
|
+
]);
|
|
20
62
|
return (0, bignumber_1.bnOrZero)(expectedBuyAmountCryptoPrecision8)
|
|
21
63
|
.times((0, bignumber_1.bn)(1).minus(slippageTolerance))
|
|
22
|
-
.minus((0, bignumber_1.bnOrZero)(
|
|
64
|
+
.minus((0, bignumber_1.bnOrZero)(highestPossibleFeeCryptoPrecision8))
|
|
23
65
|
.decimalPlaces(0)
|
|
24
66
|
.toString();
|
|
25
67
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { InboundAddressResponse, MidgardPoolResponse, ThornodePoolResponse } from '../../types';
|
|
2
2
|
export declare const btcMidgardPool: MidgardPoolResponse;
|
|
3
3
|
export declare const btcThornodePool: ThornodePoolResponse;
|
|
4
4
|
export declare const ethMidgardPool: MidgardPoolResponse;
|
|
5
5
|
export declare const ethThornodePool: ThornodePoolResponse;
|
|
6
6
|
export declare const foxMidgardPool: MidgardPoolResponse;
|
|
7
7
|
export declare const foxThornodePool: ThornodePoolResponse;
|
|
8
|
-
export declare const
|
|
8
|
+
export declare const mockInboundAddresses: InboundAddressResponse[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.mockInboundAddresses = exports.foxThornodePool = exports.foxMidgardPool = exports.ethThornodePool = exports.ethMidgardPool = exports.btcThornodePool = exports.btcMidgardPool = void 0;
|
|
4
4
|
exports.btcMidgardPool = {
|
|
5
5
|
asset: 'BTC.BTC',
|
|
6
6
|
assetDepth: '91027798705',
|
|
@@ -31,7 +31,7 @@ exports.ethMidgardPool = {
|
|
|
31
31
|
asset: 'ETH.ETH',
|
|
32
32
|
assetDepth: '915018987646',
|
|
33
33
|
assetPrice: '676.2118757936507',
|
|
34
|
-
assetPriceUSD: '
|
|
34
|
+
assetPriceUSD: '1595',
|
|
35
35
|
liquidityUnits: '273709963437801',
|
|
36
36
|
poolAPY: '0.10532151323548256',
|
|
37
37
|
runeDepth: '618746706022909',
|
|
@@ -79,13 +79,19 @@ exports.foxThornodePool = {
|
|
|
79
79
|
synth_supply: '17428372569375',
|
|
80
80
|
synth_units: '552604585625',
|
|
81
81
|
};
|
|
82
|
-
exports.
|
|
82
|
+
exports.mockInboundAddresses = [
|
|
83
83
|
{
|
|
84
84
|
chain: 'BCH',
|
|
85
85
|
pub_key: 'thorpub1addwnpepqfyppkehky2hn2gy26y8jqyj9fz0rnvau2r7yueawzvc662w0x9s7n4ypra',
|
|
86
86
|
address: 'qp5a6rn8zmamgmsyspqsl0p6ktk4hrheggdjdvntv0',
|
|
87
87
|
halted: false,
|
|
88
88
|
gas_rate: '3',
|
|
89
|
+
gas_rate_units: 'satsperbyte',
|
|
90
|
+
global_trading_paused: false,
|
|
91
|
+
chain_trading_paused: false,
|
|
92
|
+
chain_lp_actions_paused: false,
|
|
93
|
+
outbound_fee: '9000',
|
|
94
|
+
outbound_tx_size: '1500',
|
|
89
95
|
},
|
|
90
96
|
{
|
|
91
97
|
chain: 'BNB',
|
|
@@ -93,6 +99,12 @@ exports.mockInboundAdresses = [
|
|
|
93
99
|
address: 'bnb1d8wsuecklw6xupyqgy8mcw4ja4dca72z7nvqzw',
|
|
94
100
|
halted: false,
|
|
95
101
|
gas_rate: '11250',
|
|
102
|
+
gas_rate_units: 'ubnb',
|
|
103
|
+
global_trading_paused: false,
|
|
104
|
+
chain_trading_paused: false,
|
|
105
|
+
chain_lp_actions_paused: false,
|
|
106
|
+
outbound_fee: '22500',
|
|
107
|
+
outbound_tx_size: '1',
|
|
96
108
|
},
|
|
97
109
|
{
|
|
98
110
|
chain: 'BTC',
|
|
@@ -100,6 +112,12 @@ exports.mockInboundAdresses = [
|
|
|
100
112
|
address: 'bc1qd8wsuecklw6xupyqgy8mcw4ja4dca72z2dd88d',
|
|
101
113
|
halted: false,
|
|
102
114
|
gas_rate: '18',
|
|
115
|
+
gas_rate_units: 'satsperbyte',
|
|
116
|
+
global_trading_paused: false,
|
|
117
|
+
chain_trading_paused: false,
|
|
118
|
+
chain_lp_actions_paused: false,
|
|
119
|
+
outbound_fee: '33000',
|
|
120
|
+
outbound_tx_size: '1000',
|
|
103
121
|
},
|
|
104
122
|
{
|
|
105
123
|
chain: 'DOGE',
|
|
@@ -107,6 +125,12 @@ exports.mockInboundAdresses = [
|
|
|
107
125
|
address: 'DEnrJfAjC4KMsLsGtoTGSmYaFZLPunWw9q',
|
|
108
126
|
halted: false,
|
|
109
127
|
gas_rate: '550384',
|
|
128
|
+
gas_rate_units: 'satsperbyte',
|
|
129
|
+
global_trading_paused: false,
|
|
130
|
+
chain_trading_paused: false,
|
|
131
|
+
chain_lp_actions_paused: false,
|
|
132
|
+
outbound_fee: '1500000000',
|
|
133
|
+
outbound_tx_size: '1000',
|
|
110
134
|
},
|
|
111
135
|
{
|
|
112
136
|
chain: 'ETH',
|
|
@@ -115,6 +139,12 @@ exports.mockInboundAdresses = [
|
|
|
115
139
|
router: '0x3624525075b88B24ecc29CE226b0CEc1fFcB6976',
|
|
116
140
|
halted: false,
|
|
117
141
|
gas_rate: '280',
|
|
142
|
+
gas_rate_units: 'gwei',
|
|
143
|
+
global_trading_paused: false,
|
|
144
|
+
chain_trading_paused: false,
|
|
145
|
+
chain_lp_actions_paused: false,
|
|
146
|
+
outbound_fee: '480000',
|
|
147
|
+
outbound_tx_size: '80000',
|
|
118
148
|
},
|
|
119
149
|
{
|
|
120
150
|
chain: 'LTC',
|
|
@@ -122,12 +152,38 @@ exports.mockInboundAdresses = [
|
|
|
122
152
|
address: 'ltc1qd8wsuecklw6xupyqgy8mcw4ja4dca72zw3hrla',
|
|
123
153
|
halted: false,
|
|
124
154
|
gas_rate: '60',
|
|
155
|
+
gas_rate_units: 'satsperbyte',
|
|
156
|
+
global_trading_paused: false,
|
|
157
|
+
chain_trading_paused: false,
|
|
158
|
+
chain_lp_actions_paused: false,
|
|
159
|
+
outbound_fee: '99750',
|
|
160
|
+
outbound_tx_size: '250',
|
|
125
161
|
},
|
|
126
162
|
{
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
gas_rate: '
|
|
163
|
+
address: 'cosmos1euskj33zyqcysvn252qvwkkurkd72gvnv30h4d',
|
|
164
|
+
chain: 'GAIA',
|
|
165
|
+
chain_lp_actions_paused: false,
|
|
166
|
+
chain_trading_paused: false,
|
|
167
|
+
gas_rate: '450000',
|
|
168
|
+
gas_rate_units: 'uatom',
|
|
169
|
+
global_trading_paused: false,
|
|
170
|
+
halted: false,
|
|
171
|
+
outbound_fee: '900000',
|
|
172
|
+
outbound_tx_size: '1',
|
|
173
|
+
pub_key: 'sthorpub1addwnpepqdn3p3y3ctnr4sqdr0wswmn665asezx3c5yxags7y5ctxs5j42q278pj7fc',
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
address: '0x8d6ed3bcdafc38214af914b08bc47ce36802c87c',
|
|
177
|
+
chain: 'AVAX',
|
|
178
|
+
chain_lp_actions_paused: false,
|
|
179
|
+
chain_trading_paused: false,
|
|
180
|
+
gas_rate: '150',
|
|
181
|
+
gas_rate_units: 'nAVAX',
|
|
182
|
+
global_trading_paused: false,
|
|
183
|
+
halted: false,
|
|
184
|
+
outbound_fee: '2400000',
|
|
185
|
+
outbound_tx_size: '80000',
|
|
186
|
+
pub_key: 'sthorpub1addwnpepqdn3p3y3ctnr4sqdr0wswmn665asezx3c5yxags7y5ctxs5j42q278pj7fc',
|
|
187
|
+
router: '0xd6a6c0b3bb4150a98a379811934e440989209db6',
|
|
132
188
|
},
|
|
133
189
|
];
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.setupThorswapDeps = void 0;
|
|
7
7
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
8
8
|
const chain_adapters_1 = require("@shapeshiftoss/chain-adapters");
|
|
9
|
+
const types_1 = require("@shapeshiftoss/types");
|
|
9
10
|
const web3_1 = __importDefault(require("web3"));
|
|
10
11
|
jest.mock('web3');
|
|
11
12
|
const setupThorswapDeps = () => {
|
|
@@ -18,7 +19,7 @@ const setupThorswapDeps = () => {
|
|
|
18
19
|
};
|
|
19
20
|
const adapterManager = new Map([
|
|
20
21
|
[
|
|
21
|
-
|
|
22
|
+
types_1.KnownChainIds.EthereumMainnet,
|
|
22
23
|
{
|
|
23
24
|
buildBIP44Params: jest.fn(() => ({ purpose: 44, coinType: 60, accountNumber: 0 })),
|
|
24
25
|
getAddress: jest.fn(() => Promise.resolve('0xthisIsMyAddress')),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AVAX = exports.USDC = exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
3
|
+
exports.RUNE = exports.AVAX = exports.USDC = exports.UNSUPPORTED = exports.ETH = exports.WBTC = exports.FOX = exports.WETH = exports.BTC = void 0;
|
|
4
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
5
|
exports.BTC = {
|
|
6
6
|
assetId: 'bip122:000000000019d6689c085ae165831e93/slip44:0',
|
|
@@ -27,7 +27,7 @@ exports.WETH = {
|
|
|
27
27
|
symbol: 'WETH',
|
|
28
28
|
};
|
|
29
29
|
exports.FOX = {
|
|
30
|
-
assetId:
|
|
30
|
+
assetId: caip_1.foxAssetId,
|
|
31
31
|
chainId: caip_1.ethChainId,
|
|
32
32
|
name: 'FOX',
|
|
33
33
|
precision: 18,
|
|
@@ -98,3 +98,15 @@ exports.AVAX = {
|
|
|
98
98
|
explorerAddressLink: 'https://snowtrace.io/address/',
|
|
99
99
|
explorerTxLink: 'https://snowtrace.io/tx/',
|
|
100
100
|
};
|
|
101
|
+
exports.RUNE = {
|
|
102
|
+
assetId: caip_1.thorchainAssetId,
|
|
103
|
+
chainId: caip_1.thorchainChainId,
|
|
104
|
+
name: 'THORChain',
|
|
105
|
+
symbol: 'RUNE',
|
|
106
|
+
precision: 8,
|
|
107
|
+
color: '#33FF99',
|
|
108
|
+
icon: 'https://assets.coincap.io/assets/icons/rune@2x.png',
|
|
109
|
+
explorer: 'https://v2.viewblock.io/thorchain',
|
|
110
|
+
explorerAddressLink: 'https://v2.viewblock.io/thorchain/address/',
|
|
111
|
+
explorerTxLink: 'https://v2.viewblock.io/thorchain/tx/',
|
|
112
|
+
};
|
package/package.json
CHANGED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.cosmosTxData = void 0;
|
|
4
|
-
const types_1 = require("@shapeshiftoss/types");
|
|
5
|
-
const api_1 = require("../../../../api");
|
|
6
|
-
const getLimit_1 = require("../getLimit/getLimit");
|
|
7
|
-
const makeSwapMemo_1 = require("../makeSwapMemo/makeSwapMemo");
|
|
8
|
-
const thorService_1 = require("../thorService");
|
|
9
|
-
const cosmosTxData = async (input) => {
|
|
10
|
-
const { bip44Params, deps, destinationAddress, sellAmountCryptoPrecision, sellAsset, buyAsset, slippageTolerance, quote, wallet, sellAdapter, } = input;
|
|
11
|
-
const fromThorAsset = sellAsset.chainId == types_1.KnownChainIds.ThorchainMainnet;
|
|
12
|
-
const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.daemonUrl}/lcd/thorchain/inbound_addresses`);
|
|
13
|
-
const atomInboundAddresses = inboundAddresses.find((inbound) => inbound.chain === 'GAIA');
|
|
14
|
-
const vault = atomInboundAddresses === null || atomInboundAddresses === void 0 ? void 0 : atomInboundAddresses.address;
|
|
15
|
-
if (!vault && !fromThorAsset)
|
|
16
|
-
throw new api_1.SwapError('[buildTrade]: no vault for chain', {
|
|
17
|
-
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
18
|
-
fn: 'buildTrade',
|
|
19
|
-
details: { chainId: input.chainId },
|
|
20
|
-
});
|
|
21
|
-
const limit = await (0, getLimit_1.getLimit)({
|
|
22
|
-
buyAssetId: buyAsset.assetId,
|
|
23
|
-
destinationAddress,
|
|
24
|
-
sellAmountCryptoPrecision,
|
|
25
|
-
sellAsset,
|
|
26
|
-
buyAsset,
|
|
27
|
-
slippageTolerance,
|
|
28
|
-
deps,
|
|
29
|
-
buyAssetTradeFeeUsd: quote.feeData.buyAssetTradeFeeUsd,
|
|
30
|
-
});
|
|
31
|
-
const memo = (0, makeSwapMemo_1.makeSwapMemo)({
|
|
32
|
-
buyAssetId: buyAsset.assetId,
|
|
33
|
-
destinationAddress,
|
|
34
|
-
limit,
|
|
35
|
-
});
|
|
36
|
-
if (fromThorAsset) {
|
|
37
|
-
const buildTxResponse = await sellAdapter.buildDepositTransaction({
|
|
38
|
-
bip44Params,
|
|
39
|
-
value: sellAmountCryptoPrecision,
|
|
40
|
-
wallet,
|
|
41
|
-
memo,
|
|
42
|
-
gas: quote.feeData.chainSpecific.estimatedGas,
|
|
43
|
-
fee: quote.feeData.networkFee,
|
|
44
|
-
});
|
|
45
|
-
return buildTxResponse.txToSign;
|
|
46
|
-
}
|
|
47
|
-
if (!vault)
|
|
48
|
-
throw new api_1.SwapError('[buildTrade]: no vault for chain', {
|
|
49
|
-
code: api_1.SwapErrorTypes.BUILD_TRADE_FAILED,
|
|
50
|
-
fn: 'buildTrade',
|
|
51
|
-
details: { chainId: input.chainId },
|
|
52
|
-
});
|
|
53
|
-
const buildTxResponse = await sellAdapter.buildSendTransaction({
|
|
54
|
-
bip44Params,
|
|
55
|
-
value: sellAmountCryptoPrecision,
|
|
56
|
-
wallet,
|
|
57
|
-
to: vault,
|
|
58
|
-
memo,
|
|
59
|
-
chainSpecific: {
|
|
60
|
-
gas: quote.feeData.chainSpecific.estimatedGas,
|
|
61
|
-
fee: quote.feeData.networkFee,
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
return buildTxResponse.txToSign;
|
|
65
|
-
};
|
|
66
|
-
exports.cosmosTxData = cosmosTxData;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.estimateBuyAssetTradeFeeCrypto = void 0;
|
|
4
|
-
const caip_1 = require("@shapeshiftoss/caip");
|
|
5
|
-
const api_1 = require("../../../../api");
|
|
6
|
-
const constants_1 = require("../../constants");
|
|
7
|
-
const constants_2 = require("../constants");
|
|
8
|
-
const getPriceRatio_1 = require("../getPriceRatio/getPriceRatio");
|
|
9
|
-
const isRune_1 = require("../isRune/isRune");
|
|
10
|
-
const thorService_1 = require("../thorService");
|
|
11
|
-
const estimateBuyAssetTradeFeeCrypto = async (deps, buyAsset) => {
|
|
12
|
-
if ((0, isRune_1.isRune)(buyAsset.assetId)) {
|
|
13
|
-
return constants_1.RUNE_OUTBOUND_TRANSACTION_FEE_CRYPTO_HUMAN.toString();
|
|
14
|
-
}
|
|
15
|
-
const thorId = caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAsset.assetId });
|
|
16
|
-
if (!thorId)
|
|
17
|
-
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - undefined thorId for given buyAssetId', {
|
|
18
|
-
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
19
|
-
details: { buyAssetId: buyAsset.assetId },
|
|
20
|
-
});
|
|
21
|
-
const thorPoolChainId = thorId.slice(0, thorId.indexOf('.'));
|
|
22
|
-
const { data: inboundAddresses } = await thorService_1.thorService.get(`${deps.daemonUrl}/lcd/thorchain/inbound_addresses`);
|
|
23
|
-
const inboundInfo = inboundAddresses.find((inbound) => inbound.chain === thorPoolChainId);
|
|
24
|
-
if (!inboundInfo)
|
|
25
|
-
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - unable to locate inbound pool info', {
|
|
26
|
-
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
27
|
-
details: { thorPoolChainId },
|
|
28
|
-
});
|
|
29
|
-
const gasRate = inboundInfo.gas_rate;
|
|
30
|
-
const { chainId: buyChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
31
|
-
const buyAdapter = deps.adapterManager.get(buyChainId);
|
|
32
|
-
if (!buyAdapter)
|
|
33
|
-
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - unable to get buy asset adapter', {
|
|
34
|
-
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
35
|
-
details: { buyChainId },
|
|
36
|
-
});
|
|
37
|
-
const buyFeeAssetId = buyAdapter.getFeeAssetId();
|
|
38
|
-
if (!buyFeeAssetId)
|
|
39
|
-
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - no fee assetId', {
|
|
40
|
-
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
41
|
-
details: { buyAssetId: buyAsset.assetId },
|
|
42
|
-
});
|
|
43
|
-
const buyFeeAssetRatio = buyAsset.assetId !== buyFeeAssetId
|
|
44
|
-
? await (0, getPriceRatio_1.getPriceRatio)(deps, {
|
|
45
|
-
sellAssetId: buyAsset.assetId,
|
|
46
|
-
buyAssetId: buyFeeAssetId,
|
|
47
|
-
})
|
|
48
|
-
: '1';
|
|
49
|
-
if (!constants_2.THOR_TRADE_FEE_MULTIPLIERS[buyChainId])
|
|
50
|
-
throw new api_1.SwapError('[estimateBuyAssetTradeFeeCrypto] - no trade fee multiplier', {
|
|
51
|
-
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
52
|
-
details: { buyChainId },
|
|
53
|
-
});
|
|
54
|
-
return constants_2.THOR_TRADE_FEE_MULTIPLIERS[buyChainId].times(buyFeeAssetRatio).times(gasRate).toString();
|
|
55
|
-
};
|
|
56
|
-
exports.estimateBuyAssetTradeFeeCrypto = estimateBuyAssetTradeFeeCrypto;
|