| 1 | import 'package:cake_wallet/anypay/any_pay_chain.dart'; |
| 2 | import 'package:cake_wallet/anypay/any_pay_payment_instruction.dart'; |
| 3 | import 'package:cake_wallet/bitcoin/bitcoin.dart'; |
| 4 | import 'package:cake_wallet/monero/monero.dart'; |
| 5 | |
| 6 | class AnyPayPayment { |
| 7 | AnyPayPayment( |
| 8 | {required this.time, |
| 9 | required this.expires, |
| 10 | required this.memo, |
| 11 | required this.paymentUrl, |
| 12 | required this.paymentId, |
| 13 | required this.chain, |
| 14 | required this.network, |
| 15 | required this.instructions}); |
| 16 | |
| 17 | factory AnyPayPayment.fromMap(Map<String, dynamic> obj) { |
| 18 | final instructions = (obj['instructions'] as List<dynamic>) |
| 19 | .map((dynamic instruction) => |
| 20 | AnyPayPaymentInstruction.fromMap(instruction as Map<String, dynamic>)) |
| 21 | .toList(); |
| 22 | return AnyPayPayment( |
| 23 | time: DateTime.parse(obj['time'] as String), |
| 24 | expires: DateTime.parse(obj['expires'] as String), |
| 25 | memo: obj['memo'] as String, |
| 26 | paymentUrl: obj['paymentUrl'] as String, |
| 27 | paymentId: obj['paymentId'] as String, |
| 28 | chain: obj['chain'] as String, |
| 29 | network: obj['network'] as String, |
| 30 | instructions: instructions); |
| 31 | } |
| 32 | |
| 33 | final DateTime time; |
| 34 | final DateTime expires; |
| 35 | final String memo; |
| 36 | final String paymentUrl; |
| 37 | final String paymentId; |
| 38 | final String chain; |
| 39 | final String network; |
| 40 | final List<AnyPayPaymentInstruction> instructions; |
| 41 | |
| 42 | String get totalAmount { |
| 43 | final total = instructions.fold<int>( |
| 44 | 0, |
| 45 | (int acc, instruction) => |
| 46 | acc + instruction.outputs.fold<int>(0, (int outAcc, out) => outAcc + out.amount)); |
| 47 | switch (chain) { |
| 48 | case AnyPayChain.xmr: |
| 49 | return monero!.formatterMoneroAmountToString(amount: total); |
| 50 | case AnyPayChain.btc: |
| 51 | return bitcoin!.formatterBitcoinAmountToString(amount: total); |
| 52 | case AnyPayChain.ltc: |
| 53 | return bitcoin!.formatterBitcoinAmountToString(amount: total); |
| 54 | default: |
| 55 | return ''; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | List<String> get outAddresses { |
| 60 | return instructions |
| 61 | .map((instruction) => instruction.outputs.map((out) => out.address)) |
| 62 | .expand((e) => e) |
| 63 | .toList(); |
| 64 | } |
| 65 | } |