Tomar precios directamente de OpenSea
No sabía que existía total_price
This commit is contained in:
parent
8a5dace926
commit
cd9650368e
3 changed files with 42 additions and 71 deletions
50
etherscan.go
50
etherscan.go
|
@ -1,50 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Transaction struct {
|
||||
Value float64
|
||||
}
|
||||
|
||||
func GetTransaction(id string) (Transaction, error) {
|
||||
transaction := Transaction{}
|
||||
key, exists := os.LookupEnv("ETHERSCAN_KEY")
|
||||
if !exists {
|
||||
return transaction, errors.New("No tengo ETHERSCAN_KEY, conseguir en https://etherscan.io/apis")
|
||||
}
|
||||
res, err := http.DefaultClient.Get("https://api.etherscan.io/api?module=proxy" +
|
||||
"&action=eth_getTransactionByHash" +
|
||||
"&txhash=" + id +
|
||||
"&apikey=" + key)
|
||||
if err != nil {
|
||||
return transaction, err
|
||||
}
|
||||
result := struct {
|
||||
Result struct {
|
||||
Value string `json:"value"`
|
||||
} `json:"result,omitempty"`
|
||||
Error struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error,omitempty"`
|
||||
}{}
|
||||
err = json.NewDecoder(res.Body).Decode(&result)
|
||||
if err != nil {
|
||||
return transaction, err
|
||||
}
|
||||
if len(result.Error.Message) > 0 {
|
||||
return transaction, errors.New(result.Error.Message)
|
||||
}
|
||||
parsed, err := strconv.ParseUint(result.Result.Value[2:], 16, 64)
|
||||
if err != nil {
|
||||
return transaction, err
|
||||
}
|
||||
transaction.Value = float64(parsed) / math.Pow10(18)
|
||||
return transaction, nil
|
||||
}
|
16
main.go
16
main.go
|
@ -102,18 +102,17 @@ func getRandomOpenSeaAsset() (Asset, error) {
|
|||
type downloadedOpenSeaAsset struct {
|
||||
Id string `json:"Id"`
|
||||
Asset `json:"Asset"`
|
||||
Transaction `json:"Transaction"`
|
||||
FileName string `json:"FileName"`
|
||||
}
|
||||
|
||||
func (d downloadedOpenSeaAsset) GetUSDPrice() float64 {
|
||||
return d.Transaction.Value * d.Asset.LastSale.TokenUSDPrice
|
||||
return d.Asset.LastSale.TotalPrice * d.Asset.LastSale.PaymentToken.USDPrice
|
||||
}
|
||||
func (d downloadedOpenSeaAsset) IsVideo() bool {
|
||||
return path.Ext(d.FileName) == ".mp4"
|
||||
}
|
||||
|
||||
func downloadOpenSeaAsset(asset Asset, transaction Transaction) (string, error) {
|
||||
func downloadOpenSeaAsset(asset Asset) (string, error) {
|
||||
id := getOpenSeaId(asset.Id)
|
||||
|
||||
u, err := url.Parse(asset.ImageUrl)
|
||||
|
@ -143,7 +142,6 @@ func downloadOpenSeaAsset(asset Asset, transaction Transaction) (string, error)
|
|||
err = json.NewEncoder(jsonFile).Encode(downloadedOpenSeaAsset{
|
||||
Id: id,
|
||||
Asset: asset,
|
||||
Transaction: transaction,
|
||||
FileName: filename,
|
||||
})
|
||||
if err != nil {
|
||||
|
@ -190,15 +188,7 @@ func main() {
|
|||
}
|
||||
log.Println(asset)
|
||||
|
||||
transaction, err := GetTransaction(asset.LastSale.TransactionHash)
|
||||
if err != nil {
|
||||
log.Println("Error consiguiendo transacción de Etherscan", err)
|
||||
internalError(w, "Error hablandole a Etherscan.")
|
||||
return
|
||||
}
|
||||
log.Println(transaction)
|
||||
|
||||
id, err := downloadOpenSeaAsset(asset, transaction)
|
||||
id, err := downloadOpenSeaAsset(asset)
|
||||
if err != nil {
|
||||
log.Println("Error descargando OpenSea asset", err)
|
||||
internalError(w, "Error descargando NFT.")
|
||||
|
|
35
opensea.go
35
opensea.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -17,8 +18,17 @@ type Asset struct {
|
|||
}
|
||||
|
||||
type AssetLastSale struct {
|
||||
TokenUSDPrice float64
|
||||
TransactionHash string
|
||||
PaymentToken
|
||||
// TotalPrice has 16 decimals trimmed off if PaymentToken.decimals > 16
|
||||
TotalPrice float64
|
||||
}
|
||||
|
||||
type PaymentToken struct {
|
||||
Id uint64
|
||||
Symbol string
|
||||
USDPrice float64
|
||||
Decimals int
|
||||
}
|
||||
|
||||
// https://docs.opensea.io/reference/getting-assets
|
||||
|
@ -33,8 +43,12 @@ func GetAssets() (Assets, error) {
|
|||
ImageUrl string `json:"image_url"`
|
||||
Name string `json:"name"`
|
||||
LastSale struct {
|
||||
TotalPrice string `json:"total_price"`
|
||||
PaymentToken struct {
|
||||
Id uint64 `json:"id"`
|
||||
Symbol string `json:"symbol"`
|
||||
USDPrice string `json:"usd_price"`
|
||||
Decimals int `json:"decimals"`
|
||||
} `json:"payment_token"`
|
||||
Transaction struct {
|
||||
Hash string `json:"transaction_hash"`
|
||||
|
@ -60,6 +74,17 @@ func GetAssets() (Assets, error) {
|
|||
if err != nil {
|
||||
return assets, err
|
||||
}
|
||||
trimmed := a.LastSale.TotalPrice
|
||||
decimals := a.LastSale.PaymentToken.Decimals
|
||||
if a.LastSale.PaymentToken.Decimals > 16 {
|
||||
trimmed = trimmed[:len(trimmed)-16]
|
||||
decimals = decimals - 16
|
||||
}
|
||||
totalprice, err := strconv.ParseFloat(trimmed, 64)
|
||||
if err != nil {
|
||||
return assets, err
|
||||
}
|
||||
totalprice = totalprice / math.Pow10(decimals)
|
||||
assets = append(assets, Asset{
|
||||
Id: strconv.FormatUint(a.Id, 10),
|
||||
TokenId: a.TokenId,
|
||||
|
@ -67,7 +92,13 @@ func GetAssets() (Assets, error) {
|
|||
ImageUrl: a.ImageUrl,
|
||||
Name: a.Name,
|
||||
LastSale: AssetLastSale{
|
||||
TokenUSDPrice: tokenUSDPrice,
|
||||
TotalPrice: totalprice,
|
||||
PaymentToken: PaymentToken{
|
||||
Id: a.LastSale.PaymentToken.Id,
|
||||
Symbol: a.LastSale.PaymentToken.Symbol,
|
||||
Decimals: decimals,
|
||||
USDPrice: tokenUSDPrice,
|
||||
},
|
||||
TransactionHash: a.LastSale.Transaction.Hash,
|
||||
},
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue