refactor of money utils with new money2 package
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/data/dto/money.dart';
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:pshared/utils/money.dart';
|
||||
|
||||
|
||||
extension MoneyMapper on Money {
|
||||
MoneyDTO toDTO() => MoneyDTO(
|
||||
amount: amount,
|
||||
currency: currency,
|
||||
);
|
||||
MoneyDTO toDTO() =>
|
||||
MoneyDTO(amount: toDecimal().toString(), currency: currency.isoCode);
|
||||
}
|
||||
|
||||
extension MoneyDTOMapper on MoneyDTO {
|
||||
Money toDomain() => Money(
|
||||
amount: amount,
|
||||
currency: currency,
|
||||
);
|
||||
Money toDomain() {
|
||||
final parsed = parseMoneyWithCurrencyCode(amount, currency);
|
||||
if (parsed == null) {
|
||||
throw FormatException('Invalid money dto: $currency $amount');
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import 'package:pshared/data/dto/wallet/chain_asset.dart';
|
||||
import 'package:pshared/data/mapper/payment/enums.dart';
|
||||
import 'package:pshared/models/wallet/chain_asset.dart';
|
||||
|
||||
|
||||
extension ChainAssetDTOMapper on ChainAssetDTO {
|
||||
ChainAsset toDomain() => ChainAsset(
|
||||
chain: chainNetworkFromValue(chain),
|
||||
tokenSymbol: tokenSymbol,
|
||||
);
|
||||
}
|
||||
|
||||
extension ChainAssetMapper on ChainAsset {
|
||||
ChainAssetDTO toDTO() => ChainAssetDTO(
|
||||
chain: chainNetworkToValue(chain),
|
||||
tokenSymbol: tokenSymbol,
|
||||
);
|
||||
}
|
||||
@@ -1,16 +1,13 @@
|
||||
import 'package:pshared/models/wallet/wallet.dart' as domain;
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/money.dart';
|
||||
|
||||
|
||||
extension WalletUiMapper on domain.WalletModel {
|
||||
Wallet toUi() => Wallet(
|
||||
id: walletRef,
|
||||
walletUserID: walletRef,
|
||||
balance: parseMoneyAmount(
|
||||
availableMoney?.amount ?? balance?.available?.amount,
|
||||
),
|
||||
balance: availableMoney?.toDouble() ?? balance?.available?.toDouble() ?? 0,
|
||||
currency: currencyStringToCode(asset.tokenSymbol),
|
||||
calculatedAt: balance?.calculatedAt ?? DateTime.now(),
|
||||
depositAddress: depositAddress,
|
||||
|
||||
@@ -16,15 +16,19 @@ extension WalletDTOMapper on WalletDTO {
|
||||
depositAddress: depositAddress,
|
||||
status: status,
|
||||
metadata: metadata,
|
||||
createdAt: (createdAt == null || createdAt!.isEmpty) ? null : DateTime.tryParse(createdAt!),
|
||||
updatedAt: (updatedAt == null || updatedAt!.isEmpty) ? null : DateTime.tryParse(updatedAt!),
|
||||
createdAt: (createdAt == null || createdAt!.isEmpty)
|
||||
? null
|
||||
: DateTime.tryParse(createdAt!),
|
||||
updatedAt: (updatedAt == null || updatedAt!.isEmpty)
|
||||
? null
|
||||
: DateTime.tryParse(updatedAt!),
|
||||
balance: balance?.toDomain(),
|
||||
availableMoney: balance?.available?.toDomain(),
|
||||
describable: newDescribable(
|
||||
name: name.isNotEmpty ? name : (metadata?['name']?.toString() ?? ''),
|
||||
description: (description != null && description!.isNotEmpty)
|
||||
? description
|
||||
: metadata?['description'],
|
||||
? description
|
||||
: metadata?['description'],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import 'package:pshared/models/currency.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
|
||||
|
||||
class Asset {
|
||||
final Currency currency;
|
||||
final double amount;
|
||||
|
||||
const Asset({
|
||||
required this.currency,
|
||||
required this.amount,
|
||||
});
|
||||
}
|
||||
|
||||
Asset createAsset(String currencyCode, String amount) => Asset(
|
||||
currency: currencyStringToCode(currencyCode),
|
||||
amount: double.parse(amount),
|
||||
);
|
||||
@@ -1 +1 @@
|
||||
enum Currency {usd, eur, rub, usdt, usdc}
|
||||
enum CurrencyCode {usd, eur, rub, usdt, usdc}
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
|
||||
class LedgerBalance {
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
class Money {
|
||||
final String amount;
|
||||
final String currency;
|
||||
|
||||
const Money({
|
||||
required this.amount,
|
||||
required this.currency,
|
||||
});
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
|
||||
class PaymentExecutionOperation {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
|
||||
class FeeLine {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
|
||||
class FxQuote {
|
||||
final String? quoteRef;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/models/payment/fees/treatment.dart';
|
||||
import 'package:pshared/models/payment/fx/intent.dart';
|
||||
import 'package:pshared/models/payment/kind.dart';
|
||||
import 'package:pshared/models/payment/customer.dart';
|
||||
import 'package:pshared/models/payment/methods/data.dart';
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:pshared/models/payment/settlement_mode.dart';
|
||||
|
||||
|
||||
class PaymentIntent {
|
||||
final PaymentKind kind;
|
||||
final String? sourceRef;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
|
||||
class QuoteAmounts {
|
||||
final Money? sourcePrincipal;
|
||||
|
||||
@@ -7,7 +7,7 @@ class Wallet implements Describable {
|
||||
final String id;
|
||||
final String walletUserID; // ID or number that we show the user
|
||||
final double balance;
|
||||
final Currency currency;
|
||||
final CurrencyCode currency;
|
||||
final DateTime calculatedAt;
|
||||
final String? depositAddress;
|
||||
final ChainNetwork? network;
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
import 'package:pshared/models/wallet/chain_asset.dart';
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
|
||||
|
||||
class WalletAsset extends ChainAsset {
|
||||
class WalletAsset {
|
||||
final ChainNetwork chain;
|
||||
final String tokenSymbol;
|
||||
final String contractAddress;
|
||||
|
||||
const WalletAsset({
|
||||
required super.chain,
|
||||
required super.tokenSymbol,
|
||||
required this.chain,
|
||||
required this.tokenSymbol,
|
||||
required this.contractAddress,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
|
||||
class WalletBalance {
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
|
||||
|
||||
class ChainAsset {
|
||||
final ChainNetwork chain;
|
||||
final String tokenSymbol;
|
||||
|
||||
const ChainAsset({
|
||||
required this.chain,
|
||||
required this.tokenSymbol,
|
||||
});
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:pshared/models/describable.dart';
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
import 'package:pshared/models/wallet/asset.dart';
|
||||
import 'package:pshared/models/wallet/balance.dart';
|
||||
|
||||
@@ -39,9 +39,7 @@ class WalletModel implements Describable {
|
||||
required this.describable,
|
||||
});
|
||||
|
||||
WalletModel copyWith({
|
||||
Describable? describable,
|
||||
}) => WalletModel(
|
||||
WalletModel copyWith({Describable? describable}) => WalletModel(
|
||||
walletRef: walletRef,
|
||||
organizationRef: organizationRef,
|
||||
ownerRef: ownerRef,
|
||||
|
||||
@@ -15,6 +15,7 @@ import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/ledger.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class LedgerAccountsProvider with ChangeNotifier {
|
||||
final LedgerService _service;
|
||||
OrganizationsProvider? _organizations;
|
||||
@@ -179,7 +180,7 @@ class LedgerAccountsProvider with ChangeNotifier {
|
||||
|
||||
Future<void> create({
|
||||
required Describable describable,
|
||||
required Currency currency,
|
||||
required CurrencyCode currency,
|
||||
String? ownerRef,
|
||||
}) async {
|
||||
final org = _organizations;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/controllers/payment/source.dart';
|
||||
import 'package:pshared/models/payment/asset.dart';
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
@@ -13,19 +15,18 @@ import 'package:pshared/models/payment/methods/data.dart';
|
||||
import 'package:pshared/models/payment/methods/ledger.dart';
|
||||
import 'package:pshared/models/payment/methods/managed_wallet.dart';
|
||||
import 'package:pshared/models/payment/methods/type.dart';
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:pshared/models/payment/settlement_mode.dart';
|
||||
import 'package:pshared/models/payment/intent.dart';
|
||||
import 'package:pshared/models/recipient/recipient.dart';
|
||||
import 'package:pshared/provider/payment/amount.dart';
|
||||
import 'package:pshared/provider/payment/flow.dart';
|
||||
import 'package:pshared/provider/recipient/provider.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/payment/fx_helpers.dart';
|
||||
|
||||
|
||||
class QuotationIntentBuilder {
|
||||
static const String _settlementCurrency = 'RUB';
|
||||
static const String _addressBookCustomerFallbackId = 'address_book_customer';
|
||||
|
||||
PaymentIntent? build({
|
||||
required PaymentAmountProvider payment,
|
||||
@@ -38,10 +39,9 @@ class QuotationIntentBuilder {
|
||||
final paymentData = flow.selectedPaymentData;
|
||||
final selectedMethod = flow.selectedMethod;
|
||||
final amountValue = payment.amount;
|
||||
if (sourceMethod == null || sourceCurrency == null || paymentData == null) {
|
||||
if (sourceCurrency == null) {
|
||||
return null;
|
||||
}
|
||||
if (amountValue == null) return null;
|
||||
|
||||
final customer = _buildCustomer(
|
||||
recipient: recipients.currentObject,
|
||||
@@ -51,22 +51,22 @@ class QuotationIntentBuilder {
|
||||
final amountCurrency = payment.settlementMode == SettlementMode.fixReceived
|
||||
? _settlementCurrency
|
||||
: sourceCurrency;
|
||||
final amount = Money(
|
||||
amount: amountValue.toString(),
|
||||
currency: amountCurrency,
|
||||
);
|
||||
final currency = money2CurrencyFromCode(amountCurrency);
|
||||
if (currency == null) return null;
|
||||
final amount = amountValue == null
|
||||
? null
|
||||
: Money.fromNumWithCurrency(amountValue, currency);
|
||||
final isLedgerSource = source.selectedLedgerAccount != null;
|
||||
final isCryptoToCrypto =
|
||||
paymentData is CryptoAddressPaymentMethod &&
|
||||
(paymentData.asset?.tokenSymbol ?? '').trim().toUpperCase() ==
|
||||
amount.currency;
|
||||
sourceCurrency.trim().toUpperCase();
|
||||
final fxIntent = _buildFxIntent(
|
||||
sourceCurrency: sourceCurrency,
|
||||
settlementMode: payment.settlementMode,
|
||||
isLedgerSource: isLedgerSource,
|
||||
enabled: !isCryptoToCrypto,
|
||||
);
|
||||
final comment = _resolveComment(payment.comment);
|
||||
return PaymentIntent(
|
||||
kind: PaymentKind.payout,
|
||||
amount: amount,
|
||||
@@ -77,7 +77,7 @@ class QuotationIntentBuilder {
|
||||
? FeeTreatment.addToSource
|
||||
: FeeTreatment.deductFromDestination,
|
||||
settlementMode: payment.settlementMode,
|
||||
comment: comment,
|
||||
comment: payment.comment,
|
||||
customer: customer,
|
||||
);
|
||||
}
|
||||
@@ -94,14 +94,9 @@ class QuotationIntentBuilder {
|
||||
// BFF maps only settlement currency + fx side, then quotation derives pair.
|
||||
// For ledger this preserves source debit in ledger currency (e.g. USDT).
|
||||
if (isLedgerSource && settlementMode == SettlementMode.fixReceived) {
|
||||
final base = sourceCurrency.trim();
|
||||
final quote = _settlementCurrency;
|
||||
if (base.isEmpty || base.toUpperCase() == quote.toUpperCase()) {
|
||||
return null;
|
||||
}
|
||||
return FxIntent(
|
||||
pair: CurrencyPair(base: base, quote: quote),
|
||||
side: FxSide.sellBaseBuyQuote,
|
||||
pair: CurrencyPair(base: sourceCurrency, quote: _settlementCurrency),
|
||||
side: FxSide.buyBaseSellQuote,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -137,39 +132,59 @@ class QuotationIntentBuilder {
|
||||
required PaymentMethod? method,
|
||||
required PaymentMethodData? data,
|
||||
}) {
|
||||
final name = recipient?.name.trim();
|
||||
if (name == null || name.isEmpty) return null;
|
||||
final id = recipient?.id.trim();
|
||||
final customerId = id == null || id.isEmpty
|
||||
? _addressBookCustomerFallbackId
|
||||
: id;
|
||||
final customerId = recipient?.id.trim() ?? '';
|
||||
final card = _resolveCard(method: method, data: data);
|
||||
final fromRecipient = _buildCustomerFromName(
|
||||
customerId: customerId,
|
||||
fullName: recipient?.name,
|
||||
country: card?.country,
|
||||
);
|
||||
if (fromRecipient != null) return fromRecipient;
|
||||
|
||||
final parts = name.split(RegExp(r'\s+'));
|
||||
if (card != null) {
|
||||
final firstName = _normalizedOrNull(card.firstName);
|
||||
final lastName = _normalizedOrNull(card.lastName);
|
||||
if (firstName == null && lastName == null) return null;
|
||||
return Customer(
|
||||
id: customerId,
|
||||
firstName: firstName,
|
||||
lastName: lastName,
|
||||
country: card.country,
|
||||
);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
CardPaymentMethod? _resolveCard({
|
||||
required PaymentMethod? method,
|
||||
required PaymentMethodData? data,
|
||||
}) => method?.cardData ?? (data is CardPaymentMethod ? data : null);
|
||||
|
||||
Customer? _buildCustomerFromName({
|
||||
required String customerId,
|
||||
required String? fullName,
|
||||
String? country,
|
||||
}) {
|
||||
final normalizedName = _normalizedOrNull(fullName);
|
||||
if (normalizedName == null) return null;
|
||||
final parts = normalizedName.split(RegExp(r'\s+'));
|
||||
final firstName = parts.isNotEmpty ? parts.first : null;
|
||||
final lastName = parts.length >= 2 ? parts.last : null;
|
||||
final middleName = parts.length > 2
|
||||
? parts.sublist(1, parts.length - 1).join(' ')
|
||||
: null;
|
||||
|
||||
return Customer(
|
||||
id: customerId,
|
||||
firstName: firstName,
|
||||
middleName: middleName,
|
||||
lastName: lastName,
|
||||
country: _resolveCustomerCountry(method: method, data: data),
|
||||
country: country,
|
||||
);
|
||||
}
|
||||
|
||||
String? _resolveCustomerCountry({
|
||||
required PaymentMethod? method,
|
||||
required PaymentMethodData? data,
|
||||
}) {
|
||||
final card = method?.cardData ?? (data is CardPaymentMethod ? data : null);
|
||||
return card?.country;
|
||||
}
|
||||
|
||||
String? _resolveComment(String comment) {
|
||||
final normalized = comment.trim();
|
||||
String? _normalizedOrNull(String? value) {
|
||||
if (value == null) return null;
|
||||
final normalized = value.trim();
|
||||
return normalized.isEmpty ? null : normalized;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,13 +6,13 @@ import 'package:logging/logging.dart';
|
||||
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/api/requests/payment/quote.dart';
|
||||
import 'package:pshared/controllers/payment/source.dart';
|
||||
import 'package:pshared/data/mapper/payment/intent/payment.dart';
|
||||
import 'package:pshared/models/asset.dart';
|
||||
import 'package:pshared/models/payment/intent.dart';
|
||||
import 'package:pshared/models/payment/quote/quote.dart';
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:pshared/models/auto_refresh_mode.dart';
|
||||
import 'package:pshared/provider/organizations.dart';
|
||||
import 'package:pshared/provider/payment/amount.dart';
|
||||
@@ -79,20 +79,12 @@ class QuotationProvider extends ChangeNotifier {
|
||||
return DateTime.fromMillisecondsSinceEpoch(expiresAtUnixMs, isUtc: true);
|
||||
}
|
||||
|
||||
Asset? get fee => _assetFromMoney(quoteFeeTotal(quotation));
|
||||
Asset? get total => _assetFromMoney(
|
||||
quoteSourceDebitTotal(
|
||||
quotation,
|
||||
preferredSourceCurrency: _sourceCurrencyCode,
|
||||
),
|
||||
Money? get fee => quoteFeeTotal(quotation);
|
||||
Money? get total => quoteSourceDebitTotal(
|
||||
quotation,
|
||||
preferredSourceCurrency: _sourceCurrencyCode,
|
||||
);
|
||||
Asset? get recipientGets =>
|
||||
_assetFromMoney(quotation?.amounts?.destinationSettlement);
|
||||
|
||||
Asset? _assetFromMoney(Money? money) {
|
||||
if (money == null) return null;
|
||||
return createAsset(money.currency, money.amount);
|
||||
}
|
||||
Money? get recipientGets => quotation?.amounts?.destinationSettlement;
|
||||
|
||||
void _setResource(Resource<PaymentQuote> quotation) {
|
||||
_quotation = quotation;
|
||||
|
||||
@@ -5,14 +5,16 @@ import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'package:pshared/models/currency.dart';
|
||||
import 'package:pshared/models/describable.dart';
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
import 'package:pshared/models/wallet/chain_asset.dart';
|
||||
import 'package:pshared/provider/organizations.dart';
|
||||
import 'package:pshared/provider/resource.dart';
|
||||
import 'package:pshared/service/payment/wallets.dart';
|
||||
import 'package:pshared/utils/exception.dart';
|
||||
|
||||
|
||||
class WalletsProvider with ChangeNotifier {
|
||||
final WalletsService _service;
|
||||
OrganizationsProvider? _organizations;
|
||||
@@ -180,7 +182,8 @@ class WalletsProvider with ChangeNotifier {
|
||||
|
||||
Future<void> create({
|
||||
required Describable describable,
|
||||
required ChainAsset asset,
|
||||
required ChainNetwork chain,
|
||||
required CurrencyCode currency,
|
||||
required String? ownerRef,
|
||||
}) async {
|
||||
final org = _organizations;
|
||||
@@ -195,7 +198,8 @@ class WalletsProvider with ChangeNotifier {
|
||||
await _service.create(
|
||||
organizationRef: org.current.id,
|
||||
describable: describable,
|
||||
asset: asset,
|
||||
chain: chain,
|
||||
currency: currency,
|
||||
ownerRef: ownerRef,
|
||||
);
|
||||
await loadWalletsWithBalances();
|
||||
|
||||
@@ -43,7 +43,7 @@ class LedgerService {
|
||||
required String organizationRef,
|
||||
required Describable describable,
|
||||
required String? ownerRef,
|
||||
required Currency currency,
|
||||
required CurrencyCode currency,
|
||||
}) async => AuthorizationService.getPOSTResponse(
|
||||
_objectType,
|
||||
'/$organizationRef',
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import 'package:pshared/data/mapper/wallet/ui.dart';
|
||||
import 'package:pshared/models/currency.dart';
|
||||
import 'package:pshared/models/describable.dart';
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
import 'package:pshared/models/payment/wallet.dart';
|
||||
import 'package:pshared/models/wallet/chain_asset.dart';
|
||||
import 'package:pshared/service/wallet.dart' as shared_wallet_service;
|
||||
import 'package:pshared/utils/money.dart';
|
||||
|
||||
|
||||
abstract class WalletsService {
|
||||
@@ -12,7 +12,8 @@ abstract class WalletsService {
|
||||
Future<void> create({
|
||||
required String organizationRef,
|
||||
required Describable describable,
|
||||
required ChainAsset asset,
|
||||
required ChainNetwork chain,
|
||||
required CurrencyCode currency,
|
||||
required String? ownerRef,
|
||||
});
|
||||
}
|
||||
@@ -30,19 +31,21 @@ class ApiWalletsService implements WalletsService {
|
||||
organizationRef: organizationRef,
|
||||
walletRef: walletRef,
|
||||
);
|
||||
return parseMoneyAmount(balance.available?.amount);
|
||||
return balance.available?.toDouble() ?? 0;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> create({
|
||||
required String organizationRef,
|
||||
required Describable describable,
|
||||
required ChainAsset asset,
|
||||
required ChainNetwork chain,
|
||||
required CurrencyCode currency,
|
||||
required String? ownerRef,
|
||||
}) => shared_wallet_service.WalletService.create(
|
||||
organizationRef: organizationRef,
|
||||
describable: describable,
|
||||
asset: asset,
|
||||
chain: chain,
|
||||
currency: currency,
|
||||
ownerRef: ownerRef,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
import 'package:pshared/api/requests/wallet/create.dart';
|
||||
import 'package:pshared/api/responses/wallet_balance.dart';
|
||||
import 'package:pshared/api/responses/wallets.dart';
|
||||
import 'package:pshared/data/dto/wallet/chain_asset.dart';
|
||||
import 'package:pshared/data/mapper/describable.dart';
|
||||
import 'package:pshared/data/mapper/wallet/chain_asset.dart';
|
||||
import 'package:pshared/data/mapper/payment/enums.dart';
|
||||
import 'package:pshared/data/mapper/wallet/response.dart';
|
||||
import 'package:pshared/models/currency.dart';
|
||||
import 'package:pshared/models/describable.dart';
|
||||
import 'package:pshared/models/payment/chain_network.dart';
|
||||
import 'package:pshared/models/wallet/balance.dart';
|
||||
import 'package:pshared/models/wallet/chain_asset.dart';
|
||||
import 'package:pshared/models/wallet/wallet.dart';
|
||||
import 'package:pshared/service/authorization/service.dart';
|
||||
import 'package:pshared/service/services.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
|
||||
|
||||
class WalletService {
|
||||
@@ -37,13 +40,19 @@ class WalletService {
|
||||
static Future<void> create({
|
||||
required String organizationRef,
|
||||
required Describable describable,
|
||||
required ChainAsset asset,
|
||||
required ChainNetwork chain,
|
||||
required CurrencyCode currency,
|
||||
required String? ownerRef,
|
||||
}) async => AuthorizationService.getPOSTResponse(
|
||||
_objectType,
|
||||
'/$organizationRef',
|
||||
CreateWalletRequest(
|
||||
asset: asset.toDTO(),
|
||||
asset: ChainAssetDTO(
|
||||
chain: chainNetworkToValue(chain),
|
||||
tokenSymbol:
|
||||
money2CurrencyFromCode(currencyCodeToString(currency))?.isoCode ??
|
||||
currencyCodeToString(currency),
|
||||
),
|
||||
describable: describable.toDTO(),
|
||||
ownerRef: ownerRef,
|
||||
).toJson(),
|
||||
|
||||
@@ -1,102 +1,78 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/models/asset.dart';
|
||||
import 'package:pshared/models/currency.dart';
|
||||
|
||||
|
||||
const nonBreakingSpace = '\u00A0';
|
||||
final Currency _usdtCurrency = Currency.create(
|
||||
'USDT',
|
||||
6,
|
||||
symbol: '₮',
|
||||
isIso: false,
|
||||
country: 'Digital',
|
||||
unit: 'Tether',
|
||||
name: 'Tether',
|
||||
);
|
||||
|
||||
String withTrailingNonBreakingSpace(String value) {
|
||||
return '$value$nonBreakingSpace';
|
||||
}
|
||||
final Currency _usdcCurrency = Currency.create(
|
||||
'USDC',
|
||||
6,
|
||||
symbol: r'($)',
|
||||
isIso: false,
|
||||
country: 'Digital',
|
||||
unit: 'USD Coin',
|
||||
name: 'USD Coin',
|
||||
);
|
||||
|
||||
String joinWithNonBreakingSpace(String left, String right) {
|
||||
return '$left$nonBreakingSpace$right';
|
||||
}
|
||||
final Map<String, Currency> _commonCurrenciesByCode =
|
||||
<String, Currency>{
|
||||
for (final currency in CommonCurrencies().asList())
|
||||
currency.isoCode: currency,
|
||||
_usdtCurrency.isoCode: _usdtCurrency,
|
||||
_usdcCurrency.isoCode: _usdcCurrency,
|
||||
};
|
||||
|
||||
String currencyCodeToSymbol(Currency currencyCode) {
|
||||
switch (currencyCode) {
|
||||
case Currency.usd:
|
||||
return '\$';
|
||||
case Currency.usdt:
|
||||
return '₮';
|
||||
case Currency.usdc:
|
||||
return '\$';
|
||||
case Currency.rub:
|
||||
return '₽';
|
||||
case Currency.eur:
|
||||
return '€';
|
||||
String currencyCodeToSymbol(CurrencyCode currencyCode) {
|
||||
final symbol = currencySymbolFromCode(currencyCodeToString(currencyCode));
|
||||
if (symbol == null || symbol.trim().isEmpty) {
|
||||
return currencyCodeToString(currencyCode);
|
||||
}
|
||||
return symbol;
|
||||
}
|
||||
|
||||
String amountToString(double amount) {
|
||||
return amount.toStringAsFixed(2);
|
||||
}
|
||||
|
||||
String currencyToString(Currency currencyCode, double amount) {
|
||||
return joinWithNonBreakingSpace(
|
||||
currencyCodeToSymbol(currencyCode),
|
||||
amountToString(amount),
|
||||
);
|
||||
}
|
||||
|
||||
String assetToString(Asset asset) {
|
||||
return currencyToString(asset.currency, asset.amount);
|
||||
}
|
||||
|
||||
Currency currencyStringToCode(String currencyCode) {
|
||||
switch (currencyCode) {
|
||||
case 'USD':
|
||||
return Currency.usd;
|
||||
case 'USDT':
|
||||
return Currency.usdt;
|
||||
case 'USDC':
|
||||
return Currency.usdc;
|
||||
case 'RUB':
|
||||
return Currency.rub;
|
||||
case 'EUR':
|
||||
return Currency.eur;
|
||||
default:
|
||||
throw ArgumentError('Unknown currency code: $currencyCode');
|
||||
String currencyToString(CurrencyCode currencyCode, double amount) {
|
||||
final code = currencyCodeToString(currencyCode);
|
||||
final currency = money2CurrencyFromCode(code);
|
||||
if (currency == null) {
|
||||
return '$amount $code';
|
||||
}
|
||||
final money = Money.fromNumWithCurrency(amount, currency);
|
||||
return money.toString();
|
||||
}
|
||||
|
||||
String currencyCodeToString(Currency currencyCode) {
|
||||
switch (currencyCode) {
|
||||
case Currency.usd:
|
||||
return 'USD';
|
||||
case Currency.usdt:
|
||||
return 'USDT';
|
||||
case Currency.usdc:
|
||||
return 'USDC';
|
||||
case Currency.rub:
|
||||
return 'RUB';
|
||||
case Currency.eur:
|
||||
return 'EUR';
|
||||
CurrencyCode currencyStringToCode(String currencyCode) {
|
||||
final normalized = currencyCode.trim().toUpperCase();
|
||||
for (final value in CurrencyCode.values) {
|
||||
if (currencyCodeToString(value) == normalized) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
throw ArgumentError('Unknown currency code: $currencyCode');
|
||||
}
|
||||
|
||||
IconData iconForCurrencyType(Currency currencyCode) {
|
||||
switch (currencyCode) {
|
||||
case Currency.usd:
|
||||
return Icons.currency_exchange;
|
||||
case Currency.eur:
|
||||
return Icons.currency_exchange;
|
||||
case Currency.rub:
|
||||
return Icons.currency_ruble;
|
||||
case Currency.usdt:
|
||||
return Icons.currency_exchange;
|
||||
case Currency.usdc:
|
||||
return Icons.money;
|
||||
}
|
||||
String currencyCodeToString(CurrencyCode currencyCode) {
|
||||
return currencyCode.name.toUpperCase();
|
||||
}
|
||||
|
||||
String? currencySymbolFromCode(String? code) {
|
||||
final normalized = code?.trim();
|
||||
if (normalized == null || normalized.isEmpty) return null;
|
||||
try {
|
||||
return currencyCodeToSymbol(currencyStringToCode(normalized.toUpperCase()));
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
final currency = money2CurrencyFromCode(code);
|
||||
if (currency == null) return null;
|
||||
final symbol = currency.symbol.trim();
|
||||
return symbol.isEmpty ? null : symbol;
|
||||
}
|
||||
|
||||
Currency? money2CurrencyFromCode(String? code) {
|
||||
final normalized = code?.trim().toUpperCase();
|
||||
if (normalized == null || normalized.isEmpty) return null;
|
||||
return _commonCurrenciesByCode[normalized] ?? Currencies().find(normalized);
|
||||
}
|
||||
|
||||
@@ -1,41 +1,35 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
|
||||
const String _decimalMoneyPattern = '0.##################';
|
||||
|
||||
double parseMoneyAmount(String? raw, {double fallback = 0}) {
|
||||
final trimmed = raw?.trim();
|
||||
if (trimmed == null || trimmed.isEmpty) return fallback;
|
||||
return double.tryParse(trimmed) ?? fallback;
|
||||
Money? parseMoneyWithCurrency(String? amount, Currency? currency) {
|
||||
if (currency == null) return null;
|
||||
final value = _normalizeMoneyAmount(amount);
|
||||
if (value == null || value.isEmpty) return null;
|
||||
|
||||
try {
|
||||
return Money.parseWithCurrency(
|
||||
value,
|
||||
currency,
|
||||
pattern: _decimalMoneyPattern,
|
||||
);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String formatMoneyDisplay(
|
||||
Money? money, {
|
||||
String fallback = '--',
|
||||
String separator = ' ',
|
||||
String invalidAmountFallback = '',
|
||||
}) {
|
||||
if (money == null) return fallback;
|
||||
|
||||
final rawAmount = money.amount.trim();
|
||||
final rawCurrency = money.currency.trim();
|
||||
final parsedAmount = parseMoneyAmount(rawAmount, fallback: double.nan);
|
||||
final amountToken = parsedAmount.isNaN
|
||||
? (rawAmount.isEmpty ? invalidAmountFallback : rawAmount)
|
||||
: amountToString(parsedAmount);
|
||||
|
||||
final symbol = currencySymbolFromCode(rawCurrency);
|
||||
final normalizedSymbol = symbol?.trim() ?? '';
|
||||
final hasSymbol = normalizedSymbol.isNotEmpty;
|
||||
final currencyToken = hasSymbol ? normalizedSymbol : rawCurrency;
|
||||
final first = amountToken;
|
||||
final second = currencyToken;
|
||||
|
||||
if (first.isEmpty && second.isEmpty) return fallback;
|
||||
if (first.isEmpty) return second;
|
||||
if (second.isEmpty) return first;
|
||||
return '$first$separator$second';
|
||||
Money? parseMoneyWithCurrencyCode(String? amount, String? currencyCode) {
|
||||
return parseMoneyWithCurrency(amount, money2CurrencyFromCode(currencyCode));
|
||||
}
|
||||
|
||||
extension MoneyAmountX on Money {
|
||||
double get amountValue => parseMoneyAmount(amount);
|
||||
String? _normalizeMoneyAmount(String? value) {
|
||||
final normalized = value?.trim();
|
||||
if (normalized == null || normalized.isEmpty) return null;
|
||||
|
||||
if (normalized.contains(',') && !normalized.contains('.')) {
|
||||
return normalized.replaceAll(',', '.');
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/models/payment/currency_pair.dart';
|
||||
import 'package:pshared/models/payment/fx/intent.dart';
|
||||
import 'package:pshared/models/payment/fx/side.dart';
|
||||
import 'package:pshared/models/money.dart';
|
||||
|
||||
|
||||
class FxIntentHelper {
|
||||
@@ -37,11 +38,15 @@ class FxIntentHelper {
|
||||
case FxSide.unspecified:
|
||||
break;
|
||||
}
|
||||
if (amount.currency == pair.base && pair.quote.isNotEmpty) return pair.quote;
|
||||
if (amount.currency == pair.quote && pair.base.isNotEmpty) return pair.base;
|
||||
if (amount.currency.isoCode == pair.base && pair.quote.isNotEmpty) {
|
||||
return pair.quote;
|
||||
}
|
||||
if (amount.currency.isoCode == pair.quote && pair.base.isNotEmpty) {
|
||||
return pair.base;
|
||||
}
|
||||
if (pair.quote.isNotEmpty) return pair.quote;
|
||||
if (pair.base.isNotEmpty) return pair.base;
|
||||
}
|
||||
return amount.currency;
|
||||
return amount.currency.isoCode;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ class PaymentQuotationCurrencyResolver {
|
||||
PaymentMethodData? paymentData,
|
||||
}) {
|
||||
final quoteCurrency = _normalizeCurrency(
|
||||
quote?.amounts?.destinationSettlement?.currency,
|
||||
quote?.amounts?.destinationSettlement?.currency.isoCode,
|
||||
);
|
||||
if (quoteCurrency != null) return quoteCurrency;
|
||||
|
||||
|
||||
@@ -1,17 +1,16 @@
|
||||
import 'package:pshared/models/money.dart';
|
||||
import 'package:money2/money2.dart';
|
||||
|
||||
import 'package:pshared/models/payment/fees/line.dart';
|
||||
import 'package:pshared/models/payment/quote/quote.dart';
|
||||
import 'package:pshared/utils/currency.dart';
|
||||
import 'package:pshared/utils/money.dart';
|
||||
|
||||
|
||||
Money? quoteFeeTotal(PaymentQuote? quote) {
|
||||
final preferredCurrency =
|
||||
quote?.amounts?.sourcePrincipal?.currency ??
|
||||
quote?.amounts?.sourceDebitTotal?.currency;
|
||||
return quoteFeeTotalFromLines(
|
||||
quote?.fees?.lines,
|
||||
preferredCurrency: preferredCurrency,
|
||||
preferredCurrency:
|
||||
quote?.amounts?.sourcePrincipal?.currency.isoCode ??
|
||||
quote?.amounts?.sourceDebitTotal?.currency.isoCode,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -21,7 +20,8 @@ Money? quoteSourceDebitTotal(
|
||||
}) {
|
||||
final sourceDebitTotal = quote?.amounts?.sourceDebitTotal;
|
||||
final preferredCurrency = _normalizeCurrency(
|
||||
preferredSourceCurrency ?? quote?.amounts?.sourcePrincipal?.currency,
|
||||
preferredSourceCurrency ??
|
||||
quote?.amounts?.sourcePrincipal?.currency.isoCode,
|
||||
);
|
||||
|
||||
if (sourceDebitTotal == null) {
|
||||
@@ -31,10 +31,9 @@ Money? quoteSourceDebitTotal(
|
||||
);
|
||||
}
|
||||
|
||||
final debitCurrency = _normalizeCurrency(sourceDebitTotal.currency);
|
||||
if (preferredCurrency == null ||
|
||||
debitCurrency == null ||
|
||||
debitCurrency == preferredCurrency) {
|
||||
_normalizeCurrency(sourceDebitTotal.currency.isoCode) ==
|
||||
preferredCurrency) {
|
||||
return sourceDebitTotal;
|
||||
}
|
||||
|
||||
@@ -52,22 +51,18 @@ Money? quoteFeeTotalFromLines(
|
||||
if (lines == null || lines.isEmpty) return null;
|
||||
|
||||
final normalizedPreferred = _normalizeCurrency(preferredCurrency);
|
||||
final totalsByCurrency = <String, double>{};
|
||||
final totalsByCurrency = <String, Money>{};
|
||||
|
||||
for (final line in lines) {
|
||||
final money = line.amount;
|
||||
if (money == null) continue;
|
||||
final parsedAmount = line.amount;
|
||||
if (parsedAmount == null) continue;
|
||||
|
||||
final currency = _normalizeCurrency(money.currency);
|
||||
if (currency == null) continue;
|
||||
|
||||
final amount = parseMoneyAmount(money.amount, fallback: double.nan);
|
||||
if (amount.isNaN) continue;
|
||||
|
||||
final sign = _lineSign(line.side);
|
||||
final signedAmount = sign * amount.abs();
|
||||
totalsByCurrency[currency] =
|
||||
(totalsByCurrency[currency] ?? 0) + signedAmount;
|
||||
final currencyCode = parsedAmount.currency.isoCode;
|
||||
final signedAmount = _isCreditLine(line.side) ? -parsedAmount : parsedAmount;
|
||||
final current = totalsByCurrency[currencyCode];
|
||||
totalsByCurrency[currencyCode] = current == null
|
||||
? signedAmount
|
||||
: current + signedAmount;
|
||||
}
|
||||
|
||||
if (totalsByCurrency.isEmpty) return null;
|
||||
@@ -77,85 +72,59 @@ Money? quoteFeeTotalFromLines(
|
||||
totalsByCurrency.containsKey(normalizedPreferred)
|
||||
? normalizedPreferred
|
||||
: totalsByCurrency.keys.first;
|
||||
final total = totalsByCurrency[selectedCurrency];
|
||||
if (total == null) return null;
|
||||
|
||||
return Money(amount: amountToString(total), currency: selectedCurrency);
|
||||
return totalsByCurrency[selectedCurrency];
|
||||
}
|
||||
|
||||
List<Money> aggregateMoneyByCurrency(Iterable<Money?> values) {
|
||||
final totals = <String, double>{};
|
||||
final totals = <String, Money>{};
|
||||
for (final value in values) {
|
||||
if (value == null) continue;
|
||||
|
||||
final currency = _normalizeCurrency(value.currency);
|
||||
if (currency == null) continue;
|
||||
|
||||
final amount = parseMoneyAmount(value.amount, fallback: double.nan);
|
||||
if (amount.isNaN) continue;
|
||||
|
||||
totals[currency] = (totals[currency] ?? 0) + amount;
|
||||
final currency = value.currency.isoCode;
|
||||
final current = totals[currency];
|
||||
totals[currency] = current == null ? value : current + value;
|
||||
}
|
||||
|
||||
return totals.entries
|
||||
.map(
|
||||
(entry) =>
|
||||
Money(amount: amountToString(entry.value), currency: entry.key),
|
||||
)
|
||||
.toList();
|
||||
return totals.values.toList();
|
||||
}
|
||||
|
||||
Money? _rebuildSourceDebitTotal(
|
||||
PaymentQuote? quote, {
|
||||
String? preferredSourceCurrency,
|
||||
}) {
|
||||
final sourcePrincipal = quote?.amounts?.sourcePrincipal;
|
||||
if (sourcePrincipal == null) return null;
|
||||
final principal = quote?.amounts?.sourcePrincipal;
|
||||
if (principal == null) return null;
|
||||
|
||||
final principalCurrency = _normalizeCurrency(sourcePrincipal.currency);
|
||||
if (principalCurrency == null) return null;
|
||||
final principalCurrency = principal.currency.isoCode;
|
||||
if (preferredSourceCurrency != null &&
|
||||
principalCurrency != preferredSourceCurrency) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final principalAmount = parseMoneyAmount(
|
||||
sourcePrincipal.amount,
|
||||
fallback: double.nan,
|
||||
);
|
||||
if (principalAmount.isNaN) return null;
|
||||
|
||||
double totalAmount = principalAmount;
|
||||
var totalAmount = principal;
|
||||
final fee = quoteFeeTotalFromLines(
|
||||
quote?.fees?.lines,
|
||||
preferredCurrency: principalCurrency,
|
||||
);
|
||||
if (fee != null && _normalizeCurrency(fee.currency) == principalCurrency) {
|
||||
final feeAmount = parseMoneyAmount(fee.amount, fallback: double.nan);
|
||||
if (!feeAmount.isNaN) {
|
||||
totalAmount += feeAmount;
|
||||
}
|
||||
if (fee != null && fee.currency.isoCode == principalCurrency) {
|
||||
totalAmount += fee;
|
||||
}
|
||||
|
||||
return Money(
|
||||
amount: amountToString(totalAmount),
|
||||
currency: principalCurrency,
|
||||
);
|
||||
return totalAmount;
|
||||
}
|
||||
|
||||
double _lineSign(String? side) {
|
||||
bool _isCreditLine(String? side) {
|
||||
final normalized = side?.trim().toLowerCase() ?? '';
|
||||
switch (normalized) {
|
||||
case 'entry_side_credit':
|
||||
case 'credit':
|
||||
return -1;
|
||||
return true;
|
||||
default:
|
||||
return 1;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
String? _normalizeCurrency(String? currency) {
|
||||
final normalized = currency?.trim().toUpperCase();
|
||||
final normalized = currency?.trim();
|
||||
if (normalized == null || normalized.isEmpty) return null;
|
||||
return normalized;
|
||||
return money2CurrencyFromCode(normalized)?.isoCode ?? normalized.toUpperCase();
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ dependencies:
|
||||
uuid: ^4.5.1
|
||||
image: ^4.5.4
|
||||
shared_preferences: ^2.5.3
|
||||
money2: ^6.3.0
|
||||
|
||||
dev_dependencies:
|
||||
flutter_lints: ^6.0.0
|
||||
|
||||
Reference in New Issue
Block a user