feat(activity): adapt to activityV2 API

This commit is contained in:
Dario Gabriel Lipicar 2024-11-15 02:03:24 -03:00 committed by dlipicar
parent b8a0ab0e0a
commit bde7507a46
4 changed files with 119 additions and 5 deletions

View File

@ -12,6 +12,8 @@ import app/modules/shared/wallet_utils
import web3/ethtypes as eth import web3/ethtypes as eth
import ./transaction_identities_model as txid
# Additional data needed to build an Entry, which is # Additional data needed to build an Entry, which is
# not included in the metadata and needs to be # not included in the metadata and needs to be
# fetched from a different source. # fetched from a different source.
@ -27,6 +29,8 @@ QtObject:
metadata: backend.ActivityEntry metadata: backend.ActivityEntry
extradata: ExtraData extradata: ExtraData
transactions: txid.Model
amountCurrency: CurrencyAmount amountCurrency: CurrencyAmount
noAmount: CurrencyAmount noAmount: CurrencyAmount
@ -57,8 +61,11 @@ QtObject:
new(result, delete) new(result, delete)
result.metadata = metadata result.metadata = metadata
result.extradata = extradata result.extradata = extradata
result.noAmount = newCurrencyAmount()
result.transactions = txid.newModel()
result.transactions.setItems(metadata.transactions)
result.noAmount = newCurrencyAmount()
result.amountCurrency = result.extractCurrencyAmount(currencyService) result.amountCurrency = result.extractCurrencyAmount(currencyService)
result.highlight = metadata.isNew result.highlight = metadata.isNew
@ -69,6 +76,7 @@ QtObject:
new(result, delete) new(result, delete)
result.metadata = metadata result.metadata = metadata
result.extradata = extradata result.extradata = extradata
result.transactions = txid.newModel()
result.noAmount = newCurrencyAmount() result.noAmount = newCurrencyAmount()
result.amountCurrency = result.extractCurrencyAmount(currencyService) result.amountCurrency = result.extractCurrencyAmount(currencyService)
@ -127,6 +135,8 @@ QtObject:
proc `$`*(self: ActivityEntry): string = proc `$`*(self: ActivityEntry): string =
return fmt"""ActivityEntry( return fmt"""ActivityEntry(
metadata:{$self.metadata}, metadata:{$self.metadata},
extradata:{$self.extradata},
transactions:{$self.transactions},
)""" )"""
# TODO: is this the right way to pass transaction identity? Why not use the instance? # TODO: is this the right way to pass transaction identity? Why not use the instance?
@ -138,6 +148,12 @@ QtObject:
QtProperty[string] id: QtProperty[string] id:
read = getId read = getId
proc getTransactions*(self: ActivityEntry): QVariant {.slot.} =
return newQVariant(self.transactions)
QtProperty[QVariant] transactions:
read = getTransactions
proc getMetadata*(self: ActivityEntry): backend.ActivityEntry = proc getMetadata*(self: ActivityEntry): backend.ActivityEntry =
return self.metadata return self.metadata
@ -179,11 +195,18 @@ QtObject:
QtProperty[int] timestamp: QtProperty[int] timestamp:
read = getTimestamp read = getTimestamp
proc statusChanged*(self: ActivityEntry) {.signal.}
proc getStatus*(self: ActivityEntry): int {.slot.} = proc getStatus*(self: ActivityEntry): int {.slot.} =
return self.metadata.activityStatus.int return self.metadata.activityStatus.int
proc setStatus*(self: ActivityEntry, status: ActivityStatus) =
self.metadata.activityStatus = status
self.statusChanged()
QtProperty[int] status: QtProperty[int] status:
read = getStatus read = getStatus
notify = statusChanged
proc getChainIdIn*(self: ActivityEntry): int {.slot.} = proc getChainIdIn*(self: ActivityEntry): int {.slot.} =
return self.metadata.chainIdIn.get(ChainId(0)).int return self.metadata.chainIdIn.get(ChainId(0)).int

View File

@ -5,7 +5,6 @@ import ./entry
import app/modules/shared_models/currency_amount import app/modules/shared_models/currency_amount
import app_service/service/currency/service import app_service/service/currency/service
import backend/activity as backend import backend/activity as backend
import backend/backend as importing_transactionidentity_comp
type type
ModelRole {.pure.} = enum ModelRole {.pure.} = enum
@ -108,7 +107,8 @@ QtObject:
return false return false
if m.getPayloadType() == MultiTransaction: 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() 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()) self.entries[i].setNftName(updates[j].nftName.get())
if updates[j].nftUrl.isSome(): if updates[j].nftUrl.isSome():
self.entries[i].setNftImageUrl(updates[j].nftUrl.get()) self.entries[i].setNftImageUrl(updates[j].nftUrl.get())
if updates[j].activityStatus.isSome():
self.entries[i].setStatus(updates[j].activityStatus.get())
break break
proc getHasMore*(self: Model): bool {.slot.} = proc getHasMore*(self: Model): bool {.slot.} =

View File

@ -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()

View File

@ -298,6 +298,8 @@ type
transaction: Option[TransactionIdentity] transaction: Option[TransactionIdentity]
id: int id: int
transactions*: seq[TransactionIdentity]
timestamp*: int timestamp*: int
activityType*: ActivityType activityType*: ActivityType
@ -326,6 +328,8 @@ type
transaction*: Option[TransactionIdentity] transaction*: Option[TransactionIdentity]
id*: Option[int] id*: Option[int]
transactions*: seq[TransactionIdentity]
timestamp*: Option[int] timestamp*: Option[int]
activityType*: Option[ActivityType] activityType*: Option[ActivityType]
@ -390,12 +394,18 @@ proc getMultiTransactionId*(ae: ActivityEntry): Option[int] =
return none(int) return none(int)
return some(ae.id) 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.} = proc toJson*(ae: ActivityEntry): JsonNode {.inline.} =
return %*(ae) return %*(ae)
proc fromJson*(e: JsonNode, T: typedesc[Data]): Data {.inline.} = proc fromJson*(e: JsonNode, T: typedesc[Data]): Data {.inline.} =
const transactionField = "transaction" const transactionField = "transaction"
const idField = "id" const idField = "id"
const transactionsField = "transactions"
const activityTypeField = "activityType" const activityTypeField = "activityType"
const activityStatusField = "activityStatus" const activityStatusField = "activityStatus"
const timestampField = "timestamp" const timestampField = "timestamp"
@ -421,6 +431,10 @@ proc fromJson*(e: JsonNode, T: typedesc[Data]): Data {.inline.} =
else: else:
none(TransactionIdentity), none(TransactionIdentity),
id: if e.hasKey(idField): some(e[idField].getInt()) else: none(int), 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): activityType: if e.hasKey(activityTypeField):
some(fromJson(e[activityTypeField], ActivityType)) some(fromJson(e[activityTypeField], ActivityType))
else: else:
@ -476,6 +490,7 @@ proc fromJson*(e: JsonNode, T: typedesc[ActivityEntry]): ActivityEntry {.inline.
payloadType: data.payloadType, payloadType: data.payloadType,
transaction: data.transaction, transaction: data.transaction,
id: if data.id.isSome: data.id.get() else: 0, id: if data.id.isSome: data.id.get() else: 0,
transactions: data.transactions,
activityType: data.activityType.get(), activityType: data.activityType.get(),
activityStatus: data.activityStatus.get(), activityStatus: data.activityStatus.get(),
timestamp: data.timestamp.get(), timestamp: data.timestamp.get(),
@ -501,6 +516,7 @@ proc `$`*(self: ActivityEntry): string =
payloadType:{$self.payloadType}, payloadType:{$self.payloadType},
transaction:{transactionStr}, transaction:{transactionStr},
id:{self.id}, id:{self.id},
transactions:{self.transactions},
timestamp:{self.timestamp}, timestamp:{self.timestamp},
activityType* {$self.activityType}, activityType* {$self.activityType},
activityStatus* {$self.activityStatus}, activityStatus* {$self.activityStatus},
@ -559,7 +575,7 @@ proc fromJson*(e: JsonNode, T: typedesc[SessionUpdate]): T {.inline.} =
removed: removed removed: removed
) )
rpc(startActivityFilterSession, "wallet"): rpc(startActivityFilterSessionV2, "wallet"):
addresses: seq[string] addresses: seq[string]
chainIds: seq[ChainId] chainIds: seq[ChainId]
filter: ActivityFilter filter: ActivityFilter
@ -589,7 +605,7 @@ proc newActivityFilterSession*(
count: int, count: int,
): (int32, bool) {.inline.} = ): (int32, bool) {.inline.} =
try: try:
let res = startActivityFilterSession(addresses, chainIds, filter, count) let res = startActivityFilterSessionV2(addresses, chainIds, filter, count)
if res.error != nil: if res.error != nil:
error "error starting a new session of activity fitlering: ", res.error error "error starting a new session of activity fitlering: ", res.error
return (int32(-1), false) return (int32(-1), false)