feat(@desktop/Wallet): adapt to updated collectible backend data types
Fixes #12942
This commit is contained in:
parent
4e66ca7936
commit
00d78b6710
|
@ -5,6 +5,8 @@ import backend/collectibles as backend
|
||||||
import collectible_trait_model
|
import collectible_trait_model
|
||||||
import ../../../app_service/service/community_tokens/dto/community_token
|
import ../../../app_service/service/community_tokens/dto/community_token
|
||||||
|
|
||||||
|
const invalidTimestamp* = high(int)
|
||||||
|
|
||||||
# Additional data needed to build an Entry, which is
|
# Additional data needed to build an Entry, which is
|
||||||
# not included in the backend data and needs to be
|
# not included in the backend data and needs to be
|
||||||
# fetched from a different source.
|
# fetched from a different source.
|
||||||
|
@ -91,6 +93,12 @@ QtObject:
|
||||||
proc getCommunityData(self: CollectiblesEntry): backend.CommunityData =
|
proc getCommunityData(self: CollectiblesEntry): backend.CommunityData =
|
||||||
return self.data.communityData.get()
|
return self.data.communityData.get()
|
||||||
|
|
||||||
|
proc hasOwnership(self: CollectiblesEntry): bool =
|
||||||
|
return self.data != nil and isSome(self.data.ownership)
|
||||||
|
|
||||||
|
proc getOwnership(self: CollectiblesEntry): seq[backend.AccountBalance] =
|
||||||
|
return self.data.ownership.get()
|
||||||
|
|
||||||
proc getChainID*(self: CollectiblesEntry): int {.slot.} =
|
proc getChainID*(self: CollectiblesEntry): int {.slot.} =
|
||||||
return self.id.contractID.chainID
|
return self.id.contractID.chainID
|
||||||
|
|
||||||
|
@ -233,6 +241,33 @@ QtObject:
|
||||||
read = getTraits
|
read = getTraits
|
||||||
notify = traitsChanged
|
notify = traitsChanged
|
||||||
|
|
||||||
|
proc balanceChanged*(self: CollectiblesEntry) {.signal.}
|
||||||
|
proc getBalance*(self: CollectiblesEntry): UInt256 =
|
||||||
|
var balance: UInt256 = stint.u256(0)
|
||||||
|
if self.hasOwnership():
|
||||||
|
for item in self.getOwnership():
|
||||||
|
balance += item.balance
|
||||||
|
return balance
|
||||||
|
proc getBalanceAsString*(self: CollectiblesEntry): string {.slot.} =
|
||||||
|
return $self.getBalance()
|
||||||
|
|
||||||
|
QtProperty[string] balance:
|
||||||
|
read = getBalanceAsString
|
||||||
|
notify = balanceChanged
|
||||||
|
|
||||||
|
proc lastTxTimestampChanged*(self: CollectiblesEntry) {.signal.}
|
||||||
|
proc getLastTxTimestamp*(self: CollectiblesEntry): int =
|
||||||
|
var lastTxTimestamp = -1
|
||||||
|
if self.hasOwnership():
|
||||||
|
for item in self.getOwnership():
|
||||||
|
lastTxTimestamp = max(lastTxTimestamp, item.txTimestamp)
|
||||||
|
if lastTxTimestamp < 0:
|
||||||
|
lastTxTimestamp = invalidTimestamp
|
||||||
|
return lastTxTimestamp
|
||||||
|
|
||||||
|
QtProperty[int] lastTxTimestamp:
|
||||||
|
read = getLastTxTimestamp
|
||||||
|
notify = lastTxTimestampChanged
|
||||||
|
|
||||||
proc communityIdChanged*(self: CollectiblesEntry) {.signal.}
|
proc communityIdChanged*(self: CollectiblesEntry) {.signal.}
|
||||||
proc getCommunityID*(self: CollectiblesEntry): string {.slot.} =
|
proc getCommunityID*(self: CollectiblesEntry): string {.slot.} =
|
||||||
|
@ -320,6 +355,8 @@ QtObject:
|
||||||
self.collectionNameChanged()
|
self.collectionNameChanged()
|
||||||
self.collectionImageUrlChanged()
|
self.collectionImageUrlChanged()
|
||||||
self.traitsChanged()
|
self.traitsChanged()
|
||||||
|
self.balanceChanged()
|
||||||
|
self.lastTxTimestampChanged()
|
||||||
self.communityIdChanged()
|
self.communityIdChanged()
|
||||||
self.communityNameChanged()
|
self.communityNameChanged()
|
||||||
self.communityColorChanged()
|
self.communityColorChanged()
|
||||||
|
|
|
@ -23,6 +23,8 @@ type
|
||||||
CollectionName
|
CollectionName
|
||||||
CollectionSlug
|
CollectionSlug
|
||||||
IsLoading
|
IsLoading
|
||||||
|
Balance
|
||||||
|
LastTxTimestamp
|
||||||
# Community-related roles
|
# Community-related roles
|
||||||
CommunityId
|
CommunityId
|
||||||
CommunityName
|
CommunityName
|
||||||
|
@ -160,6 +162,8 @@ QtObject:
|
||||||
CollectibleRole.CollectionName.int:"collectionName",
|
CollectibleRole.CollectionName.int:"collectionName",
|
||||||
CollectibleRole.CollectionSlug.int:"collectionSlug",
|
CollectibleRole.CollectionSlug.int:"collectionSlug",
|
||||||
CollectibleRole.IsLoading.int:"isLoading",
|
CollectibleRole.IsLoading.int:"isLoading",
|
||||||
|
CollectibleRole.Balance.int:"balance",
|
||||||
|
CollectibleRole.LastTxTimestamp.int:"lastTxTimestamp",
|
||||||
CollectibleRole.CommunityId.int:"communityId",
|
CollectibleRole.CommunityId.int:"communityId",
|
||||||
CollectibleRole.CommunityName.int:"communityName",
|
CollectibleRole.CommunityName.int:"communityName",
|
||||||
CollectibleRole.CommunityColor.int:"communityColor",
|
CollectibleRole.CommunityColor.int:"communityColor",
|
||||||
|
@ -204,6 +208,10 @@ QtObject:
|
||||||
result = newQVariant(item.getCollectionSlug())
|
result = newQVariant(item.getCollectionSlug())
|
||||||
of CollectibleRole.IsLoading:
|
of CollectibleRole.IsLoading:
|
||||||
result = newQVariant(false)
|
result = newQVariant(false)
|
||||||
|
of CollectibleRole.Balance:
|
||||||
|
result = newQVariant(item.getBalanceAsString())
|
||||||
|
of CollectibleRole.LastTxTimestamp:
|
||||||
|
result = newQVariant(item.getLastTxTimestamp())
|
||||||
of CollectibleRole.CommunityId:
|
of CollectibleRole.CommunityId:
|
||||||
result = newQVariant(item.getCommunityId())
|
result = newQVariant(item.getCommunityId())
|
||||||
of CollectibleRole.CommunityName:
|
of CollectibleRole.CommunityName:
|
||||||
|
@ -239,6 +247,8 @@ QtObject:
|
||||||
of "collectionName": result = item.getCollectionName()
|
of "collectionName": result = item.getCollectionName()
|
||||||
of "collectionSlug": result = item.getCollectionSlug()
|
of "collectionSlug": result = item.getCollectionSlug()
|
||||||
of "isLoading": result = $false
|
of "isLoading": result = $false
|
||||||
|
of "balance": result = item.getBalanceAsString()
|
||||||
|
of "lastTxTimestamp": result = $item.getLastTxTimestamp()
|
||||||
of "communityId": result = item.getCommunityID()
|
of "communityId": result = item.getCommunityID()
|
||||||
of "communityName": result = item.getCommunityName()
|
of "communityName": result = item.getCommunityName()
|
||||||
of "communityColor": result = item.getCommunityColor()
|
of "communityColor": result = item.getCommunityColor()
|
||||||
|
|
|
@ -85,7 +85,7 @@ QtObject:
|
||||||
for chainID in self.chainIds:
|
for chainID in self.chainIds:
|
||||||
self.ownershipStatus[address][chainID] = OwnershipStatus(
|
self.ownershipStatus[address][chainID] = OwnershipStatus(
|
||||||
state: OwnershipStateUpdating,
|
state: OwnershipStateUpdating,
|
||||||
timestamp: invalidTimestamp
|
timestamp: backend_collectibles.invalidTimestamp
|
||||||
)
|
)
|
||||||
self.model.setIsUpdating(true)
|
self.model.setIsUpdating(true)
|
||||||
|
|
||||||
|
@ -185,13 +185,15 @@ QtObject:
|
||||||
self.setOwnershipState(address, chainID, OwnershipStateUpdating)
|
self.setOwnershipState(address, chainID, OwnershipStateUpdating)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.eventsHandler.onCollectiblesOwnershipUpdatePartial(proc (address: string, chainID: int) =
|
self.eventsHandler.onCollectiblesOwnershipUpdatePartial(proc (address: string, chainID: int, changes: backend_collectibles.OwnershipUpdateMessage) =
|
||||||
self.setOwnershipState(address, chainID, OwnershipStateUpdating)
|
self.setOwnershipState(address, chainID, OwnershipStateUpdating)
|
||||||
|
if changes.hasChanges():
|
||||||
self.resetModel()
|
self.resetModel()
|
||||||
)
|
)
|
||||||
|
|
||||||
self.eventsHandler.onCollectiblesOwnershipUpdateFinished(proc (address: string, chainID: int) =
|
self.eventsHandler.onCollectiblesOwnershipUpdateFinished(proc (address: string, chainID: int, changes: backend_collectibles.OwnershipUpdateMessage) =
|
||||||
self.setOwnershipState(address, chainID, OwnershipStateIdle)
|
self.setOwnershipState(address, chainID, OwnershipStateIdle)
|
||||||
|
if changes.hasChanges():
|
||||||
self.resetModel()
|
self.resetModel()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import backend/collectibles as backend_collectibles
|
||||||
type EventCallbackProc = proc (eventObject: JsonNode)
|
type EventCallbackProc = proc (eventObject: JsonNode)
|
||||||
type WalletEventCallbackProc = proc (data: WalletSignal)
|
type WalletEventCallbackProc = proc (data: WalletSignal)
|
||||||
type OwnershipUpdateCallbackProc = proc (address: string, chainID: int)
|
type OwnershipUpdateCallbackProc = proc (address: string, chainID: int)
|
||||||
|
type OwnershipUpdateChangesCallbackProc = proc (address: string, chainID: int, changes: backend_collectibles.OwnershipUpdateMessage)
|
||||||
|
|
||||||
# EventsHandler responsible for catching collectibles related backend events and reporting them
|
# EventsHandler responsible for catching collectibles related backend events and reporting them
|
||||||
QtObject:
|
QtObject:
|
||||||
|
@ -22,8 +23,8 @@ QtObject:
|
||||||
subscribedChainIDs: HashSet[int]
|
subscribedChainIDs: HashSet[int]
|
||||||
|
|
||||||
collectiblesOwnershipUpdateStartedFn: OwnershipUpdateCallbackProc
|
collectiblesOwnershipUpdateStartedFn: OwnershipUpdateCallbackProc
|
||||||
collectiblesOwnershipUpdatePartialFn: OwnershipUpdateCallbackProc
|
collectiblesOwnershipUpdatePartialFn: OwnershipUpdateChangesCallbackProc
|
||||||
collectiblesOwnershipUpdateFinishedFn: OwnershipUpdateCallbackProc
|
collectiblesOwnershipUpdateFinishedFn: OwnershipUpdateChangesCallbackProc
|
||||||
collectiblesOwnershipUpdateFinishedWithErrorFn: OwnershipUpdateCallbackProc
|
collectiblesOwnershipUpdateFinishedWithErrorFn: OwnershipUpdateCallbackProc
|
||||||
|
|
||||||
requestId: int32
|
requestId: int32
|
||||||
|
@ -43,10 +44,10 @@ QtObject:
|
||||||
proc onCollectiblesOwnershipUpdateStarted*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
proc onCollectiblesOwnershipUpdateStarted*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
||||||
self.collectiblesOwnershipUpdateStartedFn = handler
|
self.collectiblesOwnershipUpdateStartedFn = handler
|
||||||
|
|
||||||
proc onCollectiblesOwnershipUpdatePartial*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
proc onCollectiblesOwnershipUpdatePartial*(self: EventsHandler, handler: OwnershipUpdateChangesCallbackProc) =
|
||||||
self.collectiblesOwnershipUpdatePartialFn = handler
|
self.collectiblesOwnershipUpdatePartialFn = handler
|
||||||
|
|
||||||
proc onCollectiblesOwnershipUpdateFinished*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
proc onCollectiblesOwnershipUpdateFinished*(self: EventsHandler, handler: OwnershipUpdateChangesCallbackProc) =
|
||||||
self.collectiblesOwnershipUpdateFinishedFn = handler
|
self.collectiblesOwnershipUpdateFinishedFn = handler
|
||||||
|
|
||||||
proc onCollectiblesOwnershipUpdateFinishedWithError*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
proc onCollectiblesOwnershipUpdateFinishedWithError*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
|
||||||
|
@ -82,12 +83,16 @@ QtObject:
|
||||||
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdatePartial] = proc (data: WalletSignal) =
|
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdatePartial] = proc (data: WalletSignal) =
|
||||||
if self.collectiblesOwnershipUpdatePartialFn == nil or self.shouldIgnoreEvent(data):
|
if self.collectiblesOwnershipUpdatePartialFn == nil or self.shouldIgnoreEvent(data):
|
||||||
return
|
return
|
||||||
self.collectiblesOwnershipUpdatePartialFn(data.accounts[0], data.chainID)
|
let changesJson = parseJson(data.message)
|
||||||
|
let changes = fromJson(changesJson, backend_collectibles.OwnershipUpdateMessage)
|
||||||
|
self.collectiblesOwnershipUpdatePartialFn(data.accounts[0], data.chainID, changes)
|
||||||
|
|
||||||
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateFinished] = proc (data: WalletSignal) =
|
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateFinished] = proc (data: WalletSignal) =
|
||||||
if self.collectiblesOwnershipUpdateFinishedFn == nil or self.shouldIgnoreEvent(data):
|
if self.collectiblesOwnershipUpdateFinishedFn == nil or self.shouldIgnoreEvent(data):
|
||||||
return
|
return
|
||||||
self.collectiblesOwnershipUpdateFinishedFn(data.accounts[0], data.chainID)
|
let changesJson = parseJson(data.message)
|
||||||
|
let changes = fromJson(changesJson, backend_collectibles.OwnershipUpdateMessage)
|
||||||
|
self.collectiblesOwnershipUpdateFinishedFn(data.accounts[0], data.chainID, changes)
|
||||||
|
|
||||||
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateFinishedWithError] = proc (data: WalletSignal) =
|
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateFinishedWithError] = proc (data: WalletSignal) =
|
||||||
if self.collectiblesOwnershipUpdateFinishedWithErrorFn == nil or self.shouldIgnoreEvent(data):
|
if self.collectiblesOwnershipUpdateFinishedWithErrorFn == nil or self.shouldIgnoreEvent(data):
|
||||||
|
|
|
@ -46,6 +46,12 @@ type
|
||||||
state*: OwnershipState
|
state*: OwnershipState
|
||||||
timestamp*: int
|
timestamp*: int
|
||||||
|
|
||||||
|
# Mirrors services/wallet/collectibles/service.go OwnershipUpdateMessage
|
||||||
|
OwnershipUpdateMessage* = ref object
|
||||||
|
added*: seq[CollectibleUniqueID]
|
||||||
|
updated*: seq[CollectibleUniqueID]
|
||||||
|
removed*: seq[CollectibleUniqueID]
|
||||||
|
|
||||||
# Mirrors services/wallet/collectibles/service.go GetOwnedCollectiblesResponse
|
# Mirrors services/wallet/collectibles/service.go GetOwnedCollectiblesResponse
|
||||||
GetOwnedCollectiblesResponse* = object
|
GetOwnedCollectiblesResponse* = object
|
||||||
collectibles*: seq[Collectible]
|
collectibles*: seq[Collectible]
|
||||||
|
@ -95,6 +101,35 @@ proc fromJson*(t: JsonNode, T: typedesc[OwnershipStatus]): OwnershipStatus {.inl
|
||||||
timestamp: t{"timestamp"}.getInt
|
timestamp: t{"timestamp"}.getInt
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# OwnershipUpdateMessage
|
||||||
|
proc `$`*(self: OwnershipUpdateMessage): string =
|
||||||
|
return fmt"""OwnershipUpdateMessage(
|
||||||
|
added:{self.added},
|
||||||
|
updated:{self.updated},
|
||||||
|
removed:{self.removed}
|
||||||
|
"""
|
||||||
|
|
||||||
|
proc hasChanges*(self: OwnershipUpdateMessage): bool {.inline.} =
|
||||||
|
return self.added.len != 0 or self.updated.len != 0 or self.removed.len != 0
|
||||||
|
|
||||||
|
proc fromJson*(t: JsonNode, T: typedesc[OwnershipUpdateMessage]): OwnershipUpdateMessage {.inline.} =
|
||||||
|
var added: seq[CollectibleUniqueID]
|
||||||
|
var updated: seq[CollectibleUniqueID]
|
||||||
|
var removed: seq[CollectibleUniqueID]
|
||||||
|
|
||||||
|
for item in t["added"].getElems():
|
||||||
|
added.add(fromJson(item, CollectibleUniqueID))
|
||||||
|
for item in t["updated"].getElems():
|
||||||
|
updated.add(fromJson(item, CollectibleUniqueID))
|
||||||
|
for item in t["removed"].getElems():
|
||||||
|
removed.add(fromJson(item, CollectibleUniqueID))
|
||||||
|
|
||||||
|
return OwnershipUpdateMessage(
|
||||||
|
added: added,
|
||||||
|
updated: updated,
|
||||||
|
removed: removed
|
||||||
|
)
|
||||||
|
|
||||||
# CollectibleFilter
|
# CollectibleFilter
|
||||||
proc newCollectibleFilterAllCommunityIds*(): seq[string] {.inline.} =
|
proc newCollectibleFilterAllCommunityIds*(): seq[string] {.inline.} =
|
||||||
return @[]
|
return @[]
|
||||||
|
|
|
@ -45,12 +45,19 @@ type
|
||||||
privilegesLevel*: PrivilegesLevel
|
privilegesLevel*: PrivilegesLevel
|
||||||
imageUrl*: Option[string]
|
imageUrl*: Option[string]
|
||||||
|
|
||||||
|
# Mirrors services/wallet/thirdparty/collectible_types.go CollectibleOwner
|
||||||
|
AccountBalance* = ref object
|
||||||
|
address*: string
|
||||||
|
balance*: UInt256
|
||||||
|
txTimestamp*: int
|
||||||
|
|
||||||
Collectible* = ref object of RootObj
|
Collectible* = ref object of RootObj
|
||||||
dataType*: CollectibleDataType
|
dataType*: CollectibleDataType
|
||||||
id* : CollectibleUniqueID
|
id* : CollectibleUniqueID
|
||||||
collectibleData*: Option[CollectibleData]
|
collectibleData*: Option[CollectibleData]
|
||||||
collectionData*: Option[CollectionData]
|
collectionData*: Option[CollectionData]
|
||||||
communityData*: Option[CommunityData]
|
communityData*: Option[CommunityData]
|
||||||
|
ownership*: Option[seq[AccountBalance]]
|
||||||
|
|
||||||
# Mirrors services/wallet/thirdparty/collectible_types.go TokenBalance
|
# Mirrors services/wallet/thirdparty/collectible_types.go TokenBalance
|
||||||
CollectibleBalance* = ref object
|
CollectibleBalance* = ref object
|
||||||
|
@ -243,6 +250,24 @@ proc fromJson*(t: JsonNode, T: typedesc[ref CommunityData]): ref CommunityData {
|
||||||
result = new(CommunityData)
|
result = new(CommunityData)
|
||||||
result[] = fromJson(t, CommunityData)
|
result[] = fromJson(t, CommunityData)
|
||||||
|
|
||||||
|
# AccountBalance
|
||||||
|
proc `$`*(self: AccountBalance): string =
|
||||||
|
return fmt"""AccountBalance(
|
||||||
|
address:{self.address},
|
||||||
|
balance:{self.balance},
|
||||||
|
txTimestamp:{self.txTimestamp}
|
||||||
|
)"""
|
||||||
|
|
||||||
|
proc getAccountBalances(jsonAsset: JsonNode): seq[AccountBalance] =
|
||||||
|
var balanceList: seq[AccountBalance] = @[]
|
||||||
|
for item in jsonAsset.getElems():
|
||||||
|
balanceList.add(AccountBalance(
|
||||||
|
address: item{"address"}.getStr,
|
||||||
|
balance: stint.parse(item{"balance"}.getStr, Uint256),
|
||||||
|
txTimestamp: item{"txTimestamp"}.getInt
|
||||||
|
))
|
||||||
|
return balanceList
|
||||||
|
|
||||||
# Collectible
|
# Collectible
|
||||||
proc `$`*(self: Collectible): string =
|
proc `$`*(self: Collectible): string =
|
||||||
return fmt"""Collectible(
|
return fmt"""Collectible(
|
||||||
|
@ -250,7 +275,8 @@ proc `$`*(self: Collectible): string =
|
||||||
id:{self.id},
|
id:{self.id},
|
||||||
collectibleData:{self.collectibleData},
|
collectibleData:{self.collectibleData},
|
||||||
collectionData:{self.collectionData},
|
collectionData:{self.collectionData},
|
||||||
communityData:{self.communityData}
|
communityData:{self.communityData},
|
||||||
|
ownership:{self.ownership}
|
||||||
)"""
|
)"""
|
||||||
|
|
||||||
proc fromJson*(t: JsonNode, T: typedesc[Collectible]): Collectible {.inline.} =
|
proc fromJson*(t: JsonNode, T: typedesc[Collectible]): Collectible {.inline.} =
|
||||||
|
@ -272,6 +298,11 @@ proc fromJson*(t: JsonNode, T: typedesc[Collectible]): Collectible {.inline.} =
|
||||||
result.communityData = some(fromJson(communityDataNode, CommunityData))
|
result.communityData = some(fromJson(communityDataNode, CommunityData))
|
||||||
else:
|
else:
|
||||||
result.communityData = none(CommunityData)
|
result.communityData = none(CommunityData)
|
||||||
|
let ownershipNode = t{"ownership"}
|
||||||
|
if ownershipNode != nil and ownershipNode.kind != JNull:
|
||||||
|
result.ownership = some(getAccountBalances(ownershipNode))
|
||||||
|
else:
|
||||||
|
result.ownership = none(seq[AccountBalance])
|
||||||
|
|
||||||
proc toIds(self: seq[Collectible]): seq[CollectibleUniqueID] =
|
proc toIds(self: seq[Collectible]): seq[CollectibleUniqueID] =
|
||||||
result = @[]
|
result = @[]
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 70b2cab096af313ae6017d4ca9554574d440eb7a
|
Subproject commit ee6621b06610f0a3223cf875cc969abfa25750c9
|
Loading…
Reference in New Issue