2021-11-09 21:47:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// https://docs.opensea.io/reference/asset-object
|
|
|
|
type Asset struct {
|
2021-11-10 14:05:45 +00:00
|
|
|
Id string
|
2021-11-09 21:47:36 +00:00
|
|
|
TokenId string
|
|
|
|
Permalink string
|
|
|
|
ImageUrl string
|
|
|
|
Name string
|
|
|
|
LastSale AssetLastSale
|
|
|
|
}
|
|
|
|
|
|
|
|
type AssetLastSale struct {
|
|
|
|
TokenUSDPrice float64
|
|
|
|
TransactionHash string
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://docs.opensea.io/reference/getting-assets
|
|
|
|
type Assets []Asset
|
|
|
|
|
|
|
|
func GetAssets() (Assets, error) {
|
|
|
|
parsed := struct {
|
|
|
|
Assets []struct {
|
2021-11-10 14:05:45 +00:00
|
|
|
Id uint64 `json:"id"`
|
2021-11-09 21:47:36 +00:00
|
|
|
TokenId string `json:"token_id"`
|
|
|
|
Permalink string `json:"permalink"`
|
|
|
|
ImageUrl string `json:"image_url"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
LastSale struct {
|
|
|
|
PaymentToken struct {
|
|
|
|
USDPrice string `json:"usd_price"`
|
|
|
|
} `json:"payment_token"`
|
|
|
|
Transaction struct {
|
|
|
|
Hash string `json:"transaction_hash"`
|
|
|
|
} `json:"transaction"`
|
|
|
|
} `json:"last_sale"`
|
|
|
|
} `json:"assets"`
|
|
|
|
}{}
|
|
|
|
assets := Assets{}
|
|
|
|
res, err := http.DefaultClient.Get("https://api.opensea.io/api/v1/assets" +
|
|
|
|
"?order_direction=desc" +
|
|
|
|
"&offset=0" +
|
|
|
|
"&limit=50" +
|
|
|
|
"&order_by=sale_date")
|
|
|
|
if err != nil {
|
|
|
|
return assets, err
|
|
|
|
}
|
|
|
|
err = json.NewDecoder(res.Body).Decode(&parsed)
|
|
|
|
if err != nil {
|
|
|
|
return assets, err
|
|
|
|
}
|
|
|
|
for _, a := range parsed.Assets {
|
|
|
|
tokenUSDPrice, err := strconv.ParseFloat(a.LastSale.PaymentToken.USDPrice, 64)
|
|
|
|
if err != nil {
|
|
|
|
return assets, err
|
|
|
|
}
|
|
|
|
assets = append(assets, Asset{
|
2021-11-10 14:05:45 +00:00
|
|
|
Id: strconv.FormatUint(a.Id, 10),
|
2021-11-09 21:47:36 +00:00
|
|
|
TokenId: a.TokenId,
|
|
|
|
Permalink: a.Permalink,
|
|
|
|
ImageUrl: a.ImageUrl,
|
|
|
|
Name: a.Name,
|
|
|
|
LastSale: AssetLastSale{
|
|
|
|
TokenUSDPrice: tokenUSDPrice,
|
|
|
|
TransactionHash: a.LastSale.Transaction.Hash,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
return assets, nil
|
|
|
|
}
|