mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2024-12-22 19:49:22 +00:00
Cw 72 implement sideshift exchange (#332)
* add sideshift exchange provider * add secret key * Fix issues * Fix issues * refactor code * add permission checks to side shift * fix formatting issues
This commit is contained in:
parent
e593f731b2
commit
6378d052ac
10 changed files with 322 additions and 2 deletions
BIN
assets/images/sideshift.png
Normal file
BIN
assets/images/sideshift.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4 KiB |
|
@ -11,6 +11,9 @@ class ExchangeProviderDescription extends EnumerableItem<int>
|
|||
static const morphToken =
|
||||
ExchangeProviderDescription(title: 'MorphToken', raw: 2);
|
||||
|
||||
static const sideShift =
|
||||
ExchangeProviderDescription(title: 'SideShift', raw: 3);
|
||||
|
||||
static ExchangeProviderDescription deserialize({int raw}) {
|
||||
switch (raw) {
|
||||
case 0:
|
||||
|
@ -19,6 +22,8 @@ class ExchangeProviderDescription extends EnumerableItem<int>
|
|||
return changeNow;
|
||||
case 2:
|
||||
return morphToken;
|
||||
case 3:
|
||||
return sideShift;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
|
265
lib/exchange/sideshift/sideshift_exchange_provider.dart
Normal file
265
lib/exchange/sideshift/sideshift_exchange_provider.dart
Normal file
|
@ -0,0 +1,265 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:cake_wallet/exchange/exchange_pair.dart';
|
||||
import 'package:cake_wallet/exchange/exchange_provider.dart';
|
||||
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
||||
import 'package:cake_wallet/exchange/sideshift/sideshift_request.dart';
|
||||
import 'package:cake_wallet/exchange/trade_not_created_exeption.dart';
|
||||
import 'package:cake_wallet/exchange/trade_not_found_exeption.dart';
|
||||
import 'package:cake_wallet/exchange/trade_state.dart';
|
||||
import 'package:cake_wallet/.secrets.g.dart' as secrets;
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cake_wallet/exchange/trade_request.dart';
|
||||
import 'package:cake_wallet/exchange/trade.dart';
|
||||
import 'package:cake_wallet/exchange/limits.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart';
|
||||
|
||||
class SideShiftExchangeProvider extends ExchangeProvider {
|
||||
SideShiftExchangeProvider()
|
||||
: super(
|
||||
pairList: CryptoCurrency.all
|
||||
.map((i) => CryptoCurrency.all
|
||||
.map((k) => ExchangePair(from: i, to: k, reverse: true))
|
||||
.where((c) => c != null))
|
||||
.expand((i) => i)
|
||||
.toList());
|
||||
|
||||
static const apiKey = secrets.sideShiftApiKey;
|
||||
static const affiliateId = secrets.sideShiftAffiliateId;
|
||||
static const apiBaseUrl = 'https://sideshift.ai/api';
|
||||
static const rangePath = '/v1/pairs';
|
||||
static const orderPath = '/v1/orders';
|
||||
static const quotePath = '/v1/quotes';
|
||||
static const permissionPath = '/v1/permissions';
|
||||
static const apiHeaderKey = 'x-sideshift-secret';
|
||||
|
||||
@override
|
||||
ExchangeProviderDescription get description =>
|
||||
ExchangeProviderDescription.sideShift;
|
||||
|
||||
@override
|
||||
Future<double> calculateAmount(
|
||||
{CryptoCurrency from,
|
||||
CryptoCurrency to,
|
||||
double amount,
|
||||
bool isFixedRateMode,
|
||||
bool isReceiveAmount}) async {
|
||||
try {
|
||||
if (amount == 0) {
|
||||
return 0.0;
|
||||
}
|
||||
final fromCurrency = normalizeCryptoCurrency(from);
|
||||
final toCurrency = normalizeCryptoCurrency(to);
|
||||
final url =
|
||||
apiBaseUrl + rangePath + '/' + fromCurrency + '/' + toCurrency;
|
||||
final response = await get(url);
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final rate = double.parse(responseJSON['rate'] as String);
|
||||
final max = double.parse(responseJSON['max'] as String);
|
||||
|
||||
if (amount > max) return 0.00;
|
||||
|
||||
final estimatedAmount = rate * amount;
|
||||
|
||||
return estimatedAmount;
|
||||
} catch (_) {
|
||||
return 0.00;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> checkIsAvailable() async {
|
||||
const url = apiBaseUrl + permissionPath;
|
||||
final response = await get(url);
|
||||
|
||||
if (response.statusCode == 500) {
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final error = responseJSON['error']['message'] as String;
|
||||
|
||||
throw Exception('$error');
|
||||
}
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final canCreateOrder = responseJSON['createOrder'] as bool;
|
||||
final canCreateQuote = responseJSON['createQuote'] as bool;
|
||||
return canCreateOrder && canCreateQuote;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Trade> createTrade(
|
||||
{TradeRequest request, bool isFixedRateMode}) async {
|
||||
final _request = request as SideShiftRequest;
|
||||
final quoteId = await _createQuote(_request);
|
||||
final url = apiBaseUrl + orderPath;
|
||||
final headers = {apiHeaderKey: apiKey, 'Content-Type': 'application/json'};
|
||||
final body = {
|
||||
'type': 'fixed',
|
||||
'quoteId': quoteId,
|
||||
'affiliateId': affiliateId,
|
||||
'settleAddress': _request.settleAddress,
|
||||
'refundAddress': _request.refundAddress
|
||||
};
|
||||
final response = await post(url, headers: headers, body: json.encode(body));
|
||||
|
||||
if (response.statusCode != 201) {
|
||||
if (response.statusCode == 400) {
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final error = responseJSON['error']['message'] as String;
|
||||
|
||||
throw TradeNotCreatedException(description, description: error);
|
||||
}
|
||||
|
||||
throw TradeNotCreatedException(description);
|
||||
}
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final id = responseJSON['id'] as String;
|
||||
final inputAddress = responseJSON['depositAddress']['address'] as String;
|
||||
final settleAddress = responseJSON['settleAddress']['address'] as String;
|
||||
|
||||
return Trade(
|
||||
id: id,
|
||||
provider: description,
|
||||
from: _request.depositMethod,
|
||||
to: _request.settleMethod,
|
||||
inputAddress: inputAddress,
|
||||
refundAddress: settleAddress,
|
||||
state: TradeState.created,
|
||||
amount: _request.depositAmount,
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<String> _createQuote(SideShiftRequest request) async {
|
||||
final url = apiBaseUrl + quotePath;
|
||||
final headers = {apiHeaderKey: apiKey, 'Content-Type': 'application/json'};
|
||||
final depositMethod = normalizeCryptoCurrency(request.depositMethod);
|
||||
final settleMethod = normalizeCryptoCurrency(request.settleMethod);
|
||||
final body = {
|
||||
'depositMethod': depositMethod,
|
||||
'settleMethod': settleMethod,
|
||||
'affiliateId': affiliateId,
|
||||
'depositAmount': request.depositAmount,
|
||||
};
|
||||
final response = await post(url, headers: headers, body: json.encode(body));
|
||||
|
||||
if (response.statusCode != 201) {
|
||||
if (response.statusCode == 400) {
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final error = responseJSON['error']['message'] as String;
|
||||
|
||||
throw TradeNotCreatedException(description, description: error);
|
||||
}
|
||||
|
||||
throw TradeNotCreatedException(description);
|
||||
}
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final quoteId = responseJSON['id'] as String;
|
||||
|
||||
return quoteId;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Limits> fetchLimits(
|
||||
{CryptoCurrency from, CryptoCurrency to, bool isFixedRateMode}) async {
|
||||
final fromCurrency = normalizeCryptoCurrency(from);
|
||||
final toCurrency = normalizeCryptoCurrency(to);
|
||||
final url = apiBaseUrl + rangePath + '/' + fromCurrency + '/' + toCurrency;
|
||||
final response = await get(url);
|
||||
|
||||
if (response.statusCode == 500) {
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final error = responseJSON['error']['message'] as String;
|
||||
|
||||
throw Exception('$error');
|
||||
}
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final min = double.parse(responseJSON['min'] as String);
|
||||
final max = double.parse(responseJSON['max'] as String);
|
||||
|
||||
return Limits(min: min, max: max);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Trade> findTradeById({@required String id}) async {
|
||||
final url = apiBaseUrl + orderPath + '/' + id;
|
||||
final response = await get(url);
|
||||
|
||||
if (response.statusCode == 404) {
|
||||
throw TradeNotFoundException(id, provider: description);
|
||||
}
|
||||
|
||||
if (response.statusCode == 400) {
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final error = responseJSON['error']['message'] as String;
|
||||
|
||||
throw TradeNotFoundException(id,
|
||||
provider: description, description: error);
|
||||
}
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final responseJSON = json.decode(response.body) as Map<String, dynamic>;
|
||||
final fromCurrency = responseJSON['depositMethodId'] as String;
|
||||
final from = CryptoCurrency.fromString(fromCurrency);
|
||||
final toCurrency = responseJSON['settleMethodId'] as String;
|
||||
final to = CryptoCurrency.fromString(toCurrency);
|
||||
final inputAddress = responseJSON['depositAddress']['address'] as String;
|
||||
final expectedSendAmount = responseJSON['depositAmount'].toString();
|
||||
final deposits = responseJSON['deposits'] as List;
|
||||
TradeState state;
|
||||
|
||||
if (deposits != null && deposits.isNotEmpty) {
|
||||
final status = deposits[0]['status'] as String;
|
||||
state = TradeState.deserialize(raw: status);
|
||||
}
|
||||
|
||||
final expiredAtRaw = responseJSON['expiresAtISO'] as String;
|
||||
final expiredAt =
|
||||
expiredAtRaw != null ? DateTime.parse(expiredAtRaw).toLocal() : null;
|
||||
|
||||
return Trade(
|
||||
id: id,
|
||||
from: from,
|
||||
to: to,
|
||||
provider: description,
|
||||
inputAddress: inputAddress,
|
||||
amount: expectedSendAmount,
|
||||
state: state,
|
||||
expiredAt: expiredAt,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get isAvailable => true;
|
||||
|
||||
@override
|
||||
String get title => 'SideShift';
|
||||
|
||||
static String normalizeCryptoCurrency(CryptoCurrency currency) {
|
||||
const bnbTitle = 'bsc';
|
||||
const usdterc20 = 'usdtErc20';
|
||||
|
||||
switch (currency) {
|
||||
case CryptoCurrency.bnb:
|
||||
return bnbTitle;
|
||||
case CryptoCurrency.usdterc20:
|
||||
return usdterc20;
|
||||
default:
|
||||
return currency.title.toLowerCase();
|
||||
}
|
||||
}
|
||||
}
|
17
lib/exchange/sideshift/sideshift_request.dart
Normal file
17
lib/exchange/sideshift/sideshift_request.dart
Normal file
|
@ -0,0 +1,17 @@
|
|||
import 'package:cake_wallet/exchange/trade_request.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
|
||||
class SideShiftRequest extends TradeRequest {
|
||||
final CryptoCurrency depositMethod;
|
||||
final CryptoCurrency settleMethod;
|
||||
final String depositAmount;
|
||||
final String settleAddress;
|
||||
final String refundAddress;
|
||||
|
||||
SideShiftRequest(
|
||||
{this.depositMethod,
|
||||
this.settleMethod,
|
||||
this.depositAmount,
|
||||
this.settleAddress,
|
||||
this.refundAddress,});
|
||||
}
|
|
@ -33,7 +33,8 @@ class TradeState extends EnumerableItem<String> with Serializable<String> {
|
|||
TradeState(raw: 'waitingAuthorization', title: 'Waiting authorization');
|
||||
static const failed = TradeState(raw: 'failed', title: 'Failed');
|
||||
static const completed = TradeState(raw: 'completed', title: 'Completed');
|
||||
|
||||
static const settling = TradeState(raw: 'settling', title: 'Settlement in progress');
|
||||
static const settled = TradeState(raw: 'settled', title: 'Settlement completed');
|
||||
static TradeState deserialize({String raw}) {
|
||||
switch (raw) {
|
||||
case 'pending':
|
||||
|
|
|
@ -87,6 +87,9 @@ class TradeRow extends StatelessWidget {
|
|||
case ExchangeProviderDescription.morphToken:
|
||||
image = Image.asset('assets/images/morph.png', height: 36, width: 36);
|
||||
break;
|
||||
case ExchangeProviderDescription.sideShift:
|
||||
image = Image.asset('assets/images/sideshift.png', width: 36, height: 36);
|
||||
break;
|
||||
default:
|
||||
image = null;
|
||||
}
|
||||
|
|
|
@ -71,6 +71,9 @@ class PresentProviderPicker extends StatelessWidget {
|
|||
case ExchangeProviderDescription.morphToken:
|
||||
images.add(Image.asset('assets/images/morph_icon.png'));
|
||||
break;
|
||||
case ExchangeProviderDescription.sideShift:
|
||||
images.add(Image.asset('assets/images/sideshift.png', width: 20));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
import 'package:cake_wallet/exchange/sideshift/sideshift_exchange_provider.dart';
|
||||
import 'package:cake_wallet/exchange/sideshift/sideshift_request.dart';
|
||||
import 'package:cw_core/wallet_base.dart';
|
||||
import 'package:cw_core/crypto_currency.dart';
|
||||
import 'package:cw_core/sync_status.dart';
|
||||
|
@ -33,7 +35,7 @@ abstract class ExchangeViewModelBase with Store {
|
|||
this.tradesStore, this._settingsStore) {
|
||||
const excludeDepositCurrencies = [CryptoCurrency.xhv];
|
||||
const excludeReceiveCurrencies = [CryptoCurrency.xlm, CryptoCurrency.xrp, CryptoCurrency.bnb, CryptoCurrency.xhv];
|
||||
providerList = [ChangeNowExchangeProvider()];
|
||||
providerList = [ChangeNowExchangeProvider(), SideShiftExchangeProvider()];
|
||||
_initialPairBasedOnWallet();
|
||||
isDepositAddressEnabled = !(depositCurrency == wallet.currency);
|
||||
isReceiveAddressEnabled = !(receiveCurrency == wallet.currency);
|
||||
|
@ -253,6 +255,18 @@ abstract class ExchangeViewModelBase with Store {
|
|||
String amount;
|
||||
CryptoCurrency currency;
|
||||
|
||||
if (provider is SideShiftExchangeProvider) {
|
||||
request = SideShiftRequest(
|
||||
depositMethod: depositCurrency,
|
||||
settleMethod: receiveCurrency,
|
||||
depositAmount: depositAmount?.replaceAll(',', '.'),
|
||||
settleAddress: receiveAddress,
|
||||
refundAddress: depositAddress,
|
||||
);
|
||||
amount = depositAmount;
|
||||
currency = depositCurrency;
|
||||
}
|
||||
|
||||
if (provider is XMRTOExchangeProvider) {
|
||||
request = XMRTOTradeRequest(
|
||||
from: depositCurrency,
|
||||
|
|
|
@ -3,6 +3,7 @@ import 'package:cake_wallet/exchange/changenow/changenow_exchange_provider.dart'
|
|||
import 'package:cake_wallet/exchange/exchange_provider.dart';
|
||||
import 'package:cake_wallet/exchange/exchange_provider_description.dart';
|
||||
import 'package:cake_wallet/exchange/morphtoken/morphtoken_exchange_provider.dart';
|
||||
import 'package:cake_wallet/exchange/sideshift/sideshift_exchange_provider.dart';
|
||||
import 'package:cake_wallet/exchange/trade.dart';
|
||||
import 'package:cake_wallet/exchange/xmrto/xmrto_exchange_provider.dart';
|
||||
import 'package:cake_wallet/utils/date_formatter.dart';
|
||||
|
@ -31,6 +32,9 @@ abstract class TradeDetailsViewModelBase with Store {
|
|||
case ExchangeProviderDescription.morphToken:
|
||||
_provider = MorphTokenExchangeProvider(trades: trades);
|
||||
break;
|
||||
case ExchangeProviderDescription.sideShift:
|
||||
_provider = SideShiftExchangeProvider();
|
||||
break;
|
||||
}
|
||||
|
||||
items = ObservableList<StandartListItem>();
|
||||
|
@ -102,6 +106,12 @@ abstract class TradeDetailsViewModelBase with Store {
|
|||
}));
|
||||
}
|
||||
|
||||
if (trade.provider == ExchangeProviderDescription.sideShift) {
|
||||
final buildURL = 'https://sideshift.ai/orders/${trade.id.toString()}';
|
||||
items.add(TrackTradeListItem(
|
||||
title: 'Track', value: buildURL, onTap: () => launch(buildURL)));
|
||||
}
|
||||
|
||||
if (trade.createdAt != null) {
|
||||
items.add(StandartListItem(
|
||||
title: S.current.trade_details_created_at,
|
||||
|
|
|
@ -23,6 +23,8 @@ class SecretKey {
|
|||
SecretKey('wyreAccountId', () => ''),
|
||||
SecretKey('moonPayApiKey', () => ''),
|
||||
SecretKey('moonPaySecretKey', () => ''),
|
||||
SecretKey('sideShiftAffiliateId', () => ''),
|
||||
SecretKey('sideShiftApiKey', () => ''),
|
||||
];
|
||||
|
||||
final String name;
|
||||
|
|
Loading…
Reference in a new issue