Skip to content

Commit

Permalink
fix: opensea api now requires token
Browse files Browse the repository at this point in the history
  • Loading branch information
rssnyder committed Jun 26, 2023
1 parent da97cf6 commit 43bc288
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
# vendor/

bin*
test.sh
test.*
load.py
state.db
discord-stock-ticker
test.state
dist/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ Track the floor price of an NFT collection on OpenSea, Solanart, Solsea or Magic
"nickname": true, # bool/OPTIONAL: display information in nickname vs activity
"activity": "Hello;Its;Me", # string/OPTIONAL: list of strings to show in activity section
"frequency": 10, # int/OPTIONAL: seconds between refresh
"api_key": "XXX", # int/OPTIONAL: api key, used for opensea
"discord_bot_token": "xxxxxxxxxxxxxxxxxxxxxxxx" # string: dicord bot token
}
```
Expand Down
3 changes: 2 additions & 1 deletion floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Floor struct {
Color bool `json:"color"`
Decorator string `json:"decorator"`
Currency string `json:"currency"`
ApiKey string `json:"api_key"`
ClientID string `json:"client_id"`
Token string `json:"discord_bot_token"`
close chan int `json:"-"`
Expand Down Expand Up @@ -114,7 +115,7 @@ func (f *Floor) watchFloorPrice() {
logger.Infof("Shutting down price watching for %s/%s", f.Marketplace, f.Name)
return
case <-ticker.C:
price, activity, currency, err := utils.GetFloorPrice(f.Marketplace, f.Name)
price, activity, currency, err := utils.GetFloorPrice(f.Marketplace, f.Name, f.ApiKey)
if err != nil {
logger.Errorf("Error getting floor rates: %s\n", err)
continue
Expand Down
8 changes: 4 additions & 4 deletions floor_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (m *Manager) ImportFloor() {
defer m.Unlock()

// query
rows, err := m.DB.Query("SELECT id, clientID, token, nickname, activity, color, decorator, currency, marketplace, name, frequency FROM floors")
rows, err := m.DB.Query("SELECT id, clientID, token, nickname, activity, color, decorator, currency, marketplace, name, frequency, apiKey FROM floors")
if err != nil {
logger.Warningf("Unable to query tokens in db: %s", err)
return
Expand All @@ -27,7 +27,7 @@ func (m *Manager) ImportFloor() {
var importedFloor Floor
var importedID int

err = rows.Scan(&importedID, &importedFloor.ClientID, &importedFloor.Token, &importedFloor.Nickname, &importedFloor.Activity, &importedFloor.Color, &importedFloor.Decorator, &importedFloor.Currency, &importedFloor.Marketplace, &importedFloor.Name, &importedFloor.Frequency)
err = rows.Scan(&importedID, &importedFloor.ClientID, &importedFloor.Token, &importedFloor.Nickname, &importedFloor.Activity, &importedFloor.Color, &importedFloor.Decorator, &importedFloor.Currency, &importedFloor.Marketplace, &importedFloor.Name, &importedFloor.Frequency, &importedFloor.ApiKey)
if err != nil {
logger.Errorf("Unable to load token from db: %s", err)
continue
Expand Down Expand Up @@ -166,13 +166,13 @@ func (m *Manager) WatchFloor(floor *Floor) {
func (m *Manager) StoreFloor(floor *Floor) {

// store new entry in db
stmt, err := m.DB.Prepare("INSERT INTO floors(clientId, token, nickname, activity, color, decorator, currency, marketplace, name, frequency) values(?,?,?,?,?,?,?,?,?,?)")
stmt, err := m.DB.Prepare("INSERT INTO floors(clientId, token, nickname, activity, color, decorator, currency, marketplace, name, frequency, apiKey) values(?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
logger.Warningf("Unable to store floor in db %s: %s", floor.label(), err)
return
}

res, err := stmt.Exec(floor.ClientID, floor.Token, floor.Nickname, floor.Activity, floor.Color, floor.Decorator, floor.Currency, floor.Marketplace, floor.Name, floor.Frequency)
res, err := stmt.Exec(floor.ClientID, floor.Token, floor.Nickname, floor.Activity, floor.Color, floor.Decorator, floor.Currency, floor.Marketplace, floor.Name, floor.Frequency, floor.ApiKey)
if err != nil {
logger.Warningf("Unable to store floor in db %s: %s", floor.label(), err)
return
Expand Down
24 changes: 19 additions & 5 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ func dbInit(fileName string) *sql.DB {
activity string,
marketplace string,
name string
apiKey string
);`

_, err = db.Exec(bootstrap)
Expand All @@ -409,7 +410,7 @@ func dbInit(fileName string) *sql.DB {
return dbNull
}

// v3.11.0 - add gas API Token
// v3.10.4 - add gas API Token
_, err = db.Exec("alter table gases add column apiToken default \"\";")
if err == nil {
logger.Warnln("Added new column to tickers: apiToken (1)")
Expand All @@ -422,7 +423,7 @@ func dbInit(fileName string) *sql.DB {
return dbNull
}

// v3.11.0 - add floor activity
// v3.10.4 - add floor activity
_, err = db.Exec("alter table floors add column activity default \"\";")
if err == nil {
logger.Warnln("Added new column to tickers: activity (1)")
Expand All @@ -435,7 +436,7 @@ func dbInit(fileName string) *sql.DB {
return dbNull
}

// v3.11.0 - add floor color
// v3.10.4 - add floor color
_, err = db.Exec("alter table floors add column color default false;")
if err == nil {
logger.Warnln("Added new column to tickers: color (1)")
Expand All @@ -448,7 +449,7 @@ func dbInit(fileName string) *sql.DB {
return dbNull
}

// v3.11.0 - add floor decorator
// v3.10.4 - add floor decorator
_, err = db.Exec("alter table floors add column decorator default \"\";")
if err == nil {
logger.Warnln("Added new column to tickers: decorator (1)")
Expand All @@ -461,7 +462,7 @@ func dbInit(fileName string) *sql.DB {
return dbNull
}

// v3.11.0 - add floor currency
// v3.10.4 - add floor currency
_, err = db.Exec("alter table floors add column currency default \"\";")
if err == nil {
logger.Warnln("Added new column to tickers: currency (1)")
Expand All @@ -474,6 +475,19 @@ func dbInit(fileName string) *sql.DB {
return dbNull
}

// v3.10.5 - add opensea api
_, err = db.Exec("alter table floors add column apiKey default \"\";")
if err == nil {
logger.Warnln("Added new column to tickers: apiKey (1)")
} else if err.Error() == "SQL logic error: duplicate column name: apiKey (1)" {
logger.Debug("New column already exists in floors: apiKey (1)")
} else if err != nil {
logger.Errorln(err)
logger.Warning("Will not be storing state.")
var dbNull *sql.DB
return dbNull
}

logger.Infof("Will be storing state in %s\n", fileName)

return db
Expand Down
4 changes: 2 additions & 2 deletions utils/floor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// GetFloorPrice based on marketplace and name
func GetFloorPrice(marketplace, name string) (float64, string, string, error) {
func GetFloorPrice(marketplace, name, apiKey string) (float64, string, string, error) {
var result float64
var activity string
var currency string
Expand Down Expand Up @@ -39,7 +39,7 @@ func GetFloorPrice(marketplace, name string) (float64, string, string, error) {
result = solanart.Pagination.Floorpricefilters
activity = "SolanArt: Floor"
default:
opensea, err := GetOpenSeaData(name)
opensea, err := GetOpenSeaData(name, apiKey)
currency = "ETH" // OpenSea API currently doesn't return the currency.

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion utils/opensea.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type OpenSeaCollection struct {
} `json:"stats"`
}

func GetOpenSeaData(collection string) (OpenSeaCollection, error) {
func GetOpenSeaData(collection, apiKey string) (OpenSeaCollection, error) {
var result OpenSeaCollection

reqUrl := fmt.Sprintf(OpenSeaURL, collection)
Expand All @@ -49,6 +49,7 @@ func GetOpenSeaData(collection string) (OpenSeaCollection, error) {

req.Header.Add("User-Agent", "Mozilla/5.0")
req.Header.Add("accept", "application/json")
req.Header.Add("x-api-key", apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand Down

0 comments on commit 43bc288

Please sign in to comment.