refactor(@desktop/wallet): add visible property to token

This commit is contained in:
Anthony Laibe 2021-10-19 12:58:35 +02:00 committed by Iuri Matias
parent 0d129fa7fd
commit b050796fc8
13 changed files with 85 additions and 37 deletions

View File

@ -116,15 +116,17 @@ proc newAppController*(appService: AppService): AppController =
#################################################
result.keychainService = keychain_service.newService(appService.status.events)
result.settingService = setting_service.newService()
result.accountsService = accounts_service.newService()
result.contactService = contact_service.newService()
result.chatService = chat_service.newService()
result.communityService = community_service.newService(result.chatService)
result.tokenService = token_service.newService()
result.tokenService = token_service.newService(result.settingService)
result.transactionService = transaction_service.newService()
result.collectibleService = collectible_service.newService()
result.walletAccountService = wallet_account_service.newService()
result.settingService = setting_service.newService()
result.walletAccountService = wallet_account_service.newService(
result.settingService, result.tokenService
)
# Core
result.localAccountSettingsVariant = newQVariant(
@ -179,6 +181,7 @@ proc delete*(self: AppController) =
self.tokenService.delete
self.transactionService.delete
self.collectibleService.delete
self.settingService.delete
self.walletAccountService.delete
proc startupDidLoad*(self: AppController) =
@ -209,15 +212,15 @@ proc start*(self: AppController) =
self.startupModule.load()
proc load*(self: AppController) =
self.settingService.init()
self.contactService.init()
self.chatService.init()
self.communityService.init()
self.tokenService.init()
self.walletAccountService.init()
self.settingService.init()
self.mainModule.load()
proc userLoggedIn*(self: AppController) =
#################################################
# At the end of refactoring this will be removed:

View File

@ -79,7 +79,6 @@ proc newModule*[T](
walletAccountService,
settingService
)
method delete*[T](self: Module[T]) =
self.chatSectionModule.delete
@ -116,7 +115,6 @@ method load*[T](self: Module[T]) =
self.walletSectionModule.load()
proc checkIfModuleDidLoad [T](self: Module[T]) =
if(not self.chatSectionModule.isLoaded()):
return

View File

@ -22,5 +22,5 @@ method delete*[T](self: Controller[T]) =
method init*[T](self: Controller[T]) =
discard
method getTokens*[T](self: Controller[T], chainId: int): seq[token_service.Dto] =
return self.tokenService.getTokens(chainId)
method getTokens*[T](self: Controller[T]): seq[token_service.Dto] =
return self.tokenService.getTokens()

View File

@ -10,7 +10,7 @@ method delete*(self: AccessInterface) {.base.} =
method init*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getTokens*(self: AccessInterface, chainId: int): seq[token_service.Dto] {.base.} =
method getTokens*(self: AccessInterface): seq[token_service.Dto] {.base.} =
raise newException(ValueError, "No implementation available")
type

View File

@ -26,8 +26,7 @@ method delete*[T](self: Module[T]) =
self.controller.delete
method load*[T](self: Module[T]) =
# TODO: replace chainID 1 with chain ID coming from settings
let tokens = self.controller.getTokens(1)
let tokens = self.controller.getTokens()
self.view.setItems(
tokens.map(t => initItem(
t.name,

View File

@ -2,37 +2,49 @@ import json
include ../../common/json_utils
const DEFAULT_NETWORK_ID = "mainnet_rpc"
const DEFAULT_NETWORK_SLUG = "mainnet_rpc"
const DEFAULT_CURRENCY = "usd"
type NetworkDto* = ref object of RootObj
id*: string
id*: int
slug*: string
etherscanLink*: string
name*: string
type
SettingDto* = ref object of RootObj
currentNetwork*: NetworkDto
activeTokenSymbols*: seq[string]
signingPhrase*: string
currency*: string
proc toDto*(jsonObj: JsonNode): SettingDto =
proc toSettingDto*(jsonObj: JsonNode): SettingDto =
result = SettingDto()
discard jsonObj.getProp("signing-phrase", result.signingPhrase)
discard jsonObj.getProp("currency", result.currency)
if not jsonObj.getProp("currency", result.currency):
result.currency = DEFAULT_CURRENCY
var currentNetworkId: string
if not jsonObj.getProp("networks/current-network", currentNetworkId):
currentNetworkId = DEFAULT_NETWORK_ID
var currentNetworkSlug: string
if not jsonObj.getProp("networks/current-network", currentNetworkSlug):
currentNetworkSlug = DEFAULT_NETWORK_SLUG
var networks: JsonNode
discard jsonObj.getProp("networks/networks", networks)
for networkJson in networks.getElems():
if networkJson{"id"}.getStr != currentNetworkId:
if networkJson{"id"}.getStr != currentNetworkSlug:
continue
var networkDto = NetworkDto()
discard networkJson.getProp("id", networkDto.id)
discard networkJson{"config"}.getProp("NetworkId", networkDto.id)
discard networkJson.getProp("id", networkDto.slug)
discard networkJson.getProp("name", networkDto.name)
discard networkJson.getProp("etherscan-link", networkDto.etherscanLink)
result.currentNetwork = networkDto
break
break
result.activeTokenSymbols = @[]
let symbols = parseJson(jsonObj{"wallet/visible-tokens"}.getStr)
for symbol in symbols{$result.currentNetwork.id}.getElems():
result.activeTokenSymbols.add(symbol.getStr)

View File

@ -21,8 +21,7 @@ proc newService*(): Service =
method init*(self: Service) =
try:
let response = status_go.getSettings()
self.setting = response.result.toDto()
echo self.setting.currentNetwork.etherscanLink
self.setting = response.result.toSettingDto()
except Exception as e:
let errDesription = e.msg
error "error: ", errDesription

View File

@ -12,5 +12,5 @@ method delete*(self: ServiceInterface) {.base.} =
method init*(self: ServiceInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getSetting*(self: ServiceInterface): seq[SettingDto] {.base.} =
method getSetting*(self: ServiceInterface): SettingDto {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -1,4 +1,4 @@
import json
import json, sequtils
include ../../common/json_utils
@ -16,6 +16,7 @@ type
hasIcon* {.dontSerialize.}: bool
color*: string
isCustom* {.dontSerialize.}: bool
visible* {.dontSerialize.}: bool
proc newDto*(
name: string, chainId: int, address: Address, symbol: string, decimals: int, hasIcon: bool, isCustom: bool = false
@ -24,15 +25,21 @@ proc newDto*(
name: name, chainId: chainId, address: address, symbol: symbol, decimals: decimals, hasIcon: hasIcon, isCustom: isCustom
)
proc toDto*(jsonObj: JsonNode): Dto =
proc toTokenDto*(jsonObj: JsonNode, activeTokenSymbols: seq[string]): Dto =
result = Dto()
result.isCustom = true
result.visible = false
discard jsonObj.getProp("name", result.name)
discard jsonObj.getProp("chainId", result.chainId)
discard jsonObj.getProp("address", result.address)
discard jsonObj.getProp("symbol", result.symbol)
discard jsonObj.getProp("decimals", result.decimals)
discard jsonObj.getProp("color", result.color)
if activeTokenSymbols.contains(result.symbol):
result.visible = true
proc addressAsString*(self: Dto): string =
return $self.address

View File

@ -1,5 +1,6 @@
import json, sequtils, chronicles
import status/statusgo_backend_new/custom_tokens as custom_tokens
import ../setting/service as setting_service
import ./service_interface, ./dto, ./static_token
@ -9,23 +10,33 @@ logScope:
topics = "token-service"
type
Service* = ref object of ServiceInterface
Service* = ref object of service_interface.ServiceInterface
settingService: setting_service.ServiceInterface
tokens: seq[Dto]
method delete*(self: Service) =
discard
proc newService*(): Service =
proc newService*(settingService: setting_service.Service): Service =
result = Service()
result.settingService = settingService
result.tokens = @[]
method init*(self: Service) =
try:
let response = custom_tokens.getCustomTokens()
let activeTokenSymbols = self.settingService.getSetting().activeTokenSymbols
let static_tokens = static_token.all().map(
proc(x: Dto): Dto =
x.visible = activeTokenSymbols.contains(x.symbol)
return x
)
self.tokens = concat(
static_token.all(),
map(response.result.getElems(), proc(x: JsonNode): Dto = x.toDto())
static_tokens,
map(response.result.getElems(), proc(x: JsonNode): Dto = x.toTokenDto(activeTokenSymbols))
).filter(
proc(x: Dto): bool = x.chainId == self.settingService.getSetting().currentNetwork.id
)
except Exception as e:
@ -33,5 +44,5 @@ method init*(self: Service) =
error "error: ", errDesription
return
method getTokens*(self: Service, chainId: int): seq[Dto] =
return self.tokens.filter(proc(x: Dto): bool = x.chainId == chainId)
method getTokens*(self: Service): seq[Dto] =
return self.tokens

View File

@ -12,5 +12,5 @@ method delete*(self: ServiceInterface) {.base.} =
method init*(self: ServiceInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getTokens*(self: ServiceInterface, chainId: int): seq[Dto] {.base.} =
method getTokens*(self: ServiceInterface): seq[Dto] {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -2,6 +2,18 @@ import json
include ../../common/json_utils
type
WalletTokenDto* = ref object of RootObj
name*: string
address*: Address
symbol*: string
decimals*: int
hasIcon*: bool
color*: string
isCustom*: bool
balance*: float64
currencyBalance*: float64
type
WalletAccountDto* = ref object of RootObj
name*: string
@ -12,6 +24,8 @@ type
walletType*: string
isWallet*: bool
isChat*: bool
balance*: float64
tokens: WalletTokenDto
proc newDto*(
name: string,
@ -34,7 +48,7 @@ proc newDto*(
isChat: isChat
)
proc toDto*(jsonObj: JsonNode): WalletAccountDto =
proc toWalletAccountDto*(jsonObj: JsonNode): WalletAccountDto =
result = WalletAccountDto()
discard jsonObj.getProp("name", result.name)
discard jsonObj.getProp("address", result.address)

View File

@ -1,5 +1,8 @@
import Tables, json, sequtils, sugar, chronicles
import ../setting/service as setting_service
import ../token/service as token_service
import ./service_interface, ./dto
import status/statusgo_backend_new/accounts as status_go
@ -10,19 +13,21 @@ logScope:
type
Service* = ref object of service_interface.ServiceInterface
settingService: setting_service.Service
tokenService: token_service.Service
accounts: Table[string, WalletAccountDto]
method delete*(self: Service) =
discard
proc newService*(): Service =
proc newService*(settingService: setting_service.Service, tokenService: token_service.Service): Service =
result = Service()
result.accounts = initTable[string, WalletAccountDto]()
method init*(self: Service) =
try:
let response = status_go.getAccounts()
let accounts = map(response.result.getElems(), proc(x: JsonNode): WalletAccountDto = x.toDto())
let accounts = map(response.result.getElems(), proc(x: JsonNode): WalletAccountDto = x.toWalletAccountDto())
for account in accounts:
self.accounts[account.address] = account