feat(@wallet): move fetch prices
also make only one call rather than one call per couple token/currency
This commit is contained in:
parent
c5c92fcfd1
commit
4b91888433
|
@ -28,7 +28,6 @@ const SIGNAL_WALLET_ACCOUNT_UPDATED* = "walletAccount/walletAccountUpdated"
|
|||
const SIGNAL_WALLET_ACCOUNT_NETWORK_ENABLED_UPDATED* = "walletAccount/networkEnabledUpdated"
|
||||
|
||||
var
|
||||
priceCache {.threadvar.}: Table[string, float64]
|
||||
balanceCache {.threadvar.}: Table[string, float64]
|
||||
|
||||
proc hex2Balance*(input: string, decimals: int): string =
|
||||
|
@ -45,31 +44,6 @@ proc hex2Balance*(input: string, decimals: int): string =
|
|||
result = $i
|
||||
if(r > 0): result = fmt"{result}.{d}"
|
||||
|
||||
|
||||
proc fetchPrice(crypto: string, fiat: string): float64 =
|
||||
let key = crypto & fiat
|
||||
if priceCache.hasKey(key):
|
||||
return priceCache[key]
|
||||
|
||||
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)
|
||||
let parsedResponse = parseJson(response.body)
|
||||
if (parsedResponse{"Response"} != nil and parsedResponse{"Response"}.getStr == "Error"):
|
||||
error "Error while getting price", message = parsedResponse["Message"].getStr
|
||||
return 0.0
|
||||
result = parsefloat($parseJson(response.body)[fiat.toUpper])
|
||||
priceCache[key] = result
|
||||
except Exception as e:
|
||||
error "Error getting price", message = e.msg
|
||||
result = 0.0
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
proc fetchAccounts(): seq[WalletAccountDto] =
|
||||
let response = status_go_accounts.getAccounts()
|
||||
return response.result.getElems().map(
|
||||
|
@ -134,7 +108,7 @@ proc newService*(
|
|||
proc buildTokens(
|
||||
self: Service,
|
||||
account: WalletAccountDto,
|
||||
prices: Table[string, float64],
|
||||
prices: Table[string, float],
|
||||
balances: JsonNode
|
||||
): seq[WalletTokenDto] =
|
||||
for network in self.networkService.getEnabledNetworks():
|
||||
|
@ -169,17 +143,39 @@ proc buildTokens(
|
|||
)
|
||||
|
||||
proc getPrice*(self: Service, crypto: string, fiat: string): float64 =
|
||||
return fetchPrice(crypto, fiat)
|
||||
var prices = initTable[string, float]()
|
||||
|
||||
proc fetchPrices(self: Service): Table[string, float64] =
|
||||
try:
|
||||
let response = status_go_wallet.fetchPrices(@[crypto], fiat)
|
||||
for (symbol, value) in response.result.pairs:
|
||||
prices[symbol] = value.getFloat
|
||||
except Exception as e:
|
||||
let errDesription = e.msg
|
||||
error "error: ", errDesription
|
||||
|
||||
return prices[crypto]
|
||||
|
||||
proc fetchPrices(self: Service): Table[string, float] =
|
||||
let currency = self.settingsService.getCurrency()
|
||||
var prices = initTable[string, float64]()
|
||||
|
||||
var symbols: seq[string] = @[]
|
||||
|
||||
for network in self.networkService.getEnabledNetworks():
|
||||
prices[network.nativeCurrencySymbol] = fetchPrice(network.nativeCurrencySymbol, currency)
|
||||
symbols.add(network.nativeCurrencySymbol)
|
||||
|
||||
for token in self.tokenService.getVisibleTokens():
|
||||
prices[token.symbol] = fetchPrice(token.symbol, currency)
|
||||
symbols.add(token.symbol)
|
||||
|
||||
var prices = initTable[string, float]()
|
||||
try:
|
||||
let response = status_go_wallet.fetchPrices(symbols, currency)
|
||||
for (symbol, value) in response.result.pairs:
|
||||
prices[symbol] = value.getFloat
|
||||
|
||||
except Exception as e:
|
||||
let errDesription = e.msg
|
||||
error "error: ", errDesription
|
||||
|
||||
return prices
|
||||
|
||||
proc fetchBalances(self: Service, accounts: seq[string]): JsonNode =
|
||||
|
|
|
@ -26,3 +26,7 @@ proc addAccountWithPrivateKey*(privateKey, password, name, color, emoji: string)
|
|||
proc addAccountWatch*(address, name, color, emoji: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [address, name, color, emoji]
|
||||
return core.callPrivateRPC("accounts_addAccountWatch", payload)
|
||||
|
||||
proc fetchPrices*(symbols: seq[string], currency: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [symbols, currency]
|
||||
return core.callPrivateRPC("wallet_fetchPrices", payload)
|
|
@ -1 +1 @@
|
|||
Subproject commit d0f4a94f7502ce37adf1f08065b07262b682c4bf
|
||||
Subproject commit b629d6fa74cf4f1e43a021f95a9a7736dd2fa973
|
Loading…
Reference in New Issue