zwallet/lib/accounts.dart

490 lines
11 KiB
Dart
Raw Normal View History

2023-03-14 07:59:02 -07:00
import 'dart:async';
2022-03-07 06:53:18 -08:00
import 'package:shared_preferences/shared_preferences.dart';
import 'package:warp_api/data_fb_generated.dart';
2022-03-07 06:53:18 -08:00
import 'coin/coins.dart';
import 'package:mobx/mobx.dart';
import 'db.dart';
import 'package:warp_api/warp_api.dart';
import 'coin/coin.dart';
import 'main.dart';
import 'store.dart';
part 'accounts.g.dart';
class Account {
final int coin;
final int id;
final String name;
final int balance;
2023-05-15 00:40:20 -07:00
final int type;
bool active = false;
2022-03-07 06:53:18 -08:00
int tbalance = 0;
2023-05-15 00:40:20 -07:00
Account(
this.coin, this.id, this.name, this.type, this.balance, this.tbalance);
2022-10-23 03:30:28 -07:00
String get address {
return id != 0
? WarpApi.getAddress(this.coin, this.id, settings.uaType)
: "";
2022-10-23 03:30:28 -07:00
}
2022-03-07 06:53:18 -08:00
}
2023-05-15 00:40:20 -07:00
final Account emptyAccount = Account(0, 0, "", 0, 0, 0);
2022-03-07 06:53:18 -08:00
2022-12-29 02:52:03 -08:00
class AccountList {
2022-03-07 06:53:18 -08:00
List<Account> list = [];
2022-12-29 04:06:07 -08:00
AccountList() {
refresh();
}
void refresh() {
2022-03-07 06:53:18 -08:00
List<Account> _list = [];
2022-12-29 02:52:03 -08:00
for (var coin in coins) {
var accounts = WarpApi.getAccountList(coin.coin)
2023-05-15 00:40:20 -07:00
.map(
(a) => Account(coin.coin, a.id, a.name!, a.keyType, a.balance, 0))
.toList();
final id = WarpApi.getActiveAccountId(coin.coin);
if (id != 0) {
accounts.firstWhere((a) => a.id == id).active = true;
}
_list.addAll(accounts);
2022-12-29 02:52:03 -08:00
}
2022-03-07 06:53:18 -08:00
list = _list;
}
bool get isEmpty {
return list.isEmpty;
}
2022-07-07 20:26:20 -07:00
2022-03-07 06:53:18 -08:00
Future<void> updateTBalance() async {
for (var a in list) {
final tbalance = await WarpApi.getTBalanceAsync(a.coin, a.id);
a.tbalance = tbalance;
}
}
2023-02-08 16:38:57 -08:00
void delete(int coin, int id) {
2022-03-07 06:53:18 -08:00
WarpApi.deleteAccount(coin, id);
2023-02-08 16:38:57 -08:00
active.checkAndUpdate();
2022-03-07 06:53:18 -08:00
}
Future<void> changeAccountName(int coin, int id, String name) async {
2022-12-29 04:06:07 -08:00
WarpApi.updateAccountName(coin, id, name);
refresh();
2022-03-07 06:53:18 -08:00
}
Account get(int coin, int id) =>
list.firstWhere((e) => e.coin == coin && e.id == id,
orElse: () => emptyAccount);
2022-03-07 06:53:18 -08:00
}
2023-01-21 02:35:19 -08:00
AccountId? getAvailableId(int coin) {
final nid = getActiveAccountId(coin);
if (nid.id != 0) return nid;
2023-02-08 16:38:57 -08:00
for (var coinData in settings.coins) {
// look for an account in any other coin
2023-02-08 16:38:57 -08:00
if (coinData.coin != coin) {
final nid = getActiveAccountId(coinData.coin);
2023-01-21 02:35:19 -08:00
if (nid.id != 0) return nid;
}
}
// We have no accounts
return null;
}
AccountId getActiveAccountId(int coin) {
final id = WarpApi.getActiveAccountId(coin);
return AccountId(coin, id);
}
2022-03-07 06:53:18 -08:00
class ActiveAccount = _ActiveAccount with _$ActiveAccount;
abstract class _ActiveAccount with Store {
@observable
int dataEpoch = 0;
2022-03-07 06:53:18 -08:00
@observable
int coin = 0;
@observable
int id = 0;
2022-03-07 06:53:18 -08:00
Account account = emptyAccount;
2022-07-07 20:26:20 -07:00
CoinBase coinDef = zcash;
2022-03-07 06:53:18 -08:00
bool canPay = false;
2023-04-18 17:01:14 -07:00
bool external = false;
2022-11-12 19:43:09 -08:00
Balances balances = Balances();
@observable
String taddress = "";
2022-03-07 06:53:18 -08:00
int tbalance = 0;
2022-12-05 08:21:17 -08:00
PoolBalances poolBalances = PoolBalances();
2022-03-07 06:53:18 -08:00
@observable
List<Note> notes = [];
@observable
List<Tx> txs = [];
@observable
List<Spending> spendings = [];
@observable
List<TimeSeriesPoint<double>> accountBalances = [];
@observable
List<PnL> pnls = [];
@observable
ObservableList<ZMessage> messages = ObservableList();
@observable
int unread = 0;
@observable
String banner = "";
2022-03-07 06:53:18 -08:00
@observable
2023-03-14 05:10:07 -07:00
int addrMode = 0;
2022-03-07 06:53:18 -08:00
@observable
SortConfig noteSortConfig = SortConfig("", SortOrder.Unsorted);
@observable
SortConfig txSortConfig = SortConfig("", SortOrder.Unsorted);
@observable
int pnlSeriesIndex = 0;
@observable
bool pnlDesc = false;
@observable
2023-02-08 16:38:57 -08:00
Recipient? draftRecipient;
AccountId toId() {
return AccountId(coin, id);
}
2022-03-07 06:53:18 -08:00
@action
Future<void> restore() async {
final prefs = await SharedPreferences.getInstance();
2022-07-08 18:51:23 -07:00
final coin = prefs.getInt('coin') ?? 0;
var id = prefs.getInt('account') ?? 0;
if (WarpApi.checkAccount(coin, id)) setActiveAccount(coin, id);
2023-02-08 16:38:57 -08:00
checkAndUpdate();
}
2023-02-08 16:38:57 -08:00
void checkAndUpdate() {
final aid = getAvailableId(active.coin);
if (aid == null) {
2023-01-01 12:46:31 -08:00
setActiveAccount(0, 0);
} else if (aid != active.toId()) {
2023-01-01 12:46:31 -08:00
setActiveAccount(aid.coin, aid.id);
}
2022-03-07 06:53:18 -08:00
}
void setActiveAccount(int coin, int id) {
WarpApi.setActiveAccount(coin, id);
if (coin != this.coin || id != this.id) {
this.coin = coin;
this.id = id;
_refreshAccount();
}
Future(Action(() async {
final prefs = await SharedPreferences.getInstance();
prefs.setInt('coin', coin);
prefs.setInt('account', id);
await priceStore.updateChart(force: true);
dataEpoch += 1;
}));
Future(() => priceStore.fetchCoinPrice(active.coin));
dataEpoch += 1;
}
2022-03-07 06:53:18 -08:00
void _refreshAccount() {
2022-03-07 06:53:18 -08:00
coinDef = settings.coins[coin].def;
2022-12-29 02:52:03 -08:00
final accounts = AccountList();
2022-12-29 04:06:07 -08:00
accounts.refresh();
2022-03-07 06:53:18 -08:00
account = accounts.get(coin, id);
if (id > 0) {
2022-12-29 04:06:07 -08:00
taddress = WarpApi.getTAddr(coin, id);
external = coinDef.supportsLedger &&
!isMobile() &&
WarpApi.ledgerHasAccount(coin, id);
2023-04-18 17:01:14 -07:00
canPay = WarpApi.getSK(coin, id).isNotEmpty || external;
2022-03-07 06:53:18 -08:00
}
2023-03-14 05:10:07 -07:00
addrMode = 0;
2022-11-12 19:43:09 -08:00
balances.initialized = false;
draftRecipient = null;
2022-03-07 06:53:18 -08:00
2023-01-01 12:46:31 -08:00
update();
2023-03-14 07:59:02 -07:00
Future(priceStore.updateChart);
2022-03-07 06:53:18 -08:00
}
@action
Future<void> refreshTAddr() async {
2022-12-29 04:06:07 -08:00
taddress = WarpApi.getTAddr(coin, id);
2022-03-27 22:55:48 -07:00
}
2022-03-07 06:53:18 -08:00
@action
2023-03-14 07:59:02 -07:00
void updateAddrMode(int v) {
addrMode = v;
2022-03-07 06:53:18 -08:00
}
@action
void updateTBalance() {
2022-09-24 05:05:45 -07:00
try {
tbalance = WarpApi.getTBalance();
} on String {}
2022-03-07 06:53:18 -08:00
}
@action
2022-12-29 04:06:07 -08:00
void updateBalances() {
2022-11-12 19:43:09 -08:00
final initialized = balances.initialized;
final prevBalance = balances.balance;
2022-12-29 04:06:07 -08:00
final b = WarpApi.getBalance(coin, id, syncStatus.confirmHeight);
balances.update(b.balance, b.shielded, b.unconfirmedSpent, b.underConfirmed,
b.excluded);
2022-11-12 19:43:09 -08:00
if (initialized && prevBalance != balances.balance) {
showBalanceNotification(prevBalance, balances.balance);
2022-10-12 18:25:29 -07:00
}
2022-03-07 06:53:18 -08:00
}
2022-09-14 19:19:50 -07:00
@action
void clear() {
messages.clear();
notes.clear();
txs.clear();
unread = 0;
dataEpoch += 1;
}
2022-03-07 06:53:18 -08:00
@action
2023-01-01 12:46:31 -08:00
void update() {
2022-12-29 04:06:07 -08:00
updateBalances();
2022-03-07 06:53:18 -08:00
updateTBalance();
2022-12-29 04:06:07 -08:00
poolBalances.update();
2022-12-05 08:21:17 -08:00
2022-03-07 06:53:18 -08:00
final dbr = DbReader(coin, id);
2022-12-29 04:06:07 -08:00
notes = dbr.getNotes();
txs = dbr.getTxs();
2023-01-01 12:46:31 -08:00
messages = ObservableList.of(dbr.getMessages());
2022-04-16 23:03:33 -07:00
unread = messages.where((m) => !m.read).length;
2022-03-07 06:53:18 -08:00
dataEpoch += 1;
}
@action
void setDraftRecipient(Recipient? v) {
draftRecipient = v;
}
2023-03-14 05:10:07 -07:00
String getDiversifiedAddress(int uaType, int time) {
return WarpApi.getDiversifiedAddress(uaType & settings.uaType, time);
2022-03-07 06:53:18 -08:00
}
String getAddress(int uaType) {
return WarpApi.getAddress(coin, id, uaType);
2022-11-16 06:16:44 -08:00
}
2022-03-07 06:53:18 -08:00
@computed
List<Note> get sortedNotes {
2023-02-08 16:38:57 -08:00
// ignore: unused_local_variable
final _unused = syncStatus.syncedHeight;
2022-03-07 06:53:18 -08:00
var notes2 = [...notes];
switch (noteSortConfig.field) {
case "time":
return _sort(notes2, (Note note) => note.height, noteSortConfig.order);
case "amount":
return _sort(notes2, (Note note) => note.value, noteSortConfig.order);
}
return notes2;
}
@computed
List<Tx> get sortedTxs {
2023-02-08 16:38:57 -08:00
// ignore: unused_local_variable
final _unused = syncStatus.syncedHeight;
2022-03-07 06:53:18 -08:00
var txs2 = [...txs];
switch (txSortConfig.field) {
case "time":
return _sort(txs2, (Tx tx) => tx.height, txSortConfig.order);
case "amount":
return _sort(txs2, (Tx tx) => tx.value, txSortConfig.order);
case "txid":
return _sort(txs2, (Tx tx) => tx.txid, txSortConfig.order);
case "address":
return _sort(txs2, (Tx tx) => tx.contact ?? tx.address ?? '',
txSortConfig.order);
2022-03-07 06:53:18 -08:00
case "memo":
2023-03-01 05:10:07 -08:00
return _sort(txs2, (Tx tx) => tx.memo ?? '', txSortConfig.order);
2022-03-07 06:53:18 -08:00
}
return txs2;
}
@action
void sortNotes(String field) {
noteSortConfig = noteSortConfig.sortOn(field);
}
@action
void sortTx(String field) {
txSortConfig = txSortConfig.sortOn(field);
}
List<C> _sort<C extends HasHeight, T extends Comparable>(
List<C> items, T Function(C) project, SortOrder order) {
switch (order) {
case SortOrder.Ascending:
items.sort((a, b) => project(a).compareTo(project(b)));
break;
case SortOrder.Descending:
items.sort((a, b) => -project(a).compareTo(project(b)));
break;
case SortOrder.Unsorted:
items.sort((a, b) => -a.height.compareTo(b.height));
break;
}
return items;
}
@action
void setPnlSeriesIndex(int index) {
pnlSeriesIndex = index;
}
@computed
List<PnL> get pnlSorted {
if (pnlDesc) {
var _pnls = [...pnls.reversed];
return _pnls;
}
return pnls;
}
@action
void togglePnlDesc() {
pnlDesc = !pnlDesc;
}
@action
2022-12-29 04:06:07 -08:00
void excludeNote(Note note) {
WarpApi.updateExcluded(coin, note.id, note.excluded);
2022-03-07 06:53:18 -08:00
}
@action
2022-12-29 04:06:07 -08:00
void invertExcludedNotes() {
WarpApi.invertExcluded(coin, id);
2022-03-07 06:53:18 -08:00
notes = notes.map((n) => n.invertExcluded).toList();
}
@action
2022-12-29 04:06:07 -08:00
void fetchChartData() {
final dbr = active.dbReader;
pnls = dbr.getPNL(active.id);
spendings = dbr.getSpending(active.id);
accountBalances =
dbr.getAccountBalanceTimeSeries(active.id, active.balances.balance);
2022-03-07 06:53:18 -08:00
}
2022-04-15 23:51:13 -07:00
@action
void markMessageAsRead(int index) {
2022-04-16 23:03:33 -07:00
if (!messages[index].read) {
2022-06-07 10:00:08 -07:00
WarpApi.markMessageAsRead(messages[index].id, true);
2022-04-16 23:03:33 -07:00
messages[index] = messages[index].withRead(true);
unread = unread - 1;
}
2022-04-15 23:51:13 -07:00
}
2022-04-16 20:16:45 -07:00
@action
void markAllMessagesAsRead() {
2022-06-07 10:00:08 -07:00
WarpApi.markAllMessagesAsRead(true);
2022-04-16 20:16:45 -07:00
for (var i = 0; i < messages.length; i++) {
messages[i] = messages[i].withRead(true);
}
2022-04-16 23:03:33 -07:00
unread = 0;
2022-04-16 20:16:45 -07:00
}
2022-12-29 04:06:07 -08:00
int prevInThread(int index) {
2022-04-15 23:51:13 -07:00
final message = messages[index];
final pn =
WarpApi.getPrevNextMessage(coin, id, message.subject, message.height);
2022-12-29 04:06:07 -08:00
return pn.prev;
2022-04-15 23:51:13 -07:00
}
2022-12-29 04:06:07 -08:00
int nextInThread(int index) {
2022-04-15 23:51:13 -07:00
final message = messages[index];
final pn =
WarpApi.getPrevNextMessage(coin, id, message.subject, message.height);
2022-12-29 04:06:07 -08:00
return pn.next;
2022-04-15 23:51:13 -07:00
}
2022-06-16 03:16:32 -07:00
@action
void setBanner(String msg) {
banner = msg;
}
2022-10-23 03:30:28 -07:00
2022-11-12 19:43:09 -08:00
String get address {
2022-10-23 03:30:28 -07:00
return account.address;
}
2022-12-23 09:40:16 -08:00
DbReader get dbReader => DbReader(coin, id);
int get availabeAddrs => WarpApi.getAvailableAddrs(coin, id);
2022-03-07 06:53:18 -08:00
}
2022-11-12 19:43:09 -08:00
class Balances = _Balances with _$Balances;
abstract class _Balances with Store {
bool initialized = false;
@observable
int balance = 0;
@observable
int shieldedBalance = 0;
@observable
int unconfirmedSpentBalance = 0;
@observable
int underConfirmedBalance = 0;
@observable
int excludedBalance = 0;
2022-11-12 19:43:09 -08:00
@action
void update(int balance, int shieldedBalance, int unconfirmedSpentBalance,
int underConfirmedBalance, int excludedBalance) {
2022-11-12 19:43:09 -08:00
this.balance = balance;
this.shieldedBalance = shieldedBalance;
this.unconfirmedSpentBalance = unconfirmedSpentBalance;
this.underConfirmedBalance = underConfirmedBalance;
this.excludedBalance = excludedBalance;
this.initialized = true;
}
}
2022-11-17 01:14:39 -08:00
class AccountId {
final int coin;
final int id;
AccountId(this.coin, this.id);
}
2022-12-05 08:21:17 -08:00
class PoolBalances = _PoolBalances with _$PoolBalances;
abstract class _PoolBalances with Store {
@observable
int transparent = 0;
@observable
int sapling = 0;
@observable
int orchard = 0;
2022-12-05 08:21:17 -08:00
2022-12-29 04:06:07 -08:00
void update() {
final b =
WarpApi.getBalance(active.coin, active.id, syncStatus.confirmHeight);
2022-12-29 04:06:07 -08:00
_update(active.tbalance, b.sapling, b.orchard);
2022-12-05 08:21:17 -08:00
}
@action
_update(int t, int s, int o) {
transparent = t;
sapling = s;
orchard = o;
}
}