stack_wallet/lib/models/paymint/transactions_model.dart

428 lines
12 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-08-26 08:11:35 +00:00
import 'package:dart_numerics/dart_numerics.dart';
import 'package:decimal/decimal.dart';
import 'package:hive/hive.dart';
part '../type_adaptors/transactions_model.g.dart';
String extractDateFromTimestamp(int? timestamp) {
if (timestamp == 0 || timestamp == null) {
return 'Now...';
}
final int day = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000).day;
final int month = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000).month;
final int year = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000).year;
return '$year${month < 10 ? "0$month" : month.toString()}${day < 10 ? "0$day" : day.toString()}';
}
// @HiveType(typeId: 1)
class TransactionData {
// @HiveField(0)
final List<TransactionChunk> txChunks;
TransactionData({this.txChunks = const []});
factory TransactionData.fromJson(Map<String, dynamic> json) {
2024-05-27 23:56:22 +00:00
final dateTimeChunks = json['dateTimeChunks'] as List;
final List<TransactionChunk> chunksList = dateTimeChunks
.map(
(txChunk) =>
TransactionChunk.fromJson(txChunk as Map<String, dynamic>),
)
2022-08-26 08:11:35 +00:00
.toList();
return TransactionData(txChunks: chunksList);
}
factory TransactionData.fromMap(Map<String, Transaction> transactions) {
2024-05-27 23:56:22 +00:00
final Map<String, List<Transaction>> chunks = {};
2022-08-26 08:11:35 +00:00
transactions.forEach((key, value) {
2024-05-27 23:56:22 +00:00
final String date = extractDateFromTimestamp(value.timestamp);
2022-08-26 08:11:35 +00:00
if (!chunks.containsKey(date)) {
chunks[date] = [];
}
chunks[date]!.add(value);
});
2024-05-27 23:56:22 +00:00
final List<TransactionChunk> chunksList = [];
2022-08-26 08:11:35 +00:00
chunks.forEach((key, value) {
value.sort((a, b) => b.timestamp.compareTo(a.timestamp));
chunksList.add(
2024-05-27 23:56:22 +00:00
TransactionChunk(timestamp: value[0].timestamp, transactions: value),
);
2022-08-26 08:11:35 +00:00
});
chunksList.sort((a, b) => b.timestamp.compareTo(a.timestamp));
return TransactionData(txChunks: chunksList);
}
Transaction? findTransaction(String txid) {
for (var i = 0; i < txChunks.length; i++) {
2024-05-27 23:56:22 +00:00
final txChunk = txChunks[i].transactions;
2022-08-26 08:11:35 +00:00
for (var j = 0; j < txChunk.length; j++) {
2024-05-27 23:56:22 +00:00
final tx = txChunk[j];
2022-08-26 08:11:35 +00:00
if (tx.txid == txid) {
return tx;
}
}
}
return null;
}
Map<String, Transaction> getAllTransactions() {
2024-05-27 23:56:22 +00:00
final Map<String, Transaction> transactions = {};
2022-08-26 08:11:35 +00:00
for (var i = 0; i < txChunks.length; i++) {
2024-05-27 23:56:22 +00:00
final txChunk = txChunks[i].transactions;
2022-08-26 08:11:35 +00:00
for (var j = 0; j < txChunk.length; j++) {
2024-05-27 23:56:22 +00:00
final tx = txChunk[j];
2022-08-26 08:11:35 +00:00
transactions[tx.txid] = tx;
}
}
return transactions;
}
}
// @HiveType(typeId: 2)
class TransactionChunk {
// @HiveField(0)
final int timestamp;
// @HiveField(1)
final List<Transaction> transactions;
TransactionChunk({required this.timestamp, required this.transactions});
factory TransactionChunk.fromJson(Map<String, dynamic> json) {
2024-05-27 23:56:22 +00:00
final txArray = json['transactions'] as List;
final List<Transaction> txList = txArray
2022-08-26 08:11:35 +00:00
.map((tx) => Transaction.fromJson(tx as Map<String, dynamic>))
.toList();
return TransactionChunk(
2024-05-27 23:56:22 +00:00
timestamp: int.parse(json['timestamp'].toString()),
transactions: txList,
);
2022-08-26 08:11:35 +00:00
}
@override
String toString() {
String transaction = "timestamp: $timestamp transactions: [\n";
for (final tx in transactions) {
transaction += " $tx \n";
}
transaction += "]";
return transaction;
}
}
// @HiveType(typeId: 3)
class Transaction {
// @HiveField(0)
final String txid;
// @HiveField(1)
final bool confirmedStatus;
// @HiveField(2)
final int timestamp;
// @HiveField(3)
final String txType;
// @HiveField(4)
final int amount;
// @HiveField(5)
final List<dynamic> aliens;
// @HiveField(6)
final String worthNow;
// @HiveField(7)
final String worthAtBlockTimestamp;
// @HiveField(8)
final int fees;
// @HiveField(9)
final int inputSize;
// @HiveField(10)
final int outputSize;
// @HiveField(11)
final List<Input> inputs;
// @HiveField(12)
final List<Output> outputs;
// @HiveField(13)
final String address;
// @HiveField(14)
final int height;
// @HiveField(15)
final String subType;
// @HiveField(16)
final int confirmations;
// @HiveField(17)
final bool isCancelled;
// @HiveField(18)
final String? slateId;
// @HiveField(18)
final String? otherData;
// @HiveField(16)
final int? numberOfMessages;
2022-08-26 08:11:35 +00:00
Transaction({
required this.txid,
required this.confirmedStatus,
required this.timestamp,
required this.txType,
required this.amount,
this.aliens = const [],
required this.worthNow,
required this.worthAtBlockTimestamp,
required this.fees,
required this.inputSize,
required this.outputSize,
required this.inputs,
required this.outputs,
required this.address,
required this.height,
this.subType = "",
required this.confirmations,
this.isCancelled = false,
this.slateId,
this.otherData,
this.numberOfMessages,
2022-08-26 08:11:35 +00:00
});
factory Transaction.fromJson(Map<String, dynamic> json) {
2024-05-27 23:56:22 +00:00
final inputArray = json['inputs'] as List;
final outputArray = json['outputs'] as List;
2022-08-26 08:11:35 +00:00
2024-05-27 23:56:22 +00:00
final List<Input> inputList = inputArray
2022-08-26 08:11:35 +00:00
.map((input) => Input.fromJson(Map<String, dynamic>.from(input as Map)))
.toList();
2024-05-27 23:56:22 +00:00
final List<Output> outputList = outputArray
.map(
(output) => Output.fromJson(Map<String, dynamic>.from(output as Map)),
)
2022-08-26 08:11:35 +00:00
.toList();
return Transaction(
txid: json['txid'] as String,
confirmedStatus: json['confirmed_status'] as bool,
2023-01-06 15:25:28 +00:00
timestamp: int.parse(json['timestamp'].toString()),
2022-08-26 08:11:35 +00:00
txType: json['txType'] as String,
amount: json['amount'] as int,
aliens: json['aliens'] as List,
worthNow: json['worthNow'] as String? ?? "",
worthAtBlockTimestamp: json['worthAtBlockTimestamp'] as String? ?? "",
2023-01-06 15:25:28 +00:00
fees: int.parse(json['fees'].toString()),
2022-08-26 08:11:35 +00:00
inputSize: json['inputSize'] as int,
outputSize: json['outputSize'] as int,
inputs: inputList,
outputs: outputList,
address: json['address'] as String,
height: json['height'] as int? ?? -1,
subType: json["subType"] as String? ?? "",
confirmations: json['confirmations'] as int? ?? 0,
isCancelled: json["isCancelled"] as bool? ?? false,
slateId: json["slateId"] as String?,
otherData: json["otherData"] as String?,
numberOfMessages: json["numberOfMessages"] as int?,
2022-08-26 08:11:35 +00:00
);
}
factory Transaction.fromLelantusJson(Map<String, dynamic> json) {
return Transaction(
txid: json['txid'] as String,
confirmedStatus: json['confirmed_status'] as bool? ?? false,
timestamp: json['timestamp'] as int? ??
(DateTime.now().millisecondsSinceEpoch ~/ 1000),
txType: json['txType'] as String,
2024-05-15 21:20:45 +00:00
amount: _parse(json["amount"].toString()),
2022-08-26 08:11:35 +00:00
aliens: [],
worthNow: json['worthNow'] as String,
worthAtBlockTimestamp: json['worthAtBlockTimestamp'] as String? ?? "0",
2024-05-15 21:20:45 +00:00
fees: _parse(json["fees"].toString()),
2022-08-26 08:11:35 +00:00
inputSize: json['inputSize'] as int? ?? 0,
outputSize: json['outputSize'] as int? ?? 0,
inputs: [],
outputs: [],
address: json["address"] as String,
height: json["height"] as int? ?? int64MaxValue,
subType: json["subType"] as String? ?? "",
confirmations: json["confirmations"] as int? ?? 0,
otherData: json["otherData"] as String?,
);
}
bool get isMinting => subType.toLowerCase() == "mint" && !confirmedStatus;
Transaction copyWith({
String? txid,
bool? confirmedStatus,
int? timestamp,
String? txType,
int? amount,
List<dynamic>? aliens,
String? worthNow,
String? worthAtBlockTimestamp,
int? fees,
int? inputSize,
int? outputSize,
List<Input>? inputs,
List<Output>? outputs,
String? address,
int? height,
String? subType,
int? confirmations,
bool? isCancelled,
String? slateId,
String? otherData,
int? numberOfMessages,
2022-08-26 08:11:35 +00:00
}) {
return Transaction(
txid: txid ?? this.txid,
confirmedStatus: confirmedStatus ?? this.confirmedStatus,
timestamp: timestamp ?? this.timestamp,
txType: txType ?? this.txType,
amount: amount ?? this.amount,
aliens: aliens ?? this.aliens,
worthNow: worthNow ?? this.worthNow,
worthAtBlockTimestamp:
worthAtBlockTimestamp ?? this.worthAtBlockTimestamp,
fees: fees ?? this.fees,
inputSize: inputSize ?? this.inputSize,
outputSize: outputSize ?? this.outputSize,
inputs: inputs ?? this.inputs,
outputs: outputs ?? this.outputs,
address: address ?? this.address,
height: height ?? this.height,
subType: subType ?? this.subType,
confirmations: confirmations ?? this.confirmations,
isCancelled: isCancelled ?? this.isCancelled,
slateId: slateId ?? this.slateId,
otherData: otherData ?? this.otherData,
numberOfMessages: numberOfMessages ?? this.numberOfMessages,
2022-08-26 08:11:35 +00:00
);
}
@override
String toString() {
2024-05-27 23:56:22 +00:00
final String transaction =
"{txid: $txid, type: $txType, subType: $subType, value: $amount, fee: $fees, height: $height, confirm: $confirmedStatus, confirmations: $confirmations, address: $address, timestamp: $timestamp, worthNow: $worthNow, inputs: $inputs, slateid: $slateId, numberOfMessages: $numberOfMessages }";
2022-08-26 08:11:35 +00:00
return transaction;
}
}
// @HiveType(typeId: 4)
class Input {
// @HiveField(0)
final String txid;
// @HiveField(1)
final int vout;
// // @HiveField(2)
final Output? prevout;
// @HiveField(3)
final String? scriptsig;
// @HiveField(4)
final String? scriptsigAsm;
// @HiveField(5)
final List<dynamic>? witness;
// @HiveField(6)
final bool? isCoinbase;
// @HiveField(7)
final int? sequence;
// @HiveField(8)
final String? innerRedeemscriptAsm;
Input({
required this.txid,
required this.vout,
this.prevout,
this.scriptsig,
this.scriptsigAsm,
this.witness,
this.isCoinbase,
this.sequence,
this.innerRedeemscriptAsm,
});
factory Input.fromJson(Map<String, dynamic> json) {
2024-05-27 23:56:22 +00:00
final bool iscoinBase = json['coinbase'] != null;
2022-08-26 08:11:35 +00:00
return Input(
txid: json['txid'] as String? ?? "",
vout: json['vout'] as int? ?? -1,
// electrumx calls do not return prevout so we set this to null for now
prevout: null, //Output.fromJson(json['prevout']),
scriptsig: iscoinBase ? "" : json['scriptSig']['hex'] as String?,
scriptsigAsm: iscoinBase ? "" : json['scriptSig']['asm'] as String?,
witness: json['witness'] as List? ?? [],
isCoinbase: iscoinBase ? iscoinBase : json['is_coinbase'] as bool?,
sequence: json['sequence'] as int?,
innerRedeemscriptAsm: json['innerRedeemscriptAsm'] as String? ?? "",
);
}
@override
String toString() {
2024-05-27 23:56:22 +00:00
final String transaction = "{txid: $txid}";
2022-08-26 08:11:35 +00:00
return transaction;
}
}
// @HiveType(typeId: 5)
class Output {
// @HiveField(0)
final String? scriptpubkey;
2023-01-05 16:49:55 +00:00
2022-08-26 08:11:35 +00:00
// @HiveField(1)
final String? scriptpubkeyAsm;
2023-01-05 16:49:55 +00:00
2022-08-26 08:11:35 +00:00
// @HiveField(2)
final String? scriptpubkeyType;
2023-01-05 16:49:55 +00:00
2022-08-26 08:11:35 +00:00
// @HiveField(3)
final String scriptpubkeyAddress;
2023-01-05 16:49:55 +00:00
2022-08-26 08:11:35 +00:00
// @HiveField(4)
final int value;
2024-05-27 23:56:22 +00:00
Output({
this.scriptpubkey,
this.scriptpubkeyAsm,
this.scriptpubkeyType,
required this.scriptpubkeyAddress,
required this.value,
});
2022-08-26 08:11:35 +00:00
factory Output.fromJson(Map<String, dynamic> json) {
// TODO determine if any of this code is needed.
try {
final address = json["scriptPubKey"]["addresses"] == null
? json['scriptPubKey']['type'] as String
: json["scriptPubKey"]["addresses"][0] as String;
return Output(
scriptpubkey: json['scriptPubKey']['hex'] as String?,
scriptpubkeyAsm: json['scriptPubKey']['asm'] as String?,
scriptpubkeyType: json['scriptPubKey']['type'] as String?,
scriptpubkeyAddress: address,
2024-05-15 21:20:45 +00:00
value: _parse((json["value"] ?? 0).toString()),
);
} catch (s, e) {
return Output(
2024-05-27 23:56:22 +00:00
// Return output object with null values; allows wallet history to be built
scriptpubkey: "",
scriptpubkeyAsm: "",
scriptpubkeyType: "",
scriptpubkeyAddress: "",
value: _parse(0.toString()),
);
}
2022-08-26 08:11:35 +00:00
}
}
2024-05-15 21:20:45 +00:00
int _parse(String value) {
return (Decimal.parse(value) * Decimal.fromInt(8)).toBigInt().toInt();
}