75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
// https://docs.opensea.io/reference/asset-object
|
||
|
type Asset struct {
|
||
|
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 {
|
||
|
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{
|
||
|
TokenId: a.TokenId,
|
||
|
Permalink: a.Permalink,
|
||
|
ImageUrl: a.ImageUrl,
|
||
|
Name: a.Name,
|
||
|
LastSale: AssetLastSale{
|
||
|
TokenUSDPrice: tokenUSDPrice,
|
||
|
TransactionHash: a.LastSale.Transaction.Hash,
|
||
|
},
|
||
|
})
|
||
|
|
||
|
}
|
||
|
return assets, nil
|
||
|
}
|