@xchainjs/xchain-thorchain-query 0.1.0-beta2 → 0.1.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/lib/index.esm.js +242 -242
- package/lib/index.js +202 -203
- package/lib/thorchain-cache.d.ts +2 -2
- package/lib/thorchain-query.d.ts +1 -1
- package/lib/types.d.ts +18 -11
- package/lib/utils/midgard.d.ts +3 -9
- package/lib/utils/swap.d.ts +3 -2
- package/lib/utils/thornode.d.ts +9 -1
- package/package.json +9 -9
package/lib/index.esm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ObservedTxStatusEnum, TransactionsApi, Configuration as Configuration$1, QueueApi, NetworkApi, PoolsApi, LiquidityProvidersApi } from '@xchainjs/xchain-thornode';
|
|
2
|
-
import { assetToBase, formatAssetAmountCurrency, baseToAsset, eqAsset, assetToString, baseAmount
|
|
3
|
-
import
|
|
4
|
-
import { baseAmount, AssetRuneNative } from '@xchainjs/xchain-util/lib';
|
|
2
|
+
import { assetToBase, formatAssetAmountCurrency, baseToAsset, eqAsset, assetToString, baseAmount, AssetRuneNative, Chain, AssetAtom, AssetAVAX, AssetETH, AssetBNB, AssetDOGE, AssetLTC, AssetBCH, AssetBTC, AvalancheChain, DOGEChain, LTCChain, BCHChain, CosmosChain, THORChain, ETHChain, BTCChain, BNBChain, isAssetRuneNative, assetFromString, assetAmount, assetFromStringEx } from '@xchainjs/xchain-util';
|
|
3
|
+
import { BigNumber } from 'bignumber.js';
|
|
4
|
+
import { baseAmount as baseAmount$1, AssetRuneNative as AssetRuneNative$1 } from '@xchainjs/xchain-util/lib';
|
|
5
5
|
import { Network } from '@xchainjs/xchain-client';
|
|
6
6
|
import { MidgardApi, Configuration } from '@xchainjs/xchain-midgard';
|
|
7
7
|
import axios from 'axios';
|
|
@@ -61,10 +61,6 @@ const DefaultChainAttributes = {
|
|
|
61
61
|
blockReward: 0,
|
|
62
62
|
avgBlockTimeInSecs: 6,
|
|
63
63
|
},
|
|
64
|
-
TERRA: {
|
|
65
|
-
blockReward: 0,
|
|
66
|
-
avgBlockTimeInSecs: 0,
|
|
67
|
-
},
|
|
68
64
|
BNB: {
|
|
69
65
|
blockReward: 0,
|
|
70
66
|
avgBlockTimeInSecs: 6,
|
|
@@ -177,114 +173,14 @@ var TxStage;
|
|
|
177
173
|
TxStage[TxStage["OUTBOUND_QUEUED"] = 3] = "OUTBOUND_QUEUED";
|
|
178
174
|
TxStage[TxStage["OUTBOUND_CHAIN_UNCONFIRMED"] = 4] = "OUTBOUND_CHAIN_UNCONFIRMED";
|
|
179
175
|
TxStage[TxStage["OUTBOUND_CHAIN_CONFIRMED"] = 5] = "OUTBOUND_CHAIN_CONFIRMED";
|
|
180
|
-
})(TxStage || (TxStage = {}));
|
|
181
|
-
// export type LiquidityProvider = {
|
|
182
|
-
// asset: string
|
|
183
|
-
// rune_address: string
|
|
184
|
-
// asset_address: string
|
|
185
|
-
// last_add_height: number
|
|
186
|
-
// last_withdraw_height: number
|
|
187
|
-
// units: number
|
|
188
|
-
// pending_rune: number
|
|
189
|
-
// pending_asset: number
|
|
190
|
-
// pending_tx_Id: string
|
|
191
|
-
// rune_deposit_value: number
|
|
192
|
-
// asset_deposit_value: number
|
|
193
|
-
// }
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* https://dev.thorchain.org/thorchain-dev/interface-guide/math#lp-units-add
|
|
197
|
-
* @param liquidity - asset amount added
|
|
198
|
-
* @param pool - pool depths
|
|
199
|
-
* @returns liquidity units - ownership of pool
|
|
200
|
-
*/
|
|
201
|
-
const getLiquidityUnits = (liquidity, pool) => {
|
|
202
|
-
const P = new BigNumber(pool.pool.liquidityUnits);
|
|
203
|
-
const r = liquidity.rune.amount();
|
|
204
|
-
const a = liquidity.asset.amount();
|
|
205
|
-
const R = pool.runeBalance.amount();
|
|
206
|
-
const A = pool.assetBalance.amount();
|
|
207
|
-
const part1 = R.times(a);
|
|
208
|
-
const part2 = r.times(A);
|
|
209
|
-
const numerator = P.times(part1.plus(part2));
|
|
210
|
-
const denominator = R.times(A).times(2);
|
|
211
|
-
const result = numerator.div(denominator);
|
|
212
|
-
return result;
|
|
213
|
-
};
|
|
214
|
-
/**
|
|
215
|
-
*
|
|
216
|
-
* @param unitData - units for both asset and rune
|
|
217
|
-
* @param pool - pool that the asset is bound to
|
|
218
|
-
* @returns - pool share of both asset and rune in percentage
|
|
219
|
-
*/
|
|
220
|
-
const getPoolShare = (unitData, pool) => {
|
|
221
|
-
// formula: (rune * part) / total; (asset * part) / total
|
|
222
|
-
const units = unitData.liquidityUnits.amount();
|
|
223
|
-
const total = unitData.totalUnits.amount();
|
|
224
|
-
const R = pool.runeBalance.amount();
|
|
225
|
-
const T = pool.assetBalance.amount();
|
|
226
|
-
const asset = T.times(units).div(total);
|
|
227
|
-
const rune = R.times(units).div(total);
|
|
228
|
-
const poolShareDetail = {
|
|
229
|
-
assetShare: new CryptoAmount(baseAmount(asset), pool.asset),
|
|
230
|
-
runeShare: new CryptoAmount(baseAmount(rune), AssetRuneNative),
|
|
231
|
-
};
|
|
232
|
-
return poolShareDetail;
|
|
233
|
-
};
|
|
234
|
-
/**
|
|
235
|
-
*
|
|
236
|
-
* @param poolShare - the share of asset and rune added to the pool
|
|
237
|
-
* @param pool - Pool that the asset is attached to
|
|
238
|
-
* @returns - returns bignumber representing a slip percentage
|
|
239
|
-
*/
|
|
240
|
-
const getSlipOnLiquidity = (stake, pool) => {
|
|
241
|
-
// formula: (t * R - T * r)/ (T*r + R*T)
|
|
242
|
-
const r = stake.rune.amount();
|
|
243
|
-
const t = stake.asset.amount();
|
|
244
|
-
const R = pool.runeBalance.amount();
|
|
245
|
-
const T = pool.assetBalance.amount();
|
|
246
|
-
const numerator = t.times(R).minus(T.times(r));
|
|
247
|
-
const denominator = T.times(r).plus(R.times(T));
|
|
248
|
-
const result = numerator.div(denominator).abs();
|
|
249
|
-
return result;
|
|
250
|
-
};
|
|
251
|
-
/**
|
|
252
|
-
* https://docs.thorchain.org/thorchain-finance/continuous-liquidity-pools#impermanent-loss-protection
|
|
253
|
-
* @param poolShare - the share of asset and rune added to the pool
|
|
254
|
-
* @param pool - Pool that the asset is attached to
|
|
255
|
-
* @param block - blockl object with current, last added and the constant blocksforlossProtection
|
|
256
|
-
* @returns
|
|
257
|
-
*/
|
|
258
|
-
// Blocks for full protection 1440000 // 100 days
|
|
259
|
-
const getLiquidityProtectionData = (depositValue, poolShare, block) => {
|
|
260
|
-
//Coverage formula coverage=((A0∗P1)+R0)−((A1∗P1)+R1)=>((A0∗R1/A1)+R0)−(R1+R1)
|
|
261
|
-
//formula: protectionProgress (currentHeight-heightLastAdded)/blocksforfullprotection
|
|
262
|
-
const R0 = depositValue.rune.amount(); // rune deposit value
|
|
263
|
-
const A0 = depositValue.asset.amount(); // asset deposit value
|
|
264
|
-
const R1 = poolShare.runeShare.baseAmount.amount(); // rune amount to redeem
|
|
265
|
-
const A1 = poolShare.assetShare.baseAmount.amount(); // asset amount to redeem
|
|
266
|
-
const P1 = R1.div(A1); // Pool ratio at withdrawal
|
|
267
|
-
const part1 = A0.times(P1).plus(R0).minus(A1.times(P1).plus(R1)); // start position minus end position
|
|
268
|
-
const part2 = A0.times(R1.div(A1)).plus(R0).minus(R1.plus(R1)); // different way to check position
|
|
269
|
-
const coverage = part1 >= part2 ? part1 : part2; // Coverage represents how much ILP a LP is entitled to
|
|
270
|
-
const currentHeight = block.current;
|
|
271
|
-
const heightLastAdded = block.lastAdded || 0; //default to zero if undefined
|
|
272
|
-
const blocksforfullprotection = block.fullProtection;
|
|
273
|
-
const protectionProgress = (currentHeight - heightLastAdded) / blocksforfullprotection; // percentage of entitlement
|
|
274
|
-
const result = coverage.times(protectionProgress); // impermanent loss protection result
|
|
275
|
-
const ILProtection = {
|
|
276
|
-
ILProtection: new CryptoAmount(baseAmount(result), AssetRuneNative),
|
|
277
|
-
totalDays: (protectionProgress * 100).toFixed(2),
|
|
278
|
-
};
|
|
279
|
-
return ILProtection;
|
|
280
|
-
};
|
|
176
|
+
})(TxStage || (TxStage = {}));
|
|
281
177
|
|
|
282
178
|
const getBaseAmountWithDiffDecimals = (inputAmount, outDecimals) => {
|
|
283
179
|
const inDecimals = inputAmount.baseAmount.decimal;
|
|
284
180
|
let baseAmountOut = inputAmount.baseAmount.amount();
|
|
285
181
|
const adjustDecimals = outDecimals - inDecimals;
|
|
286
182
|
baseAmountOut = baseAmountOut.times(Math.pow(10, adjustDecimals));
|
|
287
|
-
return baseAmount
|
|
183
|
+
return baseAmount(baseAmountOut, outDecimals).amount();
|
|
288
184
|
};
|
|
289
185
|
/**
|
|
290
186
|
*
|
|
@@ -299,14 +195,14 @@ const getSwapFee = (inputAmount, pool, toRune) => {
|
|
|
299
195
|
const x = getBaseAmountWithDiffDecimals(inputAmount, 8);
|
|
300
196
|
const X = toRune ? pool.assetBalance.amount() : pool.runeBalance.amount(); // input is asset if toRune
|
|
301
197
|
const Y = toRune ? pool.runeBalance.amount() : pool.assetBalance.amount(); // output is rune if toRune
|
|
302
|
-
const units = toRune ? AssetRuneNative
|
|
198
|
+
const units = toRune ? AssetRuneNative : pool.asset;
|
|
303
199
|
const numerator = x.times(x).multipliedBy(Y);
|
|
304
200
|
const denominator = x.plus(X).pow(2);
|
|
305
201
|
const result = numerator.div(denominator);
|
|
306
|
-
const eightDecimalResult = new CryptoAmount(baseAmount
|
|
202
|
+
const eightDecimalResult = new CryptoAmount(baseAmount(result), units);
|
|
307
203
|
const decimals = toRune ? 8 : inputAmount.baseAmount.decimal;
|
|
308
204
|
const baseOut = getBaseAmountWithDiffDecimals(eightDecimalResult, decimals);
|
|
309
|
-
const swapFee = new CryptoAmount(baseAmount
|
|
205
|
+
const swapFee = new CryptoAmount(baseAmount(baseOut, decimals), units);
|
|
310
206
|
//console.log(` swapFee ${swapFee.assetAmountFixedString()} `)
|
|
311
207
|
return swapFee;
|
|
312
208
|
};
|
|
@@ -337,15 +233,15 @@ const getSwapOutput = (inputAmount, pool, toRune) => {
|
|
|
337
233
|
const x = getBaseAmountWithDiffDecimals(inputAmount, 8);
|
|
338
234
|
const X = toRune ? pool.assetBalance.amount() : pool.runeBalance.amount(); // input is asset if toRune
|
|
339
235
|
const Y = toRune ? pool.runeBalance.amount() : pool.assetBalance.amount(); // output is rune if toRune
|
|
340
|
-
const units = toRune ? AssetRuneNative
|
|
236
|
+
const units = toRune ? AssetRuneNative : pool.asset;
|
|
341
237
|
// const decimals = toRune || !pool.decimals ? 8 : pool.decimals
|
|
342
238
|
const numerator = x.times(X).times(Y);
|
|
343
239
|
const denominator = x.plus(X).pow(2);
|
|
344
240
|
const result = numerator.div(denominator);
|
|
345
|
-
const eightDecimalResult = new CryptoAmount(baseAmount
|
|
241
|
+
const eightDecimalResult = new CryptoAmount(baseAmount(result), units);
|
|
346
242
|
const decimals = toRune ? 8 : inputAmount.baseAmount.decimal;
|
|
347
243
|
const baseOut = getBaseAmountWithDiffDecimals(eightDecimalResult, decimals);
|
|
348
|
-
return new CryptoAmount(baseAmount
|
|
244
|
+
return new CryptoAmount(baseAmount(baseOut, decimals), units);
|
|
349
245
|
};
|
|
350
246
|
const getDoubleSwapOutput = (inputAmount, pool1, pool2) => {
|
|
351
247
|
// formula: getSwapOutput(pool1) => getSwapOutput(pool2)
|
|
@@ -382,7 +278,7 @@ const getDoubleSwapFee = (inputAmount, pool1, pool2, thorchainCache) => __awaite
|
|
|
382
278
|
const fee1InRune = getSwapFee(inputAmount, pool1, true);
|
|
383
279
|
const swapOutput = getSwapOutput(inputAmount, pool1, true);
|
|
384
280
|
const fee2InAsset = getSwapFee(swapOutput, pool2, false);
|
|
385
|
-
const fee2InRune = yield thorchainCache.convert(fee2InAsset, AssetRuneNative
|
|
281
|
+
const fee2InRune = yield thorchainCache.convert(fee2InAsset, AssetRuneNative);
|
|
386
282
|
const result = fee1InRune.plus(fee2InRune);
|
|
387
283
|
return result;
|
|
388
284
|
});
|
|
@@ -413,25 +309,25 @@ const getDoubleSwap = (inputAmount, pool1, pool2, thorchainCache) => __awaiter(v
|
|
|
413
309
|
* @see https://dev.thorchain.org/thorchain-dev/thorchain-and-fees#fee-calcuation-by-chain
|
|
414
310
|
* @returns
|
|
415
311
|
*/
|
|
416
|
-
const calcNetworkFee = (asset,
|
|
312
|
+
const calcNetworkFee = (asset, inbound) => {
|
|
417
313
|
if (asset.synth)
|
|
418
|
-
return new CryptoAmount(baseAmount
|
|
314
|
+
return new CryptoAmount(baseAmount(2000000), AssetRuneNative);
|
|
419
315
|
switch (asset.chain) {
|
|
420
316
|
case Chain.Bitcoin:
|
|
421
|
-
return new CryptoAmount(baseAmount
|
|
317
|
+
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetBTC);
|
|
422
318
|
case Chain.BitcoinCash:
|
|
423
|
-
return new CryptoAmount(baseAmount
|
|
319
|
+
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetBCH);
|
|
424
320
|
case Chain.Litecoin:
|
|
425
|
-
return new CryptoAmount(baseAmount
|
|
321
|
+
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetLTC);
|
|
426
322
|
case Chain.Doge:
|
|
427
323
|
// NOTE: UTXO chains estimate fees with a 250 byte size
|
|
428
|
-
return new CryptoAmount(baseAmount
|
|
324
|
+
return new CryptoAmount(baseAmount(inbound.gasRate.multipliedBy(inbound.outboundTxSize)), AssetDOGE);
|
|
429
325
|
case Chain.Binance:
|
|
430
326
|
//flat fee
|
|
431
|
-
return new CryptoAmount(baseAmount
|
|
327
|
+
return new CryptoAmount(baseAmount(inbound.gasRate), AssetBNB);
|
|
432
328
|
case Chain.Ethereum:
|
|
433
|
-
const gasRateinETHGwei = gasRate;
|
|
434
|
-
const gasRateinETHWei = baseAmount
|
|
329
|
+
const gasRateinETHGwei = inbound.gasRate;
|
|
330
|
+
const gasRateinETHWei = baseAmount(gasRateinETHGwei.multipliedBy(Math.pow(10, 9)), 18);
|
|
435
331
|
if (eqAsset(asset, AssetETH)) {
|
|
436
332
|
return new CryptoAmount(gasRateinETHWei.times(21000), AssetETH);
|
|
437
333
|
}
|
|
@@ -439,20 +335,18 @@ const calcNetworkFee = (asset, gasRate) => {
|
|
|
439
335
|
return new CryptoAmount(gasRateinETHWei.times(70000), AssetETH);
|
|
440
336
|
}
|
|
441
337
|
case Chain.Avalanche:
|
|
442
|
-
const gasRateinAVAXGwei = gasRate;
|
|
443
|
-
const gasRateinAVAXWei = baseAmount
|
|
338
|
+
const gasRateinAVAXGwei = inbound.gasRate;
|
|
339
|
+
const gasRateinAVAXWei = baseAmount(gasRateinAVAXGwei.multipliedBy(Math.pow(10, 9)), 18);
|
|
444
340
|
if (eqAsset(asset, AssetAVAX)) {
|
|
445
341
|
return new CryptoAmount(gasRateinAVAXWei.times(21000), AssetAVAX);
|
|
446
342
|
}
|
|
447
343
|
else {
|
|
448
344
|
return new CryptoAmount(gasRateinAVAXWei.times(70000), AssetAVAX);
|
|
449
345
|
}
|
|
450
|
-
case Chain.Terra:
|
|
451
|
-
return new CryptoAmount(baseAmount$1(gasRate), AssetLUNA);
|
|
452
346
|
case Chain.Cosmos:
|
|
453
|
-
return new CryptoAmount(baseAmount
|
|
347
|
+
return new CryptoAmount(baseAmount(inbound.gasRate), AssetAtom);
|
|
454
348
|
case Chain.THORChain:
|
|
455
|
-
return new CryptoAmount(baseAmount
|
|
349
|
+
return new CryptoAmount(baseAmount(2000000), AssetRuneNative);
|
|
456
350
|
}
|
|
457
351
|
throw new Error(`could not calculate inbound fee for ${asset.chain}`);
|
|
458
352
|
};
|
|
@@ -470,7 +364,7 @@ const getChainAsset = (chain) => {
|
|
|
470
364
|
case ETHChain:
|
|
471
365
|
return AssetETH;
|
|
472
366
|
case THORChain:
|
|
473
|
-
return AssetRuneNative
|
|
367
|
+
return AssetRuneNative;
|
|
474
368
|
case CosmosChain:
|
|
475
369
|
return AssetAtom;
|
|
476
370
|
case BCHChain:
|
|
@@ -479,8 +373,6 @@ const getChainAsset = (chain) => {
|
|
|
479
373
|
return AssetLTC;
|
|
480
374
|
case DOGEChain:
|
|
481
375
|
return AssetDOGE;
|
|
482
|
-
case TerraChain:
|
|
483
|
-
return AssetLUNA;
|
|
484
376
|
case AvalancheChain:
|
|
485
377
|
return AssetAVAX;
|
|
486
378
|
default:
|
|
@@ -510,14 +402,100 @@ const getChain = (chain) => {
|
|
|
510
402
|
return LTCChain;
|
|
511
403
|
case 'DOGE':
|
|
512
404
|
return DOGEChain;
|
|
513
|
-
case 'TERRA':
|
|
514
|
-
return TerraChain;
|
|
515
405
|
default:
|
|
516
406
|
throw Error('Unknown chain');
|
|
517
407
|
}
|
|
518
408
|
};
|
|
519
409
|
|
|
520
|
-
|
|
410
|
+
/**
|
|
411
|
+
* https://dev.thorchain.org/thorchain-dev/interface-guide/math#lp-units-add
|
|
412
|
+
* @param liquidity - asset amount added
|
|
413
|
+
* @param pool - pool depths
|
|
414
|
+
* @returns liquidity units - ownership of pool
|
|
415
|
+
*/
|
|
416
|
+
const getLiquidityUnits = (liquidity, pool) => {
|
|
417
|
+
const baseAmount8decimals = getBaseAmountWithDiffDecimals(liquidity.asset, 8);
|
|
418
|
+
const P = new BigNumber(pool.pool.liquidityUnits);
|
|
419
|
+
const r = liquidity.rune.baseAmount.amount();
|
|
420
|
+
const a = baseAmount8decimals;
|
|
421
|
+
const R = pool.runeBalance.amount();
|
|
422
|
+
const A = pool.assetBalance.amount();
|
|
423
|
+
const part1 = R.times(a);
|
|
424
|
+
const part2 = r.times(A);
|
|
425
|
+
const numerator = P.times(part1.plus(part2));
|
|
426
|
+
const denominator = R.times(A).times(2);
|
|
427
|
+
const result = numerator.div(denominator);
|
|
428
|
+
return result;
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
*
|
|
432
|
+
* @param unitData - units for both asset and rune
|
|
433
|
+
* @param pool - pool that the asset is bound to
|
|
434
|
+
* @returns - pool share of both asset and rune in percentage
|
|
435
|
+
*/
|
|
436
|
+
const getPoolShare = (unitData, pool) => {
|
|
437
|
+
// formula: (rune * part) / total; (asset * part) / total
|
|
438
|
+
const units = unitData.liquidityUnits;
|
|
439
|
+
const total = unitData.totalUnits;
|
|
440
|
+
const R = pool.runeBalance.amount();
|
|
441
|
+
const T = pool.assetBalance.amount();
|
|
442
|
+
const asset = T.times(units).div(total);
|
|
443
|
+
const rune = R.times(units).div(total);
|
|
444
|
+
const poolShareDetail = {
|
|
445
|
+
assetShare: new CryptoAmount(baseAmount$1(asset), pool.asset),
|
|
446
|
+
runeShare: new CryptoAmount(baseAmount$1(rune), AssetRuneNative$1),
|
|
447
|
+
};
|
|
448
|
+
return poolShareDetail;
|
|
449
|
+
};
|
|
450
|
+
/**
|
|
451
|
+
*
|
|
452
|
+
* @param poolShare - the share of asset and rune added to the pool
|
|
453
|
+
* @param pool - Pool that the asset is attached to
|
|
454
|
+
* @returns - returns bignumber representing a slip percentage
|
|
455
|
+
*/
|
|
456
|
+
const getSlipOnLiquidity = (stake, pool) => {
|
|
457
|
+
const baseAmount8decimals = getBaseAmountWithDiffDecimals(stake.asset, pool.decimals);
|
|
458
|
+
// formula: (t * R - T * r)/ (T*r + R*T)
|
|
459
|
+
const r = stake.rune.baseAmount.amount();
|
|
460
|
+
const t = baseAmount8decimals;
|
|
461
|
+
const R = pool.runeBalance.amount();
|
|
462
|
+
const T = pool.assetBalance.amount();
|
|
463
|
+
const numerator = t.times(R).minus(T.times(r));
|
|
464
|
+
const denominator = T.times(r).plus(R.times(T));
|
|
465
|
+
const result = numerator.div(denominator).abs();
|
|
466
|
+
return result;
|
|
467
|
+
};
|
|
468
|
+
/**
|
|
469
|
+
* https://docs.thorchain.org/thorchain-finance/continuous-liquidity-pools#impermanent-loss-protection
|
|
470
|
+
* @param poolShare - the share of asset and rune added to the pool
|
|
471
|
+
* @param pool - Pool that the asset is attached to
|
|
472
|
+
* @param block - blockl object with current, last added and the constant blocksforlossProtection
|
|
473
|
+
* @returns
|
|
474
|
+
*/
|
|
475
|
+
// Blocks for full protection 1440000 // 100 days
|
|
476
|
+
const getLiquidityProtectionData = (depositValue, poolShare, block) => {
|
|
477
|
+
//Coverage formula coverage=((A0∗P1)+R0)−((A1∗P1)+R1)=>((A0∗R1/A1)+R0)−(R1+R1)
|
|
478
|
+
//formula: protectionProgress (currentHeight-heightLastAdded)/blocksforfullprotection
|
|
479
|
+
const R0 = depositValue.rune.amount(); // rune deposit value
|
|
480
|
+
const A0 = depositValue.asset.amount(); // asset deposit value
|
|
481
|
+
const R1 = poolShare.runeShare.baseAmount.amount(); // rune amount to redeem
|
|
482
|
+
const A1 = poolShare.assetShare.baseAmount.amount(); // asset amount to redeem
|
|
483
|
+
const P1 = R1.div(A1); // Pool ratio at withdrawal
|
|
484
|
+
const part1 = A0.times(P1).plus(R0).minus(A1.times(P1).plus(R1)); // start position minus end position
|
|
485
|
+
const part2 = A0.times(R1.div(A1)).plus(R0).minus(R1.plus(R1)); // different way to check position
|
|
486
|
+
const coverage = part1 >= part2 ? part1 : part2; // Coverage represents how much ILP a LP is entitled to
|
|
487
|
+
const currentHeight = block.current;
|
|
488
|
+
const heightLastAdded = block.lastAdded || 0; //default to zero if undefined
|
|
489
|
+
const blocksforfullprotection = block.fullProtection;
|
|
490
|
+
const protectionProgress = (currentHeight - heightLastAdded) / blocksforfullprotection; // percentage of entitlement
|
|
491
|
+
const result = coverage.times(protectionProgress); // impermanent loss protection result
|
|
492
|
+
const ILProtection = {
|
|
493
|
+
ILProtection: new CryptoAmount(baseAmount$1(result), AssetRuneNative$1),
|
|
494
|
+
totalDays: (protectionProgress * 100).toFixed(2),
|
|
495
|
+
};
|
|
496
|
+
return ILProtection;
|
|
497
|
+
};
|
|
498
|
+
|
|
521
499
|
const BN_1 = new BigNumber(1);
|
|
522
500
|
/**
|
|
523
501
|
* THORChain Class for interacting with THORChain.
|
|
@@ -557,9 +535,7 @@ class ThorchainQuery {
|
|
|
557
535
|
});
|
|
558
536
|
const inboundDetails = yield this.thorchainCache.getInboundDetails();
|
|
559
537
|
const sourceInboundDetails = inboundDetails[input.asset.chain];
|
|
560
|
-
// console.log(JSON.stringify(sourceInboundDetails, null, 2))
|
|
561
538
|
const destinationInboundDetails = inboundDetails[destinationAsset.chain];
|
|
562
|
-
// console.log(JSON.stringify(destinationInboundDetails, null, 2))
|
|
563
539
|
const swapEstimate = yield this.calcSwapEstimate({
|
|
564
540
|
input,
|
|
565
541
|
destinationAsset,
|
|
@@ -598,7 +574,7 @@ class ThorchainQuery {
|
|
|
598
574
|
else {
|
|
599
575
|
txDetails.txEstimate.canSwap = true;
|
|
600
576
|
// Retrieve inbound Asgard address.
|
|
601
|
-
const inboundAsgard = (yield this.thorchainCache.
|
|
577
|
+
const inboundAsgard = (yield this.thorchainCache.getInboundAddresses())[input.asset.chain];
|
|
602
578
|
txDetails.toAddress = (inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.address) || '';
|
|
603
579
|
// Work out LIM from the slip percentage
|
|
604
580
|
let limPercentage = BN_1;
|
|
@@ -656,13 +632,13 @@ class ThorchainQuery {
|
|
|
656
632
|
? params.input
|
|
657
633
|
: yield this.thorchainCache.convert(params.input, params.input.asset);
|
|
658
634
|
// If asset is already rune native, skip the convert
|
|
659
|
-
const inputInRune = input.asset === AssetRuneNative
|
|
660
|
-
const inboundFeeInAsset = calcNetworkFee(input.asset, sourceInboundDetails
|
|
661
|
-
|
|
662
|
-
outboundFeeInAsset =
|
|
635
|
+
const inputInRune = input.asset === AssetRuneNative ? input : yield this.thorchainCache.convert(input, AssetRuneNative);
|
|
636
|
+
const inboundFeeInAsset = calcNetworkFee(input.asset, sourceInboundDetails);
|
|
637
|
+
// Retrieve outbound fee from inboundAddressDetails.
|
|
638
|
+
const outboundFeeInAsset = new CryptoAmount(baseAmount(destinationInboundDetails.outboundFee), params.destinationAsset);
|
|
663
639
|
// convert fees to rune
|
|
664
|
-
const inboundFeeInRune = yield this.thorchainCache.convert(inboundFeeInAsset, AssetRuneNative
|
|
665
|
-
let outboundFeeInRune = yield this.thorchainCache.convert(outboundFeeInAsset, AssetRuneNative
|
|
640
|
+
const inboundFeeInRune = yield this.thorchainCache.convert(inboundFeeInAsset, AssetRuneNative);
|
|
641
|
+
let outboundFeeInRune = yield this.thorchainCache.convert(outboundFeeInAsset, AssetRuneNative);
|
|
666
642
|
// ---------- Remove Fees from inbound before doing the swap -----------
|
|
667
643
|
// TODO confirm with chris about this change, was there a reason why this was commented out?
|
|
668
644
|
const inputMinusInboundFeeInRune = inputInRune.minus(inboundFeeInRune);
|
|
@@ -678,18 +654,18 @@ class ThorchainQuery {
|
|
|
678
654
|
const deepestUSDPOOL = yield this.thorchainCache.getDeepestUSDPool();
|
|
679
655
|
const usdAsset = deepestUSDPOOL.asset;
|
|
680
656
|
const networkValues = yield this.thorchainCache.midgard.getNetworkValues();
|
|
681
|
-
const usdMinFee = new CryptoAmount(baseAmount
|
|
657
|
+
const usdMinFee = new CryptoAmount(baseAmount(networkValues['MINIMUML1OUTBOUNDFEEUSD']), usdAsset);
|
|
682
658
|
// const FeeInUSD = await this.convert(outboundFeeInRune, usdAsset)
|
|
683
659
|
const checkOutboundFee = (yield this.convert(outboundFeeInRune, usdAsset)).gte(usdMinFee);
|
|
684
660
|
if (!checkOutboundFee) {
|
|
685
661
|
const newFee = usdMinFee;
|
|
686
|
-
outboundFeeInRune = yield this.convert(newFee, AssetRuneNative
|
|
662
|
+
outboundFeeInRune = yield this.convert(newFee, AssetRuneNative);
|
|
687
663
|
}
|
|
688
664
|
}
|
|
689
665
|
// Now calculate swap output based on inputNetAmount
|
|
690
666
|
const swapOutput = yield this.thorchainCache.getExpectedSwapOutput(inputNetInAsset, params.destinationAsset);
|
|
691
|
-
const swapFeeInRune = yield this.thorchainCache.convert(swapOutput.swapFee, AssetRuneNative
|
|
692
|
-
const outputInRune = yield this.thorchainCache.convert(swapOutput.output, AssetRuneNative
|
|
667
|
+
const swapFeeInRune = yield this.thorchainCache.convert(swapOutput.swapFee, AssetRuneNative);
|
|
668
|
+
const outputInRune = yield this.thorchainCache.convert(swapOutput.output, AssetRuneNative);
|
|
693
669
|
// ---------------- Remove Outbound Fee ---------------------- /
|
|
694
670
|
const netOutputInRune = outputInRune.minus(outboundFeeInRune);
|
|
695
671
|
const netOutputInAsset = yield this.thorchainCache.convert(netOutputInRune, params.destinationAsset);
|
|
@@ -790,8 +766,8 @@ class ThorchainQuery {
|
|
|
790
766
|
checkCoverFees(params, estimate) {
|
|
791
767
|
return __awaiter(this, void 0, void 0, function* () {
|
|
792
768
|
let result = undefined;
|
|
793
|
-
const inputInRune = yield this.thorchainCache.convert(params.input, AssetRuneNative
|
|
794
|
-
const feesInRune = yield this.getFeesIn(estimate.totalFees, AssetRuneNative
|
|
769
|
+
const inputInRune = yield this.thorchainCache.convert(params.input, AssetRuneNative);
|
|
770
|
+
const feesInRune = yield this.getFeesIn(estimate.totalFees, AssetRuneNative);
|
|
795
771
|
const totalSwapFeesInRune = feesInRune.inboundFee
|
|
796
772
|
.plus(feesInRune.outboundFee)
|
|
797
773
|
.plus(feesInRune.swapFee)
|
|
@@ -878,15 +854,15 @@ class ThorchainQuery {
|
|
|
878
854
|
outboundDelay(outboundAmount) {
|
|
879
855
|
return __awaiter(this, void 0, void 0, function* () {
|
|
880
856
|
const networkValues = yield this.thorchainCache.getNetworkValues();
|
|
881
|
-
const minTxOutVolumeThreshold = new CryptoAmount(baseAmount
|
|
857
|
+
const minTxOutVolumeThreshold = new CryptoAmount(baseAmount(networkValues['MINTXOUTVOLUMETHRESHOLD']), AssetRuneNative);
|
|
882
858
|
const maxTxOutOffset = networkValues['MAXTXOUTOFFSET'];
|
|
883
|
-
let txOutDelayRate = new CryptoAmount(baseAmount
|
|
859
|
+
let txOutDelayRate = new CryptoAmount(baseAmount(networkValues['TXOUTDELAYRATE']), AssetRuneNative).assetAmount
|
|
884
860
|
.amount()
|
|
885
861
|
.toNumber();
|
|
886
862
|
const getScheduledOutboundValue = yield this.thorchainCache.midgard.getScheduledOutboundValue();
|
|
887
863
|
const thorChainblocktime = this.chainAttributes[Chain.THORChain].avgBlockTimeInSecs; // blocks required to confirm tx
|
|
888
864
|
// If asset is equal to Rune set runeValue as outbound amount else set it to the asset's value in rune
|
|
889
|
-
const runeValue = yield this.thorchainCache.convert(outboundAmount, AssetRuneNative
|
|
865
|
+
const runeValue = yield this.thorchainCache.convert(outboundAmount, AssetRuneNative);
|
|
890
866
|
// Check rune value amount
|
|
891
867
|
if (runeValue.lt(minTxOutVolumeThreshold)) {
|
|
892
868
|
return thorChainblocktime;
|
|
@@ -1129,34 +1105,35 @@ class ThorchainQuery {
|
|
|
1129
1105
|
if (!isAssetRuneNative(params.rune.asset))
|
|
1130
1106
|
errors.push('params.rune must be THOR.RUNE');
|
|
1131
1107
|
const assetPool = yield this.thorchainCache.getPoolForAsset(params.asset.asset);
|
|
1132
|
-
const lpUnits = getLiquidityUnits({ asset: params.asset
|
|
1133
|
-
const inboundDetails = yield this.thorchainCache.
|
|
1108
|
+
const lpUnits = getLiquidityUnits({ asset: params.asset, rune: params.rune }, assetPool);
|
|
1109
|
+
const inboundDetails = yield this.thorchainCache.getInboundDetails();
|
|
1134
1110
|
const unitData = {
|
|
1135
|
-
liquidityUnits:
|
|
1136
|
-
totalUnits:
|
|
1111
|
+
liquidityUnits: lpUnits,
|
|
1112
|
+
totalUnits: new BigNumber(assetPool.pool.liquidityUnits),
|
|
1137
1113
|
};
|
|
1138
1114
|
const poolShare = getPoolShare(unitData, assetPool);
|
|
1139
1115
|
const assetWaitTimeSeconds = yield this.confCounting(params.asset);
|
|
1140
1116
|
const runeWaitTimeSeconds = yield this.confCounting(params.rune);
|
|
1141
1117
|
const waitTimeSeconds = assetWaitTimeSeconds > runeWaitTimeSeconds ? assetWaitTimeSeconds : runeWaitTimeSeconds;
|
|
1142
|
-
let assetInboundFee = new CryptoAmount(baseAmount
|
|
1143
|
-
let runeInboundFee = new CryptoAmount(baseAmount
|
|
1118
|
+
let assetInboundFee = new CryptoAmount(baseAmount(0), params.asset.asset);
|
|
1119
|
+
let runeInboundFee = new CryptoAmount(baseAmount(0), AssetRuneNative);
|
|
1144
1120
|
if (!params.asset.assetAmount.eq(0)) {
|
|
1145
|
-
assetInboundFee = calcNetworkFee(params.asset.asset, inboundDetails[params.asset.asset.chain]
|
|
1121
|
+
assetInboundFee = calcNetworkFee(params.asset.asset, inboundDetails[params.asset.asset.chain]);
|
|
1146
1122
|
if (assetInboundFee.assetAmount.amount().times(3).gt(params.asset.assetAmount.amount()))
|
|
1147
1123
|
errors.push(`Asset amount is less than fees`);
|
|
1148
1124
|
}
|
|
1149
1125
|
if (!params.rune.assetAmount.eq(0)) {
|
|
1150
|
-
runeInboundFee = calcNetworkFee(params.rune.asset, inboundDetails[params.rune.asset.chain]
|
|
1126
|
+
runeInboundFee = calcNetworkFee(params.rune.asset, inboundDetails[params.rune.asset.chain]);
|
|
1151
1127
|
if (runeInboundFee.assetAmount.amount().times(3).gt(params.rune.assetAmount.amount()))
|
|
1152
1128
|
errors.push(`Rune amount is less than fees`);
|
|
1153
1129
|
}
|
|
1154
|
-
const totalFees = (yield this.convert(assetInboundFee, AssetRuneNative
|
|
1155
|
-
const slip = getSlipOnLiquidity({ asset: params.asset
|
|
1130
|
+
const totalFees = (yield this.convert(assetInboundFee, AssetRuneNative)).plus(runeInboundFee);
|
|
1131
|
+
const slip = getSlipOnLiquidity({ asset: params.asset, rune: params.rune }, assetPool);
|
|
1156
1132
|
const estimateLP = {
|
|
1133
|
+
assetPool: assetPool.pool.asset,
|
|
1157
1134
|
slipPercent: slip.times(100),
|
|
1158
1135
|
poolShare: poolShare,
|
|
1159
|
-
lpUnits: baseAmount
|
|
1136
|
+
lpUnits: baseAmount(lpUnits),
|
|
1160
1137
|
runeToAssetRatio: assetPool.runeToAssetRatio,
|
|
1161
1138
|
transactionFee: {
|
|
1162
1139
|
assetFee: assetInboundFee,
|
|
@@ -1180,6 +1157,8 @@ class ThorchainQuery {
|
|
|
1180
1157
|
const poolAsset = yield this.thorchainCache.getPoolForAsset(asset);
|
|
1181
1158
|
if (!poolAsset)
|
|
1182
1159
|
throw Error(`Could not find pool for ${asset}`);
|
|
1160
|
+
if (!assetOrRuneAddress)
|
|
1161
|
+
throw Error(`No address provided ${assetOrRuneAddress}`);
|
|
1183
1162
|
const liquidityProvider = yield this.thorchainCache.thornode.getLiquidityProvider(poolAsset.assetString, assetOrRuneAddress);
|
|
1184
1163
|
if (!liquidityProvider)
|
|
1185
1164
|
throw Error(`Could not find LP for ${assetOrRuneAddress}`);
|
|
@@ -1189,8 +1168,8 @@ class ThorchainQuery {
|
|
|
1189
1168
|
throw Error(`Could not get block data`);
|
|
1190
1169
|
// Pools total units & Lp's total units
|
|
1191
1170
|
const unitData = {
|
|
1192
|
-
totalUnits:
|
|
1193
|
-
liquidityUnits:
|
|
1171
|
+
totalUnits: new BigNumber(poolAsset.pool.liquidityUnits),
|
|
1172
|
+
liquidityUnits: new BigNumber(liquidityProvider.units),
|
|
1194
1173
|
};
|
|
1195
1174
|
//console.log(`unit data`, unitData.totalUnits.amount().toNumber(), unitData.liquidityUnits.amount().toNumber())
|
|
1196
1175
|
const networkValues = yield this.thorchainCache.midgard.getNetworkValues();
|
|
@@ -1201,8 +1180,8 @@ class ThorchainQuery {
|
|
|
1201
1180
|
};
|
|
1202
1181
|
//
|
|
1203
1182
|
const currentLP = {
|
|
1204
|
-
asset: baseAmount
|
|
1205
|
-
rune: baseAmount
|
|
1183
|
+
asset: baseAmount(liquidityProvider.asset_deposit_value),
|
|
1184
|
+
rune: baseAmount(liquidityProvider.rune_deposit_value),
|
|
1206
1185
|
};
|
|
1207
1186
|
const poolShare = getPoolShare(unitData, poolAsset);
|
|
1208
1187
|
// console.log(poolShare.assetShare.toNumber(), poolShare.runeShare.toNumber())
|
|
@@ -1238,22 +1217,21 @@ class ThorchainQuery {
|
|
|
1238
1217
|
estimateWithdrawLP(params) {
|
|
1239
1218
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1240
1219
|
// Caution Dust Limits: BTC,BCH,LTC chains 10k sats; DOGE 1m Sats; ETH 0 wei; THOR 0 RUNE.
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
const memberDetail = yield this.checkLiquidityPosition(params.asset, params.assetAddress);
|
|
1220
|
+
const assetOrRuneAddress = params.assetAddress ? params.assetAddress : params.runeAddress;
|
|
1221
|
+
const memberDetail = yield this.checkLiquidityPosition(params.asset, assetOrRuneAddress);
|
|
1244
1222
|
const dustValues = yield this.getDustValues(params.asset); // returns asset and rune dust values
|
|
1245
1223
|
const assetPool = yield this.thorchainCache.getPoolForAsset(params.asset);
|
|
1246
1224
|
// get pool share from unit data
|
|
1247
1225
|
const poolShare = getPoolShare({
|
|
1248
|
-
liquidityUnits:
|
|
1249
|
-
totalUnits:
|
|
1226
|
+
liquidityUnits: new BigNumber(memberDetail.position.units),
|
|
1227
|
+
totalUnits: new BigNumber(assetPool.pool.liquidityUnits),
|
|
1250
1228
|
}, assetPool);
|
|
1251
1229
|
// calculate total fees
|
|
1252
|
-
const totalFees = (yield this.convert(dustValues.asset, AssetRuneNative
|
|
1230
|
+
const totalFees = (yield this.convert(dustValues.asset, AssetRuneNative)).plus(dustValues.rune);
|
|
1253
1231
|
// get slip on liquidity removal
|
|
1254
1232
|
const slip = getSlipOnLiquidity({
|
|
1255
|
-
asset: poolShare.assetShare
|
|
1256
|
-
rune: poolShare.runeShare
|
|
1233
|
+
asset: poolShare.assetShare,
|
|
1234
|
+
rune: poolShare.runeShare,
|
|
1257
1235
|
}, assetPool);
|
|
1258
1236
|
// TODO make sure we compare wait times for withdrawing both rune and asset OR just rune OR just asset
|
|
1259
1237
|
const waitTimeSecondsForAsset = yield this.confCounting(poolShare.assetShare.div(params.percentage / 100));
|
|
@@ -1269,6 +1247,8 @@ class ThorchainQuery {
|
|
|
1269
1247
|
waitTimeSeconds = waitTimeSecondsForRune;
|
|
1270
1248
|
}
|
|
1271
1249
|
const estimateLP = {
|
|
1250
|
+
assetAddress: memberDetail.position.asset_address,
|
|
1251
|
+
runeAddress: memberDetail.position.rune_address,
|
|
1272
1252
|
slipPercent: slip.times(100),
|
|
1273
1253
|
transactionFee: {
|
|
1274
1254
|
assetFee: dustValues.asset,
|
|
@@ -1279,6 +1259,7 @@ class ThorchainQuery {
|
|
|
1279
1259
|
runeAmount: poolShare.runeShare,
|
|
1280
1260
|
estimatedWaitSeconds: waitTimeSeconds,
|
|
1281
1261
|
impermanentLossProtection: memberDetail.impermanentLossProtection,
|
|
1262
|
+
assetPool: assetPool.pool.asset,
|
|
1282
1263
|
};
|
|
1283
1264
|
return estimateLP;
|
|
1284
1265
|
});
|
|
@@ -1295,42 +1276,42 @@ class ThorchainQuery {
|
|
|
1295
1276
|
case 'BNB':
|
|
1296
1277
|
dustValues = {
|
|
1297
1278
|
asset: new CryptoAmount(assetToBase(assetAmount(0.000001)), AssetBNB),
|
|
1298
|
-
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative
|
|
1279
|
+
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative),
|
|
1299
1280
|
};
|
|
1300
1281
|
return dustValues;
|
|
1301
1282
|
case 'BTC' :
|
|
1302
1283
|
// 10k sats
|
|
1303
1284
|
dustValues = {
|
|
1304
1285
|
asset: new CryptoAmount(assetToBase(assetAmount(0.0001)), asset),
|
|
1305
|
-
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative
|
|
1286
|
+
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative),
|
|
1306
1287
|
};
|
|
1307
1288
|
return dustValues;
|
|
1308
1289
|
case 'ETH':
|
|
1309
1290
|
// 0 wei
|
|
1310
1291
|
dustValues = {
|
|
1311
1292
|
asset: new CryptoAmount(assetToBase(assetAmount(0)), asset),
|
|
1312
|
-
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative
|
|
1293
|
+
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative),
|
|
1313
1294
|
};
|
|
1314
1295
|
return dustValues;
|
|
1315
1296
|
case 'THOR':
|
|
1316
1297
|
// 0 Rune
|
|
1317
1298
|
dustValues = {
|
|
1318
1299
|
asset: new CryptoAmount(assetToBase(assetAmount(0)), asset),
|
|
1319
|
-
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative
|
|
1300
|
+
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative),
|
|
1320
1301
|
};
|
|
1321
1302
|
return dustValues;
|
|
1322
1303
|
case 'GAIA':
|
|
1323
1304
|
// 0 GAIA
|
|
1324
1305
|
dustValues = {
|
|
1325
1306
|
asset: new CryptoAmount(assetToBase(assetAmount(0)), asset),
|
|
1326
|
-
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative
|
|
1307
|
+
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative),
|
|
1327
1308
|
};
|
|
1328
1309
|
return dustValues;
|
|
1329
1310
|
case 'DOGE':
|
|
1330
1311
|
// 1 million sats
|
|
1331
1312
|
dustValues = {
|
|
1332
1313
|
asset: new CryptoAmount(assetToBase(assetAmount(0.01)), asset),
|
|
1333
|
-
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative
|
|
1314
|
+
rune: new CryptoAmount(assetToBase(assetAmount(0)), AssetRuneNative),
|
|
1334
1315
|
};
|
|
1335
1316
|
return dustValues;
|
|
1336
1317
|
default:
|
|
@@ -1352,8 +1333,8 @@ class LiquidityPool {
|
|
|
1352
1333
|
this.asset = asset;
|
|
1353
1334
|
this.decimals = decimals;
|
|
1354
1335
|
this.assetString = this.pool.asset;
|
|
1355
|
-
this.assetBalance = baseAmount
|
|
1356
|
-
this.runeBalance = baseAmount
|
|
1336
|
+
this.assetBalance = baseAmount(this.pool.assetDepth);
|
|
1337
|
+
this.runeBalance = baseAmount(this.pool.runeDepth);
|
|
1357
1338
|
this.runeToAssetRatio = this.runeBalance.amount().div(this.assetBalance.amount());
|
|
1358
1339
|
this.assetToRuneRatio = this.assetBalance.amount().div(this.runeBalance.amount());
|
|
1359
1340
|
}
|
|
@@ -1515,15 +1496,17 @@ class ThorchainCache {
|
|
|
1515
1496
|
*/
|
|
1516
1497
|
refereshAsgardCache() {
|
|
1517
1498
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1518
|
-
const
|
|
1499
|
+
const inboundAddresses = yield this.thornode.getInboundAddresses();
|
|
1519
1500
|
const map = {};
|
|
1520
|
-
if (
|
|
1521
|
-
for (const inboundAddress of
|
|
1501
|
+
if (inboundAddresses) {
|
|
1502
|
+
for (const inboundAddress of inboundAddresses) {
|
|
1503
|
+
if (!inboundAddress.chain)
|
|
1504
|
+
throw Error('chain needed');
|
|
1522
1505
|
map[inboundAddress.chain] = inboundAddress;
|
|
1523
1506
|
}
|
|
1524
1507
|
this.asgardAssetsCache = {
|
|
1525
1508
|
lastRefreshed: Date.now(),
|
|
1526
|
-
|
|
1509
|
+
inboundAddresses: map,
|
|
1527
1510
|
};
|
|
1528
1511
|
}
|
|
1529
1512
|
});
|
|
@@ -1536,7 +1519,49 @@ class ThorchainCache {
|
|
|
1536
1519
|
*/
|
|
1537
1520
|
refereshInboundDetailCache() {
|
|
1538
1521
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1539
|
-
const
|
|
1522
|
+
const [mimirDetails, allInboundAddresses] = yield Promise.all([
|
|
1523
|
+
this.midgard.getMimirDetails(),
|
|
1524
|
+
this.thornode.getInboundAddresses(),
|
|
1525
|
+
]);
|
|
1526
|
+
const inboundDetails = {};
|
|
1527
|
+
for (const inbound of allInboundAddresses) {
|
|
1528
|
+
const chain = inbound.chain;
|
|
1529
|
+
if (!chain ||
|
|
1530
|
+
!inbound.gas_rate ||
|
|
1531
|
+
!inbound.address ||
|
|
1532
|
+
!inbound.gas_rate_units ||
|
|
1533
|
+
!inbound.outbound_tx_size ||
|
|
1534
|
+
!inbound.outbound_fee ||
|
|
1535
|
+
!inbound.gas_rate_units)
|
|
1536
|
+
throw new Error(`Missing required inbound info`);
|
|
1537
|
+
const details = {
|
|
1538
|
+
chain: chain,
|
|
1539
|
+
address: inbound.address,
|
|
1540
|
+
router: inbound.router,
|
|
1541
|
+
gasRate: new BigNumber(inbound.gas_rate),
|
|
1542
|
+
gasRateUnits: inbound.gas_rate_units,
|
|
1543
|
+
outboundTxSize: new BigNumber(inbound.outbound_tx_size),
|
|
1544
|
+
outboundFee: new BigNumber(inbound.outbound_fee),
|
|
1545
|
+
haltedChain: (inbound === null || inbound === void 0 ? void 0 : inbound.halted) || !!mimirDetails[`HALT${chain}CHAIN`] || !!mimirDetails['HALTCHAINGLOBAL'],
|
|
1546
|
+
haltedTrading: !!mimirDetails['HALTTRADING'] || !!mimirDetails[`HALT${chain}TRADING`],
|
|
1547
|
+
haltedLP: !!mimirDetails['PAUSELP'] || !!mimirDetails[`PAUSELP${chain}`],
|
|
1548
|
+
};
|
|
1549
|
+
inboundDetails[chain] = details;
|
|
1550
|
+
}
|
|
1551
|
+
// add mock THORCHAIN inbound details
|
|
1552
|
+
const details = {
|
|
1553
|
+
chain: Chain.THORChain,
|
|
1554
|
+
address: '',
|
|
1555
|
+
router: '',
|
|
1556
|
+
gasRate: new BigNumber(0),
|
|
1557
|
+
gasRateUnits: '',
|
|
1558
|
+
outboundTxSize: new BigNumber(0),
|
|
1559
|
+
outboundFee: new BigNumber(0),
|
|
1560
|
+
haltedChain: false,
|
|
1561
|
+
haltedTrading: !!mimirDetails['HALTTRADING'],
|
|
1562
|
+
haltedLP: false, //
|
|
1563
|
+
};
|
|
1564
|
+
inboundDetails[Chain.THORChain] = details;
|
|
1540
1565
|
this.inboundDetailCache = {
|
|
1541
1566
|
lastRefreshed: Date.now(),
|
|
1542
1567
|
inboundDetails,
|
|
@@ -1607,7 +1632,7 @@ class ThorchainCache {
|
|
|
1607
1632
|
let baseAmountOut = input.baseAmount.times(exchangeRate).amount();
|
|
1608
1633
|
const adjustDecimals = outDecimals - inDecimals;
|
|
1609
1634
|
baseAmountOut = baseAmountOut.times(Math.pow(10, adjustDecimals));
|
|
1610
|
-
const amt = baseAmount
|
|
1635
|
+
const amt = baseAmount(baseAmountOut, outDecimals);
|
|
1611
1636
|
const result = new CryptoAmount(amt, outAsset);
|
|
1612
1637
|
// console.log(
|
|
1613
1638
|
// `${input.formatedAssetString()} ${input.asset.ticker} = ${result.formatedAssetString()} ${outAsset.ticker}`,
|
|
@@ -1627,7 +1652,7 @@ class ThorchainCache {
|
|
|
1627
1652
|
}
|
|
1628
1653
|
getRouterAddressForChain(chain) {
|
|
1629
1654
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1630
|
-
const inboundAsgard = (yield this.
|
|
1655
|
+
const inboundAsgard = (yield this.getInboundAddresses())[chain];
|
|
1631
1656
|
if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.router)) {
|
|
1632
1657
|
throw new Error('router address is not defined');
|
|
1633
1658
|
}
|
|
@@ -1638,7 +1663,7 @@ class ThorchainCache {
|
|
|
1638
1663
|
*
|
|
1639
1664
|
* @returns - inbound adresses item
|
|
1640
1665
|
*/
|
|
1641
|
-
|
|
1666
|
+
getInboundAddresses() {
|
|
1642
1667
|
var _a;
|
|
1643
1668
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1644
1669
|
const millisSinceLastRefeshed = Date.now() - (((_a = this.asgardAssetsCache) === null || _a === void 0 ? void 0 : _a.lastRefreshed) || 0);
|
|
@@ -1651,7 +1676,7 @@ class ThorchainCache {
|
|
|
1651
1676
|
}
|
|
1652
1677
|
}
|
|
1653
1678
|
if (this.asgardAssetsCache) {
|
|
1654
|
-
return this.asgardAssetsCache.
|
|
1679
|
+
return this.asgardAssetsCache.inboundAddresses;
|
|
1655
1680
|
}
|
|
1656
1681
|
else {
|
|
1657
1682
|
throw Error(`Could not refresh refereshAsgardCache `);
|
|
@@ -1782,52 +1807,6 @@ class Midgard {
|
|
|
1782
1807
|
throw Error(`Midgard not responding`);
|
|
1783
1808
|
});
|
|
1784
1809
|
}
|
|
1785
|
-
getAllInboundAddresses() {
|
|
1786
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1787
|
-
for (const api of this.midgardApis) {
|
|
1788
|
-
try {
|
|
1789
|
-
return (yield api.getProxiedInboundAddresses()).data;
|
|
1790
|
-
}
|
|
1791
|
-
catch (e) {
|
|
1792
|
-
console.error(e);
|
|
1793
|
-
}
|
|
1794
|
-
}
|
|
1795
|
-
throw Error(`Midgard not responding`);
|
|
1796
|
-
});
|
|
1797
|
-
}
|
|
1798
|
-
/**
|
|
1799
|
-
* Gets the Inbound Details
|
|
1800
|
-
* @returns inbound details
|
|
1801
|
-
*/
|
|
1802
|
-
getInboundDetails() {
|
|
1803
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1804
|
-
const [mimirDetails, allInboundDetails] = yield Promise.all([this.getMimirDetails(), this.getAllInboundAddresses()]);
|
|
1805
|
-
const inboundDetails = {};
|
|
1806
|
-
for (const inboundDetail of allInboundDetails) {
|
|
1807
|
-
const chain = inboundDetail.chain;
|
|
1808
|
-
if (!inboundDetail.gas_rate)
|
|
1809
|
-
throw new Error(`Could not get gas_rate for ${chain}`);
|
|
1810
|
-
const details = {
|
|
1811
|
-
vault: inboundDetail.address,
|
|
1812
|
-
gas_rate: new BigNumber$1(inboundDetail.gas_rate),
|
|
1813
|
-
haltedChain: (inboundDetail === null || inboundDetail === void 0 ? void 0 : inboundDetail.halted) || !!mimirDetails[`HALT${chain}CHAIN`] || !!mimirDetails['HALTCHAINGLOBAL'],
|
|
1814
|
-
haltedTrading: !!mimirDetails['HALTTRADING'] || !!mimirDetails[`HALT${chain}TRADING`],
|
|
1815
|
-
haltedLP: !!mimirDetails['PAUSELP'] || !!mimirDetails[`PAUSELP${chain}`],
|
|
1816
|
-
};
|
|
1817
|
-
inboundDetails[chain] = details;
|
|
1818
|
-
}
|
|
1819
|
-
// add mock THORCHAIN inbound details
|
|
1820
|
-
const details = {
|
|
1821
|
-
vault: '',
|
|
1822
|
-
gas_rate: new BigNumber$1(0),
|
|
1823
|
-
haltedChain: false,
|
|
1824
|
-
haltedTrading: !!mimirDetails['HALTTRADING'],
|
|
1825
|
-
haltedLP: false, //
|
|
1826
|
-
};
|
|
1827
|
-
inboundDetails[Chain.THORChain] = details;
|
|
1828
|
-
return inboundDetails;
|
|
1829
|
-
});
|
|
1830
|
-
}
|
|
1831
1810
|
/**
|
|
1832
1811
|
*
|
|
1833
1812
|
* @returns - constants
|
|
@@ -1857,7 +1836,7 @@ class Midgard {
|
|
|
1857
1836
|
for (const baseUrl of this.config.midgardBaseUrls) {
|
|
1858
1837
|
try {
|
|
1859
1838
|
const { data } = yield axios.get(`${baseUrl}${path}`);
|
|
1860
|
-
const value = new CryptoAmount(baseAmount
|
|
1839
|
+
const value = new CryptoAmount(baseAmount(data['scheduled_outbound_value']), AssetRuneNative);
|
|
1861
1840
|
return value;
|
|
1862
1841
|
}
|
|
1863
1842
|
catch (e) {
|
|
@@ -2100,6 +2079,27 @@ class Thornode {
|
|
|
2100
2079
|
throw new Error(`THORNode not responding`);
|
|
2101
2080
|
});
|
|
2102
2081
|
}
|
|
2082
|
+
/**
|
|
2083
|
+
*
|
|
2084
|
+
* @param asset - asset string
|
|
2085
|
+
* @param address - address
|
|
2086
|
+
* @param height - optional block height, defaults to current tip
|
|
2087
|
+
* @returns
|
|
2088
|
+
*/
|
|
2089
|
+
getInboundAddresses() {
|
|
2090
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
2091
|
+
for (const api of this.networkApi) {
|
|
2092
|
+
try {
|
|
2093
|
+
const resp = (yield api.inboundAddresses()).data;
|
|
2094
|
+
return resp;
|
|
2095
|
+
}
|
|
2096
|
+
catch (e) {
|
|
2097
|
+
console.error(e);
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2100
|
+
throw new Error(`THORNode not responding`);
|
|
2101
|
+
});
|
|
2102
|
+
}
|
|
2103
2103
|
}
|
|
2104
2104
|
|
|
2105
2105
|
export { CryptoAmount, LiquidityPool, Midgard, ThorchainCache, ThorchainQuery, Thornode, TxStage, calcNetworkFee, getDoubleSwap, getLiquidityProtectionData, getLiquidityUnits, getPoolShare, getSingleSwap, getSlipOnLiquidity };
|