@xchainjs/xchain-thorchain-query 0.1.0-alpha → 0.1.0-alpha1

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 CHANGED
@@ -1,8 +1,6 @@
1
1
  # `@xchainjs/xchain-thorchain-query`
2
2
 
3
- Thorchain-query module to query thorchain for
4
-
5
- ## Modules
3
+ Thorchain-query module to query thorchain for estimation of swaps. Returns a TxDetail object with all the information needed to conduct a swap.
6
4
 
7
5
  ## Installation
8
6
 
@@ -10,10 +8,46 @@ Thorchain-query module to query thorchain for
10
8
  yarn add @xchainjs/xchain-thorchain-query
11
9
  ```
12
10
 
11
+ Following peer dependencies have to be installed into your project. These are not included in `@xchainjs/xchain-thorchain-query`.
12
+
13
+ ```
14
+ yarn add @xchainjs/xchain-client @xchainjs/xchain-util @xchainjs/xchain-midgard @xchainjs/xchain-thornode axios
15
+
16
+ ```
17
+
13
18
  ## Examples
14
19
 
20
+ Estimation example from a swap of 2 BTC to RUNE
21
+
22
+ ```ts
23
+ {
24
+ memo: '=:THOR.RUNE::2071168559999',
25
+ expiry: 2022-09-07T02:16:45.732Z,
26
+ toAddress: '',
27
+ txEstimate: {
28
+ input: '₿ 2',
29
+ totalFees: {
30
+ inboundFee: 'ᚱ 0.02',
31
+ swapFee: 'ᚱ 52.85380999',
32
+ outboundFee: 'ᚱ 0.06',
33
+ affiliateFee: 'ᚱ 0'
34
+ },
35
+ slipPercentage: '0.00246920801878638026',
36
+ netOutput: 'ᚱ 21,352.25318742',
37
+ waitTimeSeconds: '1248',
38
+ canSwap: true,
39
+ errors: []
40
+ }
41
+ }
15
42
  ```
16
43
 
17
44
  ## Documentation
18
45
 
46
+ For bash exmples, see example folder at the base of this repository xchainjs/xchainjs-lib.
47
+
48
+ ```
49
+ ### [`xchain-thorchain-query`](http://docs.xchainjs.org/xchain-thorchain-query/)
50
+
51
+ [`How xchain-thorchain-query works`](http://docs.xchainjs.org/xchain-thorchain-query/how-it-works.html)\
52
+ [`How to use xchain-thorchain-query`](http://docs.xchainjs.org/xchain-thorchain-query/how-to-use.html)
19
53
  ```
package/lib/index.esm.js CHANGED
@@ -431,23 +431,47 @@ class ThorchainQuery {
431
431
 
432
432
  * @returns The SwapEstimate
433
433
  */
434
- estimateSwap(params, destinationAddress = '', affiliateAddress = '', interfaceID = 999) {
434
+ estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress = '', interfaceID = 999, affiliateFeePercent = 0, slipLimit, }) {
435
435
  return __awaiter(this, void 0, void 0, function* () {
436
- this.isValidSwap(params);
436
+ this.isValidSwap({
437
+ input,
438
+ destinationAsset,
439
+ destinationAddress,
440
+ affiliateAddress,
441
+ interfaceID,
442
+ affiliateFeePercent,
443
+ slipLimit,
444
+ });
437
445
  const inboundDetails = yield this.thorchainCache.getInboundDetails();
438
- const sourceInboundDetails = inboundDetails[params.input.asset.chain];
446
+ const sourceInboundDetails = inboundDetails[input.asset.chain];
439
447
  // console.log(JSON.stringify(sourceInboundDetails, null, 2))
440
- const destinationInboundDetails = inboundDetails[params.destinationAsset.chain];
448
+ const destinationInboundDetails = inboundDetails[destinationAsset.chain];
441
449
  // console.log(JSON.stringify(destinationInboundDetails, null, 2))
442
- const swapEstimate = yield this.calcSwapEstimate(params, sourceInboundDetails, destinationInboundDetails);
450
+ const swapEstimate = yield this.calcSwapEstimate({
451
+ input,
452
+ destinationAsset,
453
+ destinationAddress,
454
+ affiliateAddress,
455
+ interfaceID,
456
+ affiliateFeePercent,
457
+ slipLimit,
458
+ }, sourceInboundDetails, destinationInboundDetails);
443
459
  // Remove any affiliateFee. netInput * affiliateFee (%age) of the destination asset type
444
- const affiliateFee = params.input.baseAmount.times(params.affiliateFeePercent || 0);
460
+ const affiliateFee = input.baseAmount.times(affiliateFeePercent || 0);
445
461
  // Calculate expiry time
446
462
  const currentDatetime = new Date();
447
463
  const minutesToAdd = 15;
448
464
  const expiryDatetime = new Date(currentDatetime.getTime() + minutesToAdd * 60000);
449
465
  // Check for errors
450
- const errors = yield this.getSwapEstimateErrors(params, swapEstimate, sourceInboundDetails, destinationInboundDetails);
466
+ const errors = yield this.getSwapEstimateErrors({
467
+ input,
468
+ destinationAsset,
469
+ destinationAddress,
470
+ affiliateAddress,
471
+ interfaceID,
472
+ affiliateFeePercent,
473
+ slipLimit,
474
+ }, swapEstimate, sourceInboundDetails, destinationInboundDetails);
451
475
  const txDetails = {
452
476
  memo: '',
453
477
  toAddress: '',
@@ -461,26 +485,26 @@ class ThorchainQuery {
461
485
  else {
462
486
  txDetails.txEstimate.canSwap = true;
463
487
  // Retrieve inbound Asgard address.
464
- const inboundAsgard = (yield this.thorchainCache.getInboundAddressesItems())[params.input.asset.chain];
488
+ const inboundAsgard = (yield this.thorchainCache.getInboundAddressesItems())[input.asset.chain];
465
489
  txDetails.toAddress = (inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.address) || '';
466
490
  // Work out LIM from the slip percentage
467
491
  let limPercentage = BN_1;
468
- if (params.slipLimit) {
469
- limPercentage = BN_1.minus(params.slipLimit || 1);
492
+ if (slipLimit) {
493
+ limPercentage = BN_1.minus(slipLimit || 1);
470
494
  } // else allowed slip is 100%
471
495
  const limAssetAmount = swapEstimate.netOutput.times(limPercentage);
472
- const inboundDelay = yield this.confCounting(params.input);
496
+ const inboundDelay = yield this.confCounting(input);
473
497
  const outboundDelay = yield this.outboundDelay(limAssetAmount);
474
498
  txDetails.txEstimate.waitTimeSeconds = outboundDelay + inboundDelay;
475
499
  // Construct memo
476
500
  txDetails.memo = this.constructSwapMemo({
477
- input: params.input,
478
- destinationAsset: params.destinationAsset,
501
+ input: input,
502
+ destinationAsset: destinationAsset,
479
503
  limit: limAssetAmount.baseAmount,
480
- destinationAddress,
481
- affiliateAddress,
504
+ destinationAddress: destinationAddress,
505
+ affiliateAddress: affiliateAddress,
482
506
  affiliateFee,
483
- interfaceID,
507
+ interfaceID: interfaceID,
484
508
  });
485
509
  }
486
510
  return txDetails;
package/lib/index.js CHANGED
@@ -440,23 +440,47 @@ class ThorchainQuery {
440
440
 
441
441
  * @returns The SwapEstimate
442
442
  */
443
- estimateSwap(params, destinationAddress = '', affiliateAddress = '', interfaceID = 999) {
443
+ estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress = '', interfaceID = 999, affiliateFeePercent = 0, slipLimit, }) {
444
444
  return __awaiter(this, void 0, void 0, function* () {
445
- this.isValidSwap(params);
445
+ this.isValidSwap({
446
+ input,
447
+ destinationAsset,
448
+ destinationAddress,
449
+ affiliateAddress,
450
+ interfaceID,
451
+ affiliateFeePercent,
452
+ slipLimit,
453
+ });
446
454
  const inboundDetails = yield this.thorchainCache.getInboundDetails();
447
- const sourceInboundDetails = inboundDetails[params.input.asset.chain];
455
+ const sourceInboundDetails = inboundDetails[input.asset.chain];
448
456
  // console.log(JSON.stringify(sourceInboundDetails, null, 2))
449
- const destinationInboundDetails = inboundDetails[params.destinationAsset.chain];
457
+ const destinationInboundDetails = inboundDetails[destinationAsset.chain];
450
458
  // console.log(JSON.stringify(destinationInboundDetails, null, 2))
451
- const swapEstimate = yield this.calcSwapEstimate(params, sourceInboundDetails, destinationInboundDetails);
459
+ const swapEstimate = yield this.calcSwapEstimate({
460
+ input,
461
+ destinationAsset,
462
+ destinationAddress,
463
+ affiliateAddress,
464
+ interfaceID,
465
+ affiliateFeePercent,
466
+ slipLimit,
467
+ }, sourceInboundDetails, destinationInboundDetails);
452
468
  // Remove any affiliateFee. netInput * affiliateFee (%age) of the destination asset type
453
- const affiliateFee = params.input.baseAmount.times(params.affiliateFeePercent || 0);
469
+ const affiliateFee = input.baseAmount.times(affiliateFeePercent || 0);
454
470
  // Calculate expiry time
455
471
  const currentDatetime = new Date();
456
472
  const minutesToAdd = 15;
457
473
  const expiryDatetime = new Date(currentDatetime.getTime() + minutesToAdd * 60000);
458
474
  // Check for errors
459
- const errors = yield this.getSwapEstimateErrors(params, swapEstimate, sourceInboundDetails, destinationInboundDetails);
475
+ const errors = yield this.getSwapEstimateErrors({
476
+ input,
477
+ destinationAsset,
478
+ destinationAddress,
479
+ affiliateAddress,
480
+ interfaceID,
481
+ affiliateFeePercent,
482
+ slipLimit,
483
+ }, swapEstimate, sourceInboundDetails, destinationInboundDetails);
460
484
  const txDetails = {
461
485
  memo: '',
462
486
  toAddress: '',
@@ -470,26 +494,26 @@ class ThorchainQuery {
470
494
  else {
471
495
  txDetails.txEstimate.canSwap = true;
472
496
  // Retrieve inbound Asgard address.
473
- const inboundAsgard = (yield this.thorchainCache.getInboundAddressesItems())[params.input.asset.chain];
497
+ const inboundAsgard = (yield this.thorchainCache.getInboundAddressesItems())[input.asset.chain];
474
498
  txDetails.toAddress = (inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.address) || '';
475
499
  // Work out LIM from the slip percentage
476
500
  let limPercentage = BN_1;
477
- if (params.slipLimit) {
478
- limPercentage = BN_1.minus(params.slipLimit || 1);
501
+ if (slipLimit) {
502
+ limPercentage = BN_1.minus(slipLimit || 1);
479
503
  } // else allowed slip is 100%
480
504
  const limAssetAmount = swapEstimate.netOutput.times(limPercentage);
481
- const inboundDelay = yield this.confCounting(params.input);
505
+ const inboundDelay = yield this.confCounting(input);
482
506
  const outboundDelay = yield this.outboundDelay(limAssetAmount);
483
507
  txDetails.txEstimate.waitTimeSeconds = outboundDelay + inboundDelay;
484
508
  // Construct memo
485
509
  txDetails.memo = this.constructSwapMemo({
486
- input: params.input,
487
- destinationAsset: params.destinationAsset,
510
+ input: input,
511
+ destinationAsset: destinationAsset,
488
512
  limit: limAssetAmount.baseAmount,
489
- destinationAddress,
490
- affiliateAddress,
513
+ destinationAddress: destinationAddress,
514
+ affiliateAddress: affiliateAddress,
491
515
  affiliateFee,
492
- interfaceID,
516
+ interfaceID: interfaceID,
493
517
  });
494
518
  }
495
519
  return txDetails;
@@ -26,7 +26,7 @@ export declare class ThorchainQuery {
26
26
 
27
27
  * @returns The SwapEstimate
28
28
  */
29
- estimateSwap(params: EstimateSwapParams, destinationAddress?: string, affiliateAddress?: string, interfaceID?: number): Promise<TxDetails>;
29
+ estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress, interfaceID, affiliateFeePercent, slipLimit, }: EstimateSwapParams): Promise<TxDetails>;
30
30
  /**
31
31
  * Basic Checks for swap information
32
32
  * @param params
package/lib/types.d.ts CHANGED
@@ -41,6 +41,9 @@ export declare type MidgardConfig = {
41
41
  export declare type EstimateSwapParams = {
42
42
  input: CryptoAmount;
43
43
  destinationAsset: Asset;
44
+ destinationAddress: Address;
45
+ affiliateAddress?: Address;
46
+ interfaceID?: number;
44
47
  affiliateFeePercent?: number;
45
48
  slipLimit?: BigNumber;
46
49
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-thorchain-query",
3
- "version": "0.1.0-alpha",
3
+ "version": "0.1.0-alpha1",
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": [