@xchainjs/xchain-thorchain-amm 0.3.2 → 0.3.4
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 +50 -55
- package/lib/index.js +49 -54
- package/lib/thorchain-amm.d.ts +1 -1
- package/lib/types.d.ts +1 -18
- package/lib/wallet.d.ts +22 -8
- package/package.json +5 -5
package/lib/index.esm.js
CHANGED
|
@@ -8,11 +8,10 @@ import { Client as Client$2 } from '@xchainjs/xchain-doge';
|
|
|
8
8
|
import { MAX_APPROVAL, ETH_DECIMAL, Client as Client$3 } from '@xchainjs/xchain-ethereum';
|
|
9
9
|
import { Client as Client$5 } from '@xchainjs/xchain-litecoin';
|
|
10
10
|
import { Client as Client$4 } from '@xchainjs/xchain-thorchain';
|
|
11
|
-
import { eqAsset, AssetETH, getContractAddressFromAsset, baseAmount, Chain
|
|
11
|
+
import { eqAsset, AssetETH, getContractAddressFromAsset, baseAmount, Chain } from '@xchainjs/xchain-util';
|
|
12
12
|
import { ethers } from 'ethers';
|
|
13
13
|
import { MAX_APPROVAL as MAX_APPROVAL$1 } from '@xchainjs/xchain-evm';
|
|
14
14
|
import { ThorchainQuery } from '@xchainjs/xchain-thorchain-query';
|
|
15
|
-
import { BigNumber } from 'bignumber.js';
|
|
16
15
|
|
|
17
16
|
/*! *****************************************************************************
|
|
18
17
|
Copyright (c) Microsoft Corporation.
|
|
@@ -423,7 +422,7 @@ class EthHelper {
|
|
|
423
422
|
*/
|
|
424
423
|
sendDeposit(params) {
|
|
425
424
|
return __awaiter(this, void 0, void 0, function* () {
|
|
426
|
-
const inboundAsgard = (yield this.thorchainCache.
|
|
425
|
+
const inboundAsgard = (yield this.thorchainCache.getInboundDetails())[params.asset.chain];
|
|
427
426
|
if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.router)) {
|
|
428
427
|
throw new Error('router address is not defined');
|
|
429
428
|
}
|
|
@@ -518,7 +517,7 @@ class EvmHelper {
|
|
|
518
517
|
*/
|
|
519
518
|
sendDeposit(params) {
|
|
520
519
|
return __awaiter(this, void 0, void 0, function* () {
|
|
521
|
-
const inboundAsgard = (yield this.thorchainCache.
|
|
520
|
+
const inboundAsgard = (yield this.thorchainCache.getInboundDetails())[params.asset.chain];
|
|
522
521
|
if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.router)) {
|
|
523
522
|
throw new Error('router address is not defined');
|
|
524
523
|
}
|
|
@@ -677,24 +676,10 @@ class Wallet {
|
|
|
677
676
|
}
|
|
678
677
|
});
|
|
679
678
|
}
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
// create the full memo
|
|
685
|
-
let memo = `=:${assetToString(swap.destinationAsset)}`;
|
|
686
|
-
if (swap.affiliateAddress != '' || swap.affiliateFee == undefined) {
|
|
687
|
-
memo = memo.concat(`:${swap.destinationAddress}:${lim}:${swap.affiliateAddress}:${swap.affiliateFee.amount().toFixed()}`);
|
|
688
|
-
}
|
|
689
|
-
else {
|
|
690
|
-
memo = memo.concat(`:${swap.destinationAddress}:${lim}`);
|
|
691
|
-
}
|
|
692
|
-
// If memo length is too long for BTC, trim it
|
|
693
|
-
if (eqAsset(swap.input.asset, AssetBTC) && memo.length > 80) {
|
|
694
|
-
memo = `=:${assetToString(swap.destinationAsset)}:${swap.destinationAddress}`;
|
|
695
|
-
}
|
|
696
|
-
return memo;
|
|
697
|
-
}
|
|
679
|
+
/** Validate swap object
|
|
680
|
+
*
|
|
681
|
+
* @param swap - swap parameters
|
|
682
|
+
*/
|
|
698
683
|
validateSwap(swap) {
|
|
699
684
|
const errors = [];
|
|
700
685
|
const isThorchainDestinationAsset = swap.destinationAsset.synth || swap.destinationAsset.chain === Chain.THORChain;
|
|
@@ -702,11 +687,31 @@ class Wallet {
|
|
|
702
687
|
if (!this.clients[chain].validateAddress(swap.destinationAddress)) {
|
|
703
688
|
errors.push(`destinationAddress ${swap.destinationAddress} is not a valid address`);
|
|
704
689
|
}
|
|
705
|
-
|
|
706
|
-
|
|
690
|
+
// Affiliate address should be THORName or THORAddress
|
|
691
|
+
const checkAffiliateAddress = swap.memo.split(':');
|
|
692
|
+
if (checkAffiliateAddress.length > 4) {
|
|
693
|
+
const affiliateAddress = checkAffiliateAddress[4];
|
|
694
|
+
if (affiliateAddress.length > 0) {
|
|
695
|
+
const isValidThorchainAddress = this.clients[Chain.THORChain].validateAddress(affiliateAddress);
|
|
696
|
+
const isValidThorname = this.isThorname(affiliateAddress);
|
|
697
|
+
if (!(isValidThorchainAddress || isValidThorname))
|
|
698
|
+
errors.push(`affiliateAddress ${affiliateAddress} is not a valid THOR address`);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
707
701
|
if (errors.length > 0)
|
|
708
702
|
throw Error(errors.join('\n'));
|
|
709
703
|
}
|
|
704
|
+
isThorname(name) {
|
|
705
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
706
|
+
const thornameDetails = yield this.thorchainQuery.thorchainCache.midgard.getTHORNameDetails(name);
|
|
707
|
+
return thornameDetails !== undefined;
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
/** Function handles all swaps from Rune to asset
|
|
711
|
+
*
|
|
712
|
+
* @param swap - swap parameters
|
|
713
|
+
* @returns - tx submitted object
|
|
714
|
+
*/
|
|
710
715
|
swapRuneTo(swap) {
|
|
711
716
|
return __awaiter(this, void 0, void 0, function* () {
|
|
712
717
|
const thorClient = this.clients.THOR;
|
|
@@ -714,11 +719,16 @@ class Wallet {
|
|
|
714
719
|
const hash = yield thorClient.deposit({
|
|
715
720
|
amount: swap.input.baseAmount,
|
|
716
721
|
asset: swap.input.asset,
|
|
717
|
-
memo:
|
|
722
|
+
memo: swap.memo,
|
|
718
723
|
});
|
|
719
724
|
return { hash, url: this.clients.THOR.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
720
725
|
});
|
|
721
726
|
}
|
|
727
|
+
/** Function handles all swaps from Non Rune
|
|
728
|
+
*
|
|
729
|
+
* @param swap - swap object
|
|
730
|
+
* @returns - TxSubmitted object
|
|
731
|
+
*/
|
|
722
732
|
swapNonRune(swap) {
|
|
723
733
|
return __awaiter(this, void 0, void 0, function* () {
|
|
724
734
|
const client = this.clients[swap.input.asset.chain];
|
|
@@ -732,7 +742,7 @@ class Wallet {
|
|
|
732
742
|
asset: swap.input.asset,
|
|
733
743
|
amount: swap.input.baseAmount,
|
|
734
744
|
feeOption: swap.feeOption || FeeOption.Fast,
|
|
735
|
-
memo:
|
|
745
|
+
memo: swap.memo,
|
|
736
746
|
};
|
|
737
747
|
const hash = yield this.ethHelper.sendDeposit(params);
|
|
738
748
|
return { hash, url: client.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
@@ -743,7 +753,7 @@ class Wallet {
|
|
|
743
753
|
asset: swap.input.asset,
|
|
744
754
|
amount: swap.input.baseAmount,
|
|
745
755
|
feeOption: swap.feeOption || FeeOption.Fast,
|
|
746
|
-
memo:
|
|
756
|
+
memo: swap.memo,
|
|
747
757
|
};
|
|
748
758
|
const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
|
|
749
759
|
const hash = yield evmHelper.sendDeposit(params);
|
|
@@ -755,15 +765,15 @@ class Wallet {
|
|
|
755
765
|
asset: swap.input.asset,
|
|
756
766
|
amount: swap.input.baseAmount,
|
|
757
767
|
recipient: inbound.address,
|
|
758
|
-
memo:
|
|
768
|
+
memo: swap.memo,
|
|
759
769
|
};
|
|
760
770
|
const hash = yield client.transfer(params);
|
|
761
771
|
return { hash, url: client.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
762
772
|
}
|
|
763
773
|
});
|
|
764
774
|
}
|
|
765
|
-
/**
|
|
766
|
-
*
|
|
775
|
+
/** Function handles liquidity Add
|
|
776
|
+
* BASED OFF https://dev.thorchain.or›g/thorchain-dev/network/memos
|
|
767
777
|
* @param params input parameters needed to add liquidity
|
|
768
778
|
* @returns transaction details submitted
|
|
769
779
|
*/
|
|
@@ -800,7 +810,7 @@ class Wallet {
|
|
|
800
810
|
}
|
|
801
811
|
});
|
|
802
812
|
}
|
|
803
|
-
/**
|
|
813
|
+
/** Function handles liquidity Withdraw
|
|
804
814
|
*
|
|
805
815
|
* @param params - parameters required for liquidity position
|
|
806
816
|
* @returns object with tx response, url and wait time in seconds
|
|
@@ -836,7 +846,7 @@ class Wallet {
|
|
|
836
846
|
}
|
|
837
847
|
});
|
|
838
848
|
}
|
|
839
|
-
/**
|
|
849
|
+
/** Function handles liquidity add for all non rune assets
|
|
840
850
|
*
|
|
841
851
|
* @param params - parameters for add liquidity
|
|
842
852
|
* @param constructedMemo - memo needed for thorchain
|
|
@@ -889,7 +899,7 @@ class Wallet {
|
|
|
889
899
|
}
|
|
890
900
|
});
|
|
891
901
|
}
|
|
892
|
-
/**
|
|
902
|
+
/** Function handles liquidity Withdraw for Non rune assets
|
|
893
903
|
*
|
|
894
904
|
* @param params - parameters for withdraw liquidity
|
|
895
905
|
* @param constructedMemo - memo needed for thorchain execution
|
|
@@ -943,7 +953,7 @@ class Wallet {
|
|
|
943
953
|
}
|
|
944
954
|
});
|
|
945
955
|
}
|
|
946
|
-
/**
|
|
956
|
+
/** Function handles liquidity Add for Rune only
|
|
947
957
|
*
|
|
948
958
|
* @param params - deposit parameters
|
|
949
959
|
* @param memo - memo needed to withdraw lp
|
|
@@ -957,12 +967,11 @@ class Wallet {
|
|
|
957
967
|
amount: params.rune.baseAmount,
|
|
958
968
|
memo: memo,
|
|
959
969
|
};
|
|
960
|
-
// console.log(addParams)
|
|
961
970
|
const hash = yield thorClient.deposit(addParams);
|
|
962
971
|
return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
963
972
|
});
|
|
964
973
|
}
|
|
965
|
-
/**
|
|
974
|
+
/** Function handles liquidity Withdraw for Rune only
|
|
966
975
|
*
|
|
967
976
|
* @param params - withdraw parameters
|
|
968
977
|
* @param memo - memo needed to withdraw lp
|
|
@@ -976,14 +985,12 @@ class Wallet {
|
|
|
976
985
|
amount: params.runeFee.baseAmount,
|
|
977
986
|
memo: memo,
|
|
978
987
|
};
|
|
979
|
-
// console.log(addParams)
|
|
980
988
|
const hash = yield thorClient.deposit(addParams);
|
|
981
989
|
return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
982
990
|
});
|
|
983
991
|
}
|
|
984
992
|
}
|
|
985
993
|
|
|
986
|
-
const BN_1 = new BigNumber(1);
|
|
987
994
|
const defaultQuery = new ThorchainQuery();
|
|
988
995
|
/**
|
|
989
996
|
* THORChain Class for interacting with THORChain.
|
|
@@ -1008,14 +1015,14 @@ class ThorchainAMM {
|
|
|
1008
1015
|
|
|
1009
1016
|
* @returns The SwapEstimate
|
|
1010
1017
|
*/
|
|
1011
|
-
estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress = '', interfaceID =
|
|
1018
|
+
estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress = '', interfaceID = `555`, affiliateFeeBasisPoints = 0, slipLimit, }) {
|
|
1012
1019
|
return this.thorchainQuery.estimateSwap({
|
|
1013
1020
|
input,
|
|
1014
1021
|
destinationAsset,
|
|
1015
1022
|
destinationAddress,
|
|
1016
1023
|
affiliateAddress,
|
|
1017
1024
|
interfaceID,
|
|
1018
|
-
|
|
1025
|
+
affiliateFeeBasisPoints,
|
|
1019
1026
|
slipLimit,
|
|
1020
1027
|
});
|
|
1021
1028
|
}
|
|
@@ -1028,29 +1035,17 @@ class ThorchainAMM {
|
|
|
1028
1035
|
*/
|
|
1029
1036
|
doSwap(wallet, params) {
|
|
1030
1037
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1031
|
-
//
|
|
1038
|
+
// Thorchain-query call satisfies the data needed for executeSwap to be called.
|
|
1032
1039
|
const txDetails = yield this.thorchainQuery.estimateSwap(params);
|
|
1033
1040
|
if (!txDetails.txEstimate.canSwap) {
|
|
1034
1041
|
throw Error(txDetails.txEstimate.errors.join('\n'));
|
|
1035
1042
|
}
|
|
1036
|
-
// remove any affiliateFee. netInput * affiliateFee (%age) of the destination asset type
|
|
1037
|
-
const affiliateFee = params.input.baseAmount.times(params.affiliateFeePercent || 0);
|
|
1038
|
-
// Work out LIM from the slip percentage
|
|
1039
|
-
let limPercentage = BN_1;
|
|
1040
|
-
if (params.slipLimit) {
|
|
1041
|
-
limPercentage = BN_1.minus(params.slipLimit || 1);
|
|
1042
|
-
} // else allowed slip is 100%
|
|
1043
|
-
const limAssetAmount = txDetails.txEstimate.netOutput.times(limPercentage);
|
|
1044
|
-
const waitTimeSeconds = txDetails.txEstimate.waitTimeSeconds;
|
|
1045
1043
|
return yield wallet.executeSwap({
|
|
1046
1044
|
input: params.input,
|
|
1047
1045
|
destinationAsset: params.destinationAsset,
|
|
1048
|
-
limit: limAssetAmount.baseAmount,
|
|
1049
1046
|
destinationAddress: params.destinationAddress,
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
interfaceID: params.interfaceID || 999,
|
|
1053
|
-
waitTimeSeconds,
|
|
1047
|
+
memo: txDetails.memo,
|
|
1048
|
+
waitTimeSeconds: txDetails.txEstimate.waitTimeSeconds,
|
|
1054
1049
|
});
|
|
1055
1050
|
});
|
|
1056
1051
|
}
|
package/lib/index.js
CHANGED
|
@@ -16,7 +16,6 @@ var xchainUtil = require('@xchainjs/xchain-util');
|
|
|
16
16
|
var ethers = require('ethers');
|
|
17
17
|
var xchainEvm = require('@xchainjs/xchain-evm');
|
|
18
18
|
var xchainThorchainQuery = require('@xchainjs/xchain-thorchain-query');
|
|
19
|
-
var bignumber_js = require('bignumber.js');
|
|
20
19
|
|
|
21
20
|
/*! *****************************************************************************
|
|
22
21
|
Copyright (c) Microsoft Corporation.
|
|
@@ -427,7 +426,7 @@ class EthHelper {
|
|
|
427
426
|
*/
|
|
428
427
|
sendDeposit(params) {
|
|
429
428
|
return __awaiter(this, void 0, void 0, function* () {
|
|
430
|
-
const inboundAsgard = (yield this.thorchainCache.
|
|
429
|
+
const inboundAsgard = (yield this.thorchainCache.getInboundDetails())[params.asset.chain];
|
|
431
430
|
if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.router)) {
|
|
432
431
|
throw new Error('router address is not defined');
|
|
433
432
|
}
|
|
@@ -522,7 +521,7 @@ class EvmHelper {
|
|
|
522
521
|
*/
|
|
523
522
|
sendDeposit(params) {
|
|
524
523
|
return __awaiter(this, void 0, void 0, function* () {
|
|
525
|
-
const inboundAsgard = (yield this.thorchainCache.
|
|
524
|
+
const inboundAsgard = (yield this.thorchainCache.getInboundDetails())[params.asset.chain];
|
|
526
525
|
if (!(inboundAsgard === null || inboundAsgard === void 0 ? void 0 : inboundAsgard.router)) {
|
|
527
526
|
throw new Error('router address is not defined');
|
|
528
527
|
}
|
|
@@ -681,24 +680,10 @@ class Wallet {
|
|
|
681
680
|
}
|
|
682
681
|
});
|
|
683
682
|
}
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
// create the full memo
|
|
689
|
-
let memo = `=:${xchainUtil.assetToString(swap.destinationAsset)}`;
|
|
690
|
-
if (swap.affiliateAddress != '' || swap.affiliateFee == undefined) {
|
|
691
|
-
memo = memo.concat(`:${swap.destinationAddress}:${lim}:${swap.affiliateAddress}:${swap.affiliateFee.amount().toFixed()}`);
|
|
692
|
-
}
|
|
693
|
-
else {
|
|
694
|
-
memo = memo.concat(`:${swap.destinationAddress}:${lim}`);
|
|
695
|
-
}
|
|
696
|
-
// If memo length is too long for BTC, trim it
|
|
697
|
-
if (xchainUtil.eqAsset(swap.input.asset, xchainUtil.AssetBTC) && memo.length > 80) {
|
|
698
|
-
memo = `=:${xchainUtil.assetToString(swap.destinationAsset)}:${swap.destinationAddress}`;
|
|
699
|
-
}
|
|
700
|
-
return memo;
|
|
701
|
-
}
|
|
683
|
+
/** Validate swap object
|
|
684
|
+
*
|
|
685
|
+
* @param swap - swap parameters
|
|
686
|
+
*/
|
|
702
687
|
validateSwap(swap) {
|
|
703
688
|
const errors = [];
|
|
704
689
|
const isThorchainDestinationAsset = swap.destinationAsset.synth || swap.destinationAsset.chain === xchainUtil.Chain.THORChain;
|
|
@@ -706,11 +691,31 @@ class Wallet {
|
|
|
706
691
|
if (!this.clients[chain].validateAddress(swap.destinationAddress)) {
|
|
707
692
|
errors.push(`destinationAddress ${swap.destinationAddress} is not a valid address`);
|
|
708
693
|
}
|
|
709
|
-
|
|
710
|
-
|
|
694
|
+
// Affiliate address should be THORName or THORAddress
|
|
695
|
+
const checkAffiliateAddress = swap.memo.split(':');
|
|
696
|
+
if (checkAffiliateAddress.length > 4) {
|
|
697
|
+
const affiliateAddress = checkAffiliateAddress[4];
|
|
698
|
+
if (affiliateAddress.length > 0) {
|
|
699
|
+
const isValidThorchainAddress = this.clients[xchainUtil.Chain.THORChain].validateAddress(affiliateAddress);
|
|
700
|
+
const isValidThorname = this.isThorname(affiliateAddress);
|
|
701
|
+
if (!(isValidThorchainAddress || isValidThorname))
|
|
702
|
+
errors.push(`affiliateAddress ${affiliateAddress} is not a valid THOR address`);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
711
705
|
if (errors.length > 0)
|
|
712
706
|
throw Error(errors.join('\n'));
|
|
713
707
|
}
|
|
708
|
+
isThorname(name) {
|
|
709
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
710
|
+
const thornameDetails = yield this.thorchainQuery.thorchainCache.midgard.getTHORNameDetails(name);
|
|
711
|
+
return thornameDetails !== undefined;
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
/** Function handles all swaps from Rune to asset
|
|
715
|
+
*
|
|
716
|
+
* @param swap - swap parameters
|
|
717
|
+
* @returns - tx submitted object
|
|
718
|
+
*/
|
|
714
719
|
swapRuneTo(swap) {
|
|
715
720
|
return __awaiter(this, void 0, void 0, function* () {
|
|
716
721
|
const thorClient = this.clients.THOR;
|
|
@@ -718,11 +723,16 @@ class Wallet {
|
|
|
718
723
|
const hash = yield thorClient.deposit({
|
|
719
724
|
amount: swap.input.baseAmount,
|
|
720
725
|
asset: swap.input.asset,
|
|
721
|
-
memo:
|
|
726
|
+
memo: swap.memo,
|
|
722
727
|
});
|
|
723
728
|
return { hash, url: this.clients.THOR.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
724
729
|
});
|
|
725
730
|
}
|
|
731
|
+
/** Function handles all swaps from Non Rune
|
|
732
|
+
*
|
|
733
|
+
* @param swap - swap object
|
|
734
|
+
* @returns - TxSubmitted object
|
|
735
|
+
*/
|
|
726
736
|
swapNonRune(swap) {
|
|
727
737
|
return __awaiter(this, void 0, void 0, function* () {
|
|
728
738
|
const client = this.clients[swap.input.asset.chain];
|
|
@@ -736,7 +746,7 @@ class Wallet {
|
|
|
736
746
|
asset: swap.input.asset,
|
|
737
747
|
amount: swap.input.baseAmount,
|
|
738
748
|
feeOption: swap.feeOption || xchainClient.FeeOption.Fast,
|
|
739
|
-
memo:
|
|
749
|
+
memo: swap.memo,
|
|
740
750
|
};
|
|
741
751
|
const hash = yield this.ethHelper.sendDeposit(params);
|
|
742
752
|
return { hash, url: client.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
@@ -747,7 +757,7 @@ class Wallet {
|
|
|
747
757
|
asset: swap.input.asset,
|
|
748
758
|
amount: swap.input.baseAmount,
|
|
749
759
|
feeOption: swap.feeOption || xchainClient.FeeOption.Fast,
|
|
750
|
-
memo:
|
|
760
|
+
memo: swap.memo,
|
|
751
761
|
};
|
|
752
762
|
const evmHelper = new EvmHelper(this.clients.AVAX, this.thorchainQuery.thorchainCache);
|
|
753
763
|
const hash = yield evmHelper.sendDeposit(params);
|
|
@@ -759,15 +769,15 @@ class Wallet {
|
|
|
759
769
|
asset: swap.input.asset,
|
|
760
770
|
amount: swap.input.baseAmount,
|
|
761
771
|
recipient: inbound.address,
|
|
762
|
-
memo:
|
|
772
|
+
memo: swap.memo,
|
|
763
773
|
};
|
|
764
774
|
const hash = yield client.transfer(params);
|
|
765
775
|
return { hash, url: client.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
766
776
|
}
|
|
767
777
|
});
|
|
768
778
|
}
|
|
769
|
-
/**
|
|
770
|
-
*
|
|
779
|
+
/** Function handles liquidity Add
|
|
780
|
+
* BASED OFF https://dev.thorchain.or›g/thorchain-dev/network/memos
|
|
771
781
|
* @param params input parameters needed to add liquidity
|
|
772
782
|
* @returns transaction details submitted
|
|
773
783
|
*/
|
|
@@ -804,7 +814,7 @@ class Wallet {
|
|
|
804
814
|
}
|
|
805
815
|
});
|
|
806
816
|
}
|
|
807
|
-
/**
|
|
817
|
+
/** Function handles liquidity Withdraw
|
|
808
818
|
*
|
|
809
819
|
* @param params - parameters required for liquidity position
|
|
810
820
|
* @returns object with tx response, url and wait time in seconds
|
|
@@ -840,7 +850,7 @@ class Wallet {
|
|
|
840
850
|
}
|
|
841
851
|
});
|
|
842
852
|
}
|
|
843
|
-
/**
|
|
853
|
+
/** Function handles liquidity add for all non rune assets
|
|
844
854
|
*
|
|
845
855
|
* @param params - parameters for add liquidity
|
|
846
856
|
* @param constructedMemo - memo needed for thorchain
|
|
@@ -893,7 +903,7 @@ class Wallet {
|
|
|
893
903
|
}
|
|
894
904
|
});
|
|
895
905
|
}
|
|
896
|
-
/**
|
|
906
|
+
/** Function handles liquidity Withdraw for Non rune assets
|
|
897
907
|
*
|
|
898
908
|
* @param params - parameters for withdraw liquidity
|
|
899
909
|
* @param constructedMemo - memo needed for thorchain execution
|
|
@@ -947,7 +957,7 @@ class Wallet {
|
|
|
947
957
|
}
|
|
948
958
|
});
|
|
949
959
|
}
|
|
950
|
-
/**
|
|
960
|
+
/** Function handles liquidity Add for Rune only
|
|
951
961
|
*
|
|
952
962
|
* @param params - deposit parameters
|
|
953
963
|
* @param memo - memo needed to withdraw lp
|
|
@@ -961,12 +971,11 @@ class Wallet {
|
|
|
961
971
|
amount: params.rune.baseAmount,
|
|
962
972
|
memo: memo,
|
|
963
973
|
};
|
|
964
|
-
// console.log(addParams)
|
|
965
974
|
const hash = yield thorClient.deposit(addParams);
|
|
966
975
|
return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
967
976
|
});
|
|
968
977
|
}
|
|
969
|
-
/**
|
|
978
|
+
/** Function handles liquidity Withdraw for Rune only
|
|
970
979
|
*
|
|
971
980
|
* @param params - withdraw parameters
|
|
972
981
|
* @param memo - memo needed to withdraw lp
|
|
@@ -980,14 +989,12 @@ class Wallet {
|
|
|
980
989
|
amount: params.runeFee.baseAmount,
|
|
981
990
|
memo: memo,
|
|
982
991
|
};
|
|
983
|
-
// console.log(addParams)
|
|
984
992
|
const hash = yield thorClient.deposit(addParams);
|
|
985
993
|
return { hash, url: thorchainClient.getExplorerTxUrl(hash), waitTimeSeconds };
|
|
986
994
|
});
|
|
987
995
|
}
|
|
988
996
|
}
|
|
989
997
|
|
|
990
|
-
const BN_1 = new bignumber_js.BigNumber(1);
|
|
991
998
|
const defaultQuery = new xchainThorchainQuery.ThorchainQuery();
|
|
992
999
|
/**
|
|
993
1000
|
* THORChain Class for interacting with THORChain.
|
|
@@ -1012,14 +1019,14 @@ class ThorchainAMM {
|
|
|
1012
1019
|
|
|
1013
1020
|
* @returns The SwapEstimate
|
|
1014
1021
|
*/
|
|
1015
|
-
estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress = '', interfaceID =
|
|
1022
|
+
estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress = '', interfaceID = `555`, affiliateFeeBasisPoints = 0, slipLimit, }) {
|
|
1016
1023
|
return this.thorchainQuery.estimateSwap({
|
|
1017
1024
|
input,
|
|
1018
1025
|
destinationAsset,
|
|
1019
1026
|
destinationAddress,
|
|
1020
1027
|
affiliateAddress,
|
|
1021
1028
|
interfaceID,
|
|
1022
|
-
|
|
1029
|
+
affiliateFeeBasisPoints,
|
|
1023
1030
|
slipLimit,
|
|
1024
1031
|
});
|
|
1025
1032
|
}
|
|
@@ -1032,29 +1039,17 @@ class ThorchainAMM {
|
|
|
1032
1039
|
*/
|
|
1033
1040
|
doSwap(wallet, params) {
|
|
1034
1041
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1035
|
-
//
|
|
1042
|
+
// Thorchain-query call satisfies the data needed for executeSwap to be called.
|
|
1036
1043
|
const txDetails = yield this.thorchainQuery.estimateSwap(params);
|
|
1037
1044
|
if (!txDetails.txEstimate.canSwap) {
|
|
1038
1045
|
throw Error(txDetails.txEstimate.errors.join('\n'));
|
|
1039
1046
|
}
|
|
1040
|
-
// remove any affiliateFee. netInput * affiliateFee (%age) of the destination asset type
|
|
1041
|
-
const affiliateFee = params.input.baseAmount.times(params.affiliateFeePercent || 0);
|
|
1042
|
-
// Work out LIM from the slip percentage
|
|
1043
|
-
let limPercentage = BN_1;
|
|
1044
|
-
if (params.slipLimit) {
|
|
1045
|
-
limPercentage = BN_1.minus(params.slipLimit || 1);
|
|
1046
|
-
} // else allowed slip is 100%
|
|
1047
|
-
const limAssetAmount = txDetails.txEstimate.netOutput.times(limPercentage);
|
|
1048
|
-
const waitTimeSeconds = txDetails.txEstimate.waitTimeSeconds;
|
|
1049
1047
|
return yield wallet.executeSwap({
|
|
1050
1048
|
input: params.input,
|
|
1051
1049
|
destinationAsset: params.destinationAsset,
|
|
1052
|
-
limit: limAssetAmount.baseAmount,
|
|
1053
1050
|
destinationAddress: params.destinationAddress,
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
interfaceID: params.interfaceID || 999,
|
|
1057
|
-
waitTimeSeconds,
|
|
1051
|
+
memo: txDetails.memo,
|
|
1052
|
+
waitTimeSeconds: txDetails.txEstimate.waitTimeSeconds,
|
|
1058
1053
|
});
|
|
1059
1054
|
});
|
|
1060
1055
|
}
|
package/lib/thorchain-amm.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export declare class ThorchainAMM {
|
|
|
23
23
|
|
|
24
24
|
* @returns The SwapEstimate
|
|
25
25
|
*/
|
|
26
|
-
estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress, interfaceID,
|
|
26
|
+
estimateSwap({ input, destinationAsset, destinationAddress, affiliateAddress, interfaceID, affiliateFeeBasisPoints, slipLimit, }: EstimateSwapParams): Promise<TxDetails>;
|
|
27
27
|
/**
|
|
28
28
|
* Conducts a swap with the given inputs. Should be called after estimateSwap() to ensure the swap is valid
|
|
29
29
|
*
|
package/lib/types.d.ts
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
1
|
import { FeeOption } from '@xchainjs/xchain-client';
|
|
2
2
|
import { CryptoAmount, LiquidityPool } from '@xchainjs/xchain-thorchain-query';
|
|
3
3
|
import { Address, Asset, BaseAmount } from '@xchainjs/xchain-util';
|
|
4
|
-
import { BigNumber } from 'bignumber.js';
|
|
5
|
-
export declare type MidgardConfig = {
|
|
6
|
-
apiRetries: number;
|
|
7
|
-
midgardBaseUrls: string[];
|
|
8
|
-
};
|
|
9
4
|
export declare type ExecuteSwap = {
|
|
10
5
|
input: CryptoAmount;
|
|
11
6
|
destinationAsset: Asset;
|
|
12
|
-
limit: BaseAmount;
|
|
13
7
|
destinationAddress: Address;
|
|
14
|
-
|
|
15
|
-
affiliateFee: BaseAmount;
|
|
8
|
+
memo: string;
|
|
16
9
|
feeOption?: FeeOption;
|
|
17
|
-
interfaceID: number;
|
|
18
10
|
waitTimeSeconds: number;
|
|
19
11
|
};
|
|
20
12
|
export declare type TxSubmitted = {
|
|
@@ -50,12 +42,3 @@ export declare type DepositParams = {
|
|
|
50
42
|
feeOption: FeeOption;
|
|
51
43
|
memo: string;
|
|
52
44
|
};
|
|
53
|
-
export declare type SwapOutput = {
|
|
54
|
-
output: CryptoAmount;
|
|
55
|
-
swapFee: CryptoAmount;
|
|
56
|
-
slip: BigNumber;
|
|
57
|
-
};
|
|
58
|
-
export declare type UnitData = {
|
|
59
|
-
liquidityUnits: BigNumber;
|
|
60
|
-
totalUnits: BigNumber;
|
|
61
|
-
};
|
package/lib/wallet.d.ts
CHANGED
|
@@ -36,23 +36,37 @@ export declare class Wallet {
|
|
|
36
36
|
* @see ThorchainAMM.doSwap()
|
|
37
37
|
*/
|
|
38
38
|
executeSwap(swap: ExecuteSwap): Promise<TxSubmitted>;
|
|
39
|
-
|
|
39
|
+
/** Validate swap object
|
|
40
|
+
*
|
|
41
|
+
* @param swap - swap parameters
|
|
42
|
+
*/
|
|
40
43
|
private validateSwap;
|
|
44
|
+
private isThorname;
|
|
45
|
+
/** Function handles all swaps from Rune to asset
|
|
46
|
+
*
|
|
47
|
+
* @param swap - swap parameters
|
|
48
|
+
* @returns - tx submitted object
|
|
49
|
+
*/
|
|
41
50
|
private swapRuneTo;
|
|
42
|
-
|
|
43
|
-
/** BASED OFF https://dev.thorchain.or›g/thorchain-dev/network/memos
|
|
51
|
+
/** Function handles all swaps from Non Rune
|
|
44
52
|
*
|
|
53
|
+
* @param swap - swap object
|
|
54
|
+
* @returns - TxSubmitted object
|
|
55
|
+
*/
|
|
56
|
+
private swapNonRune;
|
|
57
|
+
/** Function handles liquidity Add
|
|
58
|
+
* BASED OFF https://dev.thorchain.or›g/thorchain-dev/network/memos
|
|
45
59
|
* @param params input parameters needed to add liquidity
|
|
46
60
|
* @returns transaction details submitted
|
|
47
61
|
*/
|
|
48
62
|
addLiquidity(params: AddLiquidity): Promise<TxSubmitted[]>;
|
|
49
|
-
/**
|
|
63
|
+
/** Function handles liquidity Withdraw
|
|
50
64
|
*
|
|
51
65
|
* @param params - parameters required for liquidity position
|
|
52
66
|
* @returns object with tx response, url and wait time in seconds
|
|
53
67
|
*/
|
|
54
68
|
withdrawLiquidity(params: WithdrawLiquidity): Promise<TxSubmitted[]>;
|
|
55
|
-
/**
|
|
69
|
+
/** Function handles liquidity add for all non rune assets
|
|
56
70
|
*
|
|
57
71
|
* @param params - parameters for add liquidity
|
|
58
72
|
* @param constructedMemo - memo needed for thorchain
|
|
@@ -62,7 +76,7 @@ export declare class Wallet {
|
|
|
62
76
|
* @returns - tx object
|
|
63
77
|
*/
|
|
64
78
|
private addAssetLP;
|
|
65
|
-
/**
|
|
79
|
+
/** Function handles liquidity Withdraw for Non rune assets
|
|
66
80
|
*
|
|
67
81
|
* @param params - parameters for withdraw liquidity
|
|
68
82
|
* @param constructedMemo - memo needed for thorchain execution
|
|
@@ -72,14 +86,14 @@ export declare class Wallet {
|
|
|
72
86
|
* @returns - tx object
|
|
73
87
|
*/
|
|
74
88
|
private withdrawAssetLP;
|
|
75
|
-
/**
|
|
89
|
+
/** Function handles liquidity Add for Rune only
|
|
76
90
|
*
|
|
77
91
|
* @param params - deposit parameters
|
|
78
92
|
* @param memo - memo needed to withdraw lp
|
|
79
93
|
* @returns - tx object
|
|
80
94
|
*/
|
|
81
95
|
private addRuneLP;
|
|
82
|
-
/**
|
|
96
|
+
/** Function handles liquidity Withdraw for Rune only
|
|
83
97
|
*
|
|
84
98
|
* @param params - withdraw parameters
|
|
85
99
|
* @param memo - memo needed to withdraw lp
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xchainjs/xchain-thorchain-amm",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "module that exposes estimating & swappping cryptocurrency assets on thorchain",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"THORChain",
|
|
@@ -51,10 +51,10 @@
|
|
|
51
51
|
"@xchainjs/xchain-evm": "^0.1.0",
|
|
52
52
|
"@xchainjs/xchain-avax": "^0.1.0",
|
|
53
53
|
"@xchainjs/xchain-litecoin": "^0.10.3",
|
|
54
|
-
"@xchainjs/xchain-midgard": "0.
|
|
54
|
+
"@xchainjs/xchain-midgard": "0.2.0",
|
|
55
55
|
"@xchainjs/xchain-thorchain": "^0.27.0",
|
|
56
|
-
"@xchainjs/xchain-thorchain-query": "^0.1.
|
|
57
|
-
"@xchainjs/xchain-thornode": "^0.1.
|
|
56
|
+
"@xchainjs/xchain-thorchain-query": "^0.1.5",
|
|
57
|
+
"@xchainjs/xchain-thornode": "^0.1.1",
|
|
58
58
|
"@xchainjs/xchain-util": "^0.11.0",
|
|
59
59
|
"axios": "^0.25.0",
|
|
60
60
|
"axios-retry": "^3.2.5",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
"@xchainjs/xchain-evm": "^0.1.0",
|
|
85
85
|
"@xchainjs/xchain-avax": "^0.1.0",
|
|
86
86
|
"@xchainjs/xchain-litecoin": "^0.10.4",
|
|
87
|
-
"@xchainjs/xchain-midgard": "0.
|
|
87
|
+
"@xchainjs/xchain-midgard": "0.2.0",
|
|
88
88
|
"@xchainjs/xchain-thornode": "^0.1.0",
|
|
89
89
|
"@xchainjs/xchain-thorchain": "^0.27.1",
|
|
90
90
|
"@xchainjs/xchain-thorchain-query": "^0.1.2",
|