-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: return btc price in stats endpoint (#167)
- Loading branch information
Showing
18 changed files
with
556 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package coinmarketcap | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
baseclient "github.com/babylonlabs-io/staking-api-service/internal/clients/base" | ||
"github.com/babylonlabs-io/staking-api-service/internal/config" | ||
"github.com/babylonlabs-io/staking-api-service/internal/types" | ||
) | ||
|
||
type CoinMarketCapClient struct { | ||
config *config.CoinMarketCapConfig | ||
defaultHeaders map[string]string | ||
httpClient *http.Client | ||
} | ||
|
||
type CMCResponse struct { | ||
Data map[string]CryptoData `json:"data"` | ||
} | ||
|
||
type CryptoData struct { | ||
Quote map[string]QuoteData `json:"quote"` | ||
} | ||
|
||
type QuoteData struct { | ||
Price float64 `json:"price"` | ||
} | ||
|
||
func NewCoinMarketCapClient(config *config.CoinMarketCapConfig) *CoinMarketCapClient { | ||
// Client is disabled if config is nil | ||
if config == nil { | ||
return nil | ||
} | ||
|
||
httpClient := &http.Client{} | ||
headers := map[string]string{ | ||
"X-CMC_PRO_API_KEY": config.APIKey, | ||
"Accept": "application/json", | ||
} | ||
|
||
return &CoinMarketCapClient{ | ||
config, | ||
headers, | ||
httpClient, | ||
} | ||
} | ||
|
||
// Necessary for the BaseClient interface | ||
func (c *CoinMarketCapClient) GetBaseURL() string { | ||
return c.config.BaseURL | ||
} | ||
|
||
func (c *CoinMarketCapClient) GetDefaultRequestTimeout() int { | ||
return int(c.config.Timeout.Milliseconds()) | ||
} | ||
|
||
func (c *CoinMarketCapClient) GetHttpClient() *http.Client { | ||
return c.httpClient | ||
} | ||
|
||
func (c *CoinMarketCapClient) GetLatestBtcPrice(ctx context.Context) (float64, *types.Error) { | ||
path := "/cryptocurrency/quotes/latest" | ||
|
||
opts := &baseclient.BaseClientOptions{ | ||
Path: path + "?symbol=BTC", | ||
TemplatePath: path, | ||
Headers: c.defaultHeaders, | ||
} | ||
|
||
// Use struct{} for input (no request body) | ||
// Use CMCResponse for response type | ||
response, err := baseclient.SendRequest[struct{}, CMCResponse]( | ||
ctx, c, http.MethodGet, opts, nil, | ||
) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
btcData, exists := response.Data["BTC"] | ||
if !exists { | ||
return 0, types.NewErrorWithMsg( | ||
http.StatusInternalServerError, | ||
types.InternalServiceError, | ||
"BTC data not found in response", | ||
) | ||
} | ||
|
||
usdQuote, exists := btcData.Quote["USD"] | ||
if !exists { | ||
return 0, types.NewErrorWithMsg( | ||
http.StatusInternalServerError, | ||
types.InternalServiceError, | ||
"USD quote not found in response", | ||
) | ||
} | ||
|
||
return usdQuote.Price, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package coinmarketcap | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/types" | ||
) | ||
|
||
type CoinMarketCapClientInterface interface { | ||
GetLatestBtcPrice(ctx context.Context) (float64, *types.Error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
type ExternalAPIsConfig struct { | ||
CoinMarketCap *CoinMarketCapConfig `mapstructure:"coinmarketcap"` | ||
} | ||
|
||
type CoinMarketCapConfig struct { | ||
APIKey string `mapstructure:"api_key"` | ||
BaseURL string `mapstructure:"base_url"` | ||
Timeout time.Duration `mapstructure:"timeout"` | ||
CacheTTL time.Duration `mapstructure:"cache_ttl"` | ||
} | ||
|
||
func (cfg *ExternalAPIsConfig) Validate() error { | ||
if cfg.CoinMarketCap == nil { | ||
return fmt.Errorf("missing coinmarketcap config") | ||
} | ||
|
||
if err := cfg.CoinMarketCap.Validate(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cfg *CoinMarketCapConfig) Validate() error { | ||
if cfg.APIKey == "" { | ||
return fmt.Errorf("missing coinmarketcap api key") | ||
} | ||
|
||
if cfg.BaseURL == "" { | ||
return fmt.Errorf("missing coinmarketcap base url") | ||
} | ||
|
||
if cfg.Timeout <= 0 { | ||
return fmt.Errorf("invalid coinmarketcap timeout") | ||
} | ||
|
||
if cfg.CacheTTL <= 0 { | ||
return fmt.Errorf("invalid coinmarketcap cache ttl") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/db/model" | ||
) | ||
|
||
func (db *Database) GetLatestBtcPrice(ctx context.Context) (*model.BtcPrice, error) { | ||
client := db.Client.Database(db.DbName).Collection(model.BtcPriceCollection) | ||
|
||
var btcPrice model.BtcPrice | ||
err := client.FindOne(ctx, bson.M{"_id": model.BtcPriceDocID}).Decode(&btcPrice) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &btcPrice, nil | ||
} | ||
|
||
func (db *Database) SetBtcPrice(ctx context.Context, price float64) error { | ||
client := db.Client.Database(db.DbName).Collection(model.BtcPriceCollection) | ||
|
||
btcPrice := model.BtcPrice{ | ||
ID: model.BtcPriceDocID, // Fixed ID for single document | ||
Price: price, | ||
CreatedAt: time.Now(), // For TTL index | ||
} | ||
|
||
opts := options.Update().SetUpsert(true) | ||
filter := bson.M{"_id": model.BtcPriceDocID} | ||
update := bson.M{"$set": btcPrice} | ||
|
||
_, err := client.UpdateOne(ctx, filter, update, opts) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package model | ||
|
||
import "time" | ||
|
||
const BtcPriceDocID = "btc_price" | ||
|
||
type BtcPrice struct { | ||
ID string `bson:"_id"` // primary key, will always be "btc_price" to ensure single document | ||
Price float64 `bson:"price"` | ||
CreatedAt time.Time `bson:"created_at"` // TTL index will be on this field | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.