mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-25 19:55:52 +00:00
amount serialization
This commit is contained in:
parent
f44774745e
commit
3ab605c065
1 changed files with 38 additions and 1 deletions
|
@ -1,8 +1,11 @@
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:decimal/decimal.dart';
|
import 'package:decimal/decimal.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
final _ten = BigInt.from(10);
|
final _ten = BigInt.from(10);
|
||||||
|
|
||||||
class Amount {
|
class Amount implements Equatable {
|
||||||
Amount({
|
Amount({
|
||||||
required BigInt rawValue,
|
required BigInt rawValue,
|
||||||
required this.fractionDigits,
|
required this.fractionDigits,
|
||||||
|
@ -40,4 +43,38 @@ class Amount {
|
||||||
/// convenience getter
|
/// convenience getter
|
||||||
@Deprecated("provided for convenience only. Use fractionDigits instead.")
|
@Deprecated("provided for convenience only. Use fractionDigits instead.")
|
||||||
int get decimals => fractionDigits;
|
int get decimals => fractionDigits;
|
||||||
|
|
||||||
|
Map<String, dynamic> toMap() {
|
||||||
|
// ===========================================================================
|
||||||
|
// ======= Serialization =====================================================
|
||||||
|
|
||||||
|
return {"raw": raw.toString(), "fractionDigits": fractionDigits};
|
||||||
|
}
|
||||||
|
|
||||||
|
String toJsonString() {
|
||||||
|
return jsonEncode(toMap());
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===========================================================================
|
||||||
|
// ======= Deserialization ===================================================
|
||||||
|
|
||||||
|
static Amount fromSerializedJsonString(String json) {
|
||||||
|
final map = jsonDecode(json) as Map;
|
||||||
|
return Amount(
|
||||||
|
rawValue: BigInt.parse(map["raw"] as String),
|
||||||
|
fractionDigits: map["fractionDigits"] as int,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===========================================================================
|
||||||
|
// ======= Overrides =========================================================
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => "Amount($raw, $fractionDigits)";
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [fractionDigits, _value];
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool? get stringify => false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue