standards

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2024 License: Apache-2.0 Imports: 7 Imported by: 6

Documentation

Overview

Package standards provides structures and functions to represent and manipulate Ethereum Improvement Proposals (EIPs) and Ethereum standards.

Index

Constants

View Source
const (
	// PerfectConfidenceThreshold represents a perfect confidence threshold value.
	PerfectConfidenceThreshold ConfidenceThreshold = 1.0

	// HighConfidenceThreshold represents a high confidence threshold value.
	HighConfidenceThreshold ConfidenceThreshold = 0.9

	// MediumConfidenceThreshold represents a medium confidence threshold value.
	MediumConfidenceThreshold ConfidenceThreshold = 0.5

	// LowConfidenceThreshold represents a low confidence threshold value.
	LowConfidenceThreshold ConfidenceThreshold = 0.1

	// NoConfidenceThreshold represents no confidence threshold value.
	NoConfidenceThreshold ConfidenceThreshold = 0.0

	// PerfectConfidence represents a perfect confidence level.
	PerfectConfidence ConfidenceLevel = 4

	// HighConfidence represents a high confidence level.
	HighConfidence ConfidenceLevel = 3

	// MediumConfidence represents a medium confidence level.
	MediumConfidence ConfidenceLevel = 2

	// LowConfidence represents a low confidence level.
	LowConfidence ConfidenceLevel = 1

	// NoConfidence represents no confidence level.
	NoConfidence ConfidenceLevel = 0
)
View Source
const (
	// TypeString represents the Ethereum "string" data type.
	TypeString = "string"

	// TypeAddress represents the Ethereum "address" data type.
	TypeAddress = "address"

	// TypeUint256 represents the Ethereum "uint256" data type.
	TypeUint256 = "uint256"

	// TypeBool represents the Ethereum "bool" data type.
	TypeBool = "bool"

	// TypeBytes represents the Ethereum "bytes" data type.
	TypeBytes = "bytes"

	// TypeBytes32 represents the Ethereum "bytes32" data type.
	TypeBytes32 = "bytes32"

	// TypeAddressArray represents an array of Ethereum "address" data types.
	TypeAddressArray = "address[]"

	// TypeUint256Array represents an array of Ethereum "uint256" data types.
	TypeUint256Array = "uint256[]"
)

Constants representing common Ethereum data types.

Variables

View Source
var (

	// ErrStandardNotFound is returned when a standard is not found.
	ErrStandardNotFound = errors.New("standard not found")
)

Functions

func CalculateDiscoveryConfidence

func CalculateDiscoveryConfidence(totalConfidence float64) (ConfidenceLevel, ConfidenceThreshold)

CalculateDiscoveryConfidence calculates the confidence level and threshold based on the total confidence.

func EventMatch added in v0.3.3

func EventMatch(newEvent *Event, standardEvent, event Event) (int, bool)

EventMatch matches an event from a contract to a standard event and returns the total token count and a boolean indicating if a match was found.

func Exists

func Exists(s Standard) bool

Exists checks if a given Ethereum standard is registered in the storage.

Parameters: - s: The Ethereum standard type.

Returns: - bool: A boolean indicating if the standard exists in the storage.

func FunctionMatch added in v0.3.3

func FunctionMatch(newFn *Function, standardFunction, contractFunction Function) (int, bool)

FunctionMatch matches a function from a contract to a standard function and returns the total token count and a boolean indicating if a match was found.

func FunctionTokenCount added in v0.3.3

func FunctionTokenCount(fn Function) int

FunctionTokenCount calculates the total number of tokens present in a given Ethereum smart contract function. It considers the function name as the initial token, then iterates over all inputs and outputs of the function. For each input and output, it increments the count by one for the parameter itself, an additional one if the type is specified (non-empty), and another for the 'Indexed' attribute (for inputs only), acknowledging it as a boolean. This count provides an estimate of the complexity or size of the function in terms of its components.

func GetProtoStandardFromString

func GetProtoStandardFromString(s string) (eip_pb.Standard, error)

GetProtoStandardFromString converts a string representation of an Ethereum standard to its corresponding protobuf enum value. If the standard is not recognized, it returns an error.

Parameters: s: The string representation of the Ethereum standard.

Returns: - The corresponding protobuf enum value of the Ethereum standard. - An error if the standard is not recognized.

func GetRegisteredStandards

func GetRegisteredStandards() map[Standard]EIP

GetRegisteredStandards retrieves all the registered Ethereum standards from the storage.

Returns: - map[Standard]ContractStandard: A map of all registered Ethereum standards.

func LoadStandards

func LoadStandards() error

LoadStandards loads list of supported Ethereum EIPs into the registry.

func RegisterStandard

func RegisterStandard(s Standard, cs EIP) error

RegisterStandard registers a new Ethereum standard to the storage. If the standard already exists, it returns an error.

Parameters: - s: The Ethereum standard type. - cs: The details of the Ethereum standard.

Returns: - error: An error if the standard already exists, otherwise nil.

func StandardsLoaded

func StandardsLoaded() bool

StandardsLoaded returns a boolean indicating whether the storage has any registered Ethereum standards.

func ToJSON

func ToJSON(data interface{}) ([]byte, error)

ToJSON converts a Go struct to its JSON representation.

func ToJSONPretty

func ToJSONPretty(data interface{}) ([]byte, error)

ToJSONPretty returns a pretty-printed JSON representation of the provided interface. This function is primarily used for debugging purposes.

func ToProtoJSON

func ToProtoJSON(data interface{}) ([]byte, error)

ToProtoJSON converts a Go struct to its JSON representation.

func TokenCount

func TokenCount(cs ContractStandard) int

TokenCount calculates and returns the total number of tokens (inputs and outputs) present in the functions and events of a given ContractStandard.

func WriteToFile

func WriteToFile(path string, data []byte) error

WriteToFile writes the provided data byte array to a file at the provided path.

Types

type ConfidenceLevel

type ConfidenceLevel int

ConfidenceLevel represents the confidence level of a discovery.

func (ConfidenceLevel) String

func (c ConfidenceLevel) String() string

String returns the string representation of the confidence level.

func (ConfidenceLevel) ToProto

ToProto converts a ConfidenceLevel to its protobuf representation.

type ConfidenceThreshold

type ConfidenceThreshold float64

ConfidenceThreshold represents the threshold value for a confidence level.

func (ConfidenceThreshold) ToProto

ToProto converts a ConfidenceThreshold to its protobuf representation.

type Contract

type Contract struct {
	// Standard holds the details of the contract standard.
	Standard ContractStandard
}

Contract represents the contract standard.

func (*Contract) ConfidenceCheck

func (e *Contract) ConfidenceCheck(contract *ContractMatcher) (Discovery, bool)

ConfidenceCheck performs a general confidence check of the contract standard against a provided contract matcher. It evaluates the entire contract standard to determine how closely it matches the criteria specified in the contract matcher. This method returns a Discovery struct that details the overall matching confidence and a boolean indicating if a match was found.

func (*Contract) FunctionConfidenceCheck added in v0.3.3

func (e *Contract) FunctionConfidenceCheck(fn *Function) (FunctionDiscovery, bool)

FunctionConfidenceCheck performs a confidence check on a specific function within the contract standard against a provided function matcher. It assesses whether the function in question matches the criteria defined in the function matcher, returning a FunctionDiscovery struct that details the matching confidence and a boolean indicating if a match was found.

func (*Contract) FunctionTokenCount added in v0.3.3

func (e *Contract) FunctionTokenCount(fnName string) int

FunctionTokenCount calculates the total number of tokens present in a given function of the contract standard. It searches for the function by its name within the contract's associated functions. If found, it calculates the token count based on the function's inputs, outputs, and their respective types. The function name is also considered as an initial token. If the function name is not found within the contract's functions, it returns 0.

func (*Contract) GetABI

func (e *Contract) GetABI() string

GetABI returns the ABI of the standard.

func (*Contract) GetEvents

func (e *Contract) GetEvents() []Event

GetEvents returns the events associated with the standard.

func (*Contract) GetFunctions

func (e *Contract) GetFunctions() []Function

GetFunctions returns the functions associated with the standard.

func (*Contract) GetName

func (e *Contract) GetName() string

