diff --git a/routes/base.go b/routes/base.go index 8daa0ef1..f66a29b0 100644 --- a/routes/base.go +++ b/routes/base.go @@ -76,7 +76,7 @@ type GetExchangeRateResponse struct { USDCentsPerDeSoReserveExchangeRate uint64 BuyDeSoFeeBasisPoints uint64 USDCentsPerDeSoBlockchainDotCom uint64 - USDCentsPerDeSoCoinbase uint64 + USDCentsPerDeSoCoinbase uint64 // Deprecated SatoshisPerBitCloutExchangeRate uint64 // Deprecated USDCentsPerBitCloutExchangeRate uint64 // Deprecated @@ -131,11 +131,10 @@ func (fes *APIServer) GetExchangeRate(ww http.ResponseWriter, rr *http.Request) func (fes *APIServer) GetExchangeDeSoPrice() uint64 { // We no longer observe a reserve rate. + if fes.MostRecentDesoDexPriceUSDCents == 0 { + return fes.MostRecentGatePriceUSDCents + } return fes.MostRecentDesoDexPriceUSDCents - //if fes.UsdCentsPerDeSoExchangeRate > fes.USDCentsToDESOReserveExchangeRate { - // return fes.UsdCentsPerDeSoExchangeRate - //} - //return fes.USDCentsToDESOReserveExchangeRate } type BlockchainDeSoTickerResponse struct { @@ -340,7 +339,7 @@ func (fes *APIServer) GetExchangeRateFromDeSoDex() (float64, error) { if err != nil { return 0, err } - usdcProfileEntry := utxoView.GetProfileEntryForUsername([]byte("dusdc_")) + usdcProfileEntry := utxoView.GetProfileEntryForUsername([]byte(dusdcProfileUsername)) if usdcProfileEntry == nil { return 0, fmt.Errorf("GetExchangeRateFromDeSoDex: Could not find profile entry for dusdc_") } @@ -385,14 +384,8 @@ func (fes *APIServer) UpdateUSDCentsToDeSoExchangeRate() { glog.Errorf("UpdateUSDCentsToDeSoExchangeRate: Error fetching exchange rate from DeSoDex: %v", err) } - // Take the max - lastTradePrice, err := stats.Max([]float64{blockchainDotComPrice, gatePrice, desoDexPrice}) - // store the most recent exchange prices - // For now, we are using gate.io as the primary exchange - // for pricing of deso. - //fes.MostRecentCoinbasePriceUSDCents = uint64(coinbasePrice) - fes.MostRecentCoinbasePriceUSDCents = uint64(gatePrice) + fes.MostRecentCoinbasePriceUSDCents = uint64(desoDexPrice) fes.MostRecentBlockchainDotComPriceUSDCents = uint64(blockchainDotComPrice) fes.MostRecentGatePriceUSDCents = uint64(gatePrice) fes.MostRecentDesoDexPriceUSDCents = uint64(desoDexPrice) @@ -400,7 +393,7 @@ func (fes *APIServer) UpdateUSDCentsToDeSoExchangeRate() { // Get the current timestamp and append the current last trade price to the LastTradeDeSoPriceHistory slice timestamp := uint64(time.Now().UnixNano()) fes.LastTradeDeSoPriceHistory = append(fes.LastTradeDeSoPriceHistory, LastTradePriceHistoryItem{ - LastTradePrice: uint64(lastTradePrice), + LastTradePrice: uint64(desoDexPrice), Timestamp: timestamp, }) diff --git a/routes/dao_coin_exchange_with_fees.go b/routes/dao_coin_exchange_with_fees.go index d24bc171..2c8162a6 100644 --- a/routes/dao_coin_exchange_with_fees.go +++ b/routes/dao_coin_exchange_with_fees.go @@ -16,6 +16,10 @@ import ( "strings" ) +const ( + dusdcProfileUsername = "dusdc_" +) + type UpdateDaoCoinMarketFeesRequest struct { // The profile that the fees are being modified for. ProfilePublicKeyBase58Check string `safeForLogging:"true"` @@ -908,22 +912,31 @@ const FOCUS_FLOOR_PRICE_DESO_NANOS = 166666 func (fes *APIServer) GetQuoteCurrencyPriceInUsd( quoteCurrencyPublicKey string) (_midmarket string, _bid string, _ask string, _err error) { - if IsDesoPkid(quoteCurrencyPublicKey) { - // TODO: We're taking the Coinbase price directly here, but ideally we would get it from - // a function that abstracts away the exchange we're getting it from. We do this for now - // in order to minimize discrepancies with other sources. - desoUsdCents := fes.MostRecentCoinbasePriceUSDCents - if desoUsdCents == 0 { - return "", "", "", fmt.Errorf("GetQuoteCurrencyPriceInUsd: Coinbase DESO price is zero") - } - price := fmt.Sprintf("%0.9f", float64(desoUsdCents)/100) - return price, price, price, nil // TODO: get real bid and ask prices. - } utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView() if err != nil { return "", "", "", fmt.Errorf( "GetQuoteCurrencyPriceInUsd: Error fetching mempool view: %v", err) } + if IsDesoPkid(quoteCurrencyPublicKey) { + usdcProfileEntry := utxoView.GetProfileEntryForUsername([]byte(dusdcProfileUsername)) + if usdcProfileEntry == nil { + return "", "", "", fmt.Errorf("GetQuoteCurrencyPriceInUsd: Could not find profile entry for dusdc_") + } + + usdcPKID := utxoView.GetPKIDForPublicKey(usdcProfileEntry.PublicKey) + midMarketPrice, highestBidPrice, lowestAskPrice, err := fes.GetHighestBidAndLowestAskPriceFromPKIDs( + &lib.ZeroPKID, usdcPKID.PKID, utxoView, 0) + if err != nil { + return "", "", "", fmt.Errorf("GetQuoteCurrencyPriceInUsd: Error getting price for DESO: %v", err) + } + if highestBidPrice == 0.0 || lowestAskPrice == math.MaxFloat64 { + return "", "", "", fmt.Errorf("GetQuoteCurrencyPriceInUsd: Error calculating price for DESO") + } + return fmt.Sprintf("%0.9f", midMarketPrice), + fmt.Sprintf("%0.9f", highestBidPrice), + fmt.Sprintf("%0.9f", lowestAskPrice), + nil + } pkBytes, _, err := lib.Base58CheckDecode(quoteCurrencyPublicKey) if err != nil || len(pkBytes) != btcec.PubKeyBytesLenCompressed { @@ -942,15 +955,14 @@ func (fes *APIServer) GetQuoteCurrencyPriceInUsd( // If the profile is the dusdc profile then just return 1.0 lowerUsername := strings.ToLower(string(existingProfileEntry.Username)) - if lowerUsername == "dusdc_" { + if lowerUsername == dusdcProfileUsername { return "1.0", "1.0", "1.0", nil } else if lowerUsername == "focus" || lowerUsername == "openfund" { - // TODO: We're taking the Coinbase price directly here, but ideally we would get it from - // a function that abstracts away the exchange we're getting it from. We do this for now - // in order to minimize discrepancies with other sources. - desoUsdCents := fes.MostRecentCoinbasePriceUSDCents + // Get the exchange deso price. currently this function + // just returns the price from the deso dex. + desoUsdCents := fes.GetExchangeDeSoPrice() if desoUsdCents == 0 { return "", "", "", fmt.Errorf("GetQuoteCurrencyPriceInUsd: Coinbase DESO price is zero") } diff --git a/routes/server.go b/routes/server.go index b6384640..41430c81 100644 --- a/routes/server.go +++ b/routes/server.go @@ -398,7 +398,7 @@ type APIServer struct { LastTradePriceLookback uint64 // most recent exchange prices fetched - MostRecentCoinbasePriceUSDCents uint64 + MostRecentCoinbasePriceUSDCents uint64 // Deprecated MostRecentBlockchainDotComPriceUSDCents uint64 MostRecentGatePriceUSDCents uint64 MostRecentDesoDexPriceUSDCents uint64 diff --git a/routes/transaction.go b/routes/transaction.go index 61b94deb..41fb3e88 100644 --- a/routes/transaction.go +++ b/routes/transaction.go @@ -1199,7 +1199,7 @@ func (fes *APIServer) GetNanosFromUSDCents(usdCents float64, feeBasisPoints uint } func (fes *APIServer) GetUSDFromNanos(nanos uint64) float64 { - usdCentsPerDeSo := float64(fes.UsdCentsPerDeSoExchangeRate) + usdCentsPerDeSo := float64(fes.GetExchangeDeSoPrice()) return usdCentsPerDeSo * float64(nanos/lib.NanosPerUnit) / 100 }