Skip to content

Commit

Permalink
chore(@desktop/wallet): Simplify the wallet networks service
Browse files Browse the repository at this point in the history
fixes #12717
  • Loading branch information
Khushboo-dev-cpp committed Mar 13, 2024
1 parent 666ba77 commit 5963be3
Show file tree
Hide file tree
Showing 18 changed files with 624 additions and 346 deletions.
4 changes: 2 additions & 2 deletions src/app/modules/main/chat_section/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,9 @@ proc getContractAddressesForToken*(self: Controller, symbol: string): Table[int,
let token = self.tokenService.findTokenBySymbol(symbol)
if token != nil:
for addrPerChain in token.addressPerChainId:
# depending on areTestNetworksEnabled (in getNetwork), contractAddresses will
# depending on areTestNetworksEnabled (in getNetworkByChainId), contractAddresses will
# contain mainnets or testnets only
let network = self.networkService.getNetwork(addrPerChain.chainId)
let network = self.networkService.getNetworkByChainId(addrPerChain.chainId)
if network == nil:
continue
contractAddresses[addrPerChain.chainId] = addrPerChain.address
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/communities/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ proc getCommunityTokens*(self: Controller, communityId: string): seq[CommunityTo
proc getAllCommunityTokens*(self: Controller): seq[CommunityTokenDto] =
self.communityTokensService.getAllCommunityTokens()

proc getNetwork*(self:Controller, chainId: int): NetworkDto =
self.networksService.getNetwork(chainId)
proc getNetworkByChainId*(self:Controller, chainId: int): NetworkDto =
self.networksService.getNetworkByChainId(chainId)

proc getTokenBySymbolList*(self: Controller): seq[TokenBySymbolItem] =
return self.tokenService.getTokenBySymbolList()
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/communities/tokens/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ proc findContractByUniqueId*(self: Controller, contractUniqueKey: string): Commu
proc computeBurnFee*(self: Controller, contractUniqueKey: string, amount: Uint256, addressFrom: string, requestId: string) =
self.communityTokensService.computeBurnFee(contractUniqueKey, amount, addressFrom, requestId)

proc getNetwork*(self:Controller, chainId: int): NetworkDto =
self.networksService.getNetwork(chainId)
proc getNetworkByChainId*(self:Controller, chainId: int): NetworkDto =
self.networksService.getNetworkByChainId(chainId)

proc getOwnerToken*(self: Controller, communityId: string): CommunityTokenDto =
return self.communityTokensService.getOwnerToken(communityId)
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/communities/tokens/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ method computeBurnFee*(self: Module, contractUniqueKey: string, amount: string,
self.controller.computeBurnFee(contractUniqueKey, amount.parse(Uint256), addressFrom, requestId)

proc createUrl(self: Module, chainId: int, transactionHash: string): string =
let network = self.controller.getNetwork(chainId)
let network = self.controller.getNetworkByChainId(chainId)
result = if network != nil: network.blockExplorerURL & "/tx/" & transactionHash else: ""

proc getChainName(self: Module, chainId: int): string =
let network = self.controller.getNetwork(chainId)
let network = self.controller.getNetworkByChainId(chainId)
result = if network != nil: network.chainName else: ""

method onCommunityTokenDeployStateChanged*(self: Module, communityId: string, chainId: int, transactionHash: string, deployState: DeployState) =
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ proc getRemainingSupply*(self: Controller, chainId: int, contractAddress: string
proc getRemoteDestructedAmount*(self: Controller, chainId: int, contractAddress: string): Uint256 =
return self.communityTokensService.getRemoteDestructedAmount(chainId, contractAddress)

proc getNetwork*(self:Controller, chainId: int): NetworkDto =
self.networksService.getNetwork(chainId)
proc getNetworkByChainId*(self:Controller, chainId: int): NetworkDto =
self.networksService.getNetworkByChainId(chainId)

proc getAppNetwork*(self:Controller): NetworkDto =
self.networksService.getAppNetwork()
Expand Down
4 changes: 2 additions & 2 deletions src/app/modules/main/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ method onAppNetworkChanged*[T](self: Module[T]) =
self.view.emitAppNetworkChangedSignal()

proc createTokenItem[T](self: Module[T], tokenDto: CommunityTokenDto) : token_item.TokenItem =
let network = self.controller.getNetwork(tokenDto.chainId)
let network = self.controller.getNetworkByChainId(tokenDto.chainId)
let tokenOwners = self.controller.getCommunityTokenOwners(tokenDto.communityId, tokenDto.chainId, tokenDto.address)
let ownerAddressName = if len(tokenDto.deployer) > 0: self.controller.getCommunityTokenOwnerName(tokenDto.deployer) else: ""
let remainingSupply = if tokenDto.infiniteSupply: stint.parse("0", Uint256) else: self.controller.getRemainingSupply(tokenDto.chainId, tokenDto.address)
Expand All @@ -277,7 +277,7 @@ proc createTokenItem[T](self: Module[T], tokenDto: CommunityTokenDto) : token_it

proc createTokenItemImproved[T](self: Module[T], tokenDto: CommunityTokenDto, communityTokenJsonItems: JsonNode) : token_item.TokenItem =
# These 3 values come from local caches so they can be done sync
let network = self.controller.getNetwork(tokenDto.chainId)
let network = self.controller.getNetworkByChainId(tokenDto.chainId)
let tokenOwners = self.controller.getCommunityTokenOwners(tokenDto.communityId, tokenDto.chainId, tokenDto.address)
let ownerAddressName = if len(tokenDto.deployer) > 0: self.controller.getCommunityTokenOwnerName(tokenDto.deployer) else: ""

Expand Down
46 changes: 46 additions & 0 deletions src/app/modules/main/wallet_section/networks/combined_item.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import strformat

import ./item

type
CombinedItem* = object
prod: Item
test: Item
layer: int

proc initCombinedItem*(
prod: Item,
test: Item,
layer: int
): CombinedItem =
result.prod = prod
result.test = test
result.layer = layer

proc `$`*(self: CombinedItem): string =
result = fmt"""CombinedItem(
prod: {self.prod},
test: {self.test},
layer: {self.layer},
]"""

proc getProd*(self: CombinedItem): Item =
return self.prod

proc getTest*(self: CombinedItem): Item =
return self.test

proc getLayer*(self: CombinedItem): int =
return self.layer

proc getShortName*(self: CombinedItem, areTestNetworksEnabled: bool): string =
if areTestNetworksEnabled:
return self.test.getShortName()
else:
return self.prod.getShortName()

proc getChainId*(self: CombinedItem, areTestNetworksEnabled: bool): int =
if areTestNetworksEnabled:
return self.test.getChainId()
else:
return self.prod.getChainId()
89 changes: 89 additions & 0 deletions src/app/modules/main/wallet_section/networks/combined_model.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import NimQml, Tables, strutils, strformat

import ./combined_item

type
ModelRole* {.pure.} = enum
Prod = UserRole + 1,
Test
Layer

QtObject:
type
CombinedModel* = ref object of QAbstractListModel
items: seq[CombinedItem]

proc delete(self: CombinedModel) =
self.items = @[]
self.QAbstractListModel.delete

proc setup(self: CombinedModel) =
self.QAbstractListModel.setup

proc newCombinedModel*(): CombinedModel =
new(result, delete)
result.setup

proc `$`*(self: CombinedModel): string =
for i in 0 ..< self.items.len:
result &= fmt"""[{i}]:({$self.items[i]})"""

proc countChanged(self: CombinedModel) {.signal.}

proc getCount(self: CombinedModel): int {.slot.} =
self.items.len

QtProperty[int] count:
read = getCount
notify = countChanged

method rowCount*(self: CombinedModel, index: QModelIndex = nil): int =
return self.items.len

method roleNames(self: CombinedModel): Table[int, string] =
{
ModelRole.Prod.int:"prod",
ModelRole.Test.int:"test",
ModelRole.Layer.int:"layer",
}.toTable

method data(self: CombinedModel, 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.Prod:
result = newQVariant(item.getProd())
of ModelRole.Test:
result = newQVariant(item.getTest())
of ModelRole.Layer:
result = newQVariant(item.getLayer())

proc setItems*(self: CombinedModel, items: seq[CombinedItem]) =
self.beginResetModel()
self.items = items
self.endResetModel()
self.countChanged()

proc getAllNetworksChainIds*(self: CombinedModel, areTestNetworksEnabled: bool): string =
var networks: seq[int] = @[]
for item in self.items:
networks.add(item.getChainId(areTestNetworksEnabled))
return networks.join(":")

proc getNetworkShortNames*(self: CombinedModel, preferredNetworks: string, areTestNetworksEnabled: bool): string =
var networkString = ""
let networks = preferredNetworks.split(":")
for nw in networks:
for item in self.items:
if $item.getChainId(areTestNetworksEnabled) == nw:
networkString = networkString & $item.getShortName(areTestNetworksEnabled) & ':'
break
return networkString

5 changes: 4 additions & 1 deletion src/app/modules/main/wallet_section/networks/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ proc init*(self: Controller) =
self.events.on(SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED) do(e: Args):
self.delegate.refreshNetworks()

proc getFlatNetworks*(self: Controller): seq[NetworkDto] =
proc getFlatNetworks*(self: Controller): var seq[NetworkDto] =
return self.networkService.getFlatNetworks()

proc getCombinedNetworks*(self: Controller): seq[CombinedNetworkDto] =
return self.networkService.getCombinedNetworks()

proc setNetworksState*(self: Controller, chainIds: seq[int], enabled: bool) =
self.walletAccountService.setNetworksState(chainIds, enabled)

Expand Down
10 changes: 10 additions & 0 deletions src/app/modules/main/wallet_section/networks/io_interface.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import app_service/service/network/dto

type
NetworksDataSource* = tuple[
getFlatNetworksList: proc(): var seq[NetworkDto]
]

type
AccessInterface* {.pure inheritable.} = ref object of RootObj
## Abstract class for any input/interaction with this module.
Expand Down Expand Up @@ -25,3 +32,6 @@ method refreshNetworks*(self: AccessInterface) {.base.} =

method getNetworkLayer*(self: AccessInterface, chainId: int): string {.base.} =
raise newException(ValueError, "No implementation available")

method getNetworksDataSource*(self: AccessInterface): NetworksDataSource {.base.} =
raise newException(ValueError, "No implementation available")
Loading

0 comments on commit 5963be3

Please # to comment.