GetName returns the name of the standard.

func (*Contract) GetStandard

func (e *Contract) GetStandard() ContractStandard

GetStandard returns the complete contract standard.

func (*Contract) GetType

func (e *Contract) GetType() Standard

GetType returns the type of the standard.

func (*Contract) GetUrl

func (e *Contract) GetUrl() string

GetUrl returns the URL of the standard.

func (*Contract) IsStagnant

func (e *Contract) IsStagnant() bool

IsStagnant returns a boolean indicating whether the standard is stagnant.

func (*Contract) String

func (e *Contract) String() string

String returns the name of the standard.

func (*Contract) ToProto

func (e *Contract) ToProto() *eip_pb.ContractStandard

ToProto returns a protobuf representation of the standard.

func (*Contract) TokenCount

func (e *Contract) TokenCount() int

TokenCount returns the number of tokens associated with the standard.

type ContractMatcher

type ContractMatcher struct {
	// Name of the contract.
	Name string `json:"name"`

	// Functions is a slice of Function structs, representing the functions defined in the contract standard.
	Functions []Function `json:"functions"`

	// Events is a slice of Event structs, representing the events defined in the contract standard.
	Events []Event `json:"events"`
}

ContractMatcher represents an Ethereum smart contract that attempts to confirm to a standard interface, such as the ERC-20 or ERC-721 standards. Used while performing a contract standard detection.

func (*ContractMatcher) ToProto

func (c *ContractMatcher) ToProto() *eip_pb.Contract

ToProto converts the Event to its protobuf representation.

type ContractStandard

type ContractStandard struct {
	// Name specifies the name of the contract standard, e.g., "ERC-20 Token Standard".
	Name string `json:"name"`

	// Url specifies the URL of the contract standard, e.g., "https://eips.ethereum.org/EIPS/eip-20".
	Url string `json:"url"`

	// Type specifies the type of the contract standard, e.g., ERC20 or ERC721.
	Type Standard `json:"type"`

	// Stagnant indicates whether the contract standard is stagnant in terms of development.
	Stagnant bool `json:"stagnant"`

	// ABI specifies the ABI of the contract standard.
	ABI string `json:"abi"`

	// Functions is a slice of Function structs, representing the functions defined in the contract standard.
	Functions []Function `json:"functions"`

	// Events is a slice of Event structs, representing the events defined in the contract standard.
	Events []Event `json:"events"`
}

ContractStandard represents a standard interface for Ethereum smart contracts, such as the ERC-20 or ERC-721 standards.

func (*ContractStandard) ToProto

func (cs *ContractStandard) ToProto() *eip_pb.ContractStandard

ToProto converts the ContractStandard to its protobuf representation.

type Discovery

type Discovery struct {
	Confidence       ConfidenceLevel     `json:"confidence"`        // Confidence level of the discovery.
	ConfidencePoints float64             `json:"confidence_points"` // Confidence points of the discovery.
	Threshold        ConfidenceThreshold `json:"threshold"`         // Threshold level of the discovery.
	MaximumTokens    int                 `json:"maximum_tokens"`    // Maximum number of tokens in the standard.
	DiscoveredTokens int                 `json:"discovered_tokens"` // Number of tokens discovered in the standard.
	Standard         Standard            `json:"standard"`          // Contract standard being scanned.
	Contract         *ContractMatcher    `json:"contract"`          // Contract including matched functions and events.
}

Discovery represents the result of attempting to discover a contract standard.

func ConfidenceCheck

func ConfidenceCheck(standard EIP, contract *ContractMatcher) (Discovery, bool)

ConfidenceCheck checks the confidence of a contract against a standard EIP.

func (*Discovery) ToProto

func (d *Discovery) ToProto() *eip_pb.Discovery

ToProto converts the Discovery to its protobuf representation.

type EIP

