lez-programs/apps/amm/src/AccountModel.cpp

80 lines
2.2 KiB
C++
Raw Normal View History

feat(amm): wire the AMM app to the LEZ wallet module Turns the dummy-data AMM UI into a real client of the on-chain LEZ wallet. Adds a hand-written ui_qml C++ backend (src/AmmUi*) over the core logos_execution_zone module: create/open a local wallet, create and list public/private accounts, and a navbar Connect / Connected + account-selector + Disconnect flow. Onboarding is password-only (no path picking) with a per-app wallet at ~/.lee/amm-wallet (override: AMM_WALLET_HOME_DIR); standalone gets its own wallet, Basecamp shares accounts via adopt-on-start. Requires Nix with flakes; macOS also needs `sandbox = false` (the default). The logos_execution_zone input is pinned to a module rev whose LEZ (lssa) already includes the macOS Metal-build fix, so no `--override-input` is needed — plain `nix run .` works: cd apps/amm nix run . - create_new now returns the new wallet's BIP39 mnemonic (not an int status); the app currently discards it, so the wallet can't yet be recovered. Surfacing it in onboarding (+ restore_storage) is a follow-up. - The wallet password is currently a no-op upstream (storage.rs: "TODO: use password for storage encryption"); storage.json is plaintext. So Disconnect is a UI-level lock and reconnect does not (cannot yet) re-prompt for it. - wallet-ffi requires explicit config/storage paths; a *_default() FFI would let the app drop its path handling. - Bundled network config: connects to whatever WalletConfig::default() points at; real testnet endpoints still TBD.
2026-06-24 14:50:17 +02:00
#include "AccountModel.h"
#include <QJsonObject>
AccountModel::AccountModel(QObject* parent)
: QAbstractListModel(parent)
{
}
int AccountModel::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return m_entries.size();
}
QVariant AccountModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_entries.size())
return QVariant();
const AccountEntry& e = m_entries.at(index.row());
switch (role) {
case NameRole: return e.name;
case AddressRole: return e.address;
case BalanceRole: return e.balance;
case IsPublicRole: return e.isPublic;
default: return QVariant();
}
}
QHash<int, QByteArray> AccountModel::roleNames() const
{
return {
{ NameRole, "name" },
{ AddressRole, "address" },
{ BalanceRole, "balance" },
{ IsPublicRole, "isPublic" }
};
}
void AccountModel::replaceFromJsonArray(const QJsonArray& arr)
{
beginResetModel();
const int oldCount = m_entries.size();
m_entries.clear();
int idx = 0;
for (const QJsonValue& v : arr) {
AccountEntry e;
e.name = QStringLiteral("Account %1").arg(++idx);
e.balance = QString();
if (v.isObject()) {
const QJsonObject obj = v.toObject();
e.address = obj.value(QStringLiteral("account_id")).toString();
e.isPublic = obj.value(QStringLiteral("is_public")).toBool(true);
} else {
e.address = v.toString();
e.isPublic = true;
}
m_entries.append(e);
}
endResetModel();
if (oldCount != m_entries.size())
emit countChanged();
}
void AccountModel::setBalanceByAddress(const QString& address, const QString& balance)
{
for (int i = 0; i < m_entries.size(); ++i) {
if (m_entries.at(i).address == address) {
if (m_entries.at(i).balance != balance) {
m_entries[i].balance = balance;
const QModelIndex idx = index(i, 0);
emit dataChanged(idx, idx, { BalanceRole });
}
return;
}
}
}