@xchainjs/xchain-thorchain-query 0.2.1 → 0.2.2
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/README.md +67 -3
- package/lib/index.esm.js +34 -22
- package/lib/index.js +34 -22
- package/lib/thorchain-cache.d.ts +2 -2
- package/lib/types.d.ts +1 -0
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# `@xchainjs/xchain-thorchain-query`
|
|
2
2
|
|
|
3
|
-
Thorchain-query module to query thorchain for estimation of swaps
|
|
3
|
+
Thorchain-query module to query thorchain for estimation of swaps/ add and remove Liquidity and checking a transaction stage.
|
|
4
|
+
Returns a TxDetail object with all the information needed to conduct a swap, or add liquidity. This includes estimateAddSavers()
|
|
4
5
|
|
|
5
6
|
## Installation
|
|
6
7
|
|
|
@@ -41,11 +42,72 @@ Estimation example from a swap of 2 BTC to RUNE
|
|
|
41
42
|
}
|
|
42
43
|
```
|
|
43
44
|
|
|
45
|
+
Estimation of add symetric liquidity
|
|
46
|
+
```ts
|
|
47
|
+
{
|
|
48
|
+
rune: 'ᚱ 12,000',
|
|
49
|
+
asset: '⚡ 0',
|
|
50
|
+
slipPercent: '0.0747',
|
|
51
|
+
lpUnits: '154224962883',
|
|
52
|
+
runeToAssetRatio: '20064.69985077',
|
|
53
|
+
transactionFee: { assetFee: '⚡ 0', runeFee: 'ᚱ 0.02', totalFees: 'ᚱ 0.02' },
|
|
54
|
+
estimatedWaitSeconds: 6,
|
|
55
|
+
errors: [],
|
|
56
|
+
canAdd: true
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Estimation of remove Liquidity
|
|
61
|
+
```ts
|
|
62
|
+
{
|
|
63
|
+
asset: { chain: 'BTC', symbol: 'BTC', ticker: 'BTC', synth: false },
|
|
64
|
+
percentage: 100,
|
|
65
|
+
assetAddress: 'bc1qufc5hvfvszphksqawadpc63ujarhjpn26je2jn',
|
|
66
|
+
runeAddress: ''
|
|
67
|
+
}
|
|
68
|
+
{
|
|
69
|
+
slipPercent: '0.0000',
|
|
70
|
+
runeAmount: 'ᚱ 1,664,891.55918601',
|
|
71
|
+
assetAmount: '₿ 83.01517361',
|
|
72
|
+
inbound: {
|
|
73
|
+
assetFee: '⚡ 10,000',
|
|
74
|
+
runeFee: 'ᚱ 0.02',
|
|
75
|
+
totalFees: 'ᚱ 2.02552681'
|
|
76
|
+
},
|
|
77
|
+
impermanentLossProtection: {
|
|
78
|
+
ILProtection: CryptoAmount { asset: [Object], baseAmount: [Object] },
|
|
79
|
+
totalDays: '346.62'
|
|
80
|
+
},
|
|
81
|
+
estimatedWaitSeconds: 8400
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Estimation of Add Saver
|
|
86
|
+
```ts
|
|
87
|
+
{
|
|
88
|
+
assetAmount: '₿ 0.5',
|
|
89
|
+
estimatedDepositValue: '₿ 0.49937395',
|
|
90
|
+
fee: {
|
|
91
|
+
affiliateFee: '⚡ 0',
|
|
92
|
+
asset: { chain: 'BTC', symbol: 'BTC', ticker: 'BTC', synth: true },
|
|
93
|
+
outbound: '⚡ 0'
|
|
94
|
+
},
|
|
95
|
+
expiry: 2023-03-29T05:58:34.415Z,
|
|
96
|
+
toAddress: 'bc1qucjrczghvwl5d66klz6npv7tshkpwpzlw0zzj8',
|
|
97
|
+
memo: '+:BTC/BTC',
|
|
98
|
+
estimateWaitTime: 600,
|
|
99
|
+
saverCapFilledPercent: 266.3662135203711,
|
|
100
|
+
slipBasisPoints: 12,
|
|
101
|
+
canAdd: true,
|
|
102
|
+
errors: []
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
44
106
|
## Documentation
|
|
45
107
|
|
|
46
108
|
[`Overview `](https://dev.thorchain.org/thorchain-dev/xchainjs-integration-guide/query-package)
|
|
47
109
|
|
|
48
|
-
For bash exmples, see example folder at the base of this repository xchainjs/xchainjs-lib.
|
|
110
|
+
For bash exmples, see example folder at the base of this repository xchainjs/xchainjs-lib/examples/liquidity.
|
|
49
111
|
|
|
50
112
|
### [`xchain-thorchain-query`](http://docs.xchainjs.org/xchain-thorchain-query/)
|
|
51
113
|
|
|
@@ -63,6 +125,8 @@ Get Network Values: https://replit.com/@thorchain/networkValues#index.ts\
|
|
|
63
125
|
|
|
64
126
|
Estimate AddSaver() & WithdrawSaver() & getSaverPosition() https://replit.com/@thorchain/quoteDepositTS#index.ts
|
|
65
127
|
|
|
128
|
+
Check transaction Stage
|
|
129
|
+
|
|
66
130
|
### Setting Headers for Nine Realms endpoints
|
|
67
131
|
|
|
68
132
|
If you plan on using the publically accessible endpoints provided by Nine Realms(listed below), ensure that you add a valid 'x-client-id' to all requests
|
|
@@ -71,7 +135,7 @@ If you plan on using the publically accessible endpoints provided by Nine Realms
|
|
|
71
135
|
- https://haskoin.ninerealms.com (BTC/BCH/LTC)
|
|
72
136
|
- https://thornode.ninerealms.com
|
|
73
137
|
|
|
74
|
-
Example
|
|
138
|
+
## Example
|
|
75
139
|
|
|
76
140
|
```typescript
|
|
77
141
|
import cosmosclient from '@cosmos-client/core'
|
package/lib/index.esm.js
CHANGED
|
@@ -1018,7 +1018,7 @@ class ThorchainCache {
|
|
|
1018
1018
|
this.expireInboundDetailsCacheMillis = expireInboundDetailsCacheMillis;
|
|
1019
1019
|
this.expireNetworkValuesCacheMillis = expireNetworkValuesCacheMillis;
|
|
1020
1020
|
//initialize the cache
|
|
1021
|
-
this.
|
|
1021
|
+
this.refreshPoolCache();
|
|
1022
1022
|
}
|
|
1023
1023
|
/**
|
|
1024
1024
|
* Gets the exchange rate of the from asset in terms on the to asset
|
|
@@ -1085,7 +1085,7 @@ class ThorchainCache {
|
|
|
1085
1085
|
const millisSinceLastRefeshed = Date.now() - (((_a = this.poolCache) === null || _a === void 0 ? void 0 : _a.lastRefreshed) || 0);
|
|
1086
1086
|
if (millisSinceLastRefeshed > this.expirePoolCacheMillis) {
|
|
1087
1087
|
try {
|
|
1088
|
-
yield this.
|
|
1088
|
+
yield this.refreshPoolCache();
|
|
1089
1089
|
}
|
|
1090
1090
|
catch (e) {
|
|
1091
1091
|
console.error(e);
|
|
@@ -1102,25 +1102,35 @@ class ThorchainCache {
|
|
|
1102
1102
|
/**
|
|
1103
1103
|
* Refreshes the Pool Cache
|
|
1104
1104
|
*
|
|
1105
|
-
* NOTE: do not call
|
|
1105
|
+
* NOTE: do not call refreshPoolCache() directly, call getPools() instead
|
|
1106
1106
|
* which will refresh the cache if it's expired
|
|
1107
1107
|
*/
|
|
1108
|
-
|
|
1109
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
const
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1108
|
+
refreshPoolCache() {
|
|
1109
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1110
|
+
try {
|
|
1111
|
+
const [thornodePools, midgardPools] = yield Promise.all([this.thornode.getPools(), this.midgard.getPools()]);
|
|
1112
|
+
const poolMap = {};
|
|
1113
|
+
if (midgardPools) {
|
|
1114
|
+
for (const pool of midgardPools) {
|
|
1115
|
+
try {
|
|
1116
|
+
const thornodePool = thornodePools.find((p) => p.asset === pool.asset);
|
|
1117
|
+
if (!thornodePool)
|
|
1118
|
+
throw Error(`Could not find thornode pool ${pool.asset}`);
|
|
1119
|
+
const lp = new LiquidityPool(pool, thornodePool);
|
|
1120
|
+
poolMap[`${lp.asset.chain}.${lp.asset.ticker}`] = lp;
|
|
1121
|
+
}
|
|
1122
|
+
catch (error) {
|
|
1123
|
+
console.log(error);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
this.poolCache = {
|
|
1127
|
+
lastRefreshed: Date.now(),
|
|
1128
|
+
pools: poolMap,
|
|
1129
|
+
};
|
|
1119
1130
|
}
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
};
|
|
1131
|
+
}
|
|
1132
|
+
catch (error) {
|
|
1133
|
+
console.error('Error refreshing pool cache:', error);
|
|
1124
1134
|
}
|
|
1125
1135
|
});
|
|
1126
1136
|
}
|
|
@@ -2169,7 +2179,9 @@ class ThorchainQuery {
|
|
|
2169
2179
|
errors,
|
|
2170
2180
|
};
|
|
2171
2181
|
}
|
|
2172
|
-
|
|
2182
|
+
// request param amount should always be in 1e8 which is why we pass in adjusted decimals if chain decimals != 8
|
|
2183
|
+
const newAddAmount = addAmount.baseAmount.decimal != 8 ? getBaseAmountWithDiffDecimals(addAmount, 8) : addAmount.baseAmount.amount();
|
|
2184
|
+
const depositQuote = yield this.thorchainCache.thornode.getSaversDepositQuote(assetToString(addAmount.asset), newAddAmount.toNumber());
|
|
2173
2185
|
// Calculate transaction expiry time of the vault address
|
|
2174
2186
|
const currentDatetime = new Date();
|
|
2175
2187
|
const minutesToAdd = 15;
|
|
@@ -2181,14 +2193,14 @@ class ThorchainQuery {
|
|
|
2181
2193
|
if (addAmount.baseAmount.lte(depositQuote.expected_amount_out))
|
|
2182
2194
|
errors.push(`Amount being added to savers can't pay for fees`);
|
|
2183
2195
|
const saverFees = {
|
|
2184
|
-
affiliate: new CryptoAmount(baseAmount(depositQuote.fees.affiliate
|
|
2196
|
+
affiliate: new CryptoAmount(baseAmount(depositQuote.fees.affiliate), addAmount.asset),
|
|
2185
2197
|
asset: assetFromStringEx(depositQuote.fees.asset),
|
|
2186
|
-
outbound: new CryptoAmount(baseAmount(depositQuote.fees.outbound
|
|
2198
|
+
outbound: new CryptoAmount(baseAmount(depositQuote.fees.outbound), addAmount.asset),
|
|
2187
2199
|
};
|
|
2188
2200
|
const saverCap = 0.3 * +pool.assetDepth;
|
|
2189
2201
|
const saverCapFilledPercent = (+pool.saversDepth / saverCap) * 100;
|
|
2190
2202
|
const estimateAddSaver = {
|
|
2191
|
-
assetAmount: addAmount,
|
|
2203
|
+
assetAmount: new CryptoAmount(baseAmount(depositQuote.expected_amount_out), addAmount.asset),
|
|
2192
2204
|
estimatedDepositValue: new CryptoAmount(baseAmount(depositQuote.expected_amount_out), saverFees.asset),
|
|
2193
2205
|
fee: saverFees,
|
|
2194
2206
|
expiry: expiryDatetime,
|
package/lib/index.js
CHANGED
|
@@ -1027,7 +1027,7 @@ class ThorchainCache {
|
|
|
1027
1027
|
this.expireInboundDetailsCacheMillis = expireInboundDetailsCacheMillis;
|
|
1028
1028
|
this.expireNetworkValuesCacheMillis = expireNetworkValuesCacheMillis;
|
|
1029
1029
|
//initialize the cache
|
|
1030
|
-
this.
|
|
1030
|
+
this.refreshPoolCache();
|
|
1031
1031
|
}
|
|
1032
1032
|
/**
|
|
1033
1033
|
* Gets the exchange rate of the from asset in terms on the to asset
|
|
@@ -1094,7 +1094,7 @@ class ThorchainCache {
|
|
|
1094
1094
|
const millisSinceLastRefeshed = Date.now() - (((_a = this.poolCache) === null || _a === void 0 ? void 0 : _a.lastRefreshed) || 0);
|
|
1095
1095
|
if (millisSinceLastRefeshed > this.expirePoolCacheMillis) {
|
|
1096
1096
|
try {
|
|
1097
|
-
yield this.
|
|
1097
|
+
yield this.refreshPoolCache();
|
|
1098
1098
|
}
|
|
1099
1099
|
catch (e) {
|
|
1100
1100
|
console.error(e);
|
|
@@ -1111,25 +1111,35 @@ class ThorchainCache {
|
|
|
1111
1111
|
/**
|
|
1112
1112
|
* Refreshes the Pool Cache
|
|
1113
1113
|
*
|
|
1114
|
-
* NOTE: do not call
|
|
1114
|
+
* NOTE: do not call refreshPoolCache() directly, call getPools() instead
|
|
1115
1115
|
* which will refresh the cache if it's expired
|
|
1116
1116
|
*/
|
|
1117
|
-
|
|
1118
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
const
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1117
|
+
refreshPoolCache() {
|
|
1118
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1119
|
+
try {
|
|
1120
|
+
const [thornodePools, midgardPools] = yield Promise.all([this.thornode.getPools(), this.midgard.getPools()]);
|
|
1121
|
+
const poolMap = {};
|
|
1122
|
+
if (midgardPools) {
|
|
1123
|
+
for (const pool of midgardPools) {
|
|
1124
|
+
try {
|
|
1125
|
+
const thornodePool = thornodePools.find((p) => p.asset === pool.asset);
|
|
1126
|
+
if (!thornodePool)
|
|
1127
|
+
throw Error(`Could not find thornode pool ${pool.asset}`);
|
|
1128
|
+
const lp = new LiquidityPool(pool, thornodePool);
|
|
1129
|
+
poolMap[`${lp.asset.chain}.${lp.asset.ticker}`] = lp;
|
|
1130
|
+
}
|
|
1131
|
+
catch (error) {
|
|
1132
|
+
console.log(error);
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
this.poolCache = {
|
|
1136
|
+
lastRefreshed: Date.now(),
|
|
1137
|
+
pools: poolMap,
|
|
1138
|
+
};
|
|
1128
1139
|
}
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
};
|
|
1140
|
+
}
|
|
1141
|
+
catch (error) {
|
|
1142
|
+
console.error('Error refreshing pool cache:', error);
|
|
1133
1143
|
}
|
|
1134
1144
|
});
|
|
1135
1145
|
}
|
|
@@ -2178,7 +2188,9 @@ class ThorchainQuery {
|
|
|
2178
2188
|
errors,
|
|
2179
2189
|
};
|
|
2180
2190
|
}
|
|
2181
|
-
|
|
2191
|
+
// request param amount should always be in 1e8 which is why we pass in adjusted decimals if chain decimals != 8
|
|
2192
|
+
const newAddAmount = addAmount.baseAmount.decimal != 8 ? getBaseAmountWithDiffDecimals(addAmount, 8) : addAmount.baseAmount.amount();
|
|
2193
|
+
const depositQuote = yield this.thorchainCache.thornode.getSaversDepositQuote(xchainUtil.assetToString(addAmount.asset), newAddAmount.toNumber());
|
|
2182
2194
|
// Calculate transaction expiry time of the vault address
|
|
2183
2195
|
const currentDatetime = new Date();
|
|
2184
2196
|
const minutesToAdd = 15;
|
|
@@ -2190,14 +2202,14 @@ class ThorchainQuery {
|
|
|
2190
2202
|
if (addAmount.baseAmount.lte(depositQuote.expected_amount_out))
|
|
2191
2203
|
errors.push(`Amount being added to savers can't pay for fees`);
|
|
2192
2204
|
const saverFees = {
|
|
2193
|
-
affiliate: new CryptoAmount(xchainUtil.baseAmount(depositQuote.fees.affiliate
|
|
2205
|
+
affiliate: new CryptoAmount(xchainUtil.baseAmount(depositQuote.fees.affiliate), addAmount.asset),
|
|
2194
2206
|
asset: xchainUtil.assetFromStringEx(depositQuote.fees.asset),
|
|
2195
|
-
outbound: new CryptoAmount(xchainUtil.baseAmount(depositQuote.fees.outbound
|
|
2207
|
+
outbound: new CryptoAmount(xchainUtil.baseAmount(depositQuote.fees.outbound), addAmount.asset),
|
|
2196
2208
|
};
|
|
2197
2209
|
const saverCap = 0.3 * +pool.assetDepth;
|
|
2198
2210
|
const saverCapFilledPercent = (+pool.saversDepth / saverCap) * 100;
|
|
2199
2211
|
const estimateAddSaver = {
|
|
2200
|
-
assetAmount: addAmount,
|
|
2212
|
+
assetAmount: new CryptoAmount(xchainUtil.baseAmount(depositQuote.expected_amount_out), addAmount.asset),
|
|
2201
2213
|
estimatedDepositValue: new CryptoAmount(xchainUtil.baseAmount(depositQuote.expected_amount_out), saverFees.asset),
|
|
2202
2214
|
fee: saverFees,
|
|
2203
2215
|
expiry: expiryDatetime,
|
package/lib/thorchain-cache.d.ts
CHANGED
|
@@ -52,10 +52,10 @@ export declare class ThorchainCache {
|
|
|
52
52
|
/**
|
|
53
53
|
* Refreshes the Pool Cache
|
|
54
54
|
*
|
|
55
|
-
* NOTE: do not call
|
|
55
|
+
* NOTE: do not call refreshPoolCache() directly, call getPools() instead
|
|
56
56
|
* which will refresh the cache if it's expired
|
|
57
57
|
*/
|
|
58
|
-
private
|
|
58
|
+
private refreshPoolCache;
|
|
59
59
|
/**
|
|
60
60
|
* Refreshes the InboundDetailCache Cache
|
|
61
61
|
*
|
package/lib/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xchainjs/xchain-thorchain-query",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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,15 +33,15 @@
|
|
|
33
33
|
"postversion": "git push --follow-tags"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@xchainjs/xchain-avax": "^0.1.
|
|
36
|
+
"@xchainjs/xchain-avax": "^0.1.5",
|
|
37
37
|
"@xchainjs/xchain-binance": "^5.6.7",
|
|
38
|
-
"@xchainjs/xchain-bitcoin": "^0.21.
|
|
39
|
-
"@xchainjs/xchain-bitcoincash": "^0.15.
|
|
38
|
+
"@xchainjs/xchain-bitcoin": "^0.21.2",
|
|
39
|
+
"@xchainjs/xchain-bitcoincash": "^0.15.9",
|
|
40
40
|
"@xchainjs/xchain-client": "^0.13.5",
|
|
41
41
|
"@xchainjs/xchain-cosmos": "^0.20.8",
|
|
42
|
-
"@xchainjs/xchain-doge": "^0.5.
|
|
42
|
+
"@xchainjs/xchain-doge": "^0.5.1",
|
|
43
43
|
"@xchainjs/xchain-ethereum": "^0.27.7",
|
|
44
|
-
"@xchainjs/xchain-litecoin": "^0.11.
|
|
44
|
+
"@xchainjs/xchain-litecoin": "^0.11.2",
|
|
45
45
|
"@xchainjs/xchain-midgard": "^0.4.3",
|
|
46
46
|
"@xchainjs/xchain-thorchain": "^0.27.9",
|
|
47
47
|
"@xchainjs/xchain-thornode": "^0.2.1",
|
|
@@ -52,15 +52,15 @@
|
|
|
52
52
|
"rimraf": "~3.0.2"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@xchainjs/xchain-avax": "^0.1.
|
|
55
|
+
"@xchainjs/xchain-avax": "^0.1.5",
|
|
56
56
|
"@xchainjs/xchain-binance": "^5.6.7",
|
|
57
|
-
"@xchainjs/xchain-bitcoin": "^0.21.
|
|
58
|
-
"@xchainjs/xchain-bitcoincash": "^0.15.
|
|
57
|
+
"@xchainjs/xchain-bitcoin": "^0.21.2",
|
|
58
|
+
"@xchainjs/xchain-bitcoincash": "^0.15.9",
|
|
59
59
|
"@xchainjs/xchain-client": "^0.13.5",
|
|
60
60
|
"@xchainjs/xchain-cosmos": "^0.20.8",
|
|
61
|
-
"@xchainjs/xchain-doge": "^0.5.
|
|
61
|
+
"@xchainjs/xchain-doge": "^0.5.11",
|
|
62
62
|
"@xchainjs/xchain-ethereum": "^0.27.7",
|
|
63
|
-
"@xchainjs/xchain-litecoin": "^0.11.
|
|
63
|
+
"@xchainjs/xchain-litecoin": "^0.11.2",
|
|
64
64
|
"@xchainjs/xchain-midgard": "^0.4.3",
|
|
65
65
|
"@xchainjs/xchain-thorchain": "^0.27.9",
|
|
66
66
|
"@xchainjs/xchain-thornode": "^0.2.1",
|