-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass $ alongside TVL #489 #202
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b5e7cd7
Pass $ alongside TVL #489
kirugan 4c9169a
Add missing ExternalAPIs
kirugan 2fe6379
Add missing imports
kirugan 47687be
Fix compilation error
kirugan 326e01d
Change usage of Config in stats
kirugan 9555f91
Add coinmarketcap implementation for fetching latest quotes
kirugan ef50187
Review fixes
kirugan 7d38dce
Move price service into v2, add separate endpoint
kirugan 4dc3175
Fix compilation error
kirugan be4f866
Add method documentation
kirugan ab6e335
Update swagger docs
kirugan 5e2e0bd
PR fixes
kirugan abd88ef
Merge remote-tracking branch 'origin/main' into issue-489-return-usd-…
kirugan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,45 @@ | ||
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") | ||
} | ||
|
||
return cfg.CoinMarketCap.Validate() | ||
} | ||
|
||
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,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 | ||
kirugan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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/shared/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 | ||
jrwbabylonlab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to setup during deployment