Skip to content

Commit

Permalink
fix(@desktop/wallet): handle collectibles fetch error and account del…
Browse files Browse the repository at this point in the history
…etion

Part of #10063
  • Loading branch information
dlipicar committed Apr 3, 2023
1 parent 52bb597 commit 42139c1
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 160 deletions.
16 changes: 9 additions & 7 deletions src/app/modules/main/wallet_section/collectibles/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,29 @@ proc newController*(
proc delete*(self: Controller) =
discard

proc refreshCollectibles(self: Controller, chainId: int, address: string) =
proc resetCollectibles(self: Controller, chainId: int, address: string) =
let data = self.collectibleService.getOwnedCollectibles(chainId, address)
if not data.anyLoaded or data.lastLoadWasFromStart:
self.delegate.setCollectibles(chainId, address, data)
else:
self.delegate.appendCollectibles(chainId, address, data)
self.delegate.setCollectibles(chainId, address, data)

proc processNewCollectibles(self: Controller, chainId: int, address: string) =
let data = self.collectibleService.getOwnedCollectibles(chainId, address)
self.delegate.appendCollectibles(chainId, address, data)

if not self.nodeService.isConnected() or not self.networkConnectionService.checkIfConnected(COLLECTIBLES):
self.delegate.connectionToOpenSea(false)

proc init*(self: Controller) =
self.events.on(SIGNAL_OWNED_COLLECTIBLES_RESET) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e)
self.refreshCollectibles(args.chainId, args.address)
self.resetCollectibles(args.chainId, args.address)

self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_STARTED) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e)
self.delegate.onFetchStarted(args.chainId, args.address)

self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_FINISHED) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e)
self.refreshCollectibles(args.chainId, args.address)
self.processNewCollectibles(args.chainId, args.address)

self.events.on(SIGNAL_REFRESH_COLLECTIBLES) do(e:Args):
self.collectibleService.resetAllOwnedCollectibles()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ QtObject:
items: seq[Item]
allCollectiblesLoaded: bool
isFetching: bool
isError: bool

proc addLoadingItems(self: Model)
proc removeLoadingItems(self: Model)
Expand All @@ -46,6 +47,7 @@ QtObject:
result.items = @[]
result.allCollectiblesLoaded = false
result.isFetching = true
result.isError = false

proc `$`*(self: Model): string =
for i in 0 ..< self.items.len:
Expand Down Expand Up @@ -74,6 +76,18 @@ QtObject:
self.isFetching = value
self.isFetchingChanged()

proc isErrorChanged(self: Model) {.signal.}
proc getIsError*(self: Model): bool {.slot.} =
self.isError
QtProperty[bool] isError:
read = getIsError
notify = isErrorChanged
proc setIsError*(self: Model, value: bool) =
if value == self.isError:
return
self.isError = value
self.isErrorChanged()

proc allCollectiblesLoadedChanged(self: Model) {.signal.}
proc getAllCollectiblesLoaded*(self: Model): bool {.slot.} =
self.allCollectiblesLoaded
Expand All @@ -87,12 +101,11 @@ QtObject:
self.allCollectiblesLoadedChanged()

method canFetchMore*(self: Model, parent: QModelIndex): bool =
return not self.allCollectiblesLoaded and not self.isFetching
return not self.allCollectiblesLoaded and not self.isFetching and not self.isError

proc requestFetch(self: Model) {.signal.}
method fetchMore*(self: Model, parent: QModelIndex) =
if not self.isFetching:
self.requestFetch()
self.requestFetch()

method rowCount*(self: Model, index: QModelIndex = nil): int =
return self.items.len
Expand Down
16 changes: 10 additions & 6 deletions src/app/modules/main/wallet_section/collectibles/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,26 @@ method onFetchStarted*(self: Module, chainId: int, address: string) =
method setCollectibles*(self: Module, chainId: int, address: string, data: CollectiblesData) =
if self.chainId == chainId and self.address == address:
self.view.setIsFetching(data.isFetching)
self.view.setIsError(data.isError)

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) =
if self.chainId == chainId and self.address == address:
self.view.setIsFetching(data.isFetching)
self.view.setIsError(data.isError)

var ownedCollectiblesToAdd = newSeq[OwnedCollectible]()
for i in data.collectibles.len - data.lastLoadCount ..< data.collectibles.len:
ownedCollectiblesToAdd.add(data.collectibles[i])
if not data.isError:
var ownedCollectiblesToAdd = newSeq[OwnedCollectible]()
for i in data.collectibles.len - data.lastLoadCount ..< data.collectibles.len:
ownedCollectiblesToAdd.add(data.collectibles[i])

