From 048511cc993a93c0538285a78af09af0c56c9d2c Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Mon, 30 Aug 2021 09:50:18 +0200 Subject: [PATCH] feat: Add trait to collectible Build a custom unmarshaller for field that can be int or string see: https://docs.opensea.io/reference/asset-object --- services/wallet/opensea.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/services/wallet/opensea.go b/services/wallet/opensea.go index f217581e2..563d3dbaf 100644 --- a/services/wallet/opensea.go +++ b/services/wallet/opensea.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "strconv" "time" "github.com/ethereum/go-ethereum/common" @@ -14,6 +15,23 @@ import ( const AssetLimit = 50 const CollectionLimit = 300 +type TraitValue string + +func (st *TraitValue) UnmarshalJSON(b []byte) error { + var item interface{} + if err := json.Unmarshal(b, &item); err != nil { + return err + } + switch v := item.(type) { + case int: + *st = TraitValue(strconv.Itoa(v)) + case string: + *st = TraitValue(v) + + } + return nil +} + type OpenseaAssetContainer struct { Assets []OpenseaAsset `json:"assets"` } @@ -26,6 +44,12 @@ type OpenseaContract struct { Address string `json:"address"` } +type OpenseaTrait struct { + TraitType string `json:"trait_type"` + Value TraitValue `json:"value"` + DisplayType string `json:"display_type"` +} + type OpenseaAsset struct { ID int `json:"id"` Name string `json:"name"` @@ -35,6 +59,7 @@ type OpenseaAsset struct { ImageURL string `json:"image_url"` Contract OpenseaContract `json:"asset_contract"` Collection OpenseaAssetCollection `json:"collection"` + Traits []OpenseaTrait `json:"traits"` } type OpenseaCollection struct {