refactor(@desktop/wallet): add balance and price
This commit is contained in:
parent
b01e20302b
commit
7e22ff161c
|
@ -11,8 +11,8 @@ type
|
||||||
hasIcon*: bool
|
hasIcon*: bool
|
||||||
color*: string
|
color*: string
|
||||||
isCustom*: bool
|
isCustom*: bool
|
||||||
balance*: string
|
balance*: float64
|
||||||
currencyBalance*: string
|
currencyBalance*: float64
|
||||||
|
|
||||||
type
|
type
|
||||||
WalletAccountDto* = ref object of RootObj
|
WalletAccountDto* = ref object of RootObj
|
||||||
|
|
|
@ -1,23 +1,53 @@
|
||||||
import Tables, json, sequtils, sugar, chronicles
|
import Tables, json, sequtils, sugar, chronicles, strformat, stint, httpclient, net, strutils
|
||||||
from web3/conversions import `$`
|
import web3/[ethtypes, conversions]
|
||||||
|
|
||||||
import ../setting/service as setting_service
|
import ../setting/service as setting_service
|
||||||
import ../token/service as token_service
|
import ../token/service as token_service
|
||||||
|
|
||||||
import ./service_interface, ./dto
|
import ./service_interface, ./dto
|
||||||
import status/statusgo_backend_new/accounts as status_go
|
import status/statusgo_backend_new/accounts as status_go_accounts
|
||||||
|
import status/statusgo_backend_new/eth as status_go_eth
|
||||||
|
|
||||||
export service_interface
|
export service_interface
|
||||||
|
|
||||||
logScope:
|
logScope:
|
||||||
topics = "wallet-account-service"
|
topics = "wallet-account-service"
|
||||||
|
|
||||||
|
proc hex2Balance*(input: string, decimals: int): string =
|
||||||
|
var value = fromHex(Stuint[256], input)
|
||||||
|
|
||||||
|
if decimals == 0:
|
||||||
|
return fmt"{value}"
|
||||||
|
|
||||||
|
var p = u256(10).pow(decimals)
|
||||||
|
var i = value.div(p)
|
||||||
|
var r = value.mod(p)
|
||||||
|
var leading_zeros = "0".repeat(decimals - ($r).len)
|
||||||
|
var d = fmt"{leading_zeros}{$r}"
|
||||||
|
result = $i
|
||||||
|
if(r > 0): result = fmt"{result}.{d}"
|
||||||
|
|
||||||
|
|
||||||
|
proc getPrice(crypto: string, fiat: string): string =
|
||||||
|
let secureSSLContext = newContext()
|
||||||
|
let client = newHttpClient(sslContext = secureSSLContext)
|
||||||
|
try:
|
||||||
|
let url: string = fmt"https://min-api.cryptocompare.com/data/price?fsym={crypto}&tsyms={fiat}"
|
||||||
|
client.headers = newHttpHeaders({ "Content-Type": "application/json" })
|
||||||
|
|
||||||
|
let response = client.request(url)
|
||||||
|
result = $parseJson(response.body)[fiat.toUpper]
|
||||||
|
except Exception as e:
|
||||||
|
error "Error getting price", message = e.msg
|
||||||
|
result = "0.0"
|
||||||
|
finally:
|
||||||
|
client.close()
|
||||||
|
|
||||||
type
|
type
|
||||||
Service* = ref object of service_interface.ServiceInterface
|
Service* = ref object of service_interface.ServiceInterface
|
||||||
settingService: setting_service.Service
|
settingService: setting_service.Service
|
||||||
tokenService: token_service.Service
|
tokenService: token_service.Service
|
||||||
accounts: Table[string, WalletAccountDto]
|
accounts: Table[string, WalletAccountDto]
|
||||||
currencyBalance: float64
|
|
||||||
|
|
||||||
method delete*(self: Service) =
|
method delete*(self: Service) =
|
||||||
discard
|
discard
|
||||||
|
@ -28,20 +58,29 @@ proc newService*(settingService: setting_service.Service, tokenService: token_se
|
||||||
result.tokenService = tokenService
|
result.tokenService = tokenService
|
||||||
result.accounts = initTable[string, WalletAccountDto]()
|
result.accounts = initTable[string, WalletAccountDto]()
|
||||||
|
|
||||||
method buildTokens(self: Service, account: WalletAccountDto, tokens: seq[TokenDto]): seq[WalletTokenDto] =
|
method buildTokens(
|
||||||
|
self: Service,
|
||||||
|
account: WalletAccountDto,
|
||||||
|
tokens: seq[TokenDto],
|
||||||
|
prices: Table[string, float64]
|
||||||
|
): seq[WalletTokenDto] =
|
||||||
|
let ethBalanceResponse = status_go_eth.getEthBalance(account.address)
|
||||||
|
let balance = parsefloat(hex2Balance(ethBalanceResponse.result.getStr, 18))
|
||||||
result = @[WalletTokenDto(
|
result = @[WalletTokenDto(
|
||||||
name:"Ethereum",
|
name:"Ethereum",
|
||||||
address: "0x0",
|
address: account.address,
|
||||||
symbol: "ETH",
|
symbol: "ETH",
|
||||||
decimals: 18,
|
decimals: 18,
|
||||||
hasIcon: true,
|
hasIcon: true,
|
||||||
color: "blue",
|
color: "blue",
|
||||||
isCustom: false,
|
isCustom: false,
|
||||||
balance: "0.0",
|
balance: balance,
|
||||||
currencyBalance: "0.0"
|
currencyBalance: balance * prices["ETH"]
|
||||||
)]
|
)]
|
||||||
|
|
||||||
for token in tokens:
|
for token in tokens:
|
||||||
|
let tokenBalanceResponse = status_go_eth.getTokenBalance($token.address, account.address)
|
||||||
|
let balance = parsefloat(hex2Balance(tokenBalanceResponse.result.getStr, token.decimals))
|
||||||
result.add(
|
result.add(
|
||||||
WalletTokenDto(
|
WalletTokenDto(
|
||||||
name: token.name,
|
name: token.name,
|
||||||
|
@ -51,23 +90,26 @@ method buildTokens(self: Service, account: WalletAccountDto, tokens: seq[TokenDt
|
||||||
hasIcon: token.hasIcon,
|
hasIcon: token.hasIcon,
|
||||||
color: token.color,
|
color: token.color,
|
||||||
isCustom: token.isCustom,
|
isCustom: token.isCustom,
|
||||||
balance: "0.0",
|
balance: balance,
|
||||||
currencyBalance: "0.0"
|
currencyBalance: balance * prices[token.symbol]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
method init*(self: Service) =
|
method init*(self: Service) =
|
||||||
try:
|
try:
|
||||||
let response = status_go.getAccounts()
|
let response = status_go_accounts.getAccounts()
|
||||||
let accounts = map(
|
let accounts = map(
|
||||||
response.result.getElems(),
|
response.result.getElems(),
|
||||||
proc(x: JsonNode): WalletAccountDto = x.toWalletAccountDto()
|
proc(x: JsonNode): WalletAccountDto = x.toWalletAccountDto()
|
||||||
)
|
)
|
||||||
let currency = self.settingService.getSetting().currency
|
let currency = self.settingService.getSetting().currency
|
||||||
let tokens = self.tokenService.getTokens().filter(t => t.visible)
|
let tokens = self.tokenService.getTokens().filter(t => t.visible)
|
||||||
|
var prices = {"ETH": parsefloat(getPrice("ETH", currency))}.toTable
|
||||||
|
for token in tokens:
|
||||||
|
prices[token.symbol] = parsefloat(getPrice(token.symbol, currency))
|
||||||
|
|
||||||
for account in accounts:
|
for account in accounts:
|
||||||
account.tokens = self.buildTokens(account, tokens)
|
account.tokens = self.buildTokens(account, tokens, prices)
|
||||||
self.accounts[account.address] = account
|
self.accounts[account.address] = account
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -79,4 +121,4 @@ 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 =
|
method getCurrencyBalance*(self: Service): float64 =
|
||||||
return self.currencyBalance
|
return 0.0
|
|
@ -1 +1 @@
|
||||||
Subproject commit f320378bd5fee8fa4b1af66e608ef352bbf0a215
|
Subproject commit 5de6e894a57eaa2665d0d3c2207f9bd836ac82ff
|
Loading…
Reference in New Issue