Skip to content

Commit

Permalink
Ln/update quote currency endpoint for deso (#694)
Browse files Browse the repository at this point in the history
* update quote currency price in usd endpoint to use dex for price of deso

* deprecate fields and fill deprecated fields with deso dex price

* if most recent dex price is 0, return gate price

* add const for dusdc profile username
  • Loading branch information
lazynina authored Nov 4, 2024
1 parent 6f22d88 commit f51a0da
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 32 deletions.
21 changes: 7 additions & 14 deletions routes/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type GetExchangeRateResponse struct {
USDCentsPerDeSoReserveExchangeRate uint64
BuyDeSoFeeBasisPoints uint64
USDCentsPerDeSoBlockchainDotCom uint64
USDCentsPerDeSoCoinbase uint64
USDCentsPerDeSoCoinbase uint64 // Deprecated

SatoshisPerBitCloutExchangeRate uint64 // Deprecated
USDCentsPerBitCloutExchangeRate uint64 // Deprecated
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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_")
}
Expand Down Expand Up @@ -385,22 +384,16 @@ 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)

// 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,
})

Expand Down
44 changes: 28 additions & 16 deletions routes/dao_coin_exchange_with_fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion routes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion routes/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit f51a0da

Please sign in to comment.