type EIP interface {
	// GetName returns the name of the Ethereum standard, e.g., "ERC-20 Token Standard".
	GetName() string

	// GetType returns the type of the Ethereum standard, e.g., ERC20 or ERC721.
	GetType() Standard

	// GetFunctions returns a slice of Function structs, representing the
	// functions defined in the Ethereum standard.
	GetFunctions() []Function

	// GetUrl returns the URL of the Ethereum standard.
	GetUrl() string

	// IsStagnant returns a boolean indicating whether the Ethereum standard is stagnant.
	IsStagnant() bool

	// GetEvents returns a slice of Event structs, representing the
	// events defined in the Ethereum standard.
	GetEvents() []Event

	// GetStandard returns the complete representation of the Ethereum standard.
	GetStandard() ContractStandard

	// ConfidenceCheck returns a discovery confidence information and a boolean indicating whether
	// the contract is to any level compliant with the Ethereum standard.
	ConfidenceCheck(contract *ContractMatcher) (Discovery, bool)

	// FunctionConfidenceCheck returns a discovery confidence information and a boolean indicating whether
	// the contract function is to any level compliant with the Ethereum standard.
	FunctionConfidenceCheck(fn *Function) (FunctionDiscovery, bool)

	// TokenCount returns the number of tokens associated with the Ethereum standard.
	TokenCount() int

	FunctionTokenCount(fnName string) int

	// GetABI returns the ABI of the Ethereum standard.
	GetABI() string

	// ToProto converts the Ethereum standard to its protobuf representation.
	ToProto() *eip_pb.ContractStandard

	// String returns a string representation of the Ethereum standard, typically its name.
	String() string
}

EIP is an interface that defines the standard methods required for representing Ethereum Improvement Proposals and Ethereum standards.

func GetContractByStandard

func GetContractByStandard(standard Standard) (EIP, error)

GetContractByStandard returns the contract standard by its type.

func GetSortedRegisteredStandards

func GetSortedRegisteredStandards() []EIP

GetSortedRegisteredStandards retrieves all the registered Ethereum standards from the storage in a sorted order.

func GetStandard

func GetStandard(s Standard) (EIP, bool)

GetStandard retrieves the details of a registered Ethereum standard.

Parameters: - s: The Ethereum standard type.

Returns: - ContractStandard: The details of the Ethereum standard if it exists. - bool: A boolean indicating if the standard exists in the storage.

func NewContract

func NewContract(standard ContractStandard) EIP

NewContract initializes and returns an instance of the standard. It sets up the standard with its name, type, associated functions, and events.

type Event

type Event struct {
	// Name specifies the name of the event.
	Name string `json:"name"`

	// Inputs is a slice of Input structs, representing the input parameters of the event.
	Inputs []Input `json:"inputs"`

	// Outputs is a slice of Output structs, representing the data types of the event's return values.
	Outputs []Output `json:"outputs"`

	// Matched indicates whether the input has been matched via confidence check.
	Matched bool `json:"matched"`
}

Event represents an Ethereum smart contract event.

func (*Event) ToProto

func (e *Event) ToProto() *eip_pb.Event

ToProto converts the Event to its protobuf representation.

type Function

type Function struct {
	// Name specifies the name of the function.
	Name string `json:"name"`

	// Inputs is a slice of Input structs, representing the input parameters of the function.
	Inputs []Input `json:"inputs"`

	// Outputs is a slice of Output structs, representing the data types of the function's return values.
	Outputs []Output `json:"outputs"`

	// Matched indicates whether the input has been matched via confidence check.
	Matched bool `json:"matched"`
}

Function represents an Ethereum smart contract function.

func (*Function) ToProto

func (f *Function) ToProto() *eip_pb.Function

ToProto converts the Function to its protobuf representation.

type FunctionDiscovery added in v0.3.3

type FunctionDiscovery struct {
	Confidence       ConfidenceLevel     `json:"confidence"`        // Confidence level of the discovery.
	ConfidencePoints float64             `json:"confidence_points"` // Confidence points of the discovery.
	Threshold        ConfidenceThreshold `json:"threshold"`         // Threshold level of the discovery.
	MaximumTokens    int                 `json:"maximum_tokens"`    // Maximum number of tokens in the standard.
	DiscoveredTokens int                 `json:"discovered_tokens"` // Number of tokens discovered in the standard.
	Standard         Standard            `json:"standard"`          // Contract standard being scanned.
	Function         *Function           `json:"function"`          // Matched function in the contract.
}

FunctionDiscovery represents the result of attempting to discover a function within a contract standard.

