mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-09 04:19:36 +00:00
eth to btc swap
This commit is contained in:
parent
be18e2331b
commit
705189575c
15 changed files with 141 additions and 57 deletions
|
@ -185,7 +185,8 @@ abstract class ElectrumWalletBase
|
||||||
final hasMultiDestination = outputs.length > 1;
|
final hasMultiDestination = outputs.length > 1;
|
||||||
var allInputsAmount = 0;
|
var allInputsAmount = 0;
|
||||||
|
|
||||||
final String opReturnMemo = '=:ETH.ETH:0x2cd098e5662a01947d61ded62cc74782f51ac6f4';
|
final String? opReturnMemo = outputs.first.memo;
|
||||||
|
|
||||||
|
|
||||||
if (unspentCoins.isEmpty) {
|
if (unspentCoins.isEmpty) {
|
||||||
await updateUnspent();
|
await updateUnspent();
|
||||||
|
@ -330,7 +331,7 @@ abstract class ElectrumWalletBase
|
||||||
txb.addOutput(changeAddress, changeValue);
|
txb.addOutput(changeAddress, changeValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
txb.addOutputData(opReturnMemo);
|
if (opReturnMemo != null) txb.addOutputData(opReturnMemo);
|
||||||
|
|
||||||
for (var i = 0; i < inputs.length; i++) {
|
for (var i = 0; i < inputs.length; i++) {
|
||||||
final input = inputs[i];
|
final input = inputs[i];
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:bitbox/bitbox.dart' as bitbox;
|
import 'package:bitbox/bitbox.dart' as bitbox;
|
||||||
|
import 'package:bitbox/src/utils/opcodes.dart' as bitboxOPCodes;
|
||||||
import 'package:bitcoin_base/bitcoin_base.dart';
|
import 'package:bitcoin_base/bitcoin_base.dart';
|
||||||
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
import 'package:bitcoin_flutter/bitcoin_flutter.dart' as bitcoin;
|
||||||
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
import 'package:cw_bitcoin/bitcoin_address_record.dart';
|
||||||
|
@ -110,6 +111,8 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
|
||||||
|
|
||||||
var allInputsAmount = 0;
|
var allInputsAmount = 0;
|
||||||
|
|
||||||
|
final String? opReturnMemo = outputs.first.memo;
|
||||||
|
|
||||||
if (unspentCoins.isEmpty) await updateUnspent();
|
if (unspentCoins.isEmpty) await updateUnspent();
|
||||||
|
|
||||||
for (final utx in unspentCoins) {
|
for (final utx in unspentCoins) {
|
||||||
|
@ -252,6 +255,8 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
|
||||||
txb.addOutput(changeAddress, changeValue);
|
txb.addOutput(changeAddress, changeValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (opReturnMemo != null) txb.addOutput(createOpReturnScript(opReturnMemo), 0);
|
||||||
|
|
||||||
for (var i = 0; i < inputs.length; i++) {
|
for (var i = 0; i < inputs.length; i++) {
|
||||||
final input = inputs[i];
|
final input = inputs[i];
|
||||||
final keyPair = generateKeyPair(
|
final keyPair = generateKeyPair(
|
||||||
|
@ -260,7 +265,6 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
|
||||||
txb.sign(i, keyPair, input.value);
|
txb.sign(i, keyPair, input.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the transaction
|
|
||||||
final tx = txb.build();
|
final tx = txb.build();
|
||||||
|
|
||||||
return PendingBitcoinCashTransaction(tx, type,
|
return PendingBitcoinCashTransaction(tx, type,
|
||||||
|
@ -326,4 +330,11 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
|
||||||
final HD = index == null ? hd : hd.derive(index);
|
final HD = index == null ? hd : hd.derive(index);
|
||||||
return base64Encode(HD.signMessage(message));
|
return base64Encode(HD.signMessage(message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Uint8List createOpReturnScript(String data) {
|
||||||
|
List<int> script = [];
|
||||||
|
script.add(bitboxOPCodes.Opcodes.OP_RETURN);
|
||||||
|
script.addAll(utf8.encode(data));
|
||||||
|
return Uint8List.fromList(script);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,6 +81,7 @@ class AmountConverter {
|
||||||
return _moneroAmountToString(amount);
|
return _moneroAmountToString(amount);
|
||||||
case CryptoCurrency.btc:
|
case CryptoCurrency.btc:
|
||||||
case CryptoCurrency.bch:
|
case CryptoCurrency.bch:
|
||||||
|
case CryptoCurrency.ltc:
|
||||||
return _bitcoinAmountToString(amount);
|
return _bitcoinAmountToString(amount);
|
||||||
case CryptoCurrency.xhv:
|
case CryptoCurrency.xhv:
|
||||||
case CryptoCurrency.xag:
|
case CryptoCurrency.xag:
|
||||||
|
@ -102,27 +103,44 @@ class AmountConverter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int amountToSmallestUnit(
|
||||||
|
{required CryptoCurrency cryptoCurrency, required double amount}) {
|
||||||
|
switch (cryptoCurrency) {
|
||||||
|
case CryptoCurrency.btc:
|
||||||
|
return (amount * _bitcoinAmountDivider).toInt();
|
||||||
|
case CryptoCurrency.eth:
|
||||||
|
return (amount * _ethereumAmountDivider).toInt();
|
||||||
|
case CryptoCurrency.bch:
|
||||||
|
return (amount * _bitcoinCashAmountDivider).toInt();
|
||||||
|
case CryptoCurrency.ltc:
|
||||||
|
return (amount * _litecoinAmountDivider).toInt();
|
||||||
|
case CryptoCurrency.dash:
|
||||||
|
return (amount * _dashAmountDivider).toInt();
|
||||||
|
case CryptoCurrency.xmr:
|
||||||
|
return (amount * _moneroAmountDivider).toInt();
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static double cryptoAmountToDouble({required num amount, required num divider}) =>
|
static double cryptoAmountToDouble({required num amount, required num divider}) =>
|
||||||
amount / divider;
|
amount / divider;
|
||||||
|
|
||||||
static String _moneroAmountToString(int amount) => _moneroAmountFormat.format(
|
static String _moneroAmountToString(int amount) => _moneroAmountFormat
|
||||||
cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider));
|
.format(cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider));
|
||||||
|
|
||||||
static double _moneroAmountToDouble(int amount) =>
|
static double _moneroAmountToDouble(int amount) =>
|
||||||
cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider);
|
cryptoAmountToDouble(amount: amount, divider: _moneroAmountDivider);
|
||||||
|
|
||||||
static int _moneroParseAmount(String amount) =>
|
static int _moneroParseAmount(String amount) => _moneroAmountFormat.parse(amount).toInt();
|
||||||
_moneroAmountFormat.parse(amount).toInt();
|
|
||||||
|
|
||||||
static String _bitcoinAmountToString(int amount) =>
|
static String _bitcoinAmountToString(int amount) => _bitcoinAmountFormat
|
||||||
_bitcoinAmountFormat.format(
|
.format(cryptoAmountToDouble(amount: amount, divider: _bitcoinAmountDivider));
|
||||||
cryptoAmountToDouble(amount: amount, divider: _bitcoinAmountDivider));
|
|
||||||
|
|
||||||
static double _bitcoinAmountToDouble(int amount) =>
|
static double _bitcoinAmountToDouble(int amount) =>
|
||||||
cryptoAmountToDouble(amount: amount, divider: _bitcoinAmountDivider);
|
cryptoAmountToDouble(amount: amount, divider: _bitcoinAmountDivider);
|
||||||
|
|
||||||
static int _doubleToBitcoinAmount(double amount) =>
|
static int _doubleToBitcoinAmount(double amount) => (amount * _bitcoinAmountDivider).toInt();
|
||||||
(amount * _bitcoinAmountDivider).toInt();
|
|
||||||
|
|
||||||
static double _bitcoinCashAmountToDouble(int amount) =>
|
static double _bitcoinCashAmountToDouble(int amount) =>
|
||||||
cryptoAmountToDouble(amount: amount, divider: _bitcoinCashAmountDivider);
|
cryptoAmountToDouble(amount: amount, divider: _bitcoinCashAmountDivider);
|
||||||
|
|
|
@ -7,7 +7,8 @@ class OutputInfo {
|
||||||
this.formattedCryptoAmount,
|
this.formattedCryptoAmount,
|
||||||
this.fiatAmount,
|
this.fiatAmount,
|
||||||
this.note,
|
this.note,
|
||||||
this.extractedAddress,});
|
this.extractedAddress,
|
||||||
|
this.memo});
|
||||||
|
|
||||||
final String? fiatAmount;
|
final String? fiatAmount;
|
||||||
final String? cryptoAmount;
|
final String? cryptoAmount;
|
||||||
|
@ -17,4 +18,5 @@ class OutputInfo {
|
||||||
final bool sendAll;
|
final bool sendAll;
|
||||||
final bool isParsedAddress;
|
final bool isParsedAddress;
|
||||||
final int? formattedCryptoAmount;
|
final int? formattedCryptoAmount;
|
||||||
|
final String? memo;
|
||||||
}
|
}
|
|
@ -2,6 +2,7 @@ import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:cw_core/crypto_currency.dart';
|
import 'package:cw_core/crypto_currency.dart';
|
||||||
|
import 'package:hex/hex.dart' as hex;
|
||||||
import 'package:cw_ethereum/erc20_balance.dart';
|
import 'package:cw_ethereum/erc20_balance.dart';
|
||||||
import 'package:cw_core/erc20_token.dart';
|
import 'package:cw_core/erc20_token.dart';
|
||||||
import 'package:cw_ethereum/ethereum_transaction_model.dart';
|
import 'package:cw_ethereum/ethereum_transaction_model.dart';
|
||||||
|
@ -73,6 +74,7 @@ class EthereumClient {
|
||||||
required CryptoCurrency currency,
|
required CryptoCurrency currency,
|
||||||
required int exponent,
|
required int exponent,
|
||||||
String? contractAddress,
|
String? contractAddress,
|
||||||
|
String? data,
|
||||||
}) async {
|
}) async {
|
||||||
assert(currency == CryptoCurrency.eth ||
|
assert(currency == CryptoCurrency.eth ||
|
||||||
currency == CryptoCurrency.maticpoly ||
|
currency == CryptoCurrency.maticpoly ||
|
||||||
|
@ -88,6 +90,7 @@ class EthereumClient {
|
||||||
to: EthereumAddress.fromHex(toAddress),
|
to: EthereumAddress.fromHex(toAddress),
|
||||||
maxPriorityFeePerGas: EtherAmount.fromInt(EtherUnit.gwei, priority.tip),
|
maxPriorityFeePerGas: EtherAmount.fromInt(EtherUnit.gwei, priority.tip),
|
||||||
amount: _isEVMCompatibleChain ? EtherAmount.inWei(BigInt.parse(amount)) : EtherAmount.zero(),
|
amount: _isEVMCompatibleChain ? EtherAmount.inWei(BigInt.parse(amount)) : EtherAmount.zero(),
|
||||||
|
data: data != null ? hexToBytes(data) : null,
|
||||||
);
|
);
|
||||||
|
|
||||||
final signedTransaction =
|
final signedTransaction =
|
||||||
|
@ -123,6 +126,13 @@ class EthereumClient {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Uint8List hexToBytes(String hexString) {
|
||||||
|
if (hexString.startsWith('0x')) {
|
||||||
|
hexString = hexString.substring(2);
|
||||||
|
}
|
||||||
|
return Uint8List.fromList(hex.HEX.decode(hexString));
|
||||||
|
}
|
||||||
|
|
||||||
int get chainId => 1;
|
int get chainId => 1;
|
||||||
|
|
||||||
Transaction createTransaction({
|
Transaction createTransaction({
|
||||||
|
@ -130,12 +140,14 @@ class EthereumClient {
|
||||||
required EthereumAddress to,
|
required EthereumAddress to,
|
||||||
required EtherAmount amount,
|
required EtherAmount amount,
|
||||||
EtherAmount? maxPriorityFeePerGas,
|
EtherAmount? maxPriorityFeePerGas,
|
||||||
|
Uint8List? data,
|
||||||
}) {
|
}) {
|
||||||
return Transaction(
|
return Transaction(
|
||||||
from: from,
|
from: from,
|
||||||
to: to,
|
to: to,
|
||||||
maxPriorityFeePerGas: maxPriorityFeePerGas,
|
maxPriorityFeePerGas: maxPriorityFeePerGas,
|
||||||
value: amount,
|
value: amount,
|
||||||
|
data: data,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -197,6 +197,8 @@ abstract class EthereumWalletBase
|
||||||
final outputs = _credentials.outputs;
|
final outputs = _credentials.outputs;
|
||||||
final hasMultiDestination = outputs.length > 1;
|
final hasMultiDestination = outputs.length > 1;
|
||||||
|
|
||||||
|
final String? opReturnMemo = outputs.first.memo;
|
||||||
|
|
||||||
final CryptoCurrency transactionCurrency =
|
final CryptoCurrency transactionCurrency =
|
||||||
balance.keys.firstWhere((element) => element.title == _credentials.currency.title);
|
balance.keys.firstWhere((element) => element.title == _credentials.currency.title);
|
||||||
|
|
||||||
|
@ -240,6 +242,11 @@ abstract class EthereumWalletBase
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String? hexOpReturnMemo;
|
||||||
|
if (opReturnMemo != null) {
|
||||||
|
hexOpReturnMemo = '0x' + opReturnMemo.codeUnits.map((char) => char.toRadixString(16).padLeft(2, '0')).join();
|
||||||
|
}
|
||||||
|
|
||||||
final pendingEthereumTransaction = await _client.signTransaction(
|
final pendingEthereumTransaction = await _client.signTransaction(
|
||||||
privateKey: _ethPrivateKey,
|
privateKey: _ethPrivateKey,
|
||||||
toAddress: _credentials.outputs.first.isParsedAddress
|
toAddress: _credentials.outputs.first.isParsedAddress
|
||||||
|
@ -252,6 +259,7 @@ abstract class EthereumWalletBase
|
||||||
exponent: exponent,
|
exponent: exponent,
|
||||||
contractAddress:
|
contractAddress:
|
||||||
transactionCurrency is Erc20Token ? transactionCurrency.contractAddress : null,
|
transactionCurrency is Erc20Token ? transactionCurrency.contractAddress : null,
|
||||||
|
data: hexOpReturnMemo,
|
||||||
);
|
);
|
||||||
|
|
||||||
return pendingEthereumTransaction;
|
return pendingEthereumTransaction;
|
||||||
|
|
|
@ -13,6 +13,8 @@ class PolygonClient extends EthereumClient {
|
||||||
required EthereumAddress to,
|
required EthereumAddress to,
|
||||||
required EtherAmount amount,
|
required EtherAmount amount,
|
||||||
EtherAmount? maxPriorityFeePerGas,
|
EtherAmount? maxPriorityFeePerGas,
|
||||||
|
Uint8List? data,
|
||||||
|
|
||||||
}) {
|
}) {
|
||||||
return Transaction(
|
return Transaction(
|
||||||
from: from,
|
from: from,
|
||||||
|
|
|
@ -79,7 +79,8 @@ class CWBitcoin extends Bitcoin {
|
||||||
sendAll: out.sendAll,
|
sendAll: out.sendAll,
|
||||||
extractedAddress: out.extractedAddress,
|
extractedAddress: out.extractedAddress,
|
||||||
isParsedAddress: out.isParsedAddress,
|
isParsedAddress: out.isParsedAddress,
|
||||||
formattedCryptoAmount: out.formattedCryptoAmount))
|
formattedCryptoAmount: out.formattedCryptoAmount,
|
||||||
|
memo: out.memo))
|
||||||
.toList(),
|
.toList(),
|
||||||
priority: priority as BitcoinTransactionPriority,
|
priority: priority as BitcoinTransactionPriority,
|
||||||
feeRate: feeRate);
|
feeRate: feeRate);
|
||||||
|
|
|
@ -76,7 +76,8 @@ class CWEthereum extends Ethereum {
|
||||||
sendAll: out.sendAll,
|
sendAll: out.sendAll,
|
||||||
extractedAddress: out.extractedAddress,
|
extractedAddress: out.extractedAddress,
|
||||||
isParsedAddress: out.isParsedAddress,
|
isParsedAddress: out.isParsedAddress,
|
||||||
formattedCryptoAmount: out.formattedCryptoAmount))
|
formattedCryptoAmount: out.formattedCryptoAmount,
|
||||||
|
memo: out.memo))
|
||||||
.toList(),
|
.toList(),
|
||||||
priority: priority as EthereumTransactionPriority,
|
priority: priority as EthereumTransactionPriority,
|
||||||
currency: currency,
|
currency: currency,
|
||||||
|
|
|
@ -7,25 +7,28 @@ import 'package:cake_wallet/exchange/trade.dart';
|
||||||
import 'package:cake_wallet/exchange/trade_request.dart';
|
import 'package:cake_wallet/exchange/trade_request.dart';
|
||||||
import 'package:cake_wallet/exchange/trade_state.dart';
|
import 'package:cake_wallet/exchange/trade_state.dart';
|
||||||
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart';
|
import 'package:cake_wallet/exchange/utils/currency_pairs_utils.dart';
|
||||||
import 'package:cake_wallet/store/settings_store.dart';
|
import 'package:cw_core/amount_converter.dart';
|
||||||
import 'package:cw_core/crypto_currency.dart';
|
import 'package:cw_core/crypto_currency.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
class ThorChainExchangeProvider extends ExchangeProvider {
|
class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
ThorChainExchangeProvider({required SettingsStore settingsStore})
|
ThorChainExchangeProvider() : super(pairList: supportedPairs(_notSupported));
|
||||||
: _settingsStore = settingsStore,
|
|
||||||
super(pairList: supportedPairs(_notSupported));
|
|
||||||
|
|
||||||
static final List<CryptoCurrency> _notSupported = [
|
static final List<CryptoCurrency> _notSupported = [
|
||||||
...(CryptoCurrency.all
|
...(CryptoCurrency.all
|
||||||
.where((element) => ![CryptoCurrency.btc, CryptoCurrency.eth].contains(element))
|
.where((element) => ![
|
||||||
|
CryptoCurrency.btc,
|
||||||
|
CryptoCurrency.eth,
|
||||||
|
CryptoCurrency.ltc,
|
||||||
|
CryptoCurrency.bch
|
||||||
|
].contains(element))
|
||||||
.toList())
|
.toList())
|
||||||
];
|
];
|
||||||
|
|
||||||
static const _baseURL = 'https://thornode.ninerealms.com';
|
static const _baseURL = 'https://thornode.ninerealms.com';
|
||||||
static const _quotePath = '/thorchain/quote/swap';
|
static const _quotePath = '/thorchain/quote/swap';
|
||||||
|
static const _affiliateName = 'cakewallet';
|
||||||
final SettingsStore _settingsStore;
|
static const _affiliateBps = '0';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get title => 'ThorChain';
|
String get title => 'ThorChain';
|
||||||
|
@ -45,6 +48,21 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
@override
|
@override
|
||||||
Future<bool> checkIsAvailable() async => true;
|
Future<bool> checkIsAvailable() async => true;
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>> _getSwapQuote(Map<String, String> params) async {
|
||||||
|
final uri = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
|
||||||
|
final response = await http.get(uri);
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw Exception('Unexpected HTTP status: ${response.statusCode}');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.body.contains('error')) {
|
||||||
|
throw Exception('Unexpected response: ${response.body}');
|
||||||
|
}
|
||||||
|
|
||||||
|
return json.decode(response.body) as Map<String, dynamic>;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Limits> fetchLimits(
|
Future<Limits> fetchLimits(
|
||||||
{required CryptoCurrency from,
|
{required CryptoCurrency from,
|
||||||
|
@ -53,18 +71,14 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
final params = {
|
final params = {
|
||||||
'from_asset': _normalizeCurrency(from),
|
'from_asset': _normalizeCurrency(from),
|
||||||
'to_asset': _normalizeCurrency(to),
|
'to_asset': _normalizeCurrency(to),
|
||||||
'amount': '100000000',
|
'amount': AmountConverter.amountToSmallestUnit(cryptoCurrency: from, amount: 1).toString(),
|
||||||
|
'affiliate': _affiliateName,
|
||||||
|
'affiliate_bps': _affiliateBps,
|
||||||
};
|
};
|
||||||
|
|
||||||
final url = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
|
final responseJSON = await _getSwapQuote(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 minAmountIn = responseJSON['recommended_min_amount_in'] as String?;
|
||||||
final formattedMinAmountIn = minAmountIn != null ? double.parse(minAmountIn) / 100000000 : 0.0;
|
final formattedMinAmountIn = minAmountIn != null ? int.parse(minAmountIn) / 1e8 : 0.0;
|
||||||
|
|
||||||
return Limits(min: formattedMinAmountIn);
|
return Limits(min: formattedMinAmountIn);
|
||||||
}
|
}
|
||||||
|
@ -72,23 +86,23 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
@override
|
@override
|
||||||
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
Future<Trade> createTrade({required TradeRequest request, required bool isFixedRateMode}) async {
|
||||||
double amountBTC = double.parse(request.fromAmount);
|
double amountBTC = double.parse(request.fromAmount);
|
||||||
int amountSatoshi = (amountBTC * 100000000).toInt();
|
String formattedAmount = AmountConverter.amountToSmallestUnit(
|
||||||
String formattedAmount = amountSatoshi.toString();
|
cryptoCurrency: request.fromCurrency, amount: amountBTC)
|
||||||
|
.toString();
|
||||||
|
|
||||||
final params = {
|
final params = {
|
||||||
'from_asset': _normalizeCurrency(request.fromCurrency),
|
'from_asset': _normalizeCurrency(request.fromCurrency),
|
||||||
'to_asset': _normalizeCurrency(request.toCurrency),
|
'to_asset': _normalizeCurrency(request.toCurrency),
|
||||||
'amount': formattedAmount,
|
'amount': formattedAmount,
|
||||||
'destination': request.toAddress,
|
'destination': request.toAddress,
|
||||||
};
|
'affiliate': _affiliateName,
|
||||||
final url = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
|
'affiliate_bps': _affiliateBps};
|
||||||
final response = await http.get(url);
|
|
||||||
|
|
||||||
if (response.statusCode != 200)
|
final responseJSON = await _getSwapQuote(params);
|
||||||
throw Exception('Unexpected http status: ${response.statusCode}');
|
|
||||||
|
|
||||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
print('createTrade _ responseJSON________: $responseJSON');
|
||||||
final inputAddress = responseJSON['inbound_address'] as String?;
|
final inputAddress = responseJSON['inbound_address'] as String?;
|
||||||
|
final memo = responseJSON['memo'] as String?;
|
||||||
|
|
||||||
return Trade(
|
return Trade(
|
||||||
id: 'id',
|
id: 'id',
|
||||||
|
@ -101,7 +115,8 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
createdAt: DateTime.now(),
|
createdAt: DateTime.now(),
|
||||||
amount: request.fromAmount,
|
amount: request.fromAmount,
|
||||||
state: TradeState.created,
|
state: TradeState.created,
|
||||||
payoutAddress: request.toAddress);
|
payoutAddress: request.toAddress,
|
||||||
|
memo: memo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -117,23 +132,22 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
final params = {
|
final params = {
|
||||||
'from_asset': _normalizeCurrency(from),
|
'from_asset': _normalizeCurrency(from),
|
||||||
'to_asset': _normalizeCurrency(to),
|
'to_asset': _normalizeCurrency(to),
|
||||||
'amount': (amount * 100000000).toInt().toString(),
|
'amount':
|
||||||
|
AmountConverter.amountToSmallestUnit(cryptoCurrency: from, amount: amount).toString(),
|
||||||
};
|
};
|
||||||
|
|
||||||
final url = Uri.parse('$_baseURL$_quotePath${Uri(queryParameters: params)}');
|
final responseJSON = await _getSwapQuote(params);
|
||||||
final response = await http.get(url);
|
print(responseJSON);
|
||||||
|
|
||||||
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 expectedAmountOutString = responseJSON['expected_amount_out'] as String? ?? '0';
|
||||||
final expectedAmountOut = double.parse(expectedAmountOutString);
|
final expectedAmountOut = double.parse(expectedAmountOutString);
|
||||||
|
double formattedAmountOut = 0.0;
|
||||||
|
|
||||||
double formattedAmountOut = expectedAmountOut / 1e9;
|
if (to == CryptoCurrency.eth) {
|
||||||
|
formattedAmountOut = expectedAmountOut / 1e9;
|
||||||
|
} else {
|
||||||
|
formattedAmountOut = AmountConverter.amountIntToDouble(to, expectedAmountOut.toInt());
|
||||||
|
}
|
||||||
|
|
||||||
return formattedAmountOut;
|
return formattedAmountOut;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -153,6 +167,10 @@ class ThorChainExchangeProvider extends ExchangeProvider {
|
||||||
return 'BTC.BTC';
|
return 'BTC.BTC';
|
||||||
case CryptoCurrency.eth:
|
case CryptoCurrency.eth:
|
||||||
return 'ETH.ETH';
|
return 'ETH.ETH';
|
||||||
|
case CryptoCurrency.ltc:
|
||||||
|
return 'LTC.LTC';
|
||||||
|
case CryptoCurrency.bch:
|
||||||
|
return 'BCH.BCH';
|
||||||
default:
|
default:
|
||||||
return currency.title.toLowerCase();
|
return currency.title.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,8 @@ class Trade extends HiveObject {
|
||||||
this.password,
|
this.password,
|
||||||
this.providerId,
|
this.providerId,
|
||||||
this.providerName,
|
this.providerName,
|
||||||
this.fromWalletAddress
|
this.fromWalletAddress,
|
||||||
|
this.memo,
|
||||||
}) {
|
}) {
|
||||||
if (provider != null) providerRaw = provider.raw;
|
if (provider != null) providerRaw = provider.raw;
|
||||||
|
|
||||||
|
@ -105,6 +106,9 @@ class Trade extends HiveObject {
|
||||||
@HiveField(17)
|
@HiveField(17)
|
||||||
String? fromWalletAddress;
|
String? fromWalletAddress;
|
||||||
|
|
||||||
|
@HiveField(18)
|
||||||
|
String? memo;
|
||||||
|
|
||||||
static Trade fromMap(Map<String, Object?> map) {
|
static Trade fromMap(Map<String, Object?> map) {
|
||||||
return Trade(
|
return Trade(
|
||||||
id: map['id'] as String,
|
id: map['id'] as String,
|
||||||
|
@ -115,7 +119,8 @@ class Trade extends HiveObject {
|
||||||
map['date'] != null ? DateTime.fromMillisecondsSinceEpoch(map['date'] as int) : null,
|
map['date'] != null ? DateTime.fromMillisecondsSinceEpoch(map['date'] as int) : null,
|
||||||
amount: map['amount'] as String,
|
amount: map['amount'] as String,
|
||||||
walletId: map['wallet_id'] as String,
|
walletId: map['wallet_id'] as String,
|
||||||
fromWalletAddress: map['from_wallet_address'] as String?
|
fromWalletAddress: map['from_wallet_address'] as String?,
|
||||||
|
memo: map['memo'] as String?
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,7 +133,8 @@ class Trade extends HiveObject {
|
||||||
'date': createdAt != null ? createdAt!.millisecondsSinceEpoch : null,
|
'date': createdAt != null ? createdAt!.millisecondsSinceEpoch : null,
|
||||||
'amount': amount,
|
'amount': amount,
|
||||||
'wallet_id': walletId,
|
'wallet_id': walletId,
|
||||||
'from_wallet_address': fromWalletAddress
|
'from_wallet_address': fromWalletAddress,
|
||||||
|
'memo': memo
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ abstract class ExchangeTradeViewModelBase with Store {
|
||||||
_provider = ExolixExchangeProvider();
|
_provider = ExolixExchangeProvider();
|
||||||
break;
|
break;
|
||||||
case ExchangeProviderDescription.thorChain:
|
case ExchangeProviderDescription.thorChain:
|
||||||
_provider = ThorChainExchangeProvider(settingsStore: sendViewModel.balanceViewModel.settingsStore);
|
_provider = ThorChainExchangeProvider();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,6 +104,7 @@ abstract class ExchangeTradeViewModelBase with Store {
|
||||||
final output = sendViewModel.outputs.first;
|
final output = sendViewModel.outputs.first;
|
||||||
output.address = trade.inputAddress ?? '';
|
output.address = trade.inputAddress ?? '';
|
||||||
output.setCryptoAmount(trade.amount);
|
output.setCryptoAmount(trade.amount);
|
||||||
|
if (_provider is ThorChainExchangeProvider) output.memo = trade.memo;
|
||||||
sendViewModel.selectedCryptoCurrency = trade.from;
|
sendViewModel.selectedCryptoCurrency = trade.from;
|
||||||
await sendViewModel.createTransaction();
|
await sendViewModel.createTransaction();
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,7 +148,7 @@ abstract class ExchangeViewModelBase extends WalletChangeListenerViewModel with
|
||||||
SimpleSwapExchangeProvider(),
|
SimpleSwapExchangeProvider(),
|
||||||
TrocadorExchangeProvider(useTorOnly: _useTorOnly,
|
TrocadorExchangeProvider(useTorOnly: _useTorOnly,
|
||||||
providerStates: _settingsStore.trocadorProviderStates),
|
providerStates: _settingsStore.trocadorProviderStates),
|
||||||
ThorChainExchangeProvider(settingsStore: _settingsStore),
|
ThorChainExchangeProvider(),
|
||||||
if (FeatureFlag.isExolixEnabled) ExolixExchangeProvider(),
|
if (FeatureFlag.isExolixEnabled) ExolixExchangeProvider(),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,8 @@ abstract class OutputBase with Store {
|
||||||
@observable
|
@observable
|
||||||
String extractedAddress;
|
String extractedAddress;
|
||||||
|
|
||||||
|
String? memo;
|
||||||
|
|
||||||
@computed
|
@computed
|
||||||
bool get isParsedAddress =>
|
bool get isParsedAddress =>
|
||||||
parsedAddress.parseFrom != ParseFrom.notParsed && parsedAddress.name.isNotEmpty;
|
parsedAddress.parseFrom != ParseFrom.notParsed && parsedAddress.name.isNotEmpty;
|
||||||
|
@ -176,6 +178,7 @@ abstract class OutputBase with Store {
|
||||||
fiatAmount = '';
|
fiatAmount = '';
|
||||||
address = '';
|
address = '';
|
||||||
note = '';
|
note = '';
|
||||||
|
memo = null;
|
||||||
resetParsedAddress();
|
resetParsedAddress();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,7 @@ abstract class TradeDetailsViewModelBase with Store {
|
||||||
_provider = ExolixExchangeProvider();
|
_provider = ExolixExchangeProvider();
|
||||||
break;
|
break;
|
||||||
case ExchangeProviderDescription.thorChain:
|
case ExchangeProviderDescription.thorChain:
|
||||||
_provider = ThorChainExchangeProvider(settingsStore: settingsStore);
|
_provider = ThorChainExchangeProvider();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue