@xchainjs/xchain-thorchain-query 0.1.10 → 0.1.12
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/lib/index.esm.js +57 -45
- package/lib/index.js +141 -129
- package/lib/thorchain-checktx.d.ts +1 -1
- package/lib/thorchain-query.d.ts +3 -2
- package/package.json +23 -5
package/lib/index.esm.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BNBChain, AssetBNB } from '@xchainjs/xchain-binance';
|
|
2
|
+
import { BTCChain, AssetBTC } from '@xchainjs/xchain-bitcoin';
|
|
3
|
+
import { GAIAChain, AssetATOM } from '@xchainjs/xchain-cosmos';
|
|
4
|
+
import { AssetRuneNative, THORChain, isAssetRuneNative } from '@xchainjs/xchain-thorchain';
|
|
5
|
+
import { assetToBase, formatAssetAmountCurrency, baseToAsset, eqAsset, assetToString, assetFromString, baseAmount, assetFromStringEx, getContractAddressFromAsset, assetAmount } from '@xchainjs/xchain-util';
|
|
2
6
|
import { BigNumber } from 'bignumber.js';
|
|
3
7
|
import { Network } from '@xchainjs/xchain-client';
|
|
4
8
|
import { MidgardApi, Configuration } from '@xchainjs/xchain-midgard';
|
|
5
9
|
import axios from 'axios';
|
|
6
10
|
import axiosRetry from 'axios-retry';
|
|
11
|
+
import { AVAXChain, AssetAVAX } from '@xchainjs/xchain-avax';
|
|
12
|
+
import { AssetBCH, BCHChain } from '@xchainjs/xchain-bitcoincash';
|
|
13
|
+
import { DOGEChain, AssetDOGE } from '@xchainjs/xchain-doge';
|
|
14
|
+
import { ETHChain, AssetETH } from '@xchainjs/xchain-ethereum';
|
|
15
|
+
import { LTCChain, AssetLTC } from '@xchainjs/xchain-litecoin';
|
|
7
16
|
import { TransactionsApi, Configuration as Configuration$1, QueueApi, NetworkApi, PoolsApi, LiquidityProvidersApi, SaversApi, QuoteApi } from '@xchainjs/xchain-thornode';
|
|
8
|
-
import { baseAmount as baseAmount$1, AssetRuneNative as AssetRuneNative$1 } from '@xchainjs/xchain-util/lib';
|
|
9
17
|
|
|
10
18
|
/*! *****************************************************************************
|
|
11
19
|
Copyright (c) Microsoft Corporation.
|
|
@@ -546,19 +554,19 @@ const calcNetworkFee = (asset, inbound) => {
|
|
|
546
554
|
if (asset.synth)
|
|
547
555
|
return new CryptoAmount(baseAmount(2000000), AssetRuneNative);
|
|
548
556
|
switch (asset.chain) {
|
|
549
|
-
case
|
|
557
|
+
case BTCChain:
|
|
550
558
|
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetBTC);
|
|
551
|
-
case
|
|
559
|
+
case BTCChain:
|
|
552
560
|
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetBCH);
|
|
553
|
-
case
|
|
561
|
+
case LTCChain:
|
|
554
562
|
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetLTC);
|
|
555
|
-
case
|
|
563
|
+
case DOGEChain:
|
|
556
564
|
// NOTE: UTXO chains estimate fees with a 250 byte size
|
|
557
565
|
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetDOGE);
|
|
558
|
-
case
|
|
566
|
+
case BNBChain:
|
|
559
567
|
//flat fee
|
|
560
568
|
return new CryptoAmount(baseAmount(inbound.gasRate), AssetBNB);
|
|
561
|
-
case
|
|
569
|
+
case ETHChain:
|
|
562
570
|
const gasRateinETHGwei = inbound.gasRate;
|
|
563
571
|
const gasRateinETHWei = baseAmount(gasRateinETHGwei.multipliedBy(Math.pow(10, 9)), 18);
|
|
564
572
|
if (eqAsset(asset, AssetETH)) {
|
|
@@ -567,7 +575,7 @@ const calcNetworkFee = (asset, inbound) => {
|
|
|
567
575
|
else {
|
|
568
576
|
return new CryptoAmount(gasRateinETHWei.times(70000), AssetETH);
|
|
569
577
|
}
|
|
570
|
-
case
|
|
578
|
+
case AVAXChain:
|
|
571
579
|
const gasRateinAVAXGwei = inbound.gasRate;
|
|
572
580
|
const gasRateinAVAXWei = baseAmount(gasRateinAVAXGwei.multipliedBy(Math.pow(10, 9)), 18);
|
|
573
581
|
if (eqAsset(asset, AssetAVAX)) {
|
|
@@ -576,9 +584,9 @@ const calcNetworkFee = (asset, inbound) => {
|
|
|
576
584
|
else {
|
|
577
585
|
return new CryptoAmount(gasRateinAVAXWei.times(70000), AssetAVAX);
|
|
578
586
|
}
|
|
579
|
-
case
|
|
580
|
-
return new CryptoAmount(baseAmount(inbound.gasRate),
|
|
581
|
-
case
|
|
587
|
+
case GAIAChain:
|
|
588
|
+
return new CryptoAmount(baseAmount(inbound.gasRate), AssetATOM);
|
|
589
|
+
case THORChain:
|
|
582
590
|
return new CryptoAmount(baseAmount(2000000), AssetRuneNative);
|
|
583
591
|
}
|
|
584
592
|
throw new Error(`could not calculate inbound fee for ${asset.chain}`);
|
|
@@ -596,25 +604,25 @@ const calcOutboundFee = (asset, inbound) => {
|
|
|
596
604
|
if (asset.synth)
|
|
597
605
|
return new CryptoAmount(baseAmount(2000000), AssetRuneNative);
|
|
598
606
|
switch (asset.chain) {
|
|
599
|
-
case
|
|
607
|
+
case BTCChain:
|
|
600
608
|
return new CryptoAmount(baseAmount(inbound.outboundFee), AssetBTC);
|
|
601
|
-
case
|
|
609
|
+
case BTCChain:
|
|
602
610
|
return new CryptoAmount(baseAmount(inbound.outboundFee), AssetBCH);
|
|
603
|
-
case
|
|
611
|
+
case LTCChain:
|
|
604
612
|
return new CryptoAmount(baseAmount(inbound.outboundFee), AssetLTC);
|
|
605
|
-
case
|
|
613
|
+
case DOGEChain:
|
|
606
614
|
// NOTE: UTXO chains estimate fees with a 250 byte size
|
|
607
615
|
return new CryptoAmount(baseAmount(inbound.outboundFee), AssetDOGE);
|
|
608
|
-
case
|
|
616
|
+
case BNBChain:
|
|
609
617
|
//flat fee
|
|
610
618
|
return new CryptoAmount(baseAmount(inbound.outboundFee), AssetBNB);
|
|
611
|
-
case
|
|
619
|
+
case ETHChain:
|
|
612
620
|
return new CryptoAmount(baseAmount(inbound.outboundFee.multipliedBy(Math.pow(10, 9)), 18), AssetETH);
|
|
613
|
-
case
|
|
621
|
+
case AVAXChain:
|
|
614
622
|
return new CryptoAmount(baseAmount(inbound.outboundFee.multipliedBy(Math.pow(10, 9)), 18), AssetAVAX);
|
|
615
|
-
case
|
|
616
|
-
return new CryptoAmount(baseAmount(inbound.outboundFee),
|
|
617
|
-
case
|
|
623
|
+
case GAIAChain:
|
|
624
|
+
return new CryptoAmount(baseAmount(inbound.outboundFee), AssetATOM);
|
|
625
|
+
case THORChain:
|
|
618
626
|
return new CryptoAmount(baseAmount(2000000), AssetRuneNative);
|
|
619
627
|
}
|
|
620
628
|
throw new Error(`could not calculate outbound fee for ${asset.chain}`);
|
|
@@ -634,15 +642,15 @@ const getChainAsset = (chain) => {
|
|
|
634
642
|
return AssetETH;
|
|
635
643
|
case THORChain:
|
|
636
644
|
return AssetRuneNative;
|
|
637
|
-
case
|
|
638
|
-
return
|
|
645
|
+
case GAIAChain:
|
|
646
|
+
return AssetATOM;
|
|
639
647
|
case BCHChain:
|
|
640
648
|
return AssetBCH;
|
|
641
649
|
case LTCChain:
|
|
642
650
|
return AssetLTC;
|
|
643
651
|
case DOGEChain:
|
|
644
652
|
return AssetDOGE;
|
|
645
|
-
case
|
|
653
|
+
case AVAXChain:
|
|
646
654
|
return AssetAVAX;
|
|
647
655
|
default:
|
|
648
656
|
throw Error('Unknown chain');
|
|
@@ -664,7 +672,7 @@ const getChain = (chain) => {
|
|
|
664
672
|
case 'THOR':
|
|
665
673
|
return THORChain;
|
|
666
674
|
case 'GAIA':
|
|
667
|
-
return
|
|
675
|
+
return GAIAChain;
|
|
668
676
|
case 'BCH':
|
|
669
677
|
return BCHChain;
|
|
670
678
|
case 'LTC':
|
|
@@ -1123,7 +1131,7 @@ class ThorchainCache {
|
|
|
1123
1131
|
}
|
|
1124
1132
|
// add mock THORCHAIN inbound details
|
|
1125
1133
|
const details = {
|
|
1126
|
-
chain:
|
|
1134
|
+
chain: THORChain,
|
|
1127
1135
|
address: '',
|
|
1128
1136
|
router: '',
|
|
1129
1137
|
gasRate: new BigNumber(0),
|
|
@@ -1134,7 +1142,7 @@ class ThorchainCache {
|
|
|
1134
1142
|
haltedTrading: !!mimirDetails['HALTTRADING'],
|
|
1135
1143
|
haltedLP: false, //
|
|
1136
1144
|
};
|
|
1137
|
-
inboundDetails[
|
|
1145
|
+
inboundDetails[THORChain] = details;
|
|
1138
1146
|
this.inboundDetailCache = {
|
|
1139
1147
|
lastRefreshed: Date.now(),
|
|
1140
1148
|
inboundDetails,
|
|
@@ -1341,8 +1349,8 @@ const getPoolShare = (unitData, pool) => {
|
|
|
1341
1349
|
const asset = T.times(units).div(total);
|
|
1342
1350
|
const rune = R.times(units).div(total);
|
|
1343
1351
|
const poolShareDetail = {
|
|
1344
|
-
assetShare: new CryptoAmount(baseAmount
|
|
1345
|
-
runeShare: new CryptoAmount(baseAmount
|
|
1352
|
+
assetShare: new CryptoAmount(baseAmount(asset), pool.asset),
|
|
1353
|
+
runeShare: new CryptoAmount(baseAmount(rune), AssetRuneNative),
|
|
1346
1354
|
};
|
|
1347
1355
|
return poolShareDetail;
|
|
1348
1356
|
};
|
|
@@ -1391,7 +1399,7 @@ const getLiquidityProtectionData = (depositValue, poolShare, block) => {
|
|
|
1391
1399
|
const result = coverage.times(protectionProgress); // impermanent loss protection result
|
|
1392
1400
|
const maxILP = result.lt(0) ? new BigNumber(0) : result; // max negative ILP to 0
|
|
1393
1401
|
const ILProtection = {
|
|
1394
|
-
ILProtection: new CryptoAmount(baseAmount
|
|
1402
|
+
ILProtection: new CryptoAmount(baseAmount(maxILP), AssetRuneNative),
|
|
1395
1403
|
totalDays: (fractionOfFullILPProtection * 100).toFixed(2),
|
|
1396
1404
|
};
|
|
1397
1405
|
return ILProtection;
|
|
@@ -1597,6 +1605,15 @@ class ThorchainQuery {
|
|
|
1597
1605
|
return swapEstimate;
|
|
1598
1606
|
});
|
|
1599
1607
|
}
|
|
1608
|
+
abbreviateAssetString(asset) {
|
|
1609
|
+
const contractAddress = getContractAddressFromAsset(asset);
|
|
1610
|
+
if (contractAddress && contractAddress.length > 5) {
|
|
1611
|
+
const abrev = contractAddress.substring(contractAddress.length - 5);
|
|
1612
|
+
const sep = asset.chain !== THORChain && asset.synth ? '/' : '.';
|
|
1613
|
+
return `${asset.chain}${sep}${asset.ticker}-${abrev}`;
|
|
1614
|
+
}
|
|
1615
|
+
return assetToString(asset);
|
|
1616
|
+
}
|
|
1600
1617
|
/**
|
|
1601
1618
|
*
|
|
1602
1619
|
* @param params - swap object
|
|
@@ -1605,21 +1622,16 @@ class ThorchainQuery {
|
|
|
1605
1622
|
constructSwapMemo(params) {
|
|
1606
1623
|
var _a;
|
|
1607
1624
|
const limstring = params.limit.amount().toFixed();
|
|
1608
|
-
// create LIM with interface ID
|
|
1609
1625
|
const lim = limstring.substring(0, limstring.length - 3).concat(params.interfaceID);
|
|
1610
|
-
|
|
1611
|
-
let memo = `=:${assetToString(params.destinationAsset)}`;
|
|
1626
|
+
let memo = `=:${this.abbreviateAssetString(params.destinationAsset)}:${params.destinationAddress}:${lim}`;
|
|
1612
1627
|
// NOTE: we should validate affiliate address is EITHER: a thorname or valid thorchain address, currently we cannot do this without importing xchain-thorchain
|
|
1613
1628
|
if (((_a = params.affiliateAddress) === null || _a === void 0 ? void 0 : _a.length) > 0) {
|
|
1614
1629
|
// NOTE: we should validate destinationAddress address is valid destination address for the asset type requested
|
|
1615
|
-
memo = memo.concat(`:${params.
|
|
1616
|
-
}
|
|
1617
|
-
else {
|
|
1618
|
-
memo = memo.concat(`:${params.destinationAddress}:${lim}`);
|
|
1630
|
+
memo = memo.concat(`:${params.affiliateAddress}:${params.affiliateFeeBasisPoints}`);
|
|
1619
1631
|
}
|
|
1620
1632
|
// If memo length is too long for BTC, trim it
|
|
1621
1633
|
if (eqAsset(params.input.asset, AssetBTC) && memo.length > 80) {
|
|
1622
|
-
memo = `=:${
|
|
1634
|
+
memo = `=:${this.abbreviateAssetString(params.destinationAsset)}:${params.destinationAddress}:${lim}`;
|
|
1623
1635
|
}
|
|
1624
1636
|
return memo;
|
|
1625
1637
|
}
|
|
@@ -1628,7 +1640,7 @@ class ThorchainQuery {
|
|
|
1628
1640
|
// private async validateAffiliateAddress(affiliateAddress: string) {
|
|
1629
1641
|
// // Affiliate address should be THORName or THORAddress
|
|
1630
1642
|
// if (affiliateAddress.length > 0) {
|
|
1631
|
-
// const isValidThorchainAddress = this.clients[
|
|
1643
|
+
// const isValidThorchainAddress = this.clients[THORChain].validateAddress(affiliateAddress)
|
|
1632
1644
|
// const isValidThorname = await this.isThorname(affiliateAddress)
|
|
1633
1645
|
// if (!(isValidThorchainAddress || isValidThorname))
|
|
1634
1646
|
// throw Error(`affiliateAddress ${affiliateAddress} is not a valid THOR address`)
|
|
@@ -1755,10 +1767,10 @@ class ThorchainQuery {
|
|
|
1755
1767
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1756
1768
|
// RUNE, BNB and Synths have near instant finality, so no conf counting required. - need to make a BFT only case.
|
|
1757
1769
|
if (isAssetRuneNative(inbound.asset) ||
|
|
1758
|
-
inbound.asset.chain ==
|
|
1759
|
-
inbound.asset.chain ==
|
|
1770
|
+
inbound.asset.chain == BNBChain ||
|
|
1771
|
+
inbound.asset.chain == GAIAChain ||
|
|
1760
1772
|
inbound.asset.synth) {
|
|
1761
|
-
return this.chainAttributes[
|
|
1773
|
+
return this.chainAttributes[THORChain].avgBlockTimeInSecs;
|
|
1762
1774
|
}
|
|
1763
1775
|
// Get the gas asset for the inbound.asset.chain
|
|
1764
1776
|
const chainGasAsset = getChainAsset(inbound.asset.chain);
|
|
@@ -1789,7 +1801,7 @@ class ThorchainQuery {
|
|
|
1789
1801
|
.amount()
|
|
1790
1802
|
.toNumber();
|
|
1791
1803
|
const getScheduledOutboundValue = yield this.thorchainCache.midgard.getScheduledOutboundValue();
|
|
1792
|
-
const thorChainblocktime = this.chainAttributes[
|
|
1804
|
+
const thorChainblocktime = this.chainAttributes[THORChain].avgBlockTimeInSecs; // blocks required to confirm tx
|
|
1793
1805
|
// If asset is equal to Rune set runeValue as outbound amount else set it to the asset's value in rune
|
|
1794
1806
|
const runeValue = yield this.thorchainCache.convert(outboundAmount, AssetRuneNative);
|
|
1795
1807
|
// Check rune value amount
|
|
@@ -2277,7 +2289,7 @@ class TransactionStage {
|
|
|
2277
2289
|
case 1:
|
|
2278
2290
|
if (((_b = (_a = txData.observed_tx) === null || _a === void 0 ? void 0 : _a.tx) === null || _b === void 0 ? void 0 : _b.chain) != undefined) {
|
|
2279
2291
|
sourceChain = getChain(txData.observed_tx.tx.chain);
|
|
2280
|
-
if (sourceChain == (
|
|
2292
|
+
if (sourceChain == (BTCChain || BCHChain || LTCChain)) {
|
|
2281
2293
|
const lastBlockHeight = lastBlock.find((obj) => obj.chain === sourceChain);
|
|
2282
2294
|
const checkConf = yield this.checkConfcounting(sourceChain, lastBlockHeight, txData.observed_tx);
|
|
2283
2295
|
transactionProgress.seconds = checkConf.seconds;
|
package/lib/index.js
CHANGED
|
@@ -2,14 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var xchainBinance = require('@xchainjs/xchain-binance');
|
|
6
|
+
var xchainBitcoin = require('@xchainjs/xchain-bitcoin');
|
|
7
|
+
var xchainCosmos = require('@xchainjs/xchain-cosmos');
|
|
8
|
+
var xchainThorchain = require('@xchainjs/xchain-thorchain');
|
|
5
9
|
var xchainUtil = require('@xchainjs/xchain-util');
|
|
6
10
|
var bignumber_js = require('bignumber.js');
|
|
7
11
|
var xchainClient = require('@xchainjs/xchain-client');
|
|
8
12
|
var xchainMidgard = require('@xchainjs/xchain-midgard');
|
|
9
13
|
var axios = require('axios');
|
|
10
14
|
var axiosRetry = require('axios-retry');
|
|
15
|
+
var xchainAvax = require('@xchainjs/xchain-avax');
|
|
16
|
+
var xchainBitcoincash = require('@xchainjs/xchain-bitcoincash');
|
|
17
|
+
var xchainDoge = require('@xchainjs/xchain-doge');
|
|
18
|
+
var xchainEthereum = require('@xchainjs/xchain-ethereum');
|
|
19
|
+
var xchainLitecoin = require('@xchainjs/xchain-litecoin');
|
|
11
20
|
var xchainThornode = require('@xchainjs/xchain-thornode');
|
|
12
|
-
var lib = require('@xchainjs/xchain-util/lib');
|
|
13
21
|
|
|
14
22
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
15
23
|
|
|
@@ -278,7 +286,7 @@ class Midgard {
|
|
|
278
286
|
for (const baseUrl of this.config.midgardBaseUrls) {
|
|
279
287
|
try {
|
|
280
288
|
const { data } = yield axios__default['default'].get(`${baseUrl}${path}`);
|
|
281
|
-
const value = new CryptoAmount(xchainUtil.baseAmount(data['scheduled_outbound_value']),
|
|
289
|
+
const value = new CryptoAmount(xchainUtil.baseAmount(data['scheduled_outbound_value']), xchainThorchain.AssetRuneNative);
|
|
282
290
|
return value;
|
|
283
291
|
}
|
|
284
292
|
catch (e) {
|
|
@@ -436,7 +444,7 @@ const getSwapFee = (inputAmount, pool, toRune) => {
|
|
|
436
444
|
const x = getBaseAmountWithDiffDecimals(inputAmount, 8);
|
|
437
445
|
const X = toRune ? pool.assetBalance.amount() : pool.runeBalance.amount(); // input is asset if toRune
|
|
438
446
|
const Y = toRune ? pool.runeBalance.amount() : pool.assetBalance.amount(); // output is rune if toRune
|
|
439
|
-
const units = toRune ?
|
|
447
|
+
const units = toRune ? xchainThorchain.AssetRuneNative : pool.asset;
|
|
440
448
|
const numerator = x.times(x).multipliedBy(Y);
|
|
441
449
|
const denominator = x.plus(X).pow(2);
|
|
442
450
|
const result = numerator.div(denominator);
|
|
@@ -475,7 +483,7 @@ const getSwapOutput = (inputAmount, pool, toRune) => {
|
|
|
475
483
|
const x = getBaseAmountWithDiffDecimals(inputAmount, 8);
|
|
476
484
|
const X = toRune ? pool.assetBalance.amount() : pool.runeBalance.amount(); // input is asset if toRune
|
|
477
485
|
const Y = toRune ? pool.runeBalance.amount() : pool.assetBalance.amount(); // output is rune if toRune
|
|
478
|
-
const units = toRune ?
|
|
486
|
+
const units = toRune ? xchainThorchain.AssetRuneNative : pool.asset;
|
|
479
487
|
// const decimals = toRune || !pool.decimals ? 8 : pool.decimals
|
|
480
488
|
const numerator = x.times(X).times(Y);
|
|
481
489
|
const denominator = x.plus(X).pow(2);
|
|
@@ -520,7 +528,7 @@ const getDoubleSwapFee = (inputAmount, pool1, pool2, thorchainCache) => __awaite
|
|
|
520
528
|
const fee1InRune = getSwapFee(inputAmount, pool1, true);
|
|
521
529
|
const swapOutput = getSwapOutput(inputAmount, pool1, true);
|
|
522
530
|
const fee2InAsset = getSwapFee(swapOutput, pool2, false);
|
|
523
|
-
const fee2InRune = yield thorchainCache.convert(fee2InAsset,
|
|
531
|
+
const fee2InRune = yield thorchainCache.convert(fee2InAsset, xchainThorchain.AssetRuneNative);
|
|
524
532
|
const result = fee1InRune.plus(fee2InRune);
|
|
525
533
|
return result;
|
|
526
534
|
});
|
|
@@ -553,42 +561,42 @@ const getDoubleSwap = (inputAmount, pool1, pool2, thorchainCache) => __awaiter(v
|
|
|
553
561
|
*/
|
|
554
562
|
const calcNetworkFee = (asset, inbound) => {
|
|
555
563
|
if (asset.synth)
|
|
556
|
-
return new CryptoAmount(xchainUtil.baseAmount(2000000),
|
|
564
|
+
return new CryptoAmount(xchainUtil.baseAmount(2000000), xchainThorchain.AssetRuneNative);
|
|
557
565
|
switch (asset.chain) {
|
|
558
|
-
case
|
|
559
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)),
|
|
560
|
-
case
|
|
561
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)),
|
|
562
|
-
case
|
|
563
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)),
|
|
564
|
-
case
|
|
566
|
+
case xchainBitcoin.BTCChain:
|
|
567
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), xchainBitcoin.AssetBTC);
|
|
568
|
+
case xchainBitcoin.BTCChain:
|
|
569
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), xchainBitcoincash.AssetBCH);
|
|
570
|
+
case xchainLitecoin.LTCChain:
|
|
571
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), xchainLitecoin.AssetLTC);
|
|
572
|
+
case xchainDoge.DOGEChain:
|
|
565
573
|
// NOTE: UTXO chains estimate fees with a 250 byte size
|
|
566
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)),
|
|
567
|
-
case
|
|
574
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), xchainDoge.AssetDOGE);
|
|
575
|
+
case xchainBinance.BNBChain:
|
|
568
576
|
//flat fee
|
|
569
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate),
|
|
570
|
-
case
|
|
577
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate), xchainBinance.AssetBNB);
|
|
578
|
+
case xchainEthereum.ETHChain:
|
|
571
579
|
const gasRateinETHGwei = inbound.gasRate;
|
|
572
580
|
const gasRateinETHWei = xchainUtil.baseAmount(gasRateinETHGwei.multipliedBy(Math.pow(10, 9)), 18);
|
|
573
|
-
if (xchainUtil.eqAsset(asset,
|
|
574
|
-
return new CryptoAmount(gasRateinETHWei.times(21000),
|
|
581
|
+
if (xchainUtil.eqAsset(asset, xchainEthereum.AssetETH)) {
|
|
582
|
+
return new CryptoAmount(gasRateinETHWei.times(21000), xchainEthereum.AssetETH);
|
|
575
583
|
}
|
|
576
584
|
else {
|
|
577
|
-
return new CryptoAmount(gasRateinETHWei.times(70000),
|
|
585
|
+
return new CryptoAmount(gasRateinETHWei.times(70000), xchainEthereum.AssetETH);
|
|
578
586
|
}
|
|
579
|
-
case
|
|
587
|
+
case xchainAvax.AVAXChain:
|
|
580
588
|
const gasRateinAVAXGwei = inbound.gasRate;
|
|
581
589
|
const gasRateinAVAXWei = xchainUtil.baseAmount(gasRateinAVAXGwei.multipliedBy(Math.pow(10, 9)), 18);
|
|
582
|
-
if (xchainUtil.eqAsset(asset,
|
|
583
|
-
return new CryptoAmount(gasRateinAVAXWei.times(21000),
|
|
590
|
+
if (xchainUtil.eqAsset(asset, xchainAvax.AssetAVAX)) {
|
|
591
|
+
return new CryptoAmount(gasRateinAVAXWei.times(21000), xchainAvax.AssetAVAX);
|
|
584
592
|
}
|
|
585
593
|
else {
|
|
586
|
-
return new CryptoAmount(gasRateinAVAXWei.times(70000),
|
|
594
|
+
return new CryptoAmount(gasRateinAVAXWei.times(70000), xchainAvax.AssetAVAX);
|
|
587
595
|
}
|
|
588
|
-
case
|
|
589
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate),
|
|
590
|
-
case
|
|
591
|
-
return new CryptoAmount(xchainUtil.baseAmount(2000000),
|
|
596
|
+
case xchainCosmos.GAIAChain:
|
|
597
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.gasRate), xchainCosmos.AssetATOM);
|
|
598
|
+
case xchainThorchain.THORChain:
|
|
599
|
+
return new CryptoAmount(xchainUtil.baseAmount(2000000), xchainThorchain.AssetRuneNative);
|
|
592
600
|
}
|
|
593
601
|
throw new Error(`could not calculate inbound fee for ${asset.chain}`);
|
|
594
602
|
};
|
|
@@ -603,28 +611,28 @@ const calcNetworkFee = (asset, inbound) => {
|
|
|
603
611
|
*/
|
|
604
612
|
const calcOutboundFee = (asset, inbound) => {
|
|
605
613
|
if (asset.synth)
|
|
606
|
-
return new CryptoAmount(xchainUtil.baseAmount(2000000),
|
|
614
|
+
return new CryptoAmount(xchainUtil.baseAmount(2000000), xchainThorchain.AssetRuneNative);
|
|
607
615
|
switch (asset.chain) {
|
|
608
|
-
case
|
|
609
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee),
|
|
610
|
-
case
|
|
611
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee),
|
|
612
|
-
case
|
|
613
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee),
|
|
614
|
-
case
|
|
616
|
+
case xchainBitcoin.BTCChain:
|
|
617
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee), xchainBitcoin.AssetBTC);
|
|
618
|
+
case xchainBitcoin.BTCChain:
|
|
619
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee), xchainBitcoincash.AssetBCH);
|
|
620
|
+
case xchainLitecoin.LTCChain:
|
|
621
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee), xchainLitecoin.AssetLTC);
|
|
622
|
+
case xchainDoge.DOGEChain:
|
|
615
623
|
// NOTE: UTXO chains estimate fees with a 250 byte size
|
|
616
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee),
|
|
617
|
-
case
|
|
624
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee), xchainDoge.AssetDOGE);
|
|
625
|
+
case xchainBinance.BNBChain:
|
|
618
626
|
//flat fee
|
|
619
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee),
|
|
620
|
-
case
|
|
621
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee.multipliedBy(Math.pow(10, 9)), 18),
|
|
622
|
-
case
|
|
623
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee.multipliedBy(Math.pow(10, 9)), 18),
|
|
624
|
-
case
|
|
625
|
-
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee),
|
|
626
|
-
case
|
|
627
|
-
return new CryptoAmount(xchainUtil.baseAmount(2000000),
|
|
627
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee), xchainBinance.AssetBNB);
|
|
628
|
+
case xchainEthereum.ETHChain:
|
|
629
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee.multipliedBy(Math.pow(10, 9)), 18), xchainEthereum.AssetETH);
|
|
630
|
+
case xchainAvax.AVAXChain:
|
|
631
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee.multipliedBy(Math.pow(10, 9)), 18), xchainAvax.AssetAVAX);
|
|
632
|
+
case xchainCosmos.GAIAChain:
|
|
633
|
+
return new CryptoAmount(xchainUtil.baseAmount(inbound.outboundFee), xchainCosmos.AssetATOM);
|
|
634
|
+
case xchainThorchain.THORChain:
|
|
635
|
+
return new CryptoAmount(xchainUtil.baseAmount(2000000), xchainThorchain.AssetRuneNative);
|
|
628
636
|
}
|
|
629
637
|
throw new Error(`could not calculate outbound fee for ${asset.chain}`);
|
|
630
638
|
};
|
|
@@ -635,24 +643,24 @@ const calcOutboundFee = (asset, inbound) => {
|
|
|
635
643
|
*/
|
|
636
644
|
const getChainAsset = (chain) => {
|
|
637
645
|
switch (chain) {
|
|
638
|
-
case
|
|
639
|
-
return
|
|
640
|
-
case
|
|
641
|
-
return
|
|
642
|
-
case
|
|
643
|
-
return
|
|
644
|
-
case
|
|
645
|
-
return
|
|
646
|
-
case
|
|
647
|
-
return
|
|
648
|
-
case
|
|
649
|
-
return
|
|
650
|
-
case
|
|
651
|
-
return
|
|
652
|
-
case
|
|
653
|
-
return
|
|
654
|
-
case
|
|
655
|
-
return
|
|
646
|
+
case xchainBinance.BNBChain:
|
|
647
|
+
return xchainBinance.AssetBNB;
|
|
648
|
+
case xchainBitcoin.BTCChain:
|
|
649
|
+
return xchainBitcoin.AssetBTC;
|
|
650
|
+
case xchainEthereum.ETHChain:
|
|
651
|
+
return xchainEthereum.AssetETH;
|
|
652
|
+
case xchainThorchain.THORChain:
|
|
653
|
+
return xchainThorchain.AssetRuneNative;
|
|
654
|
+
case xchainCosmos.GAIAChain:
|
|
655
|
+
return xchainCosmos.AssetATOM;
|
|
656
|
+
case xchainBitcoincash.BCHChain:
|
|
657
|
+
return xchainBitcoincash.AssetBCH;
|
|
658
|
+
case xchainLitecoin.LTCChain:
|
|
659
|
+
return xchainLitecoin.AssetLTC;
|
|
660
|
+
case xchainDoge.DOGEChain:
|
|
661
|
+
return xchainDoge.AssetDOGE;
|
|
662
|
+
case xchainAvax.AVAXChain:
|
|
663
|
+
return xchainAvax.AssetAVAX;
|
|
656
664
|
default:
|
|
657
665
|
throw Error('Unknown chain');
|
|
658
666
|
}
|
|
@@ -665,21 +673,21 @@ const getChainAsset = (chain) => {
|
|
|
665
673
|
const getChain = (chain) => {
|
|
666
674
|
switch (chain) {
|
|
667
675
|
case 'BNB':
|
|
668
|
-
return
|
|
676
|
+
return xchainBinance.BNBChain;
|
|
669
677
|
case 'BTC':
|
|
670
|
-
return
|
|
678
|
+
return xchainBitcoin.BTCChain;
|
|
671
679
|
case 'ETH':
|
|
672
|
-
return
|
|
680
|
+
return xchainEthereum.ETHChain;
|
|
673
681
|
case 'THOR':
|
|
674
|
-
return
|
|
682
|
+
return xchainThorchain.THORChain;
|
|
675
683
|
case 'GAIA':
|
|
676
|
-
return
|
|
684
|
+
return xchainCosmos.GAIAChain;
|
|
677
685
|
case 'BCH':
|
|
678
|
-
return
|
|
686
|
+
return xchainBitcoincash.BCHChain;
|
|
679
687
|
case 'LTC':
|
|
680
|
-
return
|
|
688
|
+
return xchainLitecoin.LTCChain;
|
|
681
689
|
case 'DOGE':
|
|
682
|
-
return
|
|
690
|
+
return xchainDoge.DOGEChain;
|
|
683
691
|
default:
|
|
684
692
|
throw Error('Unknown chain');
|
|
685
693
|
}
|
|
@@ -1001,12 +1009,12 @@ class ThorchainCache {
|
|
|
1001
1009
|
if (xchainUtil.eqAsset(from, to)) {
|
|
1002
1010
|
exchangeRate = SAME_ASSET_EXCHANGE_RATE;
|
|
1003
1011
|
}
|
|
1004
|
-
else if (
|
|
1012
|
+
else if (xchainThorchain.isAssetRuneNative(from)) {
|
|
1005
1013
|
// Runes per Asset
|
|
1006
1014
|
const lpTo = yield this.getPoolForAsset(to);
|
|
1007
1015
|
exchangeRate = lpTo.assetToRuneRatio;
|
|
1008
1016
|
}
|
|
1009
|
-
else if (
|
|
1017
|
+
else if (xchainThorchain.isAssetRuneNative(to)) {
|
|
1010
1018
|
// Asset per rune
|
|
1011
1019
|
const lpFrom = yield this.getPoolForAsset(from);
|
|
1012
1020
|
exchangeRate = lpFrom.runeToAssetRatio;
|
|
@@ -1030,7 +1038,7 @@ class ThorchainCache {
|
|
|
1030
1038
|
*/
|
|
1031
1039
|
getPoolForAsset(asset) {
|
|
1032
1040
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1033
|
-
if (
|
|
1041
|
+
if (xchainThorchain.isAssetRuneNative(asset))
|
|
1034
1042
|
throw Error(`AssetRuneNative doesn't have a pool`);
|
|
1035
1043
|
const pools = yield this.getPools();
|
|
1036
1044
|
// Note: we use ticker, not asset string to get the same pool for both assets and synths
|
|
@@ -1132,7 +1140,7 @@ class ThorchainCache {
|
|
|
1132
1140
|
}
|
|
1133
1141
|
// add mock THORCHAIN inbound details
|
|
1134
1142
|
const details = {
|
|
1135
|
-
chain:
|
|
1143
|
+
chain: xchainThorchain.THORChain,
|
|
1136
1144
|
address: '',
|
|
1137
1145
|
router: '',
|
|
1138
1146
|
gasRate: new bignumber_js.BigNumber(0),
|
|
@@ -1143,7 +1151,7 @@ class ThorchainCache {
|
|
|
1143
1151
|
haltedTrading: !!mimirDetails['HALTTRADING'],
|
|
1144
1152
|
haltedLP: false, //
|
|
1145
1153
|
};
|
|
1146
|
-
inboundDetails[
|
|
1154
|
+
inboundDetails[xchainThorchain.THORChain] = details;
|
|
1147
1155
|
this.inboundDetailCache = {
|
|
1148
1156
|
lastRefreshed: Date.now(),
|
|
1149
1157
|
inboundDetails,
|
|
@@ -1180,12 +1188,12 @@ class ThorchainCache {
|
|
|
1180
1188
|
getExpectedSwapOutput(inputAmount, destinationAsset) {
|
|
1181
1189
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1182
1190
|
let swapOutput;
|
|
1183
|
-
if (
|
|
1191
|
+
if (xchainThorchain.isAssetRuneNative(inputAmount.asset)) {
|
|
1184
1192
|
//singleswap from rune -> asset
|
|
1185
1193
|
const pool = yield this.getPoolForAsset(destinationAsset);
|
|
1186
1194
|
swapOutput = getSingleSwap(inputAmount, pool, false);
|
|
1187
1195
|
}
|
|
1188
|
-
else if (
|
|
1196
|
+
else if (xchainThorchain.isAssetRuneNative(destinationAsset)) {
|
|
1189
1197
|
//singleswap from asset -> rune
|
|
1190
1198
|
const pool = yield this.getPoolForAsset(inputAmount.asset);
|
|
1191
1199
|
swapOutput = getSingleSwap(inputAmount, pool, true);
|
|
@@ -1228,7 +1236,7 @@ class ThorchainCache {
|
|
|
1228
1236
|
}
|
|
1229
1237
|
getDecimalForAsset(asset) {
|
|
1230
1238
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1231
|
-
if (!
|
|
1239
|
+
if (!xchainThorchain.isAssetRuneNative(asset)) {
|
|
1232
1240
|
const pool = yield this.getPoolForAsset(asset);
|
|
1233
1241
|
const decimals = Number(pool.pool.nativeDecimal);
|
|
1234
1242
|
if (decimals > 0)
|
|
@@ -1350,8 +1358,8 @@ const getPoolShare = (unitData, pool) => {
|
|
|
1350
1358
|
const asset = T.times(units).div(total);
|
|
1351
1359
|
const rune = R.times(units).div(total);
|
|
1352
1360
|
const poolShareDetail = {
|
|
1353
|
-
assetShare: new CryptoAmount(
|
|
1354
|
-
runeShare: new CryptoAmount(
|
|
1361
|
+
assetShare: new CryptoAmount(xchainUtil.baseAmount(asset), pool.asset),
|
|
1362
|
+
runeShare: new CryptoAmount(xchainUtil.baseAmount(rune), xchainThorchain.AssetRuneNative),
|
|
1355
1363
|
};
|
|
1356
1364
|
return poolShareDetail;
|
|
1357
1365
|
};
|
|
@@ -1400,7 +1408,7 @@ const getLiquidityProtectionData = (depositValue, poolShare, block) => {
|
|
|
1400
1408
|
const result = coverage.times(protectionProgress); // impermanent loss protection result
|
|
1401
1409
|
const maxILP = result.lt(0) ? new bignumber_js.BigNumber(0) : result; // max negative ILP to 0
|
|
1402
1410
|
const ILProtection = {
|
|
1403
|
-
ILProtection: new CryptoAmount(
|
|
1411
|
+
ILProtection: new CryptoAmount(xchainUtil.baseAmount(maxILP), xchainThorchain.AssetRuneNative),
|
|
1404
1412
|
totalDays: (fractionOfFullILPProtection * 100).toFixed(2),
|
|
1405
1413
|
};
|
|
1406
1414
|
return ILProtection;
|
|
@@ -1518,7 +1526,7 @@ class ThorchainQuery {
|
|
|
1518
1526
|
isValidSwap(params) {
|
|
1519
1527
|
var _a, _b;
|
|
1520
1528
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1521
|
-
if (
|
|
1529
|
+
if (xchainThorchain.isAssetRuneNative(params.input.asset)) {
|
|
1522
1530
|
if (params.input.baseAmount.decimal !== 8)
|
|
1523
1531
|
throw Error(`input asset ${xchainUtil.assetToString(params.input.asset)} must have decimals of 8`);
|
|
1524
1532
|
}
|
|
@@ -1572,15 +1580,15 @@ class ThorchainQuery {
|
|
|
1572
1580
|
const affiliateFeePercent = params.affiliateFeeBasisPoints ? params.affiliateFeeBasisPoints / 10000 : 0;
|
|
1573
1581
|
const affiliateFeeInAsset = inputMinusInboundFeeInAsset.times(affiliateFeePercent);
|
|
1574
1582
|
let affiliateFeeSwapOutputInRune;
|
|
1575
|
-
if (
|
|
1583
|
+
if (xchainThorchain.isAssetRuneNative(affiliateFeeInAsset.asset)) {
|
|
1576
1584
|
affiliateFeeSwapOutputInRune = {
|
|
1577
1585
|
output: affiliateFeeInAsset,
|
|
1578
|
-
swapFee: new CryptoAmount(xchainUtil.baseAmount(0),
|
|
1586
|
+
swapFee: new CryptoAmount(xchainUtil.baseAmount(0), xchainThorchain.AssetRuneNative),
|
|
1579
1587
|
slip: new bignumber_js.BigNumber(0),
|
|
1580
1588
|
};
|
|
1581
1589
|
}
|
|
1582
1590
|
else {
|
|
1583
|
-
affiliateFeeSwapOutputInRune = yield this.thorchainCache.getExpectedSwapOutput(affiliateFeeInAsset,
|
|
1591
|
+
affiliateFeeSwapOutputInRune = yield this.thorchainCache.getExpectedSwapOutput(affiliateFeeInAsset, xchainThorchain.AssetRuneNative);
|
|
1584
1592
|
}
|
|
1585
1593
|
// remove the affiliate fee from the input.
|
|
1586
1594
|
const inputNetInAsset = inputMinusInboundFeeInAsset.minus(affiliateFeeInAsset);
|
|
@@ -1606,6 +1614,15 @@ class ThorchainQuery {
|
|
|
1606
1614
|
return swapEstimate;
|
|
1607
1615
|
});
|
|
1608
1616
|
}
|
|
1617
|
+
abbreviateAssetString(asset) {
|
|
1618
|
+
const contractAddress = xchainUtil.getContractAddressFromAsset(asset);
|
|
1619
|
+
if (contractAddress && contractAddress.length > 5) {
|
|
1620
|
+
const abrev = contractAddress.substring(contractAddress.length - 5);
|
|
1621
|
+
const sep = asset.chain !== xchainThorchain.THORChain && asset.synth ? '/' : '.';
|
|
1622
|
+
return `${asset.chain}${sep}${asset.ticker}-${abrev}`;
|
|
1623
|
+
}
|
|
1624
|
+
return xchainUtil.assetToString(asset);
|
|
1625
|
+
}
|
|
1609
1626
|
/**
|
|
1610
1627
|
*
|
|
1611
1628
|
* @param params - swap object
|
|
@@ -1614,21 +1631,16 @@ class ThorchainQuery {
|
|
|
1614
1631
|
constructSwapMemo(params) {
|
|
1615
1632
|
var _a;
|
|
1616
1633
|
const limstring = params.limit.amount().toFixed();
|
|
1617
|
-
// create LIM with interface ID
|
|
1618
1634
|
const lim = limstring.substring(0, limstring.length - 3).concat(params.interfaceID);
|
|
1619
|
-
|
|
1620
|
-
let memo = `=:${xchainUtil.assetToString(params.destinationAsset)}`;
|
|
1635
|
+
let memo = `=:${this.abbreviateAssetString(params.destinationAsset)}:${params.destinationAddress}:${lim}`;
|
|
1621
1636
|
// NOTE: we should validate affiliate address is EITHER: a thorname or valid thorchain address, currently we cannot do this without importing xchain-thorchain
|
|
1622
1637
|
if (((_a = params.affiliateAddress) === null || _a === void 0 ? void 0 : _a.length) > 0) {
|
|
1623
1638
|
// NOTE: we should validate destinationAddress address is valid destination address for the asset type requested
|
|
1624
|
-
memo = memo.concat(`:${params.
|
|
1625
|
-
}
|
|
1626
|
-
else {
|
|
1627
|
-
memo = memo.concat(`:${params.destinationAddress}:${lim}`);
|
|
1639
|
+
memo = memo.concat(`:${params.affiliateAddress}:${params.affiliateFeeBasisPoints}`);
|
|
1628
1640
|
}
|
|
1629
1641
|
// If memo length is too long for BTC, trim it
|
|
1630
|
-
if (xchainUtil.eqAsset(params.input.asset,
|
|
1631
|
-
memo = `=:${
|
|
1642
|
+
if (xchainUtil.eqAsset(params.input.asset, xchainBitcoin.AssetBTC) && memo.length > 80) {
|
|
1643
|
+
memo = `=:${this.abbreviateAssetString(params.destinationAsset)}:${params.destinationAddress}:${lim}`;
|
|
1632
1644
|
}
|
|
1633
1645
|
return memo;
|
|
1634
1646
|
}
|
|
@@ -1637,7 +1649,7 @@ class ThorchainQuery {
|
|
|
1637
1649
|
// private async validateAffiliateAddress(affiliateAddress: string) {
|
|
1638
1650
|
// // Affiliate address should be THORName or THORAddress
|
|
1639
1651
|
// if (affiliateAddress.length > 0) {
|
|
1640
|
-
// const isValidThorchainAddress = this.clients[
|
|
1652
|
+
// const isValidThorchainAddress = this.clients[THORChain].validateAddress(affiliateAddress)
|
|
1641
1653
|
// const isValidThorname = await this.isThorname(affiliateAddress)
|
|
1642
1654
|
// if (!(isValidThorchainAddress || isValidThorname))
|
|
1643
1655
|
// throw Error(`affiliateAddress ${affiliateAddress} is not a valid THOR address`)
|
|
@@ -1665,12 +1677,12 @@ class ThorchainQuery {
|
|
|
1665
1677
|
const errors = [];
|
|
1666
1678
|
const sourceAsset = params.input.asset;
|
|
1667
1679
|
const destAsset = params.destinationAsset;
|
|
1668
|
-
if (!
|
|
1680
|
+
if (!xchainThorchain.isAssetRuneNative(sourceAsset)) {
|
|
1669
1681
|
const sourcePool = yield this.thorchainCache.getPoolForAsset(sourceAsset);
|
|
1670
1682
|
if (!sourcePool.isAvailable())
|
|
1671
1683
|
errors.push(`sourceAsset ${sourceAsset.ticker} does not have a valid liquidity pool`);
|
|
1672
1684
|
}
|
|
1673
|
-
if (!
|
|
1685
|
+
if (!xchainThorchain.isAssetRuneNative(destAsset)) {
|
|
1674
1686
|
const destPool = yield this.thorchainCache.getPoolForAsset(destAsset);
|
|
1675
1687
|
if (!destPool.isAvailable())
|
|
1676
1688
|
errors.push(`destinationAsset ${destAsset.ticker} does not have a valid liquidity pool`);
|
|
@@ -1704,8 +1716,8 @@ class ThorchainQuery {
|
|
|
1704
1716
|
checkCoverFees(params, estimate) {
|
|
1705
1717
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1706
1718
|
let result = undefined;
|
|
1707
|
-
const inputInRune = yield this.thorchainCache.convert(params.input,
|
|
1708
|
-
const feesInRune = yield this.getFeesIn(estimate.totalFees,
|
|
1719
|
+
const inputInRune = yield this.thorchainCache.convert(params.input, xchainThorchain.AssetRuneNative);
|
|
1720
|
+
const feesInRune = yield this.getFeesIn(estimate.totalFees, xchainThorchain.AssetRuneNative);
|
|
1709
1721
|
const totalSwapFeesInRune = feesInRune.inboundFee
|
|
1710
1722
|
.plus(feesInRune.outboundFee)
|
|
1711
1723
|
.plus(feesInRune.swapFee)
|
|
@@ -1763,11 +1775,11 @@ class ThorchainQuery {
|
|
|
1763
1775
|
confCounting(inbound) {
|
|
1764
1776
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1765
1777
|
// RUNE, BNB and Synths have near instant finality, so no conf counting required. - need to make a BFT only case.
|
|
1766
|
-
if (
|
|
1767
|
-
inbound.asset.chain ==
|
|
1768
|
-
inbound.asset.chain ==
|
|
1778
|
+
if (xchainThorchain.isAssetRuneNative(inbound.asset) ||
|
|
1779
|
+
inbound.asset.chain == xchainBinance.BNBChain ||
|
|
1780
|
+
inbound.asset.chain == xchainCosmos.GAIAChain ||
|
|
1769
1781
|
inbound.asset.synth) {
|
|
1770
|
-
return this.chainAttributes[
|
|
1782
|
+
return this.chainAttributes[xchainThorchain.THORChain].avgBlockTimeInSecs;
|
|
1771
1783
|
}
|
|
1772
1784
|
// Get the gas asset for the inbound.asset.chain
|
|
1773
1785
|
const chainGasAsset = getChainAsset(inbound.asset.chain);
|
|
@@ -1792,15 +1804,15 @@ class ThorchainQuery {
|
|
|
1792
1804
|
outboundDelay(outboundAmount) {
|
|
1793
1805
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1794
1806
|
const networkValues = yield this.thorchainCache.getNetworkValues();
|
|
1795
|
-
const minTxOutVolumeThreshold = new CryptoAmount(xchainUtil.baseAmount(networkValues['MINTXOUTVOLUMETHRESHOLD']),
|
|
1807
|
+
const minTxOutVolumeThreshold = new CryptoAmount(xchainUtil.baseAmount(networkValues['MINTXOUTVOLUMETHRESHOLD']), xchainThorchain.AssetRuneNative);
|
|
1796
1808
|
const maxTxOutOffset = networkValues['MAXTXOUTOFFSET'];
|
|
1797
|
-
let txOutDelayRate = new CryptoAmount(xchainUtil.baseAmount(networkValues['TXOUTDELAYRATE']),
|
|
1809
|
+
let txOutDelayRate = new CryptoAmount(xchainUtil.baseAmount(networkValues['TXOUTDELAYRATE']), xchainThorchain.AssetRuneNative).assetAmount
|
|
1798
1810
|
.amount()
|
|
1799
1811
|
.toNumber();
|
|
1800
1812
|
const getScheduledOutboundValue = yield this.thorchainCache.midgard.getScheduledOutboundValue();
|
|
1801
|
-
const thorChainblocktime = this.chainAttributes[
|
|
1813
|
+
const thorChainblocktime = this.chainAttributes[xchainThorchain.THORChain].avgBlockTimeInSecs; // blocks required to confirm tx
|
|
1802
1814
|
// If asset is equal to Rune set runeValue as outbound amount else set it to the asset's value in rune
|
|
1803
|
-
const runeValue = yield this.thorchainCache.convert(outboundAmount,
|
|
1815
|
+
const runeValue = yield this.thorchainCache.convert(outboundAmount, xchainThorchain.AssetRuneNative);
|
|
1804
1816
|
// Check rune value amount
|
|
1805
1817
|
if (runeValue.lt(minTxOutVolumeThreshold)) {
|
|
1806
1818
|
return thorChainblocktime;
|
|
@@ -1831,7 +1843,7 @@ class ThorchainQuery {
|
|
|
1831
1843
|
const errors = [];
|
|
1832
1844
|
if (params.asset.asset.synth || params.rune.asset.synth)
|
|
1833
1845
|
errors.push('you cannot add liquidity with a synth');
|
|
1834
|
-
if (!
|
|
1846
|
+
if (!xchainThorchain.isAssetRuneNative(params.rune.asset))
|
|
1835
1847
|
errors.push('params.rune must be THOR.RUNE');
|
|
1836
1848
|
const assetPool = yield this.thorchainCache.getPoolForAsset(params.asset.asset);
|
|
1837
1849
|
const lpUnits = getLiquidityUnits({ asset: params.asset, rune: params.rune }, assetPool);
|
|
@@ -1845,7 +1857,7 @@ class ThorchainQuery {
|
|
|
1845
1857
|
const runeWaitTimeSeconds = yield this.confCounting(params.rune);
|
|
1846
1858
|
const waitTimeSeconds = assetWaitTimeSeconds > runeWaitTimeSeconds ? assetWaitTimeSeconds : runeWaitTimeSeconds;
|
|
1847
1859
|
let assetInboundFee = new CryptoAmount(xchainUtil.baseAmount(0), params.asset.asset);
|
|
1848
|
-
let runeInboundFee = new CryptoAmount(xchainUtil.baseAmount(0),
|
|
1860
|
+
let runeInboundFee = new CryptoAmount(xchainUtil.baseAmount(0), xchainThorchain.AssetRuneNative);
|
|
1849
1861
|
if (!params.asset.assetAmount.eq(0)) {
|
|
1850
1862
|
assetInboundFee = calcNetworkFee(params.asset.asset, inboundDetails[params.asset.asset.chain]);
|
|
1851
1863
|
if (assetInboundFee.assetAmount.amount().times(3).gt(params.asset.assetAmount.amount()))
|
|
@@ -1856,7 +1868,7 @@ class ThorchainQuery {
|
|
|
1856
1868
|
if (runeInboundFee.assetAmount.amount().times(3).gt(params.rune.assetAmount.amount()))
|
|
1857
1869
|
errors.push(`Rune amount is less than fees`);
|
|
1858
1870
|
}
|
|
1859
|
-
const totalFees = (yield this.convert(assetInboundFee,
|
|
1871
|
+
const totalFees = (yield this.convert(assetInboundFee, xchainThorchain.AssetRuneNative)).plus(runeInboundFee);
|
|
1860
1872
|
const slip = getSlipOnLiquidity({ asset: params.asset, rune: params.rune }, assetPool);
|
|
1861
1873
|
const estimateLP = {
|
|
1862
1874
|
assetPool: assetPool.pool.asset,
|
|
@@ -1986,9 +1998,9 @@ class ThorchainQuery {
|
|
|
1986
1998
|
}
|
|
1987
1999
|
const allInboundDetails = yield this.thorchainCache.getInboundDetails();
|
|
1988
2000
|
const inboundDetails = allInboundDetails[params.asset.chain];
|
|
1989
|
-
const runeInbound = calcNetworkFee(
|
|
2001
|
+
const runeInbound = calcNetworkFee(xchainThorchain.AssetRuneNative, inboundDetails);
|
|
1990
2002
|
const assetInbound = calcNetworkFee(params.asset, inboundDetails);
|
|
1991
|
-
const runeOutbound = calcOutboundFee(
|
|
2003
|
+
const runeOutbound = calcOutboundFee(xchainThorchain.AssetRuneNative, inboundDetails);
|
|
1992
2004
|
const assetOutbound = calcOutboundFee(params.asset, inboundDetails);
|
|
1993
2005
|
const estimateLP = {
|
|
1994
2006
|
assetAddress: memberDetail.position.asset_address,
|
|
@@ -1998,18 +2010,18 @@ class ThorchainQuery {
|
|
|
1998
2010
|
minToSend: {
|
|
1999
2011
|
rune: dustValues.rune,
|
|
2000
2012
|
asset: dustValues.asset,
|
|
2001
|
-
total: (yield this.convert(dustValues.asset,
|
|
2013
|
+
total: (yield this.convert(dustValues.asset, xchainThorchain.AssetRuneNative)).plus(dustValues.rune),
|
|
2002
2014
|
},
|
|
2003
2015
|
fees: {
|
|
2004
2016
|
rune: runeInbound,
|
|
2005
2017
|
asset: assetInbound,
|
|
2006
|
-
total: (yield this.convert(assetInbound,
|
|
2018
|
+
total: (yield this.convert(assetInbound, xchainThorchain.AssetRuneNative)).plus(runeInbound),
|
|
2007
2019
|
},
|
|
2008
2020
|
},
|
|
2009
2021
|
outboundFee: {
|
|
2010
2022
|
asset: assetOutbound,
|
|
2011
2023
|
rune: runeOutbound,
|
|
2012
|
-
total: (yield this.convert(assetOutbound,
|
|
2024
|
+
total: (yield this.convert(assetOutbound, xchainThorchain.AssetRuneNative)).plus(runeOutbound),
|
|
2013
2025
|
},
|
|
2014
2026
|
assetAmount: poolShare.assetShare,
|
|
2015
2027
|
runeAmount: poolShare.runeShare,
|
|
@@ -2032,8 +2044,8 @@ class ThorchainQuery {
|
|
|
2032
2044
|
switch (asset.chain) {
|
|
2033
2045
|
case 'BNB':
|
|
2034
2046
|
dustValues = {
|
|
2035
|
-
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0.000001)),
|
|
2036
|
-
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)),
|
|
2047
|
+
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0.000001)), xchainBinance.AssetBNB),
|
|
2048
|
+
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), xchainThorchain.AssetRuneNative),
|
|
2037
2049
|
};
|
|
2038
2050
|
return dustValues;
|
|
2039
2051
|
case 'BTC':
|
|
@@ -2042,42 +2054,42 @@ class ThorchainQuery {
|
|
|
2042
2054
|
// 10k sats
|
|
2043
2055
|
dustValues = {
|
|
2044
2056
|
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0.0001)), asset),
|
|
2045
|
-
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)),
|
|
2057
|
+
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), xchainThorchain.AssetRuneNative),
|
|
2046
2058
|
};
|
|
2047
2059
|
return dustValues;
|
|
2048
2060
|
case 'ETH':
|
|
2049
2061
|
// 0 wei
|
|
2050
2062
|
dustValues = {
|
|
2051
2063
|
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), asset),
|
|
2052
|
-
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)),
|
|
2064
|
+
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), xchainThorchain.AssetRuneNative),
|
|
2053
2065
|
};
|
|
2054
2066
|
return dustValues;
|
|
2055
2067
|
case 'THOR':
|
|
2056
2068
|
// 0 Rune
|
|
2057
2069
|
dustValues = {
|
|
2058
2070
|
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), asset),
|
|
2059
|
-
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)),
|
|
2071
|
+
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), xchainThorchain.AssetRuneNative),
|
|
2060
2072
|
};
|
|
2061
2073
|
return dustValues;
|
|
2062
2074
|
case 'GAIA':
|
|
2063
2075
|
// 0 GAIA
|
|
2064
2076
|
dustValues = {
|
|
2065
2077
|
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), asset),
|
|
2066
|
-
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)),
|
|
2078
|
+
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), xchainThorchain.AssetRuneNative),
|
|
2067
2079
|
};
|
|
2068
2080
|
return dustValues;
|
|
2069
2081
|
case 'DOGE':
|
|
2070
2082
|
// 1 million sats
|
|
2071
2083
|
dustValues = {
|
|
2072
2084
|
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0.01)), asset),
|
|
2073
|
-
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)),
|
|
2085
|
+
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), xchainThorchain.AssetRuneNative),
|
|
2074
2086
|
};
|
|
2075
2087
|
return dustValues;
|
|
2076
2088
|
case 'AVAX':
|
|
2077
2089
|
// 0 AVAX
|
|
2078
2090
|
dustValues = {
|
|
2079
2091
|
asset: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), asset),
|
|
2080
|
-
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)),
|
|
2092
|
+
rune: new CryptoAmount(xchainUtil.assetToBase(xchainUtil.assetAmount(0)), xchainThorchain.AssetRuneNative),
|
|
2081
2093
|
};
|
|
2082
2094
|
return dustValues;
|
|
2083
2095
|
default:
|
|
@@ -2151,7 +2163,7 @@ class ThorchainQuery {
|
|
|
2151
2163
|
*/
|
|
2152
2164
|
estimateWithdrawSaver(withdrawParams) {
|
|
2153
2165
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2154
|
-
if (
|
|
2166
|
+
if (xchainThorchain.isAssetRuneNative(withdrawParams.asset) || withdrawParams.asset.synth)
|
|
2155
2167
|
throw Error(`Native Rune and synth assets are not supported only L1's`);
|
|
2156
2168
|
const withdrawQuote = yield this.thorchainCache.thornode.getSaversWithdrawQuote(withdrawParams);
|
|
2157
2169
|
if (!withdrawQuote.expected_amount_out)
|
|
@@ -2286,7 +2298,7 @@ class TransactionStage {
|
|
|
2286
2298
|
case 1:
|
|
2287
2299
|
if (((_b = (_a = txData.observed_tx) === null || _a === void 0 ? void 0 : _a.tx) === null || _b === void 0 ? void 0 : _b.chain) != undefined) {
|
|
2288
2300
|
sourceChain = getChain(txData.observed_tx.tx.chain);
|
|
2289
|
-
if (sourceChain == (
|
|
2301
|
+
if (sourceChain == (xchainBitcoin.BTCChain || xchainBitcoincash.BCHChain || xchainLitecoin.LTCChain)) {
|
|
2290
2302
|
const lastBlockHeight = lastBlock.find((obj) => obj.chain === sourceChain);
|
|
2291
2303
|
const checkConf = yield this.checkConfcounting(sourceChain, lastBlockHeight, txData.observed_tx);
|
|
2292
2304
|
transactionProgress.seconds = checkConf.seconds;
|
|
@@ -2396,7 +2408,7 @@ class TransactionStage {
|
|
|
2396
2408
|
else {
|
|
2397
2409
|
if ((scheduledQueueItem === null || scheduledQueueItem === void 0 ? void 0 : scheduledQueueItem.height) && (lastBlockHeight === null || lastBlockHeight === void 0 ? void 0 : lastBlockHeight.thorchain)) {
|
|
2398
2410
|
stageStatus.seconds =
|
|
2399
|
-
(scheduledQueueItem.height - (lastBlockHeight === null || lastBlockHeight === void 0 ? void 0 : lastBlockHeight.thorchain)) * this.chainAttributes[
|
|
2411
|
+
(scheduledQueueItem.height - (lastBlockHeight === null || lastBlockHeight === void 0 ? void 0 : lastBlockHeight.thorchain)) * this.chainAttributes[xchainThorchain.THORChain].avgBlockTimeInSecs;
|
|
2400
2412
|
}
|
|
2401
2413
|
}
|
|
2402
2414
|
return stageStatus;
|
|
@@ -4,7 +4,7 @@ import { ChainAttributes, TransactionProgress } from './types';
|
|
|
4
4
|
export declare class TransactionStage {
|
|
5
5
|
readonly thorchainCache: ThorchainCache;
|
|
6
6
|
private chainAttributes;
|
|
7
|
-
constructor(thorchainCache: ThorchainCache, chainAttributes?: Record<
|
|
7
|
+
constructor(thorchainCache: ThorchainCache, chainAttributes?: Record<string, ChainAttributes>);
|
|
8
8
|
/**
|
|
9
9
|
*
|
|
10
10
|
* @param inboundTxHash - Input needed to determine the transaction stage
|
package/lib/thorchain-query.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Asset
|
|
1
|
+
import { Asset } from '@xchainjs/xchain-util';
|
|
2
2
|
import { CryptoAmount } from './crypto-amount';
|
|
3
3
|
import { ThorchainCache } from './thorchain-cache';
|
|
4
4
|
import { AddliquidityPosition, ChainAttributes, EstimateAddLP, EstimateAddSaver, EstimateSwapParams, EstimateWithdrawLP, EstimateWithdrawSaver, LiquidityPosition, PoolRatios, SaversPosition, SaversWithdraw, TotalFees, TxDetails, WithdrawLiquidityPosition, getSaver } from './types';
|
|
@@ -17,7 +17,7 @@ export declare class ThorchainQuery {
|
|
|
17
17
|
* @param chainAttributes - atrributes used to calculate waitTime & conf counting
|
|
18
18
|
* @returns ThorchainAMM
|
|
19
19
|
*/
|
|
20
|
-
constructor(thorchainCache?: ThorchainCache, chainAttributes?: Record<
|
|
20
|
+
constructor(thorchainCache?: ThorchainCache, chainAttributes?: Record<string, ChainAttributes>);
|
|
21
21
|
/**
|
|
22
22
|
* Provides a swap estimate for the given swap detail. Will check the params for errors before trying to get the estimate.
|
|
23
23
|
* Uses current pool data, works out inbound and outboud fee, affiliate fees and works out the expected wait time for the swap (in and out)
|
|
@@ -45,6 +45,7 @@ export declare class ThorchainQuery {
|
|
|
45
45
|
* @returns
|
|
46
46
|
*/
|
|
47
47
|
private calcSwapEstimate;
|
|
48
|
+
private abbreviateAssetString;
|
|
48
49
|
/**
|
|
49
50
|
*
|
|
50
51
|
* @param params - swap object
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xchainjs/xchain-thorchain-query",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Thorchain query module that is resposible for estimating swap calculations and add/remove liquidity for thorchain ",
|
|
6
6
|
"keywords": [
|
|
@@ -33,20 +33,38 @@
|
|
|
33
33
|
"postversion": "git push --follow-tags"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@xchainjs/xchain-
|
|
36
|
+
"@xchainjs/xchain-avax": "^0.1.3",
|
|
37
|
+
"@xchainjs/xchain-binance": "^5.6.6",
|
|
38
|
+
"@xchainjs/xchain-bitcoin": "^0.20.7",
|
|
39
|
+
"@xchainjs/xchain-bitcoincash": "^0.15.6",
|
|
40
|
+
"@xchainjs/xchain-client": "^0.13.5",
|
|
41
|
+
"@xchainjs/xchain-cosmos": "^0.20.6",
|
|
42
|
+
"@xchainjs/xchain-doge": "^0.5.6",
|
|
43
|
+
"@xchainjs/xchain-ethereum": "^0.27.6",
|
|
44
|
+
"@xchainjs/xchain-litecoin": "^0.10.8",
|
|
37
45
|
"@xchainjs/xchain-midgard": "^0.4.0",
|
|
46
|
+
"@xchainjs/xchain-thorchain": "^0.27.7",
|
|
38
47
|
"@xchainjs/xchain-thornode": "^0.1.3",
|
|
39
|
-
"@xchainjs/xchain-util": "^0.
|
|
48
|
+
"@xchainjs/xchain-util": "^0.12.0",
|
|
40
49
|
"axios": "^0.25.0",
|
|
41
50
|
"axios-retry": "^3.2.5",
|
|
42
51
|
"bignumber.js": "^9.0.0",
|
|
43
52
|
"rimraf": "~3.0.2"
|
|
44
53
|
},
|
|
45
54
|
"peerDependencies": {
|
|
46
|
-
"@xchainjs/xchain-
|
|
55
|
+
"@xchainjs/xchain-avax": "^0.1.3",
|
|
56
|
+
"@xchainjs/xchain-binance": "^5.6.6",
|
|
57
|
+
"@xchainjs/xchain-bitcoincash": "^0.15.6",
|
|
58
|
+
"@xchainjs/xchain-cosmos": "^0.20.6",
|
|
59
|
+
"@xchainjs/xchain-doge": "^0.5.6",
|
|
60
|
+
"@xchainjs/xchain-ethereum": "^0.27.6",
|
|
61
|
+
"@xchainjs/xchain-bitcoin": "^0.20.7",
|
|
62
|
+
"@xchainjs/xchain-litecoin": "^0.10.8",
|
|
63
|
+
"@xchainjs/xchain-client": "^0.13.5",
|
|
47
64
|
"@xchainjs/xchain-midgard": "^0.4.1",
|
|
65
|
+
"@xchainjs/xchain-thorchain": "^0.27.7",
|
|
48
66
|
"@xchainjs/xchain-thornode": "^0.1.5",
|
|
49
|
-
"@xchainjs/xchain-util": "^0.
|
|
67
|
+
"@xchainjs/xchain-util": "^0.12.0",
|
|
50
68
|
"axios": "^0.25.0",
|
|
51
69
|
"axios-retry": "^3.2.5",
|
|
52
70
|
"bignumber.js": "^9.0.0",
|