Documentation
¶
Overview ¶
Package erc721 provides functionality associated with ERC721 NFTs.
Index ¶
Constants ¶
const TokenIDParam = "tokenId"
TokenIDParam is the name of the httprouter parameter matched by metadata and image endpoints. Examples of valid paths include:
/metadata/:tokenId /images/:tokenId /path/to/metadata/:tokenId/:otherParam/passed/to/user/functions
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attribute ¶
type Attribute struct { TraitType string `json:"trait_type,omitempty"` Value interface{} `json:"value"` DisplayType OpenSeaDisplayType `json:"display_type,omitempty"` }
An Attribute is a single attribute in Metadata.
type Collection ¶
A Collection is a set of Metadata, each associated with a single token ID.
func CollectionFromMetadata ¶
func CollectionFromMetadata(md []*Metadata) Collection
CollectionFromMetadata assumes that each token's ID is its index in the received Metadata slice, returning the respective Collection map.
func (Collection) Rarity ¶
func (coll Collection) Rarity(bucket func(interface{}) string) *Rarity
Rarity computes rarity of each token in the Collection based on information entropy. Every TraitType is considered as a categorical probability distribution with each Value having an associated probability and hence information content. The rarity of a particular token is the sum of information content carried by each of its Attributes, divided by the entropy of the Collection as a whole (see the Rarity struct for rationale).
Notably, the lack of a TraitType is considered as a null-Value Attribute as the absence across the majority of a Collection implies rarity in those tokens that do carry the TraitType.
Non-string Attribute Values are passed to the bucket function. The returned bucket is used in place of original value. It is valid for the bucket function to simply return the string equivalent (e.g. true/false for booleans).
type Metadata ¶
type Metadata struct { Name string `json:"name"` Description string `json:"description,omitempty"` Image string `json:"image"` AnimationURL string `json:"animation_url,omitempty"` ExternalURL string `json:"external_url,omitempty"` Attributes []*Attribute `json:"attributes,omitempty"` }
Metadata carries a parsed JSON payload from ERC721 metadata, compatible with OpenSea.
type MetadataServer ¶
type MetadataServer struct { // BaseURL is the base URL of the server; i.e. everything except the path, // which will be overwritten. BaseURL *url.URL // MetadataPath and ImagePath are the routing paths for metadata and images // respectively. They follow syntax of github.com/julienschmidt/httprouter // and use the TokenIDParam to extract the token ID. MetadataPath, ImagePath string // TokenIDBase, if non-zero, uses a custom base for decoding the token ID, // defaulting to base 10. TokenIDBase int // The Contract, if provided, is used to confirm that tokens exist before // responding with metadata or images. Checks use the ownerOf function, // which must not revert. Contract Interface // Metadata and Image are responsible for returning a token's metadata and // image, respectively (surprise, surprise!). If Contract is non-nil, the // token is guaranteed to exist if Metadata/Image is called. Only 200, 400, // 404 and 500 are allowed as HTTP codes, and these will be propagated to // the end user. Metadata func(Interface, *TokenID, httprouter.Params) (md *Metadata, httpCode int, err error) Image func(Interface, *TokenID, httprouter.Params) (img io.Reader, contentType string, httpCode int, err error) }
A MetadataServer handles HTTP routes to serve ERC721 metadata JSON and associated images. If a contract binding is provided, it is checked to ensure that the requested token already exists, thus allowing a MetadataServer to be used for delayed reveals.
func (*MetadataServer) Handler ¶
func (s *MetadataServer) Handler() (http.Handler, error)
Handler returns a Handler, for use with http.ListenAndServe(), that handles all requests for metadata and images. Unless the Handler is specifically needed for non-default uses, prefer s.ListenAndServer().
func (*MetadataServer) ListenAndServe ¶
func (s *MetadataServer) ListenAndServe(addr string) error
ListenAndServe returns http.ListenAndServe(addr, s.Handler()).
type OpenSeaDisplayType ¶
type OpenSeaDisplayType int
An OpenSeaDisplayType is an OpenSea-specific metadata concept to control how their UI treats numerical values.
See https://docs.opensea.io/docs/metadata-standards for details.
const ( DisplayDefault OpenSeaDisplayType = iota DisplayNumber DisplayBoostNumber DisplayBoostPercentage DisplayDate )
Allowable OpenSeaDisplayType values.
func (OpenSeaDisplayType) MarshalJSON ¶
func (t OpenSeaDisplayType) MarshalJSON() ([]byte, error)
MarshalJSON returns the display type as JSON.
func (OpenSeaDisplayType) String ¶
func (t OpenSeaDisplayType) String() string
String returns the display type as a string.
func (*OpenSeaDisplayType) UnmarshalJSON ¶
func (t *OpenSeaDisplayType) UnmarshalJSON(buf []byte) error
UnmarshalJSON parses the JSON buffer into the display type.
type Rarity ¶
Rarity describes the information-theoretic "rarity" of a Collection.
The concept of "rarity" can be considered as a measure of "surprise" at the occurrence of a particular token's properties, within the context of the Collection from which it is derived. Self-information is a measure of such surprise, and information entropy a measure of the expected value of self-information across a distribution (i.e. across a Collection).
It is trivial to "stuff" a Collection with extra information by merely adding additional properties to all tokens. This is reflected in the Entropy field, measured in bits—all else held equal, a Collection with more token properties will have higher Entropy. However, this information bloat is carried by the tokens themselves, so their individual information-content grows in line with Collection-wide Entropy. The Scores are therefore scaled down by the Entropy to provide unitless "relative surprise", which can be safely compared between Collections.
type TokenID ¶
type TokenID [32]byte
A TokenID is a uint256 Solidity tokenId.
func TokenIDFromBig ¶
TokenIDFromBig returns TokenIDFromUint256(uint256.FromBig(b)), or an error if b overflows 256 bits.
func TokenIDFromHex ¶
TokenIDFromHex returns TokenIDFromUint256(uint256.FromHex(b)), or any error returned by FromHex().
func TokenIDFromInt ¶
TokenIDFromInt returns TokenIDFromUint256(uint256.NewInt(uint64(i))).
func TokenIDFromUint256 ¶
TokenIDFromUint256 returns the 32-byte buffer underlying u, typed as a TokenID pointer.
func TokenIDFromUint64 ¶
TokenIDFromUint64 returns TokenIDFromUint256(uint256.NewInt(u)).