refactor(@desktop/wallet): build wallet account token
This commit is contained in:
parent
b050796fc8
commit
b01e20302b
|
@ -22,5 +22,5 @@ method delete*[T](self: Controller[T]) =
|
|||
method init*[T](self: Controller[T]) =
|
||||
discard
|
||||
|
||||
method getTokens*[T](self: Controller[T]): seq[token_service.Dto] =
|
||||
method getTokens*[T](self: Controller[T]): seq[token_service.TokenDto] =
|
||||
return self.tokenService.getTokens()
|
|
@ -10,7 +10,7 @@ method delete*(self: AccessInterface) {.base.} =
|
|||
method init*(self: AccessInterface) {.base.} =
|
||||
raise newException(ValueError, "No implementation available")
|
||||
|
||||
method getTokens*(self: AccessInterface): seq[token_service.Dto] {.base.} =
|
||||
method getTokens*(self: AccessInterface): seq[token_service.TokenDto] {.base.} =
|
||||
raise newException(ValueError, "No implementation available")
|
||||
|
||||
type
|
||||
|
|
|
@ -7,7 +7,7 @@ import
|
|||
from web3/conversions import `$`
|
||||
|
||||
type
|
||||
Dto* = ref object of RootObj
|
||||
TokenDto* = ref object of RootObj
|
||||
name*: string
|
||||
chainId*: int
|
||||
address*: Address
|
||||
|
@ -20,13 +20,13 @@ type
|
|||
|
||||
proc newDto*(
|
||||
name: string, chainId: int, address: Address, symbol: string, decimals: int, hasIcon: bool, isCustom: bool = false
|
||||
): Dto =
|
||||
return Dto(
|
||||
): TokenDto =
|
||||
return TokenDto(
|
||||
name: name, chainId: chainId, address: address, symbol: symbol, decimals: decimals, hasIcon: hasIcon, isCustom: isCustom
|
||||
)
|
||||
|
||||
proc toTokenDto*(jsonObj: JsonNode, activeTokenSymbols: seq[string]): Dto =
|
||||
result = Dto()
|
||||
proc toTokenDto*(jsonObj: JsonNode, activeTokenSymbols: seq[string]): TokenDto =
|
||||
result = TokenDto()
|
||||
result.isCustom = true
|
||||
result.visible = false
|
||||
discard jsonObj.getProp("name", result.name)
|
||||
|
@ -41,5 +41,5 @@ proc toTokenDto*(jsonObj: JsonNode, activeTokenSymbols: seq[string]): Dto =
|
|||
|
||||
|
||||
|
||||
proc addressAsString*(self: Dto): string =
|
||||
proc addressAsString*(self: TokenDto): string =
|
||||
return $self.address
|
|
@ -9,10 +9,12 @@ export service_interface
|
|||
logScope:
|
||||
topics = "token-service"
|
||||
|
||||
const DEFAULT_VISIBLE_SYMBOLS = @["SNT"]
|
||||
|
||||
type
|
||||
Service* = ref object of service_interface.ServiceInterface
|
||||
settingService: setting_service.ServiceInterface
|
||||
tokens: seq[Dto]
|
||||
tokens: seq[TokenDto]
|
||||
|
||||
method delete*(self: Service) =
|
||||
discard
|
||||
|
@ -24,19 +26,22 @@ proc newService*(settingService: setting_service.Service): Service =
|
|||
|
||||
method init*(self: Service) =
|
||||
try:
|
||||
let response = custom_tokens.getCustomTokens()
|
||||
let activeTokenSymbols = self.settingService.getSetting().activeTokenSymbols
|
||||
var activeTokenSymbols = self.settingService.getSetting().activeTokenSymbols
|
||||
if activeTokenSymbols.len == 0:
|
||||
activeTokenSymbols = DEFAULT_VISIBLE_SYMBOLS
|
||||
|
||||
let static_tokens = static_token.all().map(
|
||||
proc(x: Dto): Dto =
|
||||
proc(x: TokenDto): TokenDto =
|
||||
x.visible = activeTokenSymbols.contains(x.symbol)
|
||||
return x
|
||||
)
|
||||
|
||||
let response = custom_tokens.getCustomTokens()
|
||||
self.tokens = concat(
|
||||
static_tokens,
|
||||
map(response.result.getElems(), proc(x: JsonNode): Dto = x.toTokenDto(activeTokenSymbols))
|
||||
map(response.result.getElems(), proc(x: JsonNode): TokenDto = x.toTokenDto(activeTokenSymbols))
|
||||
).filter(
|
||||
proc(x: Dto): bool = x.chainId == self.settingService.getSetting().currentNetwork.id
|
||||
proc(x: TokenDto): bool = x.chainId == self.settingService.getSetting().currentNetwork.id
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
|
@ -44,5 +49,5 @@ method init*(self: Service) =
|
|||
error "error: ", errDesription
|
||||
return
|
||||
|
||||
method getTokens*(self: Service): seq[Dto] =
|
||||
method getTokens*(self: Service): seq[TokenDto] =
|
||||
return self.tokens
|
||||
|
|
|
@ -12,5 +12,5 @@ method delete*(self: ServiceInterface) {.base.} =
|
|||
method init*(self: ServiceInterface) {.base.} =
|
||||
raise newException(ValueError, "No implementation available")
|
||||
|
||||
method getTokens*(self: ServiceInterface): seq[Dto] {.base.} =
|
||||
method getTokens*(self: ServiceInterface): seq[TokenDto] {.base.} =
|
||||
raise newException(ValueError, "No implementation available")
|
||||
|
|
|
@ -3,7 +3,7 @@ import web3/ethtypes
|
|||
import ../network/types
|
||||
import ./dto
|
||||
|
||||
proc all*(): seq[Dto] =
|
||||
proc all*(): seq[TokenDto] =
|
||||
return @[
|
||||
newDto("Status Network Token", Mainnet, fromHex(Address, "0x744d70fdbe2ba4cf95131626614a1763df805b9e"), "SNT", 18, true),
|
||||
newDto("Dai Stablecoin", Mainnet, fromHex(Address, "0x6b175474e89094c44da98b954eedeac495271d0f"), "DAI", 18, true),
|
||||
|
|
|
@ -5,14 +5,14 @@ include ../../common/json_utils
|
|||
type
|
||||
WalletTokenDto* = ref object of RootObj
|
||||
name*: string
|
||||
address*: Address
|
||||
address*: string
|
||||
symbol*: string
|
||||
decimals*: int
|
||||
hasIcon*: bool
|
||||
color*: string
|
||||
isCustom*: bool
|
||||
balance*: float64
|
||||
currencyBalance*: float64
|
||||
balance*: string
|
||||
currencyBalance*: string
|
||||
|
||||
type
|
||||
WalletAccountDto* = ref object of RootObj
|
||||
|
@ -24,8 +24,7 @@ type
|
|||
walletType*: string
|
||||
isWallet*: bool
|
||||
isChat*: bool
|
||||
balance*: float64
|
||||
tokens: WalletTokenDto
|
||||
tokens*: seq[WalletTokenDto]
|
||||
|
||||
proc newDto*(
|
||||
name: string,
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import Tables, json, sequtils, sugar, chronicles
|
||||
from web3/conversions import `$`
|
||||
|
||||
import ../setting/service as setting_service
|
||||
import ../token/service as token_service
|
||||
|
@ -11,30 +12,71 @@ export service_interface
|
|||
logScope:
|
||||
topics = "wallet-account-service"
|
||||
|
||||
type
|
||||
type
|
||||
Service* = ref object of service_interface.ServiceInterface
|
||||
settingService: setting_service.Service
|
||||
tokenService: token_service.Service
|
||||
accounts: Table[string, WalletAccountDto]
|
||||
currencyBalance: float64
|
||||
|
||||
method delete*(self: Service) =
|
||||
discard
|
||||
|
||||
proc newService*(settingService: setting_service.Service, tokenService: token_service.Service): Service =
|
||||
result = Service()
|
||||
result.settingService = settingService
|
||||
result.tokenService = tokenService
|
||||
result.accounts = initTable[string, WalletAccountDto]()
|
||||
|
||||
method buildTokens(self: Service, account: WalletAccountDto, tokens: seq[TokenDto]): seq[WalletTokenDto] =
|
||||
result = @[WalletTokenDto(
|
||||
name:"Ethereum",
|
||||
address: "0x0",
|
||||
symbol: "ETH",
|
||||
decimals: 18,
|
||||
hasIcon: true,
|
||||
color: "blue",
|
||||
isCustom: false,
|
||||
balance: "0.0",
|
||||
currencyBalance: "0.0"
|
||||
)]
|
||||
|
||||
for token in tokens:
|
||||
result.add(
|
||||
WalletTokenDto(
|
||||
name: token.name,
|
||||
address: $token.address,
|
||||
symbol: token.symbol,
|
||||
decimals: token.decimals,
|
||||
hasIcon: token.hasIcon,
|
||||
color: token.color,
|
||||
isCustom: token.isCustom,
|
||||
balance: "0.0",
|
||||
currencyBalance: "0.0"
|
||||
)
|
||||
)
|
||||
|
||||
method init*(self: Service) =
|
||||
try:
|
||||
let response = status_go.getAccounts()
|
||||
let accounts = map(response.result.getElems(), proc(x: JsonNode): WalletAccountDto = x.toWalletAccountDto())
|
||||
let accounts = map(
|
||||
response.result.getElems(),
|
||||
proc(x: JsonNode): WalletAccountDto = x.toWalletAccountDto()
|
||||
)
|
||||
let currency = self.settingService.getSetting().currency
|
||||
let tokens = self.tokenService.getTokens().filter(t => t.visible)
|
||||
|
||||
for account in accounts:
|
||||
account.tokens = self.buildTokens(account, tokens)
|
||||
self.accounts[account.address] = account
|
||||
|
||||
except Exception as e:
|
||||
let errDesription = e.msg
|
||||
error "error: ", errDesription
|
||||
return
|
||||
|
||||
method getAccounts*(self: Service): seq[WalletAccountDto] =
|
||||
return toSeq(self.accounts.values).filter(a => a.isChat)
|
||||
return toSeq(self.accounts.values).filter(a => a.isChat)
|
||||
|
||||
method getCurrencyBalance*(self: Service): float64 =
|
||||
return self.currencyBalance
|
Loading…
Reference in New Issue