let newCollectibles = ownedCollectiblesToAdd.map(oc => self.ownedCollectibleToItem(oc))
let newCollectibles = ownedCollectiblesToAdd.map(oc => self.ownedCollectibleToItem(oc))

self.view.appendCollectibles(newCollectibles)
self.view.setAllLoaded(data.allLoaded)
self.view.appendCollectibles(newCollectibles)
self.view.setAllLoaded(data.allLoaded)

method connectionToOpenSea*(self: Module, connected: bool) =
self.view.connectionToOpenSea(connected)
3 changes: 3 additions & 0 deletions src/app/modules/main/wallet_section/collectibles/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ QtObject:
proc fetchMoreOwnedCollectibles*(self: View) {.slot.} =
self.delegate.fetchOwnedCollectibles()

proc setIsError*(self: View, isError: bool) =
self.model.setIsError(isError)

proc setIsFetching*(self: View, isFetching: bool) =
self.model.setIsFetching(isFetching)

Expand Down
72 changes: 44 additions & 28 deletions src/app_service/service/collectible/async_tasks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ type

const fetchOwnedCollectiblesTaskArg: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[FetchOwnedCollectiblesTaskArg](argEncoded)
let output = %* {
"chainId": arg.chainId,
"address": arg.address,
"cursor": arg.cursor,
"collectibles": {"assets":nil,"next":"","previous":""}
}
try:
let response = collectibles.getOpenseaAssetsByOwnerWithCursor(arg.chainId, arg.address, arg.cursor, arg.limit)
output["collectibles"] = response.result
let output = %* {
"chainId": arg.chainId,
"address": arg.address,
"cursor": arg.cursor,
"collectibles": response.result,
"error": ""
}
arg.finish(output)
except Exception as e:
let errDesription = e.msg
error "error fetchOwnedCollectiblesTaskArg: ", errDesription
arg.finish(output)
let output = %* {
"chainId": arg.chainId,
"address": arg.address,
"cursor": arg.cursor,
"collectibles": "",
"error": e.msg
}
arg.finish(output)

type
FetchOwnedCollectiblesFromContractAddressesTaskArg = ref object of QObjectTaskArg
Expand All @@ -31,19 +37,25 @@ type

const fetchOwnedCollectiblesFromContractAddressesTaskArg: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[FetchOwnedCollectiblesFromContractAddressesTaskArg](argEncoded)
let output = %* {
"chainId": arg.chainId,
"address": arg.address,
"cursor": arg.cursor,
"collectibles": ""
}
try:
let response = collectibles.getOpenseaAssetsByOwnerAndContractAddressWithCursor(arg.chainId, arg.address, arg.contractAddresses, arg.cursor, arg.limit)
output["collectibles"] = response.result
let output = %* {
"chainId": arg.chainId,
"address": arg.address,
"cursor": arg.cursor,
"collectibles": response.result,
"error": ""
}
arg.finish(output)
except Exception as e:
let errDesription = e.msg
error "error fetchOwnedCollectiblesFromContractAddressesTaskArg: ", errDesription
arg.finish(output)
let output = %* {
"chainId": arg.chainId,
"address": arg.address,
"cursor": arg.cursor,
"collectibles": "",
"error": e.msg
}
arg.finish(output)

type
FetchCollectiblesTaskArg = ref object of QObjectTaskArg
Expand All @@ -53,14 +65,18 @@ type

const fetchCollectiblesTaskArg: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[FetchCollectiblesTaskArg](argEncoded)
let output = %* {
"chainId": arg.chainId,
"collectibles": ""
}
try:
let response = collectibles.getOpenseaAssetsByNFTUniqueID(arg.chainId, arg.ids, arg.limit)
output["collectibles"] = response.result
let output = %* {
"chainId": arg.chainId,
"collectibles": response.result,
"error": ""
}
arg.finish(output)
except Exception as e:
let errDesription = e.msg
error "error fetchCollectiblesTaskArg: ", errDesription
arg.finish(output)
let output = %* {
"chainId": arg.chainId,
"collectibles": "",
"error": e.msg
}
arg.finish(output)
Loading

0 comments on commit 42139c1

Please sign in to comment.