From 28eb83cc8fd310df19287c83c730a672e0d1060b Mon Sep 17 00:00:00 2001 From: David Cumps Date: Wed, 16 Jul 2025 05:49:12 +0200 Subject: [PATCH] Migrate to new Chainflip endpoints (#2386) * feat: migrate to new chainflip endpoints * chore: remove debug comments --- .../provider/chainflip_exchange_provider.dart | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/lib/exchange/provider/chainflip_exchange_provider.dart b/lib/exchange/provider/chainflip_exchange_provider.dart index c2e2f385c..131415707 100644 --- a/lib/exchange/provider/chainflip_exchange_provider.dart +++ b/lib/exchange/provider/chainflip_exchange_provider.dart @@ -37,7 +37,7 @@ class ChainflipExchangeProvider extends ExchangeProvider { static const _baseURL = 'chainflip-broker.io'; static const _assetsPath = '/assets'; - static const _quotePath = '/quote-native'; + static const _quotePath = '/quotes-native'; static const _swapPath = '/swap'; static const _txInfoPath = '/status-by-deposit-channel'; static const _affiliateBps = secrets.chainflipAffiliateFee; @@ -145,6 +145,13 @@ class ChainflipExchangeProvider extends ExchangeProvider { 'retryDurationInBlocks': '150' }; + if (quoteResponse.containsKey('numberOfChunks') && quoteResponse.containsKey('chunkIntervalBlocks')) { + swapParams.addAll({ + 'numberOfChunks': quoteResponse['numberOfChunks'].toString(), + 'chunkIntervalBlocks': quoteResponse['chunkIntervalBlocks'].toString(), + }); + } + final swapResponse = await _openDepositChannel(swapParams); final id = '${swapResponse['issuedBlock']}-${swapResponse['network'].toString().toUpperCase()}-${swapResponse['channelId']}'; @@ -266,9 +273,6 @@ class ChainflipExchangeProvider extends ExchangeProvider { Future> _getAssets() async => _getRequest(_assetsPath, {}); - Future> _getSwapQuote(Map params) async => - _getRequest(_quotePath, params); - Future> _openDepositChannel(Map params) async => _getRequest(_swapPath, params); @@ -276,7 +280,6 @@ class ChainflipExchangeProvider extends ExchangeProvider { final uri = Uri.https(_baseURL, path, params); final response = await ProxyWrapper().get(clearnetUri: uri); - if ((response.statusCode != 200) || (response.body.contains('error'))) { throw Exception('Unexpected response: ${response.statusCode} / ${uri.toString()} / ${response.body}'); @@ -285,11 +288,31 @@ class ChainflipExchangeProvider extends ExchangeProvider { return json.decode(response.body) as Map; } + Future> _getSwapQuote(Map params) async { + final uri = Uri.https(_baseURL, _quotePath, params); + + final response = await ProxyWrapper().get(clearnetUri: uri); + + if ((response.statusCode != 200) || (response.body.contains('error'))) { + throw Exception('Unexpected response: ${response.statusCode} / ${uri.toString()} / ${response.body}'); + } + + final quotes = json.decode(response.body) as List; + + Map highestQuote = quotes.reduce((current, next) { + double currentAmount = current['egressAmount']; + double nextAmount = next['egressAmount']; + + return currentAmount > nextAmount ? current : next; + }); + + return highestQuote; + } + Future?> _getStatus(Map params) async { final uri = Uri.https(_baseURL, _txInfoPath, params); final response = await ProxyWrapper().get(clearnetUri: uri); - if (response.statusCode == 404) return null;