@xchainjs/xchain-thorchain-amm 0.3.0 → 0.3.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 CHANGED
@@ -28,11 +28,11 @@ Following peer dependencies have to be installed into your project. These are no
28
28
  "@xchainjs/xchain-ethereum": "^0.27.0",
29
29
  "@xchainjs/xchain-evm": "^0.1.0-alpha2",
30
30
  "@xchainjs/xchain-avax": "^0.1.0-alpha3",
31
- "@xchainjs/xchain-litecoin": "^0.10.0",
31
+ "@xchainjs/xchain-litecoin": "^0.10.3",
32
32
  "@xchainjs/xchain-midgard": "^0.1.0",
33
33
  "@xchainjs/xchain-terra": "^0.3.0",
34
34
  "@xchainjs/xchain-thorchain": "^0.26.0",
35
- "@xchainjs/xchain-thorchain-query": "^0.1.0",
35
+ "@xchainjs/xchain-thorchain-query": "^0.1.2",
36
36
  "@xchainjs/xchain-thornode": "^0.1.0",
37
37
  "@xchainjs/xchain-util": "^0.9.0",
38
38
  "axios": "^0.27.2",
@@ -54,6 +54,11 @@ Following peer dependencies have to be installed into your project. These are no
54
54
  [How thorchain-amm works](http://docs.xchainjs.org/xchain-thorchain-amm/how-it-works.html)\
55
55
  [How to use thorchain-amm](http://docs.xchainjs.org/xchain-thorchain-amm/how-to-use.html)
56
56
 
57
+ For live examples
58
+ [Do Swap](https://replit.com/@thorchain/doSwap-Single)
59
+ [Add Liquidity](https://replit.com/@thorchain/addLiquidity)
60
+ [Withdraw liquidity](https://replit.com/@thorchain/removeLiquidity)
61
+
57
62
  ##
58
63
 
59
64
  tsconfig compiler options
package/lib/index.esm.js CHANGED
@@ -11,6 +11,7 @@ import { Client as Client$4 } from '@xchainjs/xchain-thorchain';
11
11
  import { eqAsset, AssetETH, getContractAddressFromAsset, baseAmount, Chain, assetToString, AssetBTC } from '@xchainjs/xchain-util';
12
12
  import { ethers } from 'ethers';
13
13
  import { MAX_APPROVAL as MAX_APPROVAL$1 } from '@xchainjs/xchain-evm';
14
+ import { ThorchainQuery } from '@xchainjs/xchain-thorchain-query';
14
15
  import { BigNumber } from 'bignumber.js';
15
16
 
16
17
  /*! *****************************************************************************
@@ -779,22 +780,22 @@ class Wallet {
779
780
  const txSubmitted = [];
780
781
  // symmetrical add
781
782
  if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.gt(0)) {
782
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}:${addressRune}`;
783
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
784
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}:${addressAsset}`;
785
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
783
+ constructedMemo = `+:${params.assetPool}:${addressRune}`;
784
+ txSubmitted.push(yield this.addAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
785
+ constructedMemo = `+:${params.assetPool}:${addressAsset}`;
786
+ txSubmitted.push(yield this.addRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
786
787
  return txSubmitted;
787
788
  }
788
789
  else if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.eq(0)) {
789
790
  // asymmetrical asset only
790
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}`;
791
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
791
+ constructedMemo = `+:${params.assetPool}`;
792
+ txSubmitted.push(yield this.addAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
792
793
  return txSubmitted;
793
794
  }
794
795
  else {
795
796
  // asymmetrical rune only
796
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}`;
797
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
797
+ constructedMemo = `+:${params.assetPool}`;
798
+ txSubmitted.push(yield this.addRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
798
799
  return txSubmitted;
799
800
  }
800
801
  });
@@ -804,35 +805,33 @@ class Wallet {
804
805
  * @param params - parameters required for liquidity position
805
806
  * @returns object with tx response, url and wait time in seconds
806
807
  */
807
- removeLiquidity(params) {
808
+ withdrawLiquidity(params) {
808
809
  return __awaiter(this, void 0, void 0, function* () {
809
- const assetClient = this.clients[params.asset.asset.chain];
810
- const inboundAsgard = (yield this.thorchainQuery.thorchainCache.getInboundDetails())[params.asset.asset.chain];
811
- if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.address)) {
812
- throw new Error('Vault address is not defined');
813
- }
810
+ const assetClient = this.clients[params.assetFee.asset.chain];
811
+ const inboundAsgard = (yield this.thorchainQuery.thorchainCache.getInboundDetails())[params.assetFee.asset.chain]
812
+ .address;
814
813
  const waitTimeSeconds = params.waitTimeSeconds;
815
- const thorchainClient = this.clients[params.rune.asset.chain];
814
+ const thorchainClient = this.clients[params.runeFee.asset.chain];
816
815
  const basisPoints = (params.percentage * 100).toFixed(); // convert to basis points
817
816
  let constructedMemo = '';
818
817
  const txSubmitted = [];
819
- if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.gt(0)) {
820
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
821
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard.address));
822
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
823
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
818
+ if (params.assetAddress && params.runeAddress) {
819
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
820
+ txSubmitted.push(yield this.withdrawAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
821
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
822
+ txSubmitted.push(yield this.withdrawRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
824
823
  return txSubmitted;
825
824
  }
826
- else if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.eq(0)) {
825
+ else if (params.assetAddress && !params.runeAddress) {
827
826
  // asymmetrical asset only
828
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
829
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard.address));
827
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
828
+ txSubmitted.push(yield this.withdrawAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
830
829
  return txSubmitted;
831
830
  }
832
831
  else {
833
832
  // asymmetrical rune only
834
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
835
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
833
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
834
+ txSubmitted.push(yield this.withdrawRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
836
835
  return txSubmitted;
837
836
  }
838
837
  });
@@ -846,7 +845,7 @@ class Wallet {
846
845
  * @param inboundAsgard - inbound Asgard address for the LP
847
846
  * @returns - tx object
848
847
  */
849
- addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard) {
848
+ addAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard) {
850
849
  return __awaiter(this, void 0, void 0, function* () {
851
850
  if (params.asset.asset.chain === Chain.Ethereum) {
852
851
  const addParams = {
@@ -856,7 +855,6 @@ class Wallet {
856
855
  feeOption: FeeOption.Fast,
857
856
  memo: constructedMemo,
858
857
  };
859
- console.log(addParams.amount.amount().toNumber());
860
858
  const hash = yield this.ethHelper.sendDeposit(addParams);
861
859
  return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
862
860
  }
@@ -881,7 +879,6 @@ class Wallet {
881
879
  memo: constructedMemo,
882
880
  };
883
881
  try {
884
- console.log(addParams);
885
882
  const hash = yield assetClient.transfer(addParams);
886
883
  return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
887
884
  }
@@ -892,13 +889,67 @@ class Wallet {
892
889
  }
893
890
  });
894
891
  }
892
+ /**
893
+ *
894
+ * @param params - parameters for withdraw liquidity
895
+ * @param constructedMemo - memo needed for thorchain execution
896
+ * @param assetClient - asset client to call transfer
897
+ * @param waitTimeSeconds - return back estimated wait
898
+ * @param inboundAsgard - destination address
899
+ * @returns - tx object
900
+ */
901
+ withdrawAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard) {
902
+ return __awaiter(this, void 0, void 0, function* () {
903
+ if (params.assetFee.asset.chain === Chain.Ethereum) {
904
+ const withdrawParams = {
905
+ wallIndex: 0,
906
+ asset: params.assetFee.asset,
907
+ amount: params.assetFee.baseAmount,
908
+ feeOption: FeeOption.Fast,
909
+ memo: constructedMemo,
910
+ };
911
+ // console.log(withdrawParams.amount.amount().toNumber())
912
+ const hash = yield this.ethHelper.sendDeposit(withdrawParams);
913
+ return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
914
+ }
915
+ else if (params.assetFee.asset.chain === Chain.Avalanche) {
916
+ const withdrawParams = {
917
+ wallIndex: 0,
918
+ asset: params.assetFee.asset,
919
+ amount: params.assetFee.baseAmount,
920
+ feeOption: FeeOption.Fast,
921
+ memo: constructedMemo,
922
+ };
923
+ const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
924
+ const hash = yield evmHelper.sendDeposit(withdrawParams);
925
+ return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
926
+ }
927
+ else {
928
+ const withdrawParams = {
929
+ wallIndex: 0,
930
+ asset: params.assetFee.asset,
931
+ amount: params.assetFee.baseAmount,
932
+ recipient: inboundAsgard,
933
+ memo: constructedMemo,
934
+ };
935
+ try {
936
+ const hash = yield assetClient.transfer(withdrawParams);
937
+ return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
938
+ }
939
+ catch (err) {
940
+ const hash = JSON.stringify(err);
941
+ return { hash, url: assetClient.getExplorerAddressUrl(assetClient.getAddress()), waitTimeSeconds };
942
+ }
943
+ }
944
+ });
945
+ }
895
946
  /**
896
947
  *
897
948
  * @param params - deposit parameters
898
949
  * @param memo - memo needed to withdraw lp
899
950
  * @returns - tx object
900
951
  */
901
- addOrRemoveRuneLP(params, memo, thorchainClient, waitTimeSeconds) {
952
+ addRuneLP(params, memo, thorchainClient, waitTimeSeconds) {
902
953
  return __awaiter(this, void 0, void 0, function* () {
903
954
  const thorClient = this.clients.THOR;
904
955
  const addParams = {
@@ -906,7 +957,26 @@ class Wallet {
906
957
  amount: params.rune.baseAmount,
907
958
  memo: memo,
908
959
  };
909
- console.log(addParams);
960
+ // console.log(addParams)
961
+ const hash = yield thorClient.deposit(addParams);
962
+ return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
963
+ });
964
+ }
965
+ /**
966
+ *
967
+ * @param params - withdraw parameters
968
+ * @param memo - memo needed to withdraw lp
969
+ * @returns - tx object
970
+ */
971
+ withdrawRuneLP(params, memo, thorchainClient, waitTimeSeconds) {
972
+ return __awaiter(this, void 0, void 0, function* () {
973
+ const thorClient = this.clients.THOR;
974
+ const addParams = {
975
+ asset: params.runeFee.asset,
976
+ amount: params.runeFee.baseAmount,
977
+ memo: memo,
978
+ };
979
+ // console.log(addParams)
910
980
  const hash = yield thorClient.deposit(addParams);
911
981
  return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
912
982
  });
@@ -914,6 +984,7 @@ class Wallet {
914
984
  }
915
985
 
916
986
  const BN_1 = new BigNumber(1);
987
+ const defaultQuery = new ThorchainQuery();
917
988
  /**
918
989
  * THORChain Class for interacting with THORChain.
919
990
  * Recommended main class to use for swapping with THORChain
@@ -926,7 +997,7 @@ class ThorchainAMM {
926
997
  * @param thorchainQuery - an instance of the ThorchainQuery
927
998
  * @returns ThorchainAMM
928
999
  */
929
- constructor(thorchainQuery) {
1000
+ constructor(thorchainQuery = defaultQuery) {
930
1001
  this.thorchainQuery = thorchainQuery;
931
1002
  }
932
1003
  /**
@@ -983,6 +1054,26 @@ class ThorchainAMM {
983
1054
  });
984
1055
  });
985
1056
  }
1057
+ /**
1058
+ * Wraps estimate from thorchain query
1059
+ * @param params - estimate add liquidity
1060
+ * @returns - Estimate add lp object
1061
+ */
1062
+ estimateAddLiquidity(params) {
1063
+ return __awaiter(this, void 0, void 0, function* () {
1064
+ return this.thorchainQuery.estimateAddLP(params);
1065
+ });
1066
+ }
1067
+ /**
1068
+ * Wraps estimate withdraw from thorchain query
1069
+ * @param params - estimate withdraw liquidity
1070
+ * @returns - Estimate withdraw lp object
1071
+ */
1072
+ estimateWithdrawLiquidity(params) {
1073
+ return __awaiter(this, void 0, void 0, function* () {
1074
+ return this.thorchainQuery.estimateWithdrawLP(params);
1075
+ });
1076
+ }
986
1077
  /**
987
1078
  *
988
1079
  * @param wallet - wallet class
@@ -993,10 +1084,13 @@ class ThorchainAMM {
993
1084
  return __awaiter(this, void 0, void 0, function* () {
994
1085
  // Check amounts are greater than fees and use return estimated wait
995
1086
  const checkLPAdd = yield this.thorchainQuery.estimateAddLP(params);
1087
+ if (!checkLPAdd.canAdd)
1088
+ throw Error(`${checkLPAdd.errors}`);
996
1089
  return wallet.addLiquidity({
997
1090
  asset: params.asset,
998
1091
  rune: params.rune,
999
1092
  waitTimeSeconds: checkLPAdd.estimatedWaitSeconds,
1093
+ assetPool: checkLPAdd.assetPool,
1000
1094
  });
1001
1095
  });
1002
1096
  }
@@ -1006,15 +1100,18 @@ class ThorchainAMM {
1006
1100
  * @param wallet - wallet needed to perform tx
1007
1101
  * @return
1008
1102
  */
1009
- removeLiquidityPosition(wallet, params) {
1103
+ withdrawLiquidityPosition(wallet, params) {
1010
1104
  return __awaiter(this, void 0, void 0, function* () {
1011
1105
  // Caution Dust Limits: BTC,BCH,LTC chains 10k sats; DOGE 1m Sats; ETH 0 wei; THOR 0 RUNE.
1012
- const estimateWithrawLp = yield this.thorchainQuery.estimateWithdrawLP(params);
1013
- return wallet.removeLiquidity({
1014
- asset: estimateWithrawLp.transactionFee.assetFee,
1015
- rune: estimateWithrawLp.transactionFee.runeFee,
1106
+ const withdrawParams = yield this.thorchainQuery.estimateWithdrawLP(params);
1107
+ return wallet.withdrawLiquidity({
1108
+ assetFee: withdrawParams.transactionFee.assetFee,
1109
+ runeFee: withdrawParams.transactionFee.runeFee,
1110
+ waitTimeSeconds: withdrawParams.estimatedWaitSeconds,
1016
1111
  percentage: params.percentage,
1017
- waitTimeSeconds: estimateWithrawLp.estimatedWaitSeconds,
1112
+ assetPool: withdrawParams.assetPool,
1113
+ assetAddress: withdrawParams.assetAddress,
1114
+ runeAddress: withdrawParams.runeAddress,
1018
1115
  });
1019
1116
  });
1020
1117
  }
package/lib/index.js CHANGED
@@ -15,6 +15,7 @@ var xchainThorchain = require('@xchainjs/xchain-thorchain');
15
15
  var xchainUtil = require('@xchainjs/xchain-util');
16
16
  var ethers = require('ethers');
17
17
  var xchainEvm = require('@xchainjs/xchain-evm');
18
+ var xchainThorchainQuery = require('@xchainjs/xchain-thorchain-query');
18
19
  var bignumber_js = require('bignumber.js');
19
20
 
20
21
  /*! *****************************************************************************
@@ -783,22 +784,22 @@ class Wallet {
783
784
  const txSubmitted = [];
784
785
  // symmetrical add
785
786
  if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.gt(0)) {
786
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}:${addressRune}`;
787
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
788
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}:${addressAsset}`;
789
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
787
+ constructedMemo = `+:${params.assetPool}:${addressRune}`;
788
+ txSubmitted.push(yield this.addAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
789
+ constructedMemo = `+:${params.assetPool}:${addressAsset}`;
790
+ txSubmitted.push(yield this.addRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
790
791
  return txSubmitted;
791
792
  }
792
793
  else if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.eq(0)) {
793
794
  // asymmetrical asset only
794
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}`;
795
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
795
+ constructedMemo = `+:${params.assetPool}`;
796
+ txSubmitted.push(yield this.addAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
796
797
  return txSubmitted;
797
798
  }
798
799
  else {
799
800
  // asymmetrical rune only
800
- constructedMemo = `+:${params.asset.asset.chain}.${params.asset.asset.symbol}`;
801
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
801
+ constructedMemo = `+:${params.assetPool}`;
802
+ txSubmitted.push(yield this.addRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
802
803
  return txSubmitted;
803
804
  }
804
805
  });
@@ -808,35 +809,33 @@ class Wallet {
808
809
  * @param params - parameters required for liquidity position
809
810
  * @returns object with tx response, url and wait time in seconds
810
811
  */
811
- removeLiquidity(params) {
812
+ withdrawLiquidity(params) {
812
813
  return __awaiter(this, void 0, void 0, function* () {
813
- const assetClient = this.clients[params.asset.asset.chain];
814
- const inboundAsgard = (yield this.thorchainQuery.thorchainCache.getInboundDetails())[params.asset.asset.chain];
815
- if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.address)) {
816
- throw new Error('Vault address is not defined');
817
- }
814
+ const assetClient = this.clients[params.assetFee.asset.chain];
815
+ const inboundAsgard = (yield this.thorchainQuery.thorchainCache.getInboundDetails())[params.assetFee.asset.chain]
816
+ .address;
818
817
  const waitTimeSeconds = params.waitTimeSeconds;
819
- const thorchainClient = this.clients[params.rune.asset.chain];
818
+ const thorchainClient = this.clients[params.runeFee.asset.chain];
820
819
  const basisPoints = (params.percentage * 100).toFixed(); // convert to basis points
821
820
  let constructedMemo = '';
822
821
  const txSubmitted = [];
823
- if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.gt(0)) {
824
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
825
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard.address));
826
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
827
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
822
+ if (params.assetAddress && params.runeAddress) {
823
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
824
+ txSubmitted.push(yield this.withdrawAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
825
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
826
+ txSubmitted.push(yield this.withdrawRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
828
827
  return txSubmitted;
829
828
  }
830
- else if (params.asset.assetAmount.gt(0) && params.rune.assetAmount.eq(0)) {
829
+ else if (params.assetAddress && !params.runeAddress) {
831
830
  // asymmetrical asset only
832
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
833
- txSubmitted.push(yield this.addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard.address));
831
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
832
+ txSubmitted.push(yield this.withdrawAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard));
834
833
  return txSubmitted;
835
834
  }
836
835
  else {
837
836
  // asymmetrical rune only
838
- constructedMemo = `-:${params.asset.asset.chain}.${params.asset.asset.symbol}:${basisPoints}`;
839
- txSubmitted.push(yield this.addOrRemoveRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
837
+ constructedMemo = `-:${params.assetPool}:${basisPoints}`;
838
+ txSubmitted.push(yield this.withdrawRuneLP(params, constructedMemo, thorchainClient, waitTimeSeconds));
840
839
  return txSubmitted;
841
840
  }
842
841
  });
@@ -850,7 +849,7 @@ class Wallet {
850
849
  * @param inboundAsgard - inbound Asgard address for the LP
851
850
  * @returns - tx object
852
851
  */
853
- addOrRemoveAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard) {
852
+ addAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard) {
854
853
  return __awaiter(this, void 0, void 0, function* () {
855
854
  if (params.asset.asset.chain === xchainUtil.Chain.Ethereum) {
856
855
  const addParams = {
@@ -860,7 +859,6 @@ class Wallet {
860
859
  feeOption: xchainClient.FeeOption.Fast,
861
860
  memo: constructedMemo,
862
861
  };
863
- console.log(addParams.amount.amount().toNumber());
864
862
  const hash = yield this.ethHelper.sendDeposit(addParams);
865
863
  return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
866
864
  }
@@ -885,7 +883,6 @@ class Wallet {
885
883
  memo: constructedMemo,
886
884
  };
887
885
  try {
888
- console.log(addParams);
889
886
  const hash = yield assetClient.transfer(addParams);
890
887
  return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
891
888
  }
@@ -896,13 +893,67 @@ class Wallet {
896
893
  }
897
894
  });
898
895
  }
896
+ /**
897
+ *
898
+ * @param params - parameters for withdraw liquidity
899
+ * @param constructedMemo - memo needed for thorchain execution
900
+ * @param assetClient - asset client to call transfer
901
+ * @param waitTimeSeconds - return back estimated wait
902
+ * @param inboundAsgard - destination address
903
+ * @returns - tx object
904
+ */
905
+ withdrawAssetLP(params, constructedMemo, assetClient, waitTimeSeconds, inboundAsgard) {
906
+ return __awaiter(this, void 0, void 0, function* () {
907
+ if (params.assetFee.asset.chain === xchainUtil.Chain.Ethereum) {
908
+ const withdrawParams = {
909
+ wallIndex: 0,
910
+ asset: params.assetFee.asset,
911
+ amount: params.assetFee.baseAmount,
912
+ feeOption: xchainClient.FeeOption.Fast,
913
+ memo: constructedMemo,
914
+ };
915
+ // console.log(withdrawParams.amount.amount().toNumber())
916
+ const hash = yield this.ethHelper.sendDeposit(withdrawParams);
917
+ return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
918
+ }
919
+ else if (params.assetFee.asset.chain === xchainUtil.Chain.Avalanche) {
920
+ const withdrawParams = {
921
+ wallIndex: 0,
922
+ asset: params.assetFee.asset,
923
+ amount: params.assetFee.baseAmount,
924
+ feeOption: xchainClient.FeeOption.Fast,
925
+ memo: constructedMemo,
926
+ };
927
+ const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
928
+ const hash = yield evmHelper.sendDeposit(withdrawParams);
929
+ return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
930
+ }
931
+ else {
932
+ const withdrawParams = {
933
+ wallIndex: 0,
934
+ asset: params.assetFee.asset,
935
+ amount: params.assetFee.baseAmount,
936
+ recipient: inboundAsgard,
937
+ memo: constructedMemo,
938
+ };
939
+ try {
940
+ const hash = yield assetClient.transfer(withdrawParams);
941
+ return { hash, url: assetClient.getExplorerTxUrl(hash), waitTimeSeconds };
942
+ }
943
+ catch (err) {
944
+ const hash = JSON.stringify(err);
945
+ return { hash, url: assetClient.getExplorerAddressUrl(assetClient.getAddress()), waitTimeSeconds };
946
+ }
947
+ }
948
+ });
949
+ }
899
950
  /**
900
951
  *
901
952
  * @param params - deposit parameters
902
953
  * @param memo - memo needed to withdraw lp
903
954
  * @returns - tx object
904
955
  */
905
- addOrRemoveRuneLP(params, memo, thorchainClient, waitTimeSeconds) {
956
+ addRuneLP(params, memo, thorchainClient, waitTimeSeconds) {
906
957
  return __awaiter(this, void 0, void 0, function* () {
907
958
  const thorClient = this.clients.THOR;
908
959
  const addParams = {
@@ -910,7 +961,26 @@ class Wallet {
910
961
  amount: params.rune.baseAmount,
911
962
  memo: memo,
912
963
  };
913
- console.log(addParams);
964
+ // console.log(addParams)
965
+ const hash = yield thorClient.deposit(addParams);
966
+ return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
967
+ });
968
+ }
969
+ /**
970
+ *
971
+ * @param params - withdraw parameters
972
+ * @param memo - memo needed to withdraw lp
973
+ * @returns - tx object
974
+ */
975
+ withdrawRuneLP(params, memo, thorchainClient, waitTimeSeconds) {
976
+ return __awaiter(this, void 0, void 0, function* () {
977
+ const thorClient = this.clients.THOR;
978
+ const addParams = {
979
+ asset: params.runeFee.asset,
980
+ amount: params.runeFee.baseAmount,
981
+ memo: memo,
982
+ };
983
+ // console.log(addParams)
914
984
  const hash = yield thorClient.deposit(addParams);
915
985
  return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
916
986
  });
@@ -918,6 +988,7 @@ class Wallet {
918
988
  }
919
989
 
920
990
  const BN_1 = new bignumber_js.BigNumber(1);
991
+ const defaultQuery = new xchainThorchainQuery.ThorchainQuery();
921
992
  /**
922
993
  * THORChain Class for interacting with THORChain.
923
994
  * Recommended main class to use for swapping with THORChain
@@ -930,7 +1001,7 @@ class ThorchainAMM {
930
1001
  * @param thorchainQuery - an instance of the ThorchainQuery
931
1002
  * @returns ThorchainAMM
932
1003
  */
933
- constructor(thorchainQuery) {
1004
+ constructor(thorchainQuery = defaultQuery) {
934
1005
  this.thorchainQuery = thorchainQuery;
935
1006
  }
936
1007
  /**
@@ -987,6 +1058,26 @@ class ThorchainAMM {
987
1058
  });
988
1059
  });
989
1060
  }
1061
+ /**
1062
+ * Wraps estimate from thorchain query
1063
+ * @param params - estimate add liquidity
1064
+ * @returns - Estimate add lp object
1065
+ */
1066
+ estimateAddLiquidity(params) {
1067
+ return __awaiter(this, void 0, void 0, function* () {
1068
+ return this.thorchainQuery.estimateAddLP(params);
1069
+ });
1070
+ }
1071
+ /**
1072
+ * Wraps estimate withdraw from thorchain query
1073
+ * @param params - estimate withdraw liquidity
1074
+ * @returns - Estimate withdraw lp object
1075
+ */
1076
+ estimateWithdrawLiquidity(params) {
1077
+ return __awaiter(this, void 0, void 0, function* () {
1078
+ return this.thorchainQuery.estimateWithdrawLP(params);
1079
+ });
1080
+ }
990
1081
  /**
991
1082
  *
992
1083
  * @param wallet - wallet class
@@ -997,10 +1088,13 @@ class ThorchainAMM {
997
1088
  return __awaiter(this, void 0, void 0, function* () {
998
1089
  // Check amounts are greater than fees and use return estimated wait
999
1090
  const checkLPAdd = yield this.thorchainQuery.estimateAddLP(params);
1091
+ if (!checkLPAdd.canAdd)
1092
+ throw Error(`${checkLPAdd.errors}`);
1000
1093
  return wallet.addLiquidity({
1001
1094
  asset: params.asset,
1002
1095
  rune: params.rune,
1003
1096
  waitTimeSeconds: checkLPAdd.estimatedWaitSeconds,
1097
+ assetPool: checkLPAdd.assetPool,
1004
1098
  });
1005
1099
  });
1006
1100
  }
@@ -1010,15 +1104,18 @@ class ThorchainAMM {
1010
1104
  * @param wallet - wallet needed to perform tx
1011
1105
  * @return
1012
1106
  */
1013
- removeLiquidityPosition(wallet, params) {
1107
+ withdrawLiquidityPosition(wallet, params) {
1014
1108
  return __awaiter(this, void 0, void 0, function* () {
1015
1109
  // Caution Dust Limits: BTC,BCH,LTC chains 10k sats; DOGE 1m Sats; ETH 0 wei; THOR 0 RUNE.
1016
- const estimateWithrawLp = yield this.thorchainQuery.estimateWithdrawLP(params);
1017
- return wallet.removeLiquidity({
1018
- asset: estimateWithrawLp.transactionFee.assetFee,
1019
- rune: estimateWithrawLp.transactionFee.runeFee,
1110
+ const withdrawParams = yield this.thorchainQuery.estimateWithdrawLP(params);
1111
+ return wallet.withdrawLiquidity({
1112
+ assetFee: withdrawParams.transactionFee.assetFee,
1113
+ runeFee: withdrawParams.transactionFee.runeFee,
1114
+ waitTimeSeconds: withdrawParams.estimatedWaitSeconds,
1020
1115
  percentage: params.percentage,
1021
- waitTimeSeconds: estimateWithrawLp.estimatedWaitSeconds,
1116
+ assetPool: withdrawParams.assetPool,
1117
+ assetAddress: withdrawParams.assetAddress,
1118
+ runeAddress: withdrawParams.runeAddress,
1022
1119
  });
1023
1120
  });
1024
1121
  }
@@ -1,4 +1,4 @@
1
- import { AddliquidityPosition, EstimateSwapParams, RemoveLiquidityPosition, ThorchainQuery, TxDetails } from '@xchainjs/xchain-thorchain-query';
1
+ import { AddliquidityPosition, EstimateAddLP, EstimateSwapParams, EstimateWithdrawLP, ThorchainQuery, TxDetails, WithdrawLiquidityPosition } from '@xchainjs/xchain-thorchain-query';
2
2
  import { TxSubmitted } from './types';
3
3
  import { Wallet } from './wallet';
4
4
  /**
@@ -14,7 +14,7 @@ export declare class ThorchainAMM {
14
14
  * @param thorchainQuery - an instance of the ThorchainQuery
15
15
  * @returns ThorchainAMM
16
16
  */
17
- constructor(thorchainQuery: ThorchainQuery);
17
+ constructor(thorchainQuery?: ThorchainQuery);
18
18
  /**
19
19
  * Provides a swap estimate for the given swap detail. Will check the params for errors before trying to get the estimate.
20
20
  * 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)
@@ -32,6 +32,18 @@ export declare class ThorchainAMM {
32
32
  * @returns {SwapSubmitted} - Tx Hash, URL of BlockExplorer and expected wait time.
33
33
  */
34
34
  doSwap(wallet: Wallet, params: EstimateSwapParams): Promise<TxSubmitted>;
35
+ /**
36
+ * Wraps estimate from thorchain query
37
+ * @param params - estimate add liquidity
38
+ * @returns - Estimate add lp object
39
+ */
40
+ estimateAddLiquidity(params: AddliquidityPosition): Promise<EstimateAddLP>;
41
+ /**
42
+ * Wraps estimate withdraw from thorchain query
43
+ * @param params - estimate withdraw liquidity
44
+ * @returns - Estimate withdraw lp object
45
+ */
46
+ estimateWithdrawLiquidity(params: WithdrawLiquidityPosition): Promise<EstimateWithdrawLP>;
35
47
  /**
36
48
  *
37
49
  * @param wallet - wallet class
@@ -45,5 +57,5 @@ export declare class ThorchainAMM {
45
57
  * @param wallet - wallet needed to perform tx
46
58
  * @return
47
59
  */
48
- removeLiquidityPosition(wallet: Wallet, params: RemoveLiquidityPosition): Promise<TxSubmitted[]>;
60
+ withdrawLiquidityPosition(wallet: Wallet, params: WithdrawLiquidityPosition): Promise<TxSubmitted[]>;
49
61
  }
package/lib/types.d.ts CHANGED
@@ -22,23 +22,26 @@ export declare type TxSubmitted = {
22
22
  url: string;
23
23
  waitTimeSeconds: number;
24
24
  };
25
- export declare type AddLiquidity = {
26
- asset: CryptoAmount;
27
- rune: CryptoAmount;
28
- waitTimeSeconds: number;
29
- };
30
25
  export declare type LiquidityPosition = {
31
26
  assetPool: LiquidityPool;
32
27
  assetAmount: CryptoAmount;
33
28
  runeAmount: CryptoAmount;
34
29
  impermanentLossProtection: number;
35
30
  };
36
- export declare type RemoveLiquidity = {
31
+ export declare type AddLiquidity = {
37
32
  asset: CryptoAmount;
38
33
  rune: CryptoAmount;
39
- percentage: number;
40
34
  waitTimeSeconds: number;
41
- asymmetricalWithdraw?: Asset;
35
+ assetPool: string;
36
+ };
37
+ export declare type WithdrawLiquidity = {
38
+ assetFee: CryptoAmount;
39
+ runeFee: CryptoAmount;
40
+ waitTimeSeconds: number;
41
+ percentage: number;
42
+ assetPool: string;
43
+ assetAddress?: string;
44
+ runeAddress?: string;
42
45
  };
43
46
  export declare type DepositParams = {
44
47
  walletIndex?: number;
package/lib/wallet.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Balance, XChainClient } from '@xchainjs/xchain-client';
2
2
  import { ThorchainQuery } from '@xchainjs/xchain-thorchain-query';
3
3
  import { Chain } from '@xchainjs/xchain-util';
4
- import { AddLiquidity, ExecuteSwap, RemoveLiquidity, TxSubmitted } from './types';
4
+ import { AddLiquidity, ExecuteSwap, TxSubmitted, WithdrawLiquidity } from './types';
5
5
  declare type AllBalances = {
6
6
  chain: Chain;
7
7
  address: string;
@@ -51,7 +51,7 @@ export declare class Wallet {
51
51
  * @param params - parameters required for liquidity position
52
52
  * @returns object with tx response, url and wait time in seconds
53
53
  */
54
- removeLiquidity(params: RemoveLiquidity): Promise<TxSubmitted[]>;
54
+ withdrawLiquidity(params: WithdrawLiquidity): Promise<TxSubmitted[]>;
55
55
  /**
56
56
  *
57
57
  * @param params - parameters for add liquidity
@@ -61,13 +61,30 @@ export declare class Wallet {
61
61
  * @param inboundAsgard - inbound Asgard address for the LP
62
62
  * @returns - tx object
63
63
  */
64
- private addOrRemoveAssetLP;
64
+ private addAssetLP;
65
+ /**
66
+ *
67
+ * @param params - parameters for withdraw liquidity
68
+ * @param constructedMemo - memo needed for thorchain execution
69
+ * @param assetClient - asset client to call transfer
70
+ * @param waitTimeSeconds - return back estimated wait
71
+ * @param inboundAsgard - destination address
72
+ * @returns - tx object
73
+ */
74
+ private withdrawAssetLP;
65
75
  /**
66
76
  *
67
77
  * @param params - deposit parameters
68
78
  * @param memo - memo needed to withdraw lp
69
79
  * @returns - tx object
70
80
  */
71
- private addOrRemoveRuneLP;
81
+ private addRuneLP;
82
+ /**
83
+ *
84
+ * @param params - withdraw parameters
85
+ * @param memo - memo needed to withdraw lp
86
+ * @returns - tx object
87
+ */
88
+ private withdrawRuneLP;
72
89
  }
73
90
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-thorchain-amm",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "module that exposes estimating & swappping cryptocurrency assets on thorchain",
5
5
  "keywords": [
6
6
  "THORChain",
@@ -48,12 +48,12 @@
48
48
  "@xchainjs/xchain-crypto": "^0.2.6",
49
49
  "@xchainjs/xchain-doge": "^0.5.0",
50
50
  "@xchainjs/xchain-ethereum": "^0.27.0",
51
- "@xchainjs/xchain-evm": "^0.1.0-alpha2",
52
- "@xchainjs/xchain-avax": "^0.1.0-alpha3",
53
- "@xchainjs/xchain-litecoin": "^0.10.0",
51
+ "@xchainjs/xchain-evm": "^0.1.0",
52
+ "@xchainjs/xchain-avax": "^0.1.0",
53
+ "@xchainjs/xchain-litecoin": "^0.10.3",
54
54
  "@xchainjs/xchain-midgard": "0.1.0",
55
55
  "@xchainjs/xchain-thorchain": "^0.27.0",
56
- "@xchainjs/xchain-thorchain-query": "^0.1.0",
56
+ "@xchainjs/xchain-thorchain-query": "^0.1.2",
57
57
  "@xchainjs/xchain-thornode": "^0.1.0",
58
58
  "@xchainjs/xchain-util": "^0.11.0",
59
59
  "axios": "^0.25.0",
@@ -73,22 +73,21 @@
73
73
  "@binance-chain/javascript-sdk": "^4.2.0",
74
74
  "@cosmos-client/core": "0.45.13",
75
75
  "@psf/bitcoincashjs-lib": "^4.0.2",
76
- "@terra-money/terra.js": "^3.0.2",
77
- "@xchainjs/xchain-binance": "^5.6.0",
78
- "@xchainjs/xchain-bitcoin": "^0.20.0",
79
- "@xchainjs/xchain-bitcoincash": "^0.15.0",
76
+ "@xchainjs/xchain-binance": "^5.6.4",
77
+ "@xchainjs/xchain-bitcoin": "^0.20.4",
78
+ "@xchainjs/xchain-bitcoincash": "^0.15.3",
80
79
  "@xchainjs/xchain-client": "^0.13.2",
81
- "@xchainjs/xchain-cosmos": "^0.20.0",
80
+ "@xchainjs/xchain-cosmos": "^0.20.3",
82
81
  "@xchainjs/xchain-crypto": "^0.2.6",
83
- "@xchainjs/xchain-doge": "^0.5.0",
84
- "@xchainjs/xchain-ethereum": "^0.27.0",
85
- "@xchainjs/xchain-evm": "^0.1.0-alpha2",
86
- "@xchainjs/xchain-avax": "^0.1.0-alpha3",
87
- "@xchainjs/xchain-litecoin": "^0.10.0",
82
+ "@xchainjs/xchain-doge": "^0.5.4",
83
+ "@xchainjs/xchain-ethereum": "^0.27.3",
84
+ "@xchainjs/xchain-evm": "^0.1.0",
85
+ "@xchainjs/xchain-avax": "^0.1.0",
86
+ "@xchainjs/xchain-litecoin": "^0.10.4",
88
87
  "@xchainjs/xchain-midgard": "0.1.0",
89
88
  "@xchainjs/xchain-thornode": "^0.1.0",
90
- "@xchainjs/xchain-thorchain": "^0.26.0",
91
- "@xchainjs/xchain-thorchain-query": "^0.1.0",
89
+ "@xchainjs/xchain-thorchain": "^0.27.1",
90
+ "@xchainjs/xchain-thorchain-query": "^0.1.2",
92
91
  "@xchainjs/xchain-util": "^0.11.0",
93
92
  "axios": "^0.25.0",
94
93
  "axios-retry": "^3.2.5",