2023-03-24 21:30:29 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-03-24 17:08:02 +00:00
|
|
|
import 'package:decimal/decimal.dart';
|
2023-03-24 21:30:29 +00:00
|
|
|
import 'package:equatable/equatable.dart';
|
2023-03-24 17:08:02 +00:00
|
|
|
|
|
|
|
final _ten = BigInt.from(10);
|
|
|
|
|
2023-03-24 21:30:29 +00:00
|
|
|
class Amount implements Equatable {
|
2023-03-24 17:08:02 +00:00
|
|
|
Amount({
|
|
|
|
required BigInt rawValue,
|
|
|
|
required this.fractionDigits,
|
|
|
|
}) : assert(fractionDigits >= 0),
|
|
|
|
_value = rawValue;
|
|
|
|
|
|
|
|
/// truncate double value to [fractionDigits] places
|
|
|
|
Amount.fromDouble(double amount, {required this.fractionDigits})
|
|
|
|
: assert(fractionDigits >= 0),
|
|
|
|
_value =
|
|
|
|
Decimal.parse(amount.toString()).shift(fractionDigits).toBigInt();
|
|
|
|
|
|
|
|
/// truncate decimal value to [fractionDigits] places
|
|
|
|
Amount.fromDecimal(Decimal amount, {required this.fractionDigits})
|
|
|
|
: assert(fractionDigits >= 0),
|
|
|
|
_value = amount.shift(fractionDigits).toBigInt();
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
// ======= Instance properties ===============================================
|
|
|
|
|
|
|
|
final int fractionDigits;
|
|
|
|
final BigInt _value;
|
|
|
|
|
|
|
|
// ===========================================================================
|
|
|
|
// ======= Getters ===========================================================
|
|
|
|
|
|
|
|
/// raw base value
|
|
|
|
BigInt get raw => _value;
|
|
|
|
|
|
|
|
/// actual decimal vale represented
|
|
|
|
Decimal get decimal =>
|
|
|
|
(Decimal.fromBigInt(_value) / _ten.pow(fractionDigits).toDecimal())
|
|
|
|
.toDecimal(scaleOnInfinitePrecision: fractionDigits);
|
|
|
|
|
|
|
|
/// convenience getter
|
|
|
|
@Deprecated("provided for convenience only. Use fractionDigits instead.")
|
|
|
|
int get decimals => fractionDigits;
|
2023-03-24 21:30:29 +00:00
|
|
|
|
|
|
|
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;
|
2023-03-24 17:08:02 +00:00
|
|
|
}
|