-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
497 additions
and
18 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
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
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,32 @@ | ||
package dbclient | ||
|
||
import ( | ||
"context" | ||
model "github.com/babylonlabs-io/staking-api-service/internal/shared/db/model" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
"time" | ||
) | ||
|
||
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 dbmodel | ||
|
||
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
64 changes: 64 additions & 0 deletions
64
internal/shared/http/clients/coinmarketcap/coinmarketcap.go
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,64 @@ | ||
package coinmarketcap | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/config" | ||
"github.com/babylonlabs-io/staking-api-service/internal/shared/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) { | ||
// todo implement me | ||
return 0, 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,12 @@ | ||
package coinmarketcap | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/shared/types" | ||
) | ||
|
||
//go:generate mockery --name=CoinMarketCapClientInterface --output=../../../../../tests/mocks --outpkg=mocks --filename=mock_coinmarketcap_client.go | ||
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,30 @@ | ||
package service | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func (s *Services) GetLatestBtcPriceUsd(ctx context.Context) (float64, error) { | ||
// Try to get price from MongoDB first | ||
btcPrice, err := s.DbClient.GetLatestBtcPrice(ctx) | ||
if err != nil { | ||
if errors.Is(err, mongo.ErrNoDocuments) { | ||
// Document not found, fetch from CoinMarketCap | ||
price, err := s.Clients.CoinMarketCap.GetLatestBtcPrice(ctx) | ||
if err != nil { | ||
return 0, fmt.Errorf("failed to fetch price from CoinMarketCap: %w", err) | ||
} | ||
// Store in MongoDB with TTL | ||
if err := s.DbClient.SetBtcPrice(ctx, price); err != nil { | ||
return 0, fmt.Errorf("failed to cache btc price: %w", err) | ||
} | ||
return price, nil | ||
} | ||
// Handle other database errors | ||
return 0, fmt.Errorf("database error: %w", err) | ||
} | ||
return btcPrice.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
Oops, something went wrong.