Skip to content

Commit

Permalink
Backend/FiatValueEstimation: fix CoinCap querying
Browse files Browse the repository at this point in the history
Via the /rates API, ETC was not available, but via /assets it
is, which mitigates [1].

[1] #204
  • Loading branch information
knocte committed Feb 5, 2023
1 parent e3999e4 commit bc95e74
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/GWallet.Backend/FiatValueEstimation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@ module FiatValueEstimation =
{
"data": {
"id": "bitcoin",
"rank": "1",
"symbol": "BTC",
"currencySymbol": "x",
"type": "crypto",
"rateUsd": "6444.3132749056076909"
"name": "Bitcoin",
"supply": "19281750.0000000000000000",
"maxSupply": "21000000.0000000000000000",
"marketCapUsd": "450487811256.3001791347094000",
"volumeUsd24Hr": "6522579119.6754297896356342",
"priceUsd": "23363.4297331051475688",
"changePercent24Hr": "-0.0049558876182867",
"vwap24Hr": "23430.6497382042627725",
"explorer": "https://blockchain.info/"
},
"timestamp": 1536347871542
"timestamp": 1675571191130
}
""">

Expand All @@ -44,7 +51,7 @@ module FiatValueEstimation =
let baseUrl =
match provider with
| PriceProvider.CoinCap ->
SPrintF1 "https://api.coincap.io/v2/rates/%s" tickerName
SPrintF1 "https://api.coincap.io/v2/assets/%s" tickerName
| PriceProvider.CoinGecko ->
SPrintF1 "https://api.coingecko.com/api/v3/simple/price?ids=%s&vs_currencies=usd" tickerName
let uri = Uri baseUrl
Expand All @@ -66,14 +73,10 @@ module FiatValueEstimation =
| Some (_, json) ->
try
let tickerObj = CoinCapProvider.Parse json
return Some tickerObj.Data.RateUsd
return Some tickerObj.Data.PriceUsd
with
| ex ->
if currency = ETC then
// interestingly this can throw in CoinCap because retreiving ethereum-classic doesn't work...
return None
else
return raise <| FSharpUtil.ReRaise ex
return raise (Exception(SPrintF2 "Could not parse CoinCap's JSON (for %A): %s" currency json, ex))
}

let private QueryCoinGecko currency = async {
Expand All @@ -98,9 +101,6 @@ module FiatValueEstimation =
let coinCapJob = QueryCoinCap currency
let bothJobs = FSharpUtil.AsyncExtensions.MixedParallel2 coinGeckoJob coinCapJob
let! maybeUsdPriceFromCoinGecko, maybeUsdPriceFromCoinCap = bothJobs
if maybeUsdPriceFromCoinCap.IsSome && currency = Currency.ETC then
Infrastructure.ReportWarningMessage "Currency ETC can now be queried from CoinCap provider?"
|> ignore<bool>
let result =
match maybeUsdPriceFromCoinGecko, maybeUsdPriceFromCoinCap with
| None, None -> None
Expand Down

0 comments on commit bc95e74

Please sign in to comment.