@xchainjs/xchain-aggregator 1.0.24 → 1.0.25
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/aggregator.d.ts +1 -1
- package/lib/index.esm.js +35 -10
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +35 -10
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +1 -0
- package/package.json +5 -5
package/lib/aggregator.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export declare class Aggregator {
|
|
|
20
20
|
* @param {QuoteSwapParams} params Swap parameters
|
|
21
21
|
* @returns the swap with the greatest expected amount estimated in the supported protocols
|
|
22
22
|
*/
|
|
23
|
-
estimateSwap(params: QuoteSwapParams): Promise<QuoteSwap>;
|
|
23
|
+
estimateSwap(params: QuoteSwapParams): Promise<QuoteSwap[]>;
|
|
24
24
|
/**
|
|
25
25
|
* Do swap
|
|
26
26
|
* @param {QuoteSwapParams & { protocol?: Protocol }} params Swap parameters. If protocol is not set,
|
package/lib/index.esm.js
CHANGED
|
@@ -45630,6 +45630,7 @@ class ChainflipProtocol {
|
|
|
45630
45630
|
const destAssetData = yield this.getAssetData(params.destinationAsset);
|
|
45631
45631
|
try {
|
|
45632
45632
|
let toAddress = '';
|
|
45633
|
+
let depositChannelId = '';
|
|
45633
45634
|
const { quotes } = yield this.sdk.getQuoteV2({
|
|
45634
45635
|
srcChain: srcAssetData.chain,
|
|
45635
45636
|
srcAsset: srcAssetData.asset,
|
|
@@ -45651,6 +45652,7 @@ class ChainflipProtocol {
|
|
|
45651
45652
|
},
|
|
45652
45653
|
});
|
|
45653
45654
|
toAddress = resp.depositAddress;
|
|
45655
|
+
depositChannelId = resp.depositChannelId;
|
|
45654
45656
|
}
|
|
45655
45657
|
else if (params.destinationAddress && (selectedQuote === null || selectedQuote === void 0 ? void 0 : selectedQuote.type) === 'REGULAR' && params.fromAddress) {
|
|
45656
45658
|
const resp = yield this.sdk.requestDepositAddressV2({
|
|
@@ -45664,6 +45666,7 @@ class ChainflipProtocol {
|
|
|
45664
45666
|
},
|
|
45665
45667
|
});
|
|
45666
45668
|
toAddress = resp.depositAddress;
|
|
45669
|
+
depositChannelId = resp.depositChannelId;
|
|
45667
45670
|
}
|
|
45668
45671
|
else {
|
|
45669
45672
|
console.error('No suitable quote found or destination/refund address missing');
|
|
@@ -45689,6 +45692,7 @@ class ChainflipProtocol {
|
|
|
45689
45692
|
outboundFee: new CryptoAmount(baseAmount(outboundFee ? outboundFee.amount : 0, destAssetData.decimals), params.destinationAsset),
|
|
45690
45693
|
affiliateFee: new CryptoAmount(baseAmount(brokerFee ? brokerFee.amount : 0, destAssetData.decimals), params.destinationAsset),
|
|
45691
45694
|
},
|
|
45695
|
+
depositChannelId,
|
|
45692
45696
|
};
|
|
45693
45697
|
}
|
|
45694
45698
|
catch (e) {
|
|
@@ -45709,6 +45713,7 @@ class ChainflipProtocol {
|
|
|
45709
45713
|
outboundFee: new CryptoAmount(baseAmount(0, destAssetData.decimals), params.destinationAsset),
|
|
45710
45714
|
affiliateFee: new CryptoAmount(baseAmount(0, destAssetData.decimals), params.destinationAsset),
|
|
45711
45715
|
},
|
|
45716
|
+
depositChannelId: undefined,
|
|
45712
45717
|
};
|
|
45713
45718
|
}
|
|
45714
45719
|
});
|
|
@@ -46072,16 +46077,15 @@ class Aggregator {
|
|
|
46072
46077
|
return protocol.estimateSwap(params);
|
|
46073
46078
|
});
|
|
46074
46079
|
const results = yield Promise.allSettled(this.protocols.map((protocol) => estimateTask(protocol, params)));
|
|
46075
|
-
|
|
46080
|
+
const successfulQuotes = [];
|
|
46076
46081
|
results.forEach((result) => {
|
|
46077
46082
|
if (result.status === 'fulfilled') {
|
|
46078
|
-
|
|
46079
|
-
optimalSwap = result.value;
|
|
46083
|
+
successfulQuotes.push(result.value);
|
|
46080
46084
|
}
|
|
46081
46085
|
});
|
|
46082
|
-
if (!
|
|
46086
|
+
if (!successfulQuotes)
|
|
46083
46087
|
throw Error(`Can not estimate swap from ${assetToString(params.fromAsset)} to ${assetToString(params.destinationAsset)}`);
|
|
46084
|
-
return
|
|
46088
|
+
return successfulQuotes;
|
|
46085
46089
|
});
|
|
46086
46090
|
}
|
|
46087
46091
|
/**
|
|
@@ -46093,17 +46097,38 @@ class Aggregator {
|
|
|
46093
46097
|
*/
|
|
46094
46098
|
doSwap(params) {
|
|
46095
46099
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46096
|
-
|
|
46097
|
-
|
|
46100
|
+
let successfulQuotes = [];
|
|
46101
|
+
if (params.protocol) {
|
|
46102
|
+
// Use the specified protocol
|
|
46103
|
+
const protocol = this.protocols.find((protocol) => protocol.name === params.protocol);
|
|
46104
|
+
if (!protocol) {
|
|
46105
|
+
throw Error(`${params.protocol} protocol is not supported`);
|
|
46106
|
+
}
|
|
46107
|
+
successfulQuotes = [yield protocol.estimateSwap(params)];
|
|
46108
|
+
}
|
|
46109
|
+
else {
|
|
46110
|
+
// Fetch quotes from all protocols
|
|
46111
|
+
const results = yield Promise.allSettled(this.protocols.map((protocol) => protocol.estimateSwap(params)));
|
|
46112
|
+
successfulQuotes = results
|
|
46113
|
+
.filter((result) => result.status === 'fulfilled')
|
|
46114
|
+
.map((result) => result.value);
|
|
46115
|
+
}
|
|
46116
|
+
if (successfulQuotes.length === 0) {
|
|
46117
|
+
throw Error(`Cannot estimate swap from ${assetToString(params.fromAsset)} to ${assetToString(params.destinationAsset)}`);
|
|
46118
|
+
}
|
|
46119
|
+
// Select the quote with the highest expected amount
|
|
46120
|
+
const bestQuote = successfulQuotes.reduce((best, current) => current.expectedAmount.gt(best.expectedAmount) ? current : best);
|
|
46121
|
+
const protocol = this.protocols.find((protocol) => protocol.name === bestQuote.protocol);
|
|
46098
46122
|
if (!protocol) {
|
|
46099
|
-
throw Error(`${
|
|
46123
|
+
throw Error(`${bestQuote.protocol} protocol is not supported`);
|
|
46100
46124
|
}
|
|
46101
46125
|
if (isTokenAsset(params.fromAsset)) {
|
|
46102
|
-
|
|
46126
|
+
const approvalNeeded = yield protocol.shouldBeApproved({
|
|
46103
46127
|
asset: params.fromAsset,
|
|
46104
46128
|
amount: params.amount,
|
|
46105
46129
|
address: params.fromAddress || '',
|
|
46106
|
-
})
|
|
46130
|
+
});
|
|
46131
|
+
if (approvalNeeded) {
|
|
46107
46132
|
yield protocol.approveRouterToSpend({ asset: params.fromAsset, amount: params.amount });
|
|
46108
46133
|
}
|
|
46109
46134
|
}
|