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