func FunctionConfidenceCheck added in v0.3.3

func FunctionConfidenceCheck(standard EIP, fn *Function) (FunctionDiscovery, bool)

FunctionConfidenceCheck checks for function confidence against provided EIP standard

type FunctionMatcher added in v0.3.3

type FunctionMatcher struct {
	// Name specifies the name of the contract.
	Name string `json:"name"`

	// Functions is a list of Function structs, representing the functions defined by the contract.
	Functions []Function `json:"functions"`
}

FunctionMatcher represents an Ethereum smart contract focusing on matching specific functions to a standard interface, such as those defined by ERC-20 or ERC-721 standards.

type Input

type Input struct {
	// Type specifies the Ethereum data type of the input.
	Type string `json:"type"`

	// Indexed indicates whether the input is indexed.
	// This is particularly relevant for event parameters,
	// where indexed parameters can be used as a filter for event logs.
	Indexed bool `json:"indexed"`

	// Matched indicates whether the input has been matched via confidence check.
	Matched bool `json:"matched"`
}

Input represents an input parameter for Ethereum functions and events.

func (*Input) ToProto

func (i *Input) ToProto() *eip_pb.Input

ToProto converts the Input to its protobuf representation.

type Output

type Output struct {
	// Type specifies the Ethereum data type of the output.
	Type string `json:"type"`

	// Matched indicates whether the output has been matched via confidence check.
	Matched bool `json:"matched"`
}

Output represents an output parameter for Ethereum functions and events.

func (*Output) ToProto

func (i *Output) ToProto() *eip_pb.Output

ToProto converts the Output to its protobuf representation.

type Standard

type Standard string

Standard represents the type for Ethereum standards and EIPs.

const (
	ERC20     Standard = "ERC20"     // ERC-20 Token Standard.
	ERC721    Standard = "ERC721"    // ERC-721 Non-Fungible Token Standard.
	ERC1822   Standard = "ERC1822"   // ERC-1822 Universal Proxy Standard (UPS).
	ERC1820   Standard = "ERC1820"   // ERC-1820 Pseudo-introspection Registry Contract.
	ERC777    Standard = "ERC777"    // ERC-777 Token Standard.
	ERC1155   Standard = "ERC1155"   // ERC-1155 Multi Token Standard.
	ERC1337   Standard = "ERC1337"   // ERC-1337 Subscription Standard.
	ERC1400   Standard = "ERC1400"   // ERC-1400 Security Token Standard.
	ERC1410   Standard = "ERC1410"   // ERC-1410 Partially Fungible Token Standard.
	ERC165    Standard = "ERC165"    // ERC-165 Standard Interface Detection.
	ERC820    Standard = "ERC820"    // ERC-820 Registry Standard.
	ERC1014   Standard = "ERC1014"   // ERC-1014 Create2 Standard.
	ERC1948   Standard = "ERC1948"   // ERC-1948 Non-Fungible Data Token Standard.
	ERC1967   Standard = "ERC1967"   // ERC-1967 Proxy Storage Slots Standard.
	ERC2309   Standard = "ERC2309"   // ERC-2309 Consecutive Transfer Standard.
	ERC2535   Standard = "ERC2535"   // ERC-2535 Diamond Standard.
	ERC2771   Standard = "ERC2771"   // ERC-2771 Meta Transactions Standard.
	ERC2917   Standard = "ERC2917"   // ERC-2917 Interest-Bearing Tokens Standard.
	ERC3156   Standard = "ERC3156"   // ERC-3156 Flash Loans Standard.
	ERC3664   Standard = "ERC3664"   // ERC-3664 BitWords Standard.
	UNISWAPV2 Standard = "UNISWAPV2" // Uniswap V2 Core.
	OZOWNABLE Standard = "OZOWNABLE" // OpenZeppelin Ownable.
)

Constants representing various Ethereum standards and EIPs.

func (Standard) String

func (s Standard) String() string

String returns the string representation of the Standard.

func (Standard) ToProto

func (s Standard) ToProto() eip_pb.Standard

ToProto() converts a string representation of an Ethereum standard to its corresponding protobuf enum value. If the standard is not recognized, it returns unknown.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL