@xchainjs/xchain-aggregator 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -9,7 +9,6 @@ var xchainMayachainQuery = require('@xchainjs/xchain-mayachain-query');
9
9
  var xchainThorchain = require('@xchainjs/xchain-thorchain');
10
10
  var xchainThorchainAmm = require('@xchainjs/xchain-thorchain-amm');
11
11
  var xchainThorchainQuery = require('@xchainjs/xchain-thorchain-query');
12
- var xchainWallet = require('@xchainjs/xchain-wallet');
13
12
 
14
13
  /******************************************************************************
15
14
  Copyright (c) Microsoft Corporation.
@@ -36,171 +35,10 @@ function __awaiter(thisArg, _arguments, P, generator) {
36
35
  });
37
36
  }
38
37
 
39
- class MayachainProtocol {
40
- constructor(wallet) {
41
- this.name = 'Mayachain';
42
- this.mayachainQuery = new xchainMayachainQuery.MayachainQuery();
43
- this.mayachainAmm = new xchainMayachainAmm.MayachainAMM(this.mayachainQuery, wallet);
44
- }
45
- /**
46
- * Check if an asset is supported in the protocol
47
- * @param {Asset} asset Asset to check if it is supported
48
- * @returns {boolean} True if the asset is supported, otherwise false
49
- */
50
- isAssetSupported(asset) {
51
- return __awaiter(this, void 0, void 0, function* () {
52
- if (xchainUtil.eqAsset(asset, xchainMayachain.AssetCacao))
53
- return true;
54
- const pools = yield this.mayachainQuery.getPools();
55
- return (pools.findIndex((pool) => pool.status === 'available' && xchainUtil.eqAsset(asset, xchainUtil.assetFromStringEx(pool.asset))) !== -1);
56
- });
57
- }
58
- /**
59
- * Retrieve the supported chains by the protocol
60
- * @returns {Chain[]} the supported chains by the protocol
61
- */
62
- getSupportedChains() {
63
- return __awaiter(this, void 0, void 0, function* () {
64
- const inboundDetails = yield this.mayachainQuery.getInboundDetails();
65
- return [xchainMayachain.MAYAChain, ...Object.values(inboundDetails).map((inboundAddress) => inboundAddress.chain)];
66
- });
67
- }
68
- /**
69
- * Estimate swap by validating the swap parameters.
70
- *
71
- * @param {QuoteSwapParams} quoteSwapParams Swap parameters.
72
- * @returns {QuoteSwap} Quote swap result. If swap cannot be done, it returns an empty QuoteSwap with reasons.
73
- */
74
- estimateSwap(params) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const estimatedSwap = yield this.mayachainAmm.estimateSwap(params);
77
- return {
78
- protocol: this.name,
79
- toAddress: estimatedSwap.toAddress,
80
- memo: estimatedSwap.memo,
81
- expectedAmount: estimatedSwap.expectedAmount,
82
- dustThreshold: estimatedSwap.dustThreshold,
83
- fees: estimatedSwap.fees,
84
- totalSwapSeconds: estimatedSwap.inboundConfirmationSeconds || 0 + estimatedSwap.outboundDelaySeconds,
85
- slipBasisPoints: estimatedSwap.slipBasisPoints,
86
- canSwap: estimatedSwap.canSwap,
87
- errors: estimatedSwap.errors,
88
- warning: estimatedSwap.warning,
89
- };
90
- });
91
- }
92
- /**
93
- * Perform a swap operation between assets.
94
- * @param {QuoteSwapParams} quoteSwapParams Swap parameters
95
- * @returns {TxSubmitted} Transaction hash and URL of the swap
96
- */
97
- doSwap(params) {
98
- return __awaiter(this, void 0, void 0, function* () {
99
- return this.mayachainAmm.doSwap(params);
100
- });
101
- }
102
- /**
103
- * Get historical swaps
104
- * @param {Address[]} addresses Addresses of which return their swap history
105
- * @returns the swap history
106
- */
107
- getSwapHistory({ chainAddresses }) {
108
- return __awaiter(this, void 0, void 0, function* () {
109
- const swapHistory = yield this.mayachainQuery.getSwapHistory({
110
- addresses: Array.from(new Set(chainAddresses.map((chainAddresses) => chainAddresses.address))),
111
- });
112
- return {
113
- count: swapHistory.count,
114
- swaps: swapHistory.swaps.map((swap) => {
115
- return Object.assign({ protocol: this.name }, swap);
116
- }),
117
- };
118
- });
119
- }
120
- }
121
-
122
- class ThorchainProtocol {
123
- constructor(wallet) {
124
- this.name = 'Thorchain';
125
- this.thorchainQuery = new xchainThorchainQuery.ThorchainQuery();
126
- this.thorchainAmm = new xchainThorchainAmm.ThorchainAMM(this.thorchainQuery, wallet);
127
- }
128
- /**
129
- * Check if an asset is supported in the protocol
130
- * @param {Asset} asset Asset to check if it is supported
131
- * @returns {boolean} True if the asset is supported, otherwise false
132
- */
133
- isAssetSupported(asset) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- if (xchainUtil.eqAsset(asset, xchainThorchain.AssetRuneNative))
136
- return true;
137
- const pools = yield this.thorchainQuery.thorchainCache.getPools();
138
- return (Object.values(pools).findIndex((pool) => pool.isAvailable() && xchainUtil.assetToString(asset) === pool.assetString) !== -1);
139
- });
140
- }
141
- /**
142
- * Retrieve the supported chains by the protocol
143
- * @returns {Chain[]} the supported chains by the protocol
144
- */
145
- getSupportedChains() {
146
- return __awaiter(this, void 0, void 0, function* () {
147
- const inboundDetails = yield this.thorchainQuery.thorchainCache.getInboundDetails();
148
- return [xchainThorchain.THORChain, ...Object.values(inboundDetails).map((inboundAddress) => inboundAddress.chain)];
149
- });
150
- }
151
- /**
152
- * Estimate swap by validating the swap parameters.
153
- *
154
- * @param {QuoteSwapParams} quoteSwapParams Swap parameters.
155
- * @returns {QuoteSwap} Quote swap result. If swap cannot be done, it returns an empty QuoteSwap with reasons.
156
- */
157
- estimateSwap(params) {
158
- return __awaiter(this, void 0, void 0, function* () {
159
- const estimatedSwap = yield this.thorchainAmm.estimateSwap(params);
160
- return {
161
- protocol: this.name,
162
- toAddress: estimatedSwap.toAddress,
163
- memo: estimatedSwap.memo,
164
- expectedAmount: estimatedSwap.txEstimate.netOutput,
165
- dustThreshold: estimatedSwap.dustThreshold,
166
- fees: estimatedSwap.txEstimate.totalFees,
167
- totalSwapSeconds: estimatedSwap.txEstimate.inboundConfirmationSeconds || 0 + estimatedSwap.txEstimate.outboundDelaySeconds,
168
- slipBasisPoints: estimatedSwap.txEstimate.slipBasisPoints,
169
- canSwap: estimatedSwap.txEstimate.canSwap,
170
- errors: estimatedSwap.txEstimate.errors,
171
- warning: estimatedSwap.txEstimate.warning,
172
- };
173
- });
174
- }
175
- /**
176
- * Perform a swap operation between assets.
177
- * @param {QuoteSwapParams} quoteSwapParams Swap parameters
178
- * @returns {TxSubmitted} Transaction hash and URL of the swap
179
- */
180
- doSwap(params) {
181
- return __awaiter(this, void 0, void 0, function* () {
182
- return this.thorchainAmm.doSwap(params);
183
- });
184
- }
185
- /**x
186
- * Get historical swaps
187
- * @param {Address[]} addresses Addresses of which return their swap history
188
- * @returns the swap history
189
- */
190
- getSwapHistory({ chainAddresses }) {
191
- return __awaiter(this, void 0, void 0, function* () {
192
- const swapHistory = yield this.thorchainQuery.getSwapHistory({
193
- addresses: Array.from(new Set(chainAddresses.map((chainAddresses) => chainAddresses.address))),
194
- });
195
- return {
196
- count: swapHistory.count,
197
- swaps: swapHistory.swaps.map((swap) => {
198
- return Object.assign({ protocol: this.name }, swap);
199
- }),
200
- };
201
- });
202
- }
203
- }
38
+ const SupportedProtocols = ['Thorchain', 'Mayachain', 'Chainflip'];
39
+ const DEFAULT_CONFIG = {
40
+ protocols: SupportedProtocols,
41
+ };
204
42
 
205
43
  /** @internal Last-resort "this", if it gets here it probably would fail anyway */
206
44
  function evaluateThis(fn) {
@@ -25579,12 +25417,12 @@ const xAssetToCAsset = (asset) => {
25579
25417
  * Chainflip protocol
25580
25418
  */
25581
25419
  class ChainflipProtocol {
25582
- constructor(wallet = new xchainWallet.Wallet({})) {
25420
+ constructor(configuration) {
25583
25421
  this.name = 'Chainflip';
25584
25422
  this.sdk = new SwapSDK({
25585
25423
  network: 'mainnet',
25586
25424
  });
25587
- this.wallet = wallet;
25425
+ this.wallet = configuration === null || configuration === void 0 ? void 0 : configuration.wallet;
25588
25426
  this.assetsData = new xchainUtil.CachedValue(() => {
25589
25427
  return this.sdk.getAssets();
25590
25428
  }, 24 * 60 * 60 * 1000);
@@ -25701,6 +25539,8 @@ class ChainflipProtocol {
25701
25539
  if (!quoteSwap.canSwap) {
25702
25540
  throw Error(`Can not make swap. ${quoteSwap.errors.join('\n')}`);
25703
25541
  }
25542
+ if (!this.wallet)
25543
+ throw Error('Wallet not configured. Can not do swap');
25704
25544
  const hash = yield this.wallet.transfer({
25705
25545
  recipient: quoteSwap.toAddress,
25706
25546
  amount: params.amount.baseAmount,
@@ -25745,10 +25585,222 @@ class ChainflipProtocol {
25745
25585
  }
25746
25586
  }
25747
25587
 
25588
+ class MayachainProtocol {
25589
+ constructor(configuration) {
25590
+ this.name = 'Mayachain';
25591
+ this.mayachainQuery = new xchainMayachainQuery.MayachainQuery();
25592
+ this.mayachainAmm = new xchainMayachainAmm.MayachainAMM(this.mayachainQuery, configuration === null || configuration === void 0 ? void 0 : configuration.wallet);
25593
+ this.configuration = configuration;
25594
+ }
25595
+ /**
25596
+ * Check if an asset is supported in the protocol
25597
+ * @param {Asset} asset Asset to check if it is supported
25598
+ * @returns {boolean} True if the asset is supported, otherwise false
25599
+ */
25600
+ isAssetSupported(asset) {
25601
+ return __awaiter(this, void 0, void 0, function* () {
25602
+ if (xchainUtil.eqAsset(asset, xchainMayachain.AssetCacao))
25603
+ return true;
25604
+ const pools = yield this.mayachainQuery.getPools();
25605
+ return (pools.findIndex((pool) => pool.status === 'available' && xchainUtil.eqAsset(asset, xchainUtil.assetFromStringEx(pool.asset))) !== -1);
25606
+ });
25607
+ }
25608
+ /**
25609
+ * Retrieve the supported chains by the protocol
25610
+ * @returns {Chain[]} the supported chains by the protocol
25611
+ */
25612
+ getSupportedChains() {
25613
+ return __awaiter(this, void 0, void 0, function* () {
25614
+ const inboundDetails = yield this.mayachainQuery.getInboundDetails();
25615
+ return [xchainMayachain.MAYAChain, ...Object.values(inboundDetails).map((inboundAddress) => inboundAddress.chain)];
25616
+ });
25617
+ }
25618
+ /**
25619
+ * Estimate swap by validating the swap parameters.
25620
+ *
25621
+ * @param {QuoteSwapParams} quoteSwapParams Swap parameters.
25622
+ * @returns {QuoteSwap} Quote swap result. If swap cannot be done, it returns an empty QuoteSwap with reasons.
25623
+ */
25624
+ estimateSwap(params) {
25625
+ var _a, _b;
25626
+ return __awaiter(this, void 0, void 0, function* () {
25627
+ const estimatedSwap = yield this.mayachainAmm.estimateSwap(Object.assign(Object.assign({}, params), { affiliateBps: (_a = this.configuration) === null || _a === void 0 ? void 0 : _a.affiliateBps, affiliateAddress: (_b = this.configuration) === null || _b === void 0 ? void 0 : _b.affiliateAddress }));
25628
+ return {
25629
+ protocol: this.name,
25630
+ toAddress: estimatedSwap.toAddress,
25631
+ memo: estimatedSwap.memo,
25632
+ expectedAmount: estimatedSwap.expectedAmount,
25633
+ dustThreshold: estimatedSwap.dustThreshold,
25634
+ fees: estimatedSwap.fees,
25635
+ totalSwapSeconds: estimatedSwap.inboundConfirmationSeconds || 0 + estimatedSwap.outboundDelaySeconds,
25636
+ slipBasisPoints: estimatedSwap.slipBasisPoints,
25637
+ canSwap: estimatedSwap.canSwap,
25638
+ errors: estimatedSwap.errors,
25639
+ warning: estimatedSwap.warning,
25640
+ };
25641
+ });
25642
+ }
25643
+ /**
25644
+ * Perform a swap operation between assets.
25645
+ * @param {QuoteSwapParams} quoteSwapParams Swap parameters
25646
+ * @returns {TxSubmitted} Transaction hash and URL of the swap
25647
+ */
25648
+ doSwap(params) {
25649
+ return __awaiter(this, void 0, void 0, function* () {
25650
+ return this.mayachainAmm.doSwap(params);
25651
+ });
25652
+ }
25653
+ /**
25654
+ * Get historical swaps
25655
+ * @param {Address[]} addresses Addresses of which return their swap history
25656
+ * @returns the swap history
25657
+ */
25658
+ getSwapHistory({ chainAddresses }) {
25659
+ return __awaiter(this, void 0, void 0, function* () {
25660
+ const swapHistory = yield this.mayachainQuery.getSwapHistory({
25661
+ addresses: Array.from(new Set(chainAddresses.map((chainAddresses) => chainAddresses.address))),
25662
+ });
25663
+ return {
25664
+ count: swapHistory.count,
25665
+ swaps: swapHistory.swaps.map((swap) => {
25666
+ return Object.assign({ protocol: this.name }, swap);
25667
+ }),
25668
+ };
25669
+ });
25670
+ }
25671
+ }
25672
+
25673
+ class ThorchainProtocol {
25674
+ constructor(configuration) {
25675
+ this.name = 'Thorchain';
25676
+ this.thorchainQuery = new xchainThorchainQuery.ThorchainQuery();
25677
+ this.thorchainAmm = new xchainThorchainAmm.ThorchainAMM(this.thorchainQuery, configuration === null || configuration === void 0 ? void 0 : configuration.wallet);
25678
+ this.configuration = configuration;
25679
+ }
25680
+ /**
25681
+ * Check if an asset is supported in the protocol
25682
+ * @param {Asset} asset Asset to check if it is supported
25683
+ * @returns {boolean} True if the asset is supported, otherwise false
25684
+ */
25685
+ isAssetSupported(asset) {
25686
+ return __awaiter(this, void 0, void 0, function* () {
25687
+ if (xchainUtil.eqAsset(asset, xchainThorchain.AssetRuneNative))
25688
+ return true;
25689
+ const pools = yield this.thorchainQuery.thorchainCache.getPools();
25690
+ return (Object.values(pools).findIndex((pool) => pool.isAvailable() && xchainUtil.assetToString(asset) === pool.assetString) !== -1);
25691
+ });
25692
+ }
25693
+ /**
25694
+ * Retrieve the supported chains by the protocol
25695
+ * @returns {Chain[]} the supported chains by the protocol
25696
+ */
25697
+ getSupportedChains() {
25698
+ return __awaiter(this, void 0, void 0, function* () {
25699
+ const inboundDetails = yield this.thorchainQuery.thorchainCache.getInboundDetails();
25700
+ return [xchainThorchain.THORChain, ...Object.values(inboundDetails).map((inboundAddress) => inboundAddress.chain)];
25701
+ });
25702
+ }
25703
+ /**
25704
+ * Estimate swap by validating the swap parameters.
25705
+ *
25706
+ * @param {QuoteSwapParams} quoteSwapParams Swap parameters.
25707
+ * @returns {QuoteSwap} Quote swap result. If swap cannot be done, it returns an empty QuoteSwap with reasons.
25708
+ */
25709
+ estimateSwap(params) {
25710
+ var _a, _b;
25711
+ return __awaiter(this, void 0, void 0, function* () {
25712
+ const estimatedSwap = yield this.thorchainAmm.estimateSwap(Object.assign(Object.assign({}, params), { affiliateBps: (_a = this.configuration) === null || _a === void 0 ? void 0 : _a.affiliateBps, affiliateAddress: (_b = this.configuration) === null || _b === void 0 ? void 0 : _b.affiliateAddress }));
25713
+ return {
25714
+ protocol: this.name,
25715
+ toAddress: estimatedSwap.toAddress,
25716
+ memo: estimatedSwap.memo,
25717
+ expectedAmount: estimatedSwap.txEstimate.netOutput,
25718
+ dustThreshold: estimatedSwap.dustThreshold,
25719
+ fees: estimatedSwap.txEstimate.totalFees,
25720
+ totalSwapSeconds: estimatedSwap.txEstimate.inboundConfirmationSeconds || 0 + estimatedSwap.txEstimate.outboundDelaySeconds,
25721
+ slipBasisPoints: estimatedSwap.txEstimate.slipBasisPoints,
25722
+ canSwap: estimatedSwap.txEstimate.canSwap,
25723
+ errors: estimatedSwap.txEstimate.errors,
25724
+ warning: estimatedSwap.txEstimate.warning,
25725
+ };
25726
+ });
25727
+ }
25728
+ /**
25729
+ * Perform a swap operation between assets.
25730
+ * @param {QuoteSwapParams} quoteSwapParams Swap parameters
25731
+ * @returns {TxSubmitted} Transaction hash and URL of the swap
25732
+ */
25733
+ doSwap(params) {
25734
+ return __awaiter(this, void 0, void 0, function* () {
25735
+ return this.thorchainAmm.doSwap(params);
25736
+ });
25737
+ }
25738
+ /**x
25739
+ * Get historical swaps
25740
+ * @param {Address[]} addresses Addresses of which return their swap history
25741
+ * @returns the swap history
25742
+ */
25743
+ getSwapHistory({ chainAddresses }) {
25744
+ return __awaiter(this, void 0, void 0, function* () {
25745
+ const swapHistory = yield this.thorchainQuery.getSwapHistory({
25746
+ addresses: Array.from(new Set(chainAddresses.map((chainAddresses) => chainAddresses.address))),
25747
+ });
25748
+ return {
25749
+ count: swapHistory.count,
25750
+ swaps: swapHistory.swaps.map((swap) => {
25751
+ return Object.assign({ protocol: this.name }, swap);
25752
+ }),
25753
+ };
25754
+ });
25755
+ }
25756
+ }
25757
+
25758
+ const getProtocolConfig = (name, configuration) => {
25759
+ var _a, _b;
25760
+ return {
25761
+ wallet: configuration.wallet,
25762
+ affiliateAddress: (_a = configuration.affiliate) === null || _a === void 0 ? void 0 : _a.affiliates[name],
25763
+ affiliateBps: ((_b = configuration.affiliate) === null || _b === void 0 ? void 0 : _b.affiliates[name]) ? configuration.affiliate.basisPoints : undefined,
25764
+ };
25765
+ };
25766
+ class ProtocolFactory {
25767
+ static getProtocol(name, configuration) {
25768
+ const protocolConfig = getProtocolConfig(name, configuration);
25769
+ switch (name) {
25770
+ case 'Thorchain':
25771
+ return new ThorchainProtocol(protocolConfig);
25772
+ case 'Mayachain':
25773
+ return new MayachainProtocol(protocolConfig);
25774
+ case 'Chainflip':
25775
+ return new ChainflipProtocol(protocolConfig);
25776
+ }
25777
+ }
25778
+ }
25779
+
25748
25780
  // Class definition for an Aggregator
25749
25781
  class Aggregator {
25750
- constructor(wallet) {
25751
- this.protocols = [new ThorchainProtocol(wallet), new MayachainProtocol(wallet), new ChainflipProtocol(wallet)];
25782
+ constructor(config) {
25783
+ const fConfig = Object.assign(Object.assign({}, DEFAULT_CONFIG), config);
25784
+ this.verifyConfig(fConfig);
25785
+ this.protocols = fConfig.protocols.map((protocol) => ProtocolFactory.getProtocol(protocol, fConfig));
25786
+ this.config = Object.assign(Object.assign({}, fConfig), { protocols: this.protocols.map((protocol) => protocol.name) });
25787
+ }
25788
+ /**
25789
+ * Get the current Aggregator configuration
25790
+ * @returns {Omit<Config, 'wallet'>} the current Aggregator configuration
25791
+ */
25792
+ getConfiguration() {
25793
+ return { protocols: this.config.protocols, affiliate: this.config.affiliate };
25794
+ }
25795
+ /**
25796
+ * Update the Aggregator configuration
25797
+ * @param {Configuration} config
25798
+ */
25799
+ setConfiguration(config) {
25800
+ const fConfig = Object.assign(Object.assign({}, DEFAULT_CONFIG), config);
25801
+ this.verifyConfig(fConfig);
25802
+ this.protocols = fConfig.protocols.map((protocol) => ProtocolFactory.getProtocol(protocol, fConfig));
25803
+ this.config = Object.assign(Object.assign({}, fConfig), { protocols: this.protocols.map((protocol) => protocol.name) });
25752
25804
  }
25753
25805
  /**
25754
25806
  * Estimate swap
@@ -25830,6 +25882,12 @@ class Aggregator {
25830
25882
  };
25831
25883
  });
25832
25884
  }
25885
+ verifyConfig(config) {
25886
+ if (config.affiliate && (config.affiliate.basisPoints < 0 || config.affiliate.basisPoints > 10000))
25887
+ throw Error('Invalid affiliate basis point due to it is out of bound. It must be between [0 - 10000]');
25888
+ if (config.protocols.length === 0)
25889
+ throw Error('No protocols enabled');
25890
+ }
25833
25891
  }
25834
25892
 
25835
25893
  // ../shared/src/strings.ts