From 0d0ff1532e3247213dbb600d8f88ebca93520c45 Mon Sep 17 00:00:00 2001 From: Dario Gabriel Lipicar Date: Fri, 15 Nov 2024 02:03:24 -0300 Subject: [PATCH] feat(activity): adapt to activityV2 API --- .../main/wallet_section/activity/entry.nim | 25 ++++++- .../main/wallet_section/activity/model.nim | 6 +- .../activity/transaction_identities_model.nim | 73 +++++++++++++++++++ src/backend/activity.nim | 20 ++++- 4 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 src/app/modules/main/wallet_section/activity/transaction_identities_model.nim diff --git a/src/app/modules/main/wallet_section/activity/entry.nim b/src/app/modules/main/wallet_section/activity/entry.nim index c49e663bf9..3db63398f1 100644 --- a/src/app/modules/main/wallet_section/activity/entry.nim +++ b/src/app/modules/main/wallet_section/activity/entry.nim @@ -12,6 +12,8 @@ import app/modules/shared/wallet_utils import web3/ethtypes as eth +import ./transaction_identities_model as txid + # Additional data needed to build an Entry, which is # not included in the metadata and needs to be # fetched from a different source. @@ -27,6 +29,8 @@ QtObject: metadata: backend.ActivityEntry extradata: ExtraData + transactions: txid.Model + amountCurrency: CurrencyAmount noAmount: CurrencyAmount @@ -57,8 +61,11 @@ QtObject: new(result, delete) result.metadata = metadata result.extradata = extradata - result.noAmount = newCurrencyAmount() + result.transactions = txid.newModel() + result.transactions.setItems(metadata.transactions) + + result.noAmount = newCurrencyAmount() result.amountCurrency = result.extractCurrencyAmount(currencyService) result.highlight = metadata.isNew @@ -69,6 +76,7 @@ QtObject: new(result, delete) result.metadata = metadata result.extradata = extradata + result.transactions = txid.newModel() result.noAmount = newCurrencyAmount() result.amountCurrency = result.extractCurrencyAmount(currencyService) @@ -127,6 +135,8 @@ QtObject: proc `$`*(self: ActivityEntry): string = return fmt"""ActivityEntry( metadata:{$self.metadata}, + extradata:{$self.extradata}, + transactions:{$self.transactions}, )""" # TODO: is this the right way to pass transaction identity? Why not use the instance? @@ -138,6 +148,12 @@ QtObject: QtProperty[string] id: read = getId + proc getTransactions*(self: ActivityEntry): QVariant {.slot.} = + return newQVariant(self.transactions) + + QtProperty[QVariant] transactions: + read = getTransactions + proc getMetadata*(self: ActivityEntry): backend.ActivityEntry = return self.metadata @@ -179,11 +195,18 @@ QtObject: QtProperty[int] timestamp: read = getTimestamp + proc statusChanged*(self: ActivityEntry) {.signal.} + proc getStatus*(self: ActivityEntry): int {.slot.} = return self.metadata.activityStatus.int + proc setStatus*(self: ActivityEntry, status: ActivityStatus) = + self.metadata.activityStatus = status + self.statusChanged() + QtProperty[int] status: read = getStatus + notify = statusChanged proc getChainIdIn*(self: ActivityEntry): int {.slot.} = return self.metadata.chainIdIn.get(ChainId(0)).int diff --git a/src/app/modules/main/wallet_section/activity/model.nim b/src/app/modules/main/wallet_section/activity/model.nim index 6395734611..22c2ca5c8e 100644 --- a/src/app/modules/main/wallet_section/activity/model.nim +++ b/src/app/modules/main/wallet_section/activity/model.nim @@ -5,7 +5,6 @@ import ./entry import app/modules/shared_models/currency_amount import app_service/service/currency/service import backend/activity as backend -import backend/backend as importing_transactionidentity_comp type ModelRole {.pure.} = enum @@ -108,7 +107,8 @@ QtObject: return false if m.getPayloadType() == MultiTransaction: - return m.getMultiTransactionId().get(0) == d.id.get() + let dID = d.id.get() + return dID > 0 and m.getMultiTransactionId().get(0) == dID return m.getTransactionIdentity().isSome() and d.transaction.isSome() and m.getTransactionIdentity().get() == d.transaction.get() @@ -133,6 +133,8 @@ QtObject: self.entries[i].setNftName(updates[j].nftName.get()) if updates[j].nftUrl.isSome(): self.entries[i].setNftImageUrl(updates[j].nftUrl.get()) + if updates[j].activityStatus.isSome(): + self.entries[i].setStatus(updates[j].activityStatus.get()) break proc getHasMore*(self: Model): bool {.slot.} = diff --git a/src/app/modules/main/wallet_section/activity/transaction_identities_model.nim b/src/app/modules/main/wallet_section/activity/transaction_identities_model.nim new file mode 100644 index 0000000000..297d15e202 --- /dev/null +++ b/src/app/modules/main/wallet_section/activity/transaction_identities_model.nim @@ -0,0 +1,73 @@ +import NimQml, Tables, strutils, stew/shims/strformat, sequtils + +import backend/backend + +type + ModelRole {.pure.} = enum + ChainIdRole = UserRole + 1 + TransactionHashRole + AddressRole + +QtObject: + type + Model* = ref object of QAbstractListModel + items: seq[backend.TransactionIdentity] + + proc delete(self: Model) = + self.QAbstractListModel.delete + + proc setup(self: Model) = + self.QAbstractListModel.setup + + proc newModel*(): Model = + new(result, delete) + result.items = @[] + result.setup + + proc `$`*(self: Model): string = + for i in 0 ..< self.items.len: + result &= fmt"""[{i}]:({$self.items[i]})""" + + proc countChanged(self: Model) {.signal.} + + proc getCount*(self: Model): int {.slot.} = + self.items.len + + QtProperty[int] count: + read = getCount + notify = countChanged + + method rowCount(self: Model, index: QModelIndex = nil): int = + return self.items.len + + method roleNames(self: Model): Table[int, string] = + { + ModelRole.ChainIdRole.int:"chainId", + ModelRole.TransactionHashRole.int:"txHash", + ModelRole.AddressRole.int:"address" + }.toTable + + method data(self: Model, index: QModelIndex, role: int): QVariant = + if (not index.isValid): + return + + if (index.row < 0 or index.row >= self.items.len): + return + + let item = self.items[index.row] + let enumRole = role.ModelRole + + case enumRole: + of ModelRole.ChainIdRole: + result = newQVariant(item.chainId) + of ModelRole.TransactionHashRole: + result = newQVariant(item.hash) + of ModelRole.AddressRole: + result = newQVariant(item.address) + + proc setItems*(self: Model, items: seq[backend.TransactionIdentity]) = + self.beginResetModel() + self.items = items + self.endResetModel() + self.countChanged() + diff --git a/src/backend/activity.nim b/src/backend/activity.nim index 9449b98de6..e2addcba5a 100644 --- a/src/backend/activity.nim +++ b/src/backend/activity.nim @@ -298,6 +298,8 @@ type transaction: Option[TransactionIdentity] id: int + transactions*: seq[TransactionIdentity] + timestamp*: int activityType*: ActivityType @@ -326,6 +328,8 @@ type transaction*: Option[TransactionIdentity] id*: Option[int] + transactions*: seq[TransactionIdentity] + timestamp*: Option[int] activityType*: Option[ActivityType] @@ -390,12 +394,18 @@ proc getMultiTransactionId*(ae: ActivityEntry): Option[int] = return none(int) return some(ae.id) +proc getTransactions*(ae: ActivityEntry): seq[TransactionIdentity] = + if ae.payloadType != PayloadType.MultiTransaction: + return @[] + return ae.transactions + proc toJson*(ae: ActivityEntry): JsonNode {.inline.} = return %*(ae) proc fromJson*(e: JsonNode, T: typedesc[Data]): Data {.inline.} = const transactionField = "transaction" const idField = "id" + const transactionsField = "transactions" const activityTypeField = "activityType" const activityStatusField = "activityStatus" const timestampField = "timestamp" @@ -421,6 +431,10 @@ proc fromJson*(e: JsonNode, T: typedesc[Data]): Data {.inline.} = else: none(TransactionIdentity), id: if e.hasKey(idField): some(e[idField].getInt()) else: none(int), + transactions: if e.hasKey(transactionsField): + fromJson(e[transactionsField], seq[TransactionIdentity]) + else: + @[], activityType: if e.hasKey(activityTypeField): some(fromJson(e[activityTypeField], ActivityType)) else: @@ -476,6 +490,7 @@ proc fromJson*(e: JsonNode, T: typedesc[ActivityEntry]): ActivityEntry {.inline. payloadType: data.payloadType, transaction: data.transaction, id: if data.id.isSome: data.id.get() else: 0, + transactions: data.transactions, activityType: data.activityType.get(), activityStatus: data.activityStatus.get(), timestamp: data.timestamp.get(), @@ -501,6 +516,7 @@ proc `$`*(self: ActivityEntry): string = payloadType:{$self.payloadType}, transaction:{transactionStr}, id:{self.id}, + transactions:{self.transactions}, timestamp:{self.timestamp}, activityType* {$self.activityType}, activityStatus* {$self.activityStatus}, @@ -559,7 +575,7 @@ proc fromJson*(e: JsonNode, T: typedesc[SessionUpdate]): T {.inline.} = removed: removed ) -rpc(startActivityFilterSession, "wallet"): +rpc(startActivityFilterSessionV2, "wallet"): addresses: seq[string] chainIds: seq[ChainId] filter: ActivityFilter @@ -589,7 +605,7 @@ proc newActivityFilterSession*( count: int, ): (int32, bool) {.inline.} = try: - let res = startActivityFilterSession(addresses, chainIds, filter, count) + let res = startActivityFilterSessionV2(addresses, chainIds, filter, count) if res.error != nil: error "error starting a new session of activity fitlering: ", res.error return (int32(-1), false)