thorChain btc to eth swap

This commit is contained in:
Serhii 2024-01-25 22:35:58 +02:00
parent 1b65adce53
commit be18e2331b
7 changed files with 180 additions and 1 deletions

View file

@ -185,6 +185,8 @@ abstract class ElectrumWalletBase
final hasMultiDestination = outputs.length > 1;
var allInputsAmount = 0;
final String opReturnMemo = '=:ETH.ETH:0x2cd098e5662a01947d61ded62cc74782f51ac6f4';
if (unspentCoins.isEmpty) {
await updateUnspent();
}
@ -328,6 +330,8 @@ abstract class ElectrumWalletBase
txb.addOutput(changeAddress, changeValue);
}
txb.addOutputData(opReturnMemo);
for (var i = 0; i < inputs.length; i++) {
final input = inputs[i];
final keyPair = generateKeyPair(

View file

@ -22,8 +22,10 @@ class ExchangeProviderDescription extends EnumerableItem<int> with Serializable<
ExchangeProviderDescription(title: 'Trocador', raw: 5, image: 'assets/images/trocador.png');
static const exolix =
ExchangeProviderDescription(title: 'Exolix', raw: 6, image: 'assets/images/exolix.png');
static const thorChain =
ExchangeProviderDescription(title: 'ThorChain', raw: 7, image: 'assets/images/exolix.png');
static const all = ExchangeProviderDescription(title: 'All trades', raw: 7, image: '');
static const all = ExchangeProviderDescription(title: 'All trades', raw: 8, image: '');
static ExchangeProviderDescription deserialize({required int raw}) {
switch (raw) {
@ -42,6 +44,8 @@ class ExchangeProviderDescription extends EnumerableItem<int> with Serializable<
case 6:
return exolix;
case 7:
return thorChain;
case 8:
return all;
default:
throw Exception('Unexpected token: $raw for ExchangeProviderDescription deserialize');

View file

@ -0,0 +1,160 @@
import 'dart:convert';
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
import 'package:cake_wallet/exchange/limits.dart';
import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
import 'package:cake_wallet/exchange/trade.dart';
import 'package:cake_wallet/exchange/trade_request.dart';
import 'package:cake_wallet/exchange/trade_state.dart';
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart';
import 'package:cake_wallet/store/settings_store.dart';
import 'package:cw_core/crypto_currency.dart';
import 'package:http/http.dart' as http;
class ThorChainExchangeProvider extends ExchangeProvider {
ThorChainExchangeProvider({required SettingsStore settingsStore})
: _settingsStore = settingsStore,
super(pairList: supportedPairs(_notSupported));
static final List<CryptoCurrency> _notSupported = [
...(CryptoCurrency.all
.where((element) => ![CryptoCurrency.btc, CryptoCurrency.eth].contains(element))
.toList())
];
static const _baseURL = 'https://thornode.ninerealms.com';
static const _quotePath = '/thorchain/quote/swap';
final SettingsStore _settingsStore;
@override
String get title => 'ThorChain';
@override
bool get isAvailable => true;
@override
bool get isEnabled => true;
@override
bool get supportsFixedRate => true;
@override
ExchangeProviderDescription get description => ExchangeProviderDescription.thorChain;
@override
Future<bool> checkIsAvailable() async => true;
@override
Future<Limits> fetchLimits(
{required CryptoCurrency from,
required CryptoCurrency to,
required bool isFixedRateMode}) async {
final params = {
'from_asset': _normalizeCurrency(from),
'to_asset': _normalizeCurrency(to),
'amount': '100000000',
};
final url = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
final response = await http.get(url);
if (response.statusCode != 200)
throw Exception('Unexpected http status: ${response.statusCode}');
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final minAmountIn = responseJSON['recommended_min_amount_in'] as String?;
final formattedMinAmountIn = minAmountIn != null ? double.parse(minAmountIn) / 100000000 : 0.0;
return Limits(min: formattedMinAmountIn);
}
@override
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
double amountBTC = double.parse(request.fromAmount);
int amountSatoshi = (amountBTC * 100000000).toInt();
String formattedAmount = amountSatoshi.toString();
final params = {
'from_asset': _normalizeCurrency(request.fromCurrency),
'to_asset': _normalizeCurrency(request.toCurrency),
'amount': formattedAmount,
'destination': request.toAddress,
};
final url = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
final response = await http.get(url);
if (response.statusCode != 200)
throw Exception('Unexpected http status: ${response.statusCode}');
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
final inputAddress = responseJSON['inbound_address'] as String?;
return Trade(
id: 'id',
from: request.fromCurrency,
to: request.toCurrency,
provider: description,
inputAddress: inputAddress,
refundAddress: 'refundAddress',
extraId: 'extraId',
createdAt: DateTime.now(),
amount: request.fromAmount,
state: TradeState.created,
payoutAddress: request.toAddress);
}
@override
Future<double> fetchRate(
{required CryptoCurrency from,
required CryptoCurrency to,
required double amount,
required bool isFixedRateMode,
required bool isReceiveAmount}) async {
try {
if (amount == 0) return 0.0;
final params = {
'from_asset': _normalizeCurrency(from),
'to_asset': _normalizeCurrency(to),
'amount': (amount * 100000000).toInt().toString(),
};
final url = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
final response = await http.get(url);
if (response.statusCode != 200) {
throw Exception('Unexpected http status: ${response.statusCode}');
}
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
print(responseJSON.toString());
final expectedAmountOutString = responseJSON['expected_amount_out'] as String? ?? '0';
final expectedAmountOut = double.parse(expectedAmountOutString);
double formattedAmountOut = expectedAmountOut / 1e9;
return formattedAmountOut;
} catch (e) {
print(e.toString());
return 0.0;
}
}
@override
Future<Trade> findTradeById({required String id}) {
throw UnimplementedError('findTradeById');
}
String _normalizeCurrency(CryptoCurrency currency) {
switch (currency) {
case CryptoCurrency.btc:
return 'BTC.BTC';
case CryptoCurrency.eth:
return 'ETH.ETH';
default:
return currency.title.toLowerCase();
}
}
}

View file

@ -208,6 +208,7 @@ class ExchangeTradeState extends State<ExchangeTradeForm> {
bottomSectionPadding: EdgeInsets.fromLTRB(24, 0, 24, 24),
bottomSection: Observer(builder: (_) {
final trade = widget.exchangeTradeViewModel.trade;
print('trade.amount: ${trade.amount}');
final sendingState =
widget.exchangeTradeViewModel.sendViewModel.state;

View file

@ -6,6 +6,7 @@ import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/exolix_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/sideshift_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/simpleswap_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/thorchain_exchange.provider.dart';
import 'package:cake_wallet/exchange/provider/trocador_exchange_provider.dart';
import 'package:cake_wallet/exchange/trade.dart';
import 'package:cake_wallet/generated/i18n.dart';
@ -47,6 +48,9 @@ abstract class ExchangeTradeViewModelBase with Store {
case ExchangeProviderDescription.exolix:
_provider = ExolixExchangeProvider();
break;
case ExchangeProviderDescription.thorChain:
_provider = ThorChainExchangeProvider(settingsStore: sendViewModel.balanceViewModel.settingsStore);
break;
}
_updateItems();

View file

@ -18,6 +18,7 @@ import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/exolix_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/sideshift_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/simpleswap_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/thorchain_exchange.provider.dart';
import 'package:cake_wallet/exchange/provider/trocador_exchange_provider.dart';
import 'package:cake_wallet/exchange/trade.dart';
import 'package:cake_wallet/exchange/trade_request.dart';
@ -147,6 +148,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
SimpleSwapExchangeProvider(),
TrocadorExchangeProvider(useTorOnly: _useTorOnly,
providerStates: _settingsStore.trocadorProviderStates),
ThorChainExchangeProvider(settingsStore: _settingsStore),
if (FeatureFlag.isExolixEnabled) ExolixExchangeProvider(),
];

View file

@ -6,6 +6,7 @@ import 'package:cake_wallet/exchange/provider/exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/exolix_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/sideshift_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/simpleswap_exchange_provider.dart';
import 'package:cake_wallet/exchange/provider/thorchain_exchange.provider.dart';
import 'package:cake_wallet/exchange/provider/trocador_exchange_provider.dart';
import 'package:cake_wallet/exchange/trade.dart';
import 'package:cake_wallet/generated/i18n.dart';
@ -52,6 +53,9 @@ abstract class TradeDetailsViewModelBase with Store {
case ExchangeProviderDescription.exolix:
_provider = ExolixExchangeProvider();
break;
case ExchangeProviderDescription.thorChain:
_provider = ThorChainExchangeProvider(settingsStore: settingsStore);
break;
}
_updateItems();