@shapeshiftoss/swapper 9.3.2 → 9.4.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 +4 -2
- package/dist/swappers/thorchain/ThorchainSwapper.js +22 -8
- package/dist/swappers/thorchain/utils/constants.d.ts +3 -2
- package/dist/swappers/thorchain/utils/constants.js +15 -4
- package/dist/swappers/thorchain/utils/estimateTradeFee/estimateTradeFee.js +8 -43
- package/package.json +1 -1
|
@@ -5,8 +5,10 @@ import { ApprovalNeededInput, ApprovalNeededOutput, ApproveInfiniteInput, BuildT
|
|
|
5
5
|
import { ThorchainSwapperDeps } from './types';
|
|
6
6
|
export declare class ThorchainSwapper implements Swapper<ChainId> {
|
|
7
7
|
readonly name = "Thorchain";
|
|
8
|
-
private
|
|
9
|
-
private
|
|
8
|
+
private sellSupportedChainIds;
|
|
9
|
+
private buySupportedChainIds;
|
|
10
|
+
private supportedSellAssetIds;
|
|
11
|
+
private supportedBuyAssetIds;
|
|
10
12
|
deps: ThorchainSwapperDeps;
|
|
11
13
|
constructor(deps: ThorchainSwapperDeps);
|
|
12
14
|
initialize(): Promise<void>;
|
|
@@ -13,24 +13,38 @@ const thorService_1 = require("./utils/thorService");
|
|
|
13
13
|
class ThorchainSwapper {
|
|
14
14
|
constructor(deps) {
|
|
15
15
|
this.name = 'Thorchain';
|
|
16
|
-
this.
|
|
16
|
+
this.sellSupportedChainIds = {
|
|
17
17
|
[types_1.KnownChainIds.EthereumMainnet]: true,
|
|
18
18
|
[types_1.KnownChainIds.BitcoinMainnet]: true
|
|
19
19
|
};
|
|
20
|
-
this.
|
|
20
|
+
this.buySupportedChainIds = {
|
|
21
|
+
[types_1.KnownChainIds.EthereumMainnet]: true,
|
|
22
|
+
[types_1.KnownChainIds.BitcoinMainnet]: true,
|
|
23
|
+
[types_1.KnownChainIds.DogecoinMainnet]: true,
|
|
24
|
+
[types_1.KnownChainIds.LitecoinMainnet]: true,
|
|
25
|
+
[types_1.KnownChainIds.CosmosMainnet]: true
|
|
26
|
+
};
|
|
27
|
+
this.supportedSellAssetIds = [];
|
|
28
|
+
this.supportedBuyAssetIds = [];
|
|
21
29
|
this.deps = deps;
|
|
22
30
|
}
|
|
23
31
|
async initialize() {
|
|
24
32
|
try {
|
|
25
33
|
const { data: responseData } = await thorService_1.thorService.get(`${this.deps.midgardUrl}/pools`);
|
|
26
|
-
|
|
34
|
+
this.supportedSellAssetIds = responseData.reduce((acc, midgardPool) => {
|
|
35
|
+
const assetId = caip_1.adapters.poolAssetIdToAssetId(midgardPool.asset);
|
|
36
|
+
if (!assetId || !this.sellSupportedChainIds[(0, caip_1.fromAssetId)(assetId).chainId])
|
|
37
|
+
return acc;
|
|
38
|
+
acc.push(assetId);
|
|
39
|
+
return acc;
|
|
40
|
+
}, []);
|
|
41
|
+
this.supportedBuyAssetIds = responseData.reduce((acc, midgardPool) => {
|
|
27
42
|
const assetId = caip_1.adapters.poolAssetIdToAssetId(midgardPool.asset);
|
|
28
|
-
if (!assetId || !this.
|
|
43
|
+
if (!assetId || !this.buySupportedChainIds[(0, caip_1.fromAssetId)(assetId).chainId])
|
|
29
44
|
return acc;
|
|
30
45
|
acc.push(assetId);
|
|
31
46
|
return acc;
|
|
32
47
|
}, []);
|
|
33
|
-
this.supportedAssetIds = supportedAssetIds;
|
|
34
48
|
}
|
|
35
49
|
catch (e) {
|
|
36
50
|
throw new api_1.SwapError('[thorchainInitialize]: initialize failed to set supportedAssetIds', {
|
|
@@ -53,12 +67,12 @@ class ThorchainSwapper {
|
|
|
53
67
|
}
|
|
54
68
|
filterBuyAssetsBySellAssetId(args) {
|
|
55
69
|
const { assetIds = [], sellAssetId } = args;
|
|
56
|
-
if (!this.
|
|
70
|
+
if (!this.supportedSellAssetIds.includes(sellAssetId))
|
|
57
71
|
return [];
|
|
58
|
-
return assetIds.filter((assetId) => this.
|
|
72
|
+
return assetIds.filter((assetId) => this.supportedBuyAssetIds.includes(assetId) && assetId !== sellAssetId);
|
|
59
73
|
}
|
|
60
74
|
filterAssetIdsBySellable() {
|
|
61
|
-
return this.
|
|
75
|
+
return this.supportedSellAssetIds;
|
|
62
76
|
}
|
|
63
77
|
async buildTrade(input) {
|
|
64
78
|
return (0, buildThorTrade_1.buildTrade)({ deps: this.deps, input });
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ChainId } from '@shapeshiftoss/caip';
|
|
2
|
+
import BigNumber from 'bignumber.js';
|
|
1
3
|
export declare const MAX_THORCHAIN_TRADE = "100000000000000000000000000";
|
|
2
4
|
export declare const MAX_ALLOWANCE = "100000000000000000000000000";
|
|
3
5
|
export declare const THOR_MINIMUM_PADDING = 1.2;
|
|
@@ -5,5 +7,4 @@ export declare const THOR_ETH_GAS_LIMIT = "100000";
|
|
|
5
7
|
export declare const THORCHAIN_FIXED_PRECISION = 8;
|
|
6
8
|
export declare const THORCHAIN_AFFILIATE_NAME = "ss";
|
|
7
9
|
export declare const THORCHAIN_AFFILIATE_BIPS = "0";
|
|
8
|
-
export declare const
|
|
9
|
-
export declare const THOR_TRADE_FEE_ETH_GAS = 120000;
|
|
10
|
+
export declare const THOR_TRADE_FEE_MULTIPLIERS: Record<ChainId, BigNumber>;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.THOR_TRADE_FEE_MULTIPLIERS = 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;
|
|
4
|
+
const types_1 = require("@shapeshiftoss/types");
|
|
5
|
+
const bignumber_1 = require("../../utils/bignumber");
|
|
4
6
|
exports.MAX_THORCHAIN_TRADE = '100000000000000000000000000';
|
|
5
7
|
exports.MAX_ALLOWANCE = '100000000000000000000000000';
|
|
6
8
|
exports.THOR_MINIMUM_PADDING = 1.2;
|
|
@@ -8,7 +10,16 @@ exports.THOR_ETH_GAS_LIMIT = '100000'; // for sends of eth / erc20 into thorchai
|
|
|
8
10
|
exports.THORCHAIN_FIXED_PRECISION = 8; // limit values are precision 8 regardless of the chain
|
|
9
11
|
exports.THORCHAIN_AFFILIATE_NAME = 'ss';
|
|
10
12
|
exports.THORCHAIN_AFFILIATE_BIPS = '0'; // affiliate fee in basis points (100 = 1%)
|
|
11
|
-
// These are different from THOR_ETH_GAS_LIMIT
|
|
12
13
|
// Used to estimate the fee thorchain will take out of the buyAsset
|
|
13
|
-
|
|
14
|
-
|
|
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.CosmosMainnet]: (0, bignumber_1.bn)(0.00000002),
|
|
24
|
+
[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
|
|
25
|
+
};
|
|
@@ -1,30 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.estimateTradeFee = void 0;
|
|
4
|
-
const asset_service_1 = require("@shapeshiftoss/asset-service");
|
|
5
4
|
const caip_1 = require("@shapeshiftoss/caip");
|
|
6
5
|
const api_1 = require("../../../../api");
|
|
7
|
-
const bignumber_1 = require("../../../utils/bignumber");
|
|
8
6
|
const constants_1 = require("../constants");
|
|
9
7
|
const getPriceRatio_1 = require("../getPriceRatio/getPriceRatio");
|
|
10
8
|
const thorService_1 = require("../thorService");
|
|
11
|
-
const gweiGasPrecision = 9;
|
|
12
|
-
// Official docs on fees are incorrect
|
|
13
|
-
// https://discord.com/channels/838986635756044328/997675038675316776/998552541170253834
|
|
14
|
-
// This is still not "perfect" and tends to overestimate by a randomish amount
|
|
15
|
-
// TODO figure out if its possible to accurately estimate the outbound fee.
|
|
16
|
-
// Neither the discord nor official docs are correct
|
|
17
|
-
const ethEstimate = (gasRate) => (0, bignumber_1.bnOrZero)(gasRate)
|
|
18
|
-
.times(constants_1.THOR_TRADE_FEE_ETH_GAS)
|
|
19
|
-
.times(2)
|
|
20
|
-
.times((0, bignumber_1.bn)(10).exponentiatedBy(gweiGasPrecision))
|
|
21
|
-
.toString();
|
|
22
|
-
const erc20Estimate = (gasRate) => (0, bignumber_1.bnOrZero)(gasRate)
|
|
23
|
-
.times(constants_1.THOR_TRADE_FEE_ETH_GAS)
|
|
24
|
-
.times(2)
|
|
25
|
-
.times((0, bignumber_1.bn)(10).exponentiatedBy(gweiGasPrecision))
|
|
26
|
-
.toString();
|
|
27
|
-
const btcEstimate = (gasRate) => (0, bignumber_1.bnOrZero)(gasRate).times(constants_1.THOR_TRADE_FEE_BTC_SIZE).times(2).toString();
|
|
28
9
|
const estimateTradeFee = async (deps, buyAsset) => {
|
|
29
10
|
const thorId = caip_1.adapters.assetIdToPoolAssetId({ assetId: buyAsset.assetId });
|
|
30
11
|
if (!thorId)
|
|
@@ -41,7 +22,7 @@ const estimateTradeFee = async (deps, buyAsset) => {
|
|
|
41
22
|
details: { thorPoolChainId }
|
|
42
23
|
});
|
|
43
24
|
const gasRate = inboundInfo.gas_rate;
|
|
44
|
-
const { chainId: buyChainId
|
|
25
|
+
const { chainId: buyChainId } = (0, caip_1.fromAssetId)(buyAsset.assetId);
|
|
45
26
|
const buyAdapter = deps.adapterManager.get(buyChainId);
|
|
46
27
|
if (!buyAdapter)
|
|
47
28
|
throw new api_1.SwapError('[estimateTradeFee] - unable to get buy asset adapter', {
|
|
@@ -54,33 +35,17 @@ const estimateTradeFee = async (deps, buyAsset) => {
|
|
|
54
35
|
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
55
36
|
details: { buyAssetId: buyAsset.assetId }
|
|
56
37
|
});
|
|
57
|
-
const
|
|
38
|
+
const buyFeeAssetRatio = buyAsset.assetId !== buyFeeAssetId
|
|
58
39
|
? await (0, getPriceRatio_1.getPriceRatio)(deps, {
|
|
59
40
|
sellAssetId: buyAsset.assetId,
|
|
60
41
|
buyAssetId: buyFeeAssetId
|
|
61
42
|
})
|
|
62
43
|
: '1';
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
case 'slip44':
|
|
70
|
-
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(ethEstimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
|
|
71
|
-
case 'erc20':
|
|
72
|
-
return (0, bignumber_1.fromBaseUnit)((0, bignumber_1.bnOrZero)(erc20Estimate(gasRate)).times(feeAssetRatio).dp(0), buyFeeAsset.precision);
|
|
73
|
-
default:
|
|
74
|
-
throw new api_1.SwapError('[estimateTradeFee] - unsupported asset namespace', {
|
|
75
|
-
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
76
|
-
details: { assetNamespace }
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
default:
|
|
80
|
-
throw new api_1.SwapError('[estimateTradeFee] - unsupported chain id', {
|
|
81
|
-
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
82
|
-
details: { chainId: buyChainId }
|
|
83
|
-
});
|
|
84
|
-
}
|
|
44
|
+
if (!constants_1.THOR_TRADE_FEE_MULTIPLIERS[buyChainId])
|
|
45
|
+
throw new api_1.SwapError('[estimateTradeFee] - no trade fee multiplier', {
|
|
46
|
+
code: api_1.SwapErrorTypes.VALIDATION_FAILED,
|
|
47
|
+
details: { buyChainId }
|
|
48
|
+
});
|
|
49
|
+
return constants_1.THOR_TRADE_FEE_MULTIPLIERS[buyChainId].times(buyFeeAssetRatio).times(gasRate).toString();
|
|
85
50
|
};
|
|
86
51
|
exports.estimateTradeFee = estimateTradeFee;
|
package/package.json
CHANGED