Skip to content

Commit

Permalink
add: nfts (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano authored Nov 24, 2023
1 parent 38bede9 commit 3a3d23a
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ Coingecko API client for golang.
| /derivaties/exchanges || DerivativesExchanges |
| /derivaties/exchanges/{id} || DerivativesExchangesId |
| /derivaties/exchanges/list || DerivativesExchangesList |
| /nfts/list | | |
| /nfts/{id} | | |
| /nfts/{asset_platform_id}/contract/{contract_address} | | |
| /nfts/list | | NftsList |
| /nfts/{id} | | NftsId |
| /nfts/{asset_platform_id}/contract/{contract_address} | | NftsContract |
| /exchange_rates || ExchangeRates |
| /search || |
| /search/trending || |
Expand Down
1 change: 1 addition & 0 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
categoriesURL = fmt.Sprintf("%s/categories", coinsURL)
exchangesURL = fmt.Sprintf("%s/exchanges", baseURL)
derivativesURL = fmt.Sprintf("%s/derivatives", baseURL)
nftsURL = fmt.Sprintf("%s/nfts", baseURL)
contractURL = fmt.Sprintf("%s/coins", baseURL)
exchangeRatesURL = fmt.Sprintf("%s/exchange_rates", baseURL)
trendingURL = fmt.Sprintf("%s/search/trending", baseURL)
Expand Down
84 changes: 84 additions & 0 deletions nfts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package goingecko

import (
"encoding/json"
"fmt"
"github.com/JulianToledano/goingecko/nfts"
"net/url"
)

// NftsList Use this to obtain all the NFT ids in order to make API calls, paginated to 100 items.
//
// Cache / Update Frequency: every 5 minutes
// Parameters:
// order(string) - valid values: h24_volume_native_asc, h24_volume_native_desc, floor_price_native_asc, floor_price_native_desc, market_cap_native_asc, market_cap_native_desc, market_cap_usd_asc, market_cap_usd_desc
// assetPlatformId(string) - The id of the platform issuing tokens (See asset_platforms endpoint for list of options)
// per_page(integer) - Valid values: 1..250. Total results per page
// page(integer) - Page through results
func (c *Client) NftsList(order, assetPlatformId string, perPage, page int32) ([]nfts.Nft, error) {
params := url.Values{}
if order != "" {
params.Add("order", order)
}
if assetPlatformId != "" {
params.Add("asset_platform_id", assetPlatformId)
}
if perPage > 0 {
params.Add("per_page", string(perPage))
}
if page > 0 {
params.Add("page", string(page))
}

rUrl := fmt.Sprintf("%s/list?%s", nftsURL, params.Encode())
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}

var data []nfts.Nft
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}

return data, nil
}

// NftsId Get current data (name, price_floor, volume_24h ...) for an NFT collection. native_currency (string) is only a representative of the currency.
//
// Cache / Update Frequency: every 60 seconds
// Parameters:
// id*(string) - id of nft collection (can be obtained from /nfts/list)
func (c *Client) NftsId(id string) (*nfts.NftId, error) {
rUrl := fmt.Sprintf("%s/%s", nftsURL, id)
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}

var data *nfts.NftId
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}

return data, nil
}

// NftsContract
func (c *Client) NftsContract(assetPlatform, contract string) (*nfts.NftId, error) {
rUrl := fmt.Sprintf("%s/%s/contract/%s", nftsURL, assetPlatform, contract)
resp, err := c.MakeReq(rUrl)
if err != nil {
return nil, err
}

var data *nfts.NftId
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}

return data, nil
}
63 changes: 63 additions & 0 deletions nfts/nfts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package nfts

type Nft struct {
Id string `json:"id"`
ContractAddress string `json:"contract_address"`
Name string `json:"name"`
AssetPlatformId string `json:"asset_platform_id"`
Symbol string `json:"symbol"`
}

type NftId struct {
Id string `json:"id"`
ContractAddress string `json:"contract_address"`
AssetPlatformId string `json:"asset_platform_id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Image Image `json:"image"`
Description string `json:"description"`
NativeCurrency string `json:"native_currency"`
NativeCurrencySymbol string `json:"native_currency_symbol"`
FloorPrice Price `json:"floor_price"`
MarketCap Price `json:"market_cap"`
Volume24H Price `json:"volume_24h"`
FloorPriceInUsd24HPercentageChange float64 `json:"floor_price_in_usd_24h_percentage_change"`
FloorPrice24HPercentageChange Price `json:"floor_price_24h_percentage_change"`
MarketCap24HPercentageChange Price `json:"market_cap_24h_percentage_change"`
Volume24HPercentageChange Price `json:"volume_24h_percentage_change"`
NumberOfUniqueAddresses float64 `json:"number_of_unique_addresses"`
NumberOfUniqueAddresses24HPercentageChange float64 `json:"number_of_unique_addresses_24h_percentage_change"`
VolumeInUsd24HPercentageChange float64 `json:"volume_in_usd_24h_percentage_change"`
TotalSupply float64 `json:"total_supply"`
OneDaySales float64 `json:"one_day_sales"`
OneDaySales24HPercentageChange float64 `json:"one_day_sales_24h_percentage_change"`
OneDayAverageSalePrice float64 `json:"one_day_average_sale_price"`
OneDayAverageSalePrice24HPercentageChange float64 `json:"one_day_average_sale_price_24h_percentage_change"`
Links Links `json:"links"`
FloorPrice7DPercentageChange Price `json:"floor_price_7d_percentage_change"`
FloorPrice14DPercentageChange Price `json:"floor_price_14d_percentage_change"`
FloorPrice30DPercentageChange Price `json:"floor_price_30d_percentage_change"`
FloorPrice60DPercentageChange Price `json:"floor_price_60d_percentage_change"`
FloorPrice1YPercentageChange Price `json:"floor_price_1y_percentage_change"`
Explorers []Explorers `json:"explorers"`
}

type Price struct {
Usd float64 `json:"usd"`
NativeCurrency float64 `json:"native_currency"`
}

type Image struct {
Small string `json:"small"`
}

type Links struct {
Homepage string `json:"homepage"`
Twitter string `json:"twitter"`
Discord string `json:"discord"`
}

type Explorers struct {
Name string `json:"name"`
Link string `json:"link"`
}
39 changes: 39 additions & 0 deletions test/nfts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test

import (
"github.com/JulianToledano/goingecko"
"testing"
)

func TestNftsList(t *testing.T) {
cgClient := goingecko.NewClient(nil)
data, err := cgClient.NftsList("", "", 0, 0)
if data == nil {
t.Errorf("Error")
}
if err != nil {
t.Errorf("Error: %s", err)
}
}

func TestNftsId(t *testing.T) {
cgClient := goingecko.NewClient(nil)
data, err := cgClient.NftsId("squiggly")
if data == nil {
t.Errorf("Error")
}
if err != nil {
t.Errorf("Error: %s", err)
}
}

func TestNftsContract(t *testing.T) {
cgClient := goingecko.NewClient(nil)
data, err := cgClient.NftsContract("ethereum", "0x36F379400DE6c6BCDF4408B282F8b685c56adc60")
if data == nil {
t.Errorf("Error")
}
if err != nil {
t.Errorf("Error: %s", err)
}
}

0 comments on commit 3a3d23a

Please sign in to comment.