cake_wallet/lib/anypay/any_pay_payment.dart
2022-07-06 18:13:19 +01:00

64 lines
No EOL
1.9 KiB
Dart

import 'package:cake_wallet/anypay/any_pay_chain.dart';
import 'package:cw_bitcoin/bitcoin_amount_format.dart';
import 'package:cw_core/monero_amount_format.dart';
import 'package:flutter/foundation.dart';
import 'package:cake_wallet/anypay/any_pay_payment_instruction.dart';
class AnyPayPayment {
AnyPayPayment({
@required this.time,
@required this.expires,
@required this.memo,
@required this.paymentUrl,
@required this.paymentId,
@required this.chain,
@required this.network,
@required this.instructions});
factory AnyPayPayment.fromMap(Map<String, dynamic> obj) {
final instructions = (obj['instructions'] as List<dynamic>)
.map((dynamic instruction) => AnyPayPaymentInstruction.fromMap(instruction as Map<String, dynamic>))
.toList();
return AnyPayPayment(
time: DateTime.parse(obj['time'] as String),
expires: DateTime.parse(obj['expires'] as String),
memo: obj['memo'] as String,
paymentUrl: obj['paymentUrl'] as String,
paymentId: obj['paymentId'] as String,
chain: obj['chain'] as String,
network: obj['network'] as String,
instructions: instructions);
}
final DateTime time;
final DateTime expires;
final String memo;
final String paymentUrl;
final String paymentId;
final String chain;
final String network;
final List<AnyPayPaymentInstruction> instructions;
String get totalAmount {
final total = instructions
.fold<int>(0, (int acc, instruction) => acc + instruction.outputs
.fold<int>(0, (int outAcc, out) => outAcc + out.amount));
switch (chain) {
case AnyPayChain.xmr:
return moneroAmountToString(amount: total);
case AnyPayChain.btc:
return bitcoinAmountToString(amount: total);
case AnyPayChain.ltc:
return bitcoinAmountToString(amount: total);
default:
return null;
}
}
List<String> get outAddresses {
return instructions
.map((instuction) => instuction.outputs.map((out) => out.address))
.expand((e) => e)
.toList();
}
}