feat(@wallet): collectible multi address

This commit is contained in:
Anthony Laibe 2023-05-09 17:09:58 +02:00 committed by Anthony Laibe
parent 92a9c41238
commit 9738a3eec8
8 changed files with 79 additions and 94 deletions

View File

@ -637,8 +637,9 @@ proc ownsCollectible*(self: Controller, chainId: int, contractAddress: string, t
let addresses = self.walletAccountService.getWalletAccounts().filter(a => a.walletType != WalletTypeWatch).map(a => a.address) let addresses = self.walletAccountService.getWalletAccounts().filter(a => a.walletType != WalletTypeWatch).map(a => a.address)
for address in addresses: for address in addresses:
let data = self.collectibleService.getOwnedCollectibles(chainId, address) let data = self.collectibleService.getOwnedCollectibles(chainId, @[address])
for collectible in data.collectibles:
for collectible in data[0].collectibles:
if collectible.id.contractAddress == contractAddress.toLowerAscii: if collectible.id.contractAddress == contractAddress.toLowerAscii:
return true return true

View File

@ -17,9 +17,6 @@ type
nodeService: node_service.Service nodeService: node_service.Service
networkConnectionService: network_connection_service.Service networkConnectionService: network_connection_service.Service
# Forward declaration
proc refetchOwnedCollectibles*(self: Controller, chainId: int, address: string)
proc newController*( proc newController*(
delegate: io_interface.AccessInterface, delegate: io_interface.AccessInterface,
events: EventEmitter, events: EventEmitter,
@ -41,18 +38,14 @@ proc newController*(
proc delete*(self: Controller) = proc delete*(self: Controller) =
discard discard
proc resetCollectibles(self: Controller, chainId: int, address: string) =
let data = self.collectibleService.getOwnedCollectibles(chainId, address)
self.delegate.setCollectibles(chainId, address, data)
proc updateCollectibles(self: Controller, chainId: int, address: string) = proc updateCollectibles(self: Controller, chainId: int, address: string) =
let data = self.collectibleService.getOwnedCollectibles(chainId, address) let data = self.collectibleService.getOwnedCollectibles(chainId, @[address])
self.delegate.appendCollectibles(chainId, address, data) self.delegate.appendCollectibles(chainId, address, data[0])
proc init*(self: Controller) = proc init*(self: Controller) =
self.events.on(SIGNAL_OWNED_COLLECTIBLES_REFETCH) do(e:Args): self.events.on(SIGNAL_OWNED_COLLECTIBLES_REFETCH) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e) let args = OwnedCollectiblesUpdateArgs(e)
self.resetCollectibles(args.chainId, args.address) self.delegate.resetCollectibles()
self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_ERROR) do(e:Args): self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_ERROR) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e) let args = OwnedCollectiblesUpdateArgs(e)
@ -69,20 +62,14 @@ proc init*(self: Controller) =
self.events.on(SIGNAL_REFRESH_COLLECTIBLES) do(e:Args): self.events.on(SIGNAL_REFRESH_COLLECTIBLES) do(e:Args):
self.collectibleService.refetchAllOwnedCollectibles() self.collectibleService.refetchAllOwnedCollectibles()
proc getWalletAccountByAddress*(self: Controller, address: string): wallet_account_service.WalletAccountDto =
return self.walletAccountService.getAccountByAddress(address)
proc getNetwork*(self: Controller): network_service.NetworkDto = proc getNetwork*(self: Controller): network_service.NetworkDto =
return self.networkService.getNetworkForCollectibles() return self.networkService.getNetworkForCollectibles()
proc getOwnedCollectibles*(self: Controller, chainId: int, address: string): CollectiblesData = proc getOwnedCollectibles*(self: Controller, chainId: int, addresses: seq[string]): seq[CollectiblesData] =
return self.collectibleService.getOwnedCollectibles(chainId, address) return self.collectibleService.getOwnedCollectibles(chainId, addresses)
proc refetchOwnedCollectibles*(self: Controller, chainId: int, address: string) = proc fetchOwnedCollectibles*(self: Controller, chainId: int, addresses: seq[string]) =
self.collectibleService.refetchOwnedCollectibles(chainId, address) self.collectibleService.fetchOwnedCollectibles(chainId, addresses)
proc fetchOwnedCollectibles*(self: Controller, chainId: int, address: string) =
self.collectibleService.fetchOwnedCollectibles(chainId, address)
proc getCollectible*(self: Controller, chainId: int, id: UniqueID) : CollectibleDto = proc getCollectible*(self: Controller, chainId: int, id: UniqueID) : CollectibleDto =
self.collectibleService.getCollectible(chainId, id) self.collectibleService.getCollectible(chainId, id)

View File

@ -9,7 +9,6 @@ type
delegate: io_interface.AccessInterface delegate: io_interface.AccessInterface
collectibleService: collectible_service.Service collectibleService: collectible_service.Service
network: network_dto.NetworkDto network: network_dto.NetworkDto
address: string
proc newController*( proc newController*(
delegate: io_interface.AccessInterface, delegate: io_interface.AccessInterface,
@ -25,9 +24,8 @@ proc delete*(self: Controller) =
proc init*(self: Controller) = proc init*(self: Controller) =
discard discard
method setCurrentAddress*(self: Controller, network: network_dto.NetworkDto, address: string) = method setCurrentNetwork*(self: Controller, network: network_dto.NetworkDto) =
self.network = network self.network = network
self.address = address
proc update*(self: Controller, id: collectible_service.UniqueID) = proc update*(self: Controller, id: collectible_service.UniqueID) =
let collectible = self.collectibleService.getCollectible(self.network.chainId, id) let collectible = self.collectibleService.getCollectible(self.network.chainId, id)

View File

@ -15,7 +15,7 @@ method load*(self: AccessInterface) {.base.} =
method isLoaded*(self: AccessInterface): bool {.base.} = method isLoaded*(self: AccessInterface): bool {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method setCurrentAddress*(self: AccessInterface, network: network_dto.NetworkDto, address: string) {.base.} = method setCurrentNetwork*(self: AccessInterface, network: network_dto.NetworkDto) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method update*(self: AccessInterface, address: string, tokenId: Uint256) {.base.} = method update*(self: AccessInterface, address: string, tokenId: Uint256) {.base.} =

View File

@ -45,8 +45,8 @@ method viewDidLoad*(self: Module) =
self.moduleLoaded = true self.moduleLoaded = true
self.delegate.currentCollectibleModuleDidLoad() self.delegate.currentCollectibleModuleDidLoad()
method setCurrentAddress*(self: Module, network: network_dto.NetworkDto, address: string) = method setCurrentNetwork*(self: Module, network: network_dto.NetworkDto) =
self.controller.setCurrentAddress(network, address) self.controller.setCurrentNetwork(network)
method update*(self: Module, address: string, tokenId: Uint256) = method update*(self: Module, address: string, tokenId: Uint256) =
let id = collectible_dto.UniqueID( let id = collectible_dto.UniqueID(

View File

@ -22,12 +22,15 @@ method fetchOwnedCollectibles*(self: AccessInterface) {.base.} =
method onFetchStarted*(self: AccessInterface, chainId: int, address: string) {.base.} = method onFetchStarted*(self: AccessInterface, chainId: int, address: string) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method setCollectibles*(self: AccessInterface, chainId: int, address: string, data: CollectiblesData) {.base.} = method setCollectibles*(self: AccessInterface, data: seq[CollectiblesData]) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method appendCollectibles*(self: AccessInterface, chainId: int, address: string, data: CollectiblesData) {.base.} = method appendCollectibles*(self: AccessInterface, chainId: int, address: string, data: CollectiblesData) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method resetCollectibles*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method viewDidLoad*(self: AccessInterface) {.base.} = method viewDidLoad*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")

View File

@ -28,7 +28,7 @@ type
moduleLoaded: bool moduleLoaded: bool
chainId: int chainId: int
address: string addresses: seq[string]
currentCollectibleModule: current_collectible_module.AccessInterface currentCollectibleModule: current_collectible_module.AccessInterface
@ -47,7 +47,7 @@ proc newModule*(
result.controller = newController(result, events, collectibleService, walletAccountService, networkService, nodeService, networkConnectionService) result.controller = newController(result, events, collectibleService, walletAccountService, networkService, nodeService, networkConnectionService)
result.moduleLoaded = false result.moduleLoaded = false
result.chainId = 0 result.chainId = 0
result.address = "" result.addresses = @[]
result.currentCollectibleModule = currentCollectibleModule.newModule(result, collectibleService) result.currentCollectibleModule = currentCollectibleModule.newModule(result, collectibleService)
method delete*(self: Module) = method delete*(self: Module) =
@ -81,22 +81,21 @@ method currentCollectibleModuleDidLoad*(self: Module) =
self.checkIfModuleDidLoad() self.checkIfModuleDidLoad()
method fetchOwnedCollectibles*(self: Module) = method fetchOwnedCollectibles*(self: Module) =
self.controller.fetchOwnedCollectibles(self.chainId, self.address) self.controller.fetchOwnedCollectibles(self.chainId, self.addresses)
method filterChanged*(self: Module, addresses: seq[string], chainIds: seq[int]) = method filterChanged*(self: Module, addresses: seq[string], chainIds: seq[int]) =
let network = self.controller.getNetwork() let network = self.controller.getNetwork()
let account = self.controller.getWalletAccountByAddress(addresses[0])
self.chainId = network.chainId self.chainId = network.chainId
self.address = account.address self.addresses = addresses
self.currentCollectibleModule.setCurrentAddress(network, self.address) self.currentCollectibleModule.setCurrentNetwork(network)
self.view.setCollectibles(@[])
let data = self.controller.getOwnedCollectibles(self.chainId, self.addresses)
let data = self.controller.getOwnedCollectibles(self.chainId, self.address) for i, addressData in data.pairs:
if not addressData.anyLoaded:
self.controller.fetchOwnedCollectibles(self.chainId, @[self.addresses[i]])
# Trigger a fetch the first time we switch to an account self.setCollectibles(data)
if not data.anyLoaded:
self.controller.fetchOwnedCollectibles(self.chainId, self.address)
self.setCollectibles(self.chainId, self.address, data)
proc ownedCollectibleToItem(self: Module, oc: OwnedCollectible): Item = proc ownedCollectibleToItem(self: Module, oc: OwnedCollectible): Item =
let c = self.controller.getCollectible(self.chainId, oc.id) let c = self.controller.getCollectible(self.chainId, oc.id)
@ -104,45 +103,40 @@ proc ownedCollectibleToItem(self: Module, oc: OwnedCollectible): Item =
return collectibleToItem(c, col, oc.isFromWatchedContract) return collectibleToItem(c, col, oc.isFromWatchedContract)
method onFetchStarted*(self: Module, chainId: int, address: string) = method onFetchStarted*(self: Module, chainId: int, address: string) =
if self.chainId == chainId and self.address == address: if self.chainId == chainId and address in self.addresses:
self.view.setIsFetching(true) self.view.setIsFetching(true)
method setCollectibles*(self: Module, chainId: int, address: string, data: CollectiblesData) = method resetCollectibles*(self: Module) =
if self.chainId == chainId and self.address == address: let data = self.controller.getOwnedCollectibles(self.chainId, self.addresses)
self.view.setIsError(data.isError) self.setCollectibles(data)
if data.isError and not data.anyLoaded:
# If fetching failed before being able to get any collectibles info,
# show loading animation
self.view.setIsFetching(true)
else:
self.view.setIsFetching(data.isFetching)
var newCollectibles = data.collectibles.map(oc => self.ownedCollectibleToItem(oc))
self.view.setCollectibles(newCollectibles)
self.view.setAllLoaded(data.allLoaded)
method appendCollectibles*(self: Module, chainId: int, address: string, data: CollectiblesData) = method appendCollectibles*(self: Module, chainId: int, address: string, data: CollectiblesData) =
if self.chainId == chainId and self.address == address: if not (self.chainId == chainId and address in self.addresses):
self.view.setIsError(data.isError) return
if data.isError and not data.anyLoaded: self.view.setIsError(data.isError)
# If fetching failed before being able to get any collectibles info,
# show loading animation
self.view.setIsFetching(true)
else:
self.view.setIsFetching(data.isFetching)
if data.lastLoadCount > 0: if data.isError and not data.anyLoaded:
var ownedCollectiblesToAdd = newSeq[OwnedCollectible]() # If fetching failed before being able to get any collectibles info,
for i in data.collectibles.len - data.lastLoadCount ..< data.collectibles.len: # show loading animation
ownedCollectiblesToAdd.add(data.collectibles[i]) self.view.setIsFetching(true)
else:
self.view.setIsFetching(data.isFetching)
let newCollectibles = ownedCollectiblesToAdd.map(oc => self.ownedCollectibleToItem(oc)) if data.lastLoadCount > 0:
var ownedCollectiblesToAdd = newSeq[OwnedCollectible]()
for i in data.collectibles.len - data.lastLoadCount ..< data.collectibles.len:
ownedCollectiblesToAdd.add(data.collectibles[i])
self.view.appendCollectibles(newCollectibles) let newCollectibles = ownedCollectiblesToAdd.map(oc => self.ownedCollectibleToItem(oc))
self.view.setAllLoaded(data.allLoaded) self.view.appendCollectibles(newCollectibles)
self.view.setAllLoaded(data.allLoaded)
method setCollectibles*(self: Module, data: seq[CollectiblesData]) =
for index, address in self.addresses:
self.appendCollectibles(self.chainId, address, data[index])
method getHasCollectiblesCache*(self: Module): bool = method getHasCollectiblesCache*(self: Module): bool =
return self.controller.getHasCollectiblesCache(self.address) return self.controller.getHasCollectiblesCache(self.addresses[0])

View File

@ -307,9 +307,10 @@ QtObject:
# Re-fetch # Re-fetch
self.refetchOwnedCollectibles(chainId, address) self.refetchOwnedCollectibles(chainId, address)
proc getOwnedCollectibles*(self: Service, chainId: int, address: string) : CollectiblesData = proc getOwnedCollectibles*(self: Service, chainId: int, addresses: seq[string]) : seq[CollectiblesData] =
self.prepareOwnershipData(chainId, address) for address in addresses:
return self.accountsOwnershipData[chainId][address].data self.prepareOwnershipData(chainId, address)
result.add(self.accountsOwnershipData[chainId][address].data)
proc getCollectible*(self: Service, chainId: int, id: UniqueID) : CollectibleDto = proc getCollectible*(self: Service, chainId: int, id: UniqueID) : CollectibleDto =
try: try:
@ -466,28 +467,29 @@ QtObject:
) )
self.threadpool.start(arg) self.threadpool.start(arg)
proc fetchOwnedCollectibles*(self: Service, chainId: int, address: string, limit: int = ownedCollectiblesFetchLimit) = proc fetchOwnedCollectibles*(self: Service, chainId: int, addresses: seq[string], limit: int = ownedCollectiblesFetchLimit) =
self.prepareOwnershipData(chainId, address) for address in addresses:
self.prepareOwnershipData(chainId, address)
let ownershipData = self.accountsOwnershipData[chainId][address] let ownershipData = self.accountsOwnershipData[chainId][address]
let watchedContractAddresses = ownershipData.watchedContractAddresses let watchedContractAddresses = ownershipData.watchedContractAddresses
let collectiblesData = ownershipData.data let collectiblesData = ownershipData.data
let state = collectiblesData.state let state = collectiblesData.state
if state == State.Init and len(watchedContractAddresses) > 0: if state == State.Init and len(watchedContractAddresses) > 0:
collectiblesData.state = State.WatchedContractsLoading collectiblesData.state = State.WatchedContractsLoading
self.fetchOwnedCollectiblesFromWatchedContracts(chainId, address) self.fetchOwnedCollectiblesFromWatchedContracts(chainId, address)
elif state == State.Init or state == State.WatchedContractsLoaded or state == State.ChunkLoaded: elif state == State.Init or state == State.WatchedContractsLoaded or state == State.ChunkLoaded:
collectiblesData.state = State.ChunkLoading collectiblesData.state = State.ChunkLoading
self.fetchNextOwnedCollectiblesChunk(chainId, address) self.fetchNextOwnedCollectiblesChunk(chainId, address)
else: else:
return return
var data = OwnedCollectiblesUpdateArgs() var data = OwnedCollectiblesUpdateArgs()
data.chainId = chainId data.chainId = chainId
data.address = address data.address = address
self.events.emit(SIGNAL_OWNED_COLLECTIBLES_UPDATE_STARTED, data) self.events.emit(SIGNAL_OWNED_COLLECTIBLES_UPDATE_STARTED, data)
proc refetchOwnedCollectibles*(self: Service, chainId: int, address: string) = proc refetchOwnedCollectibles*(self: Service, chainId: int, address: string) =
self.prepareOwnershipData(chainId, address) self.prepareOwnershipData(chainId, address)
@ -500,7 +502,7 @@ QtObject:
data.address = address data.address = address
self.events.emit(SIGNAL_OWNED_COLLECTIBLES_REFETCH, data) self.events.emit(SIGNAL_OWNED_COLLECTIBLES_REFETCH, data)
self.fetchOwnedCollectibles(chainId, address) self.fetchOwnedCollectibles(chainId, @[address])
proc refetchAllOwnedCollectibles*(self: Service) = proc refetchAllOwnedCollectibles*(self: Service) =
for chainId, addressesData in self.accountsOwnershipData: for chainId, addressesData in self.accountsOwnershipData: