parent
7bbefa5518
commit
acb38a4c4c
|
@ -14,6 +14,7 @@ import ../../../backend/accounts as status_go_accounts
|
||||||
import ../../../backend/tokens as status_go_tokens
|
import ../../../backend/tokens as status_go_tokens
|
||||||
import ../../../backend/wallet as status_go_wallet
|
import ../../../backend/wallet as status_go_wallet
|
||||||
import ../../../backend/eth as status_go_eth
|
import ../../../backend/eth as status_go_eth
|
||||||
|
import ../../../backend/cache
|
||||||
|
|
||||||
export dto
|
export dto
|
||||||
|
|
||||||
|
@ -87,6 +88,9 @@ type
|
||||||
networkService: network_service.Service
|
networkService: network_service.Service
|
||||||
accounts: OrderedTable[string, WalletAccountDto]
|
accounts: OrderedTable[string, WalletAccountDto]
|
||||||
|
|
||||||
|
priceCache: TimedCache
|
||||||
|
|
||||||
|
|
||||||
proc delete*(self: Service) =
|
proc delete*(self: Service) =
|
||||||
discard
|
discard
|
||||||
|
|
||||||
|
@ -104,6 +108,7 @@ proc newService*(
|
||||||
result.tokenService = tokenService
|
result.tokenService = tokenService
|
||||||
result.networkService = networkService
|
result.networkService = networkService
|
||||||
result.accounts = initOrderedTable[string, WalletAccountDto]()
|
result.accounts = initOrderedTable[string, WalletAccountDto]()
|
||||||
|
result.priceCache = newTimedCache()
|
||||||
|
|
||||||
proc buildTokens(
|
proc buildTokens(
|
||||||
self: Service,
|
self: Service,
|
||||||
|
@ -143,12 +148,16 @@ proc buildTokens(
|
||||||
)
|
)
|
||||||
|
|
||||||
proc getPrice*(self: Service, crypto: string, fiat: string): float64 =
|
proc getPrice*(self: Service, crypto: string, fiat: string): float64 =
|
||||||
|
let cacheKey = crypto & fiat
|
||||||
|
if self.priceCache.isCached(cacheKey):
|
||||||
|
return parseFloat(self.priceCache.get(cacheKey))
|
||||||
var prices = initTable[string, float]()
|
var prices = initTable[string, float]()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
let response = status_go_wallet.fetchPrices(@[crypto], fiat)
|
let response = status_go_wallet.fetchPrices(@[crypto], fiat)
|
||||||
for (symbol, value) in response.result.pairs:
|
for (symbol, value) in response.result.pairs:
|
||||||
prices[symbol] = value.getFloat
|
prices[symbol] = value.getFloat
|
||||||
|
self.priceCache.set(cacheKey, $value.getFloat)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
let errDesription = e.msg
|
let errDesription = e.msg
|
||||||
error "error: ", errDesription
|
error "error: ", errDesription
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
|
||||||
|
import tables, times
|
||||||
|
|
||||||
|
type Value* = ref object
|
||||||
|
value*: string
|
||||||
|
timestamp*: DateTime
|
||||||
|
|
||||||
|
type TimedCache* = Table[string, Value]
|
||||||
|
|
||||||
|
proc newTimedCache*(): TimedCache = initTable[string, Value]()
|
||||||
|
|
||||||
|
proc isCached*(self: TimedCache, cacheKey: string, duration=initDuration(minutes = 5)): bool =
|
||||||
|
self.hasKey(cacheKey) and ((self[cacheKey].timestamp + duration) >= now())
|
||||||
|
|
||||||
|
proc set*(self: var TimedCache, cacheKey: string, value: string) =
|
||||||
|
self[cacheKey] = Value(value: value, timestamp: now())
|
||||||
|
|
||||||
|
proc get*(self: var TimedCache, cacheKey: string): string = self[cacheKey].value
|
Loading…
Reference in New Issue