standards

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: Apache-2.0 Imports: 7 Imported by: 9

Documentation

Overview

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

Index

Constants

View Source
const (
	// 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

	// 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 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 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)

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 specifies the confidence level of the discovery.
	Confidence ConfidenceLevel `json:"confidence"`

	// ConfidencePoints specifies the confidence points of the discovery.
	ConfidencePoints float64 `json:"confidence_points"`

	// Threshold specifies the threshold level of the discovery.
	Threshold ConfidenceThreshold `json:"threshold"`

	// MaximumTokens specifies the maximum number of tokens in the standard.
	// This is basically a standard TokenCount() function response value.
	MaximumTokens int `json:"maximum_tokens"`

	// DiscoverdTokens specifies the number of tokens discovered in the standard.
	// The more tokens discovered, the higher the confidence level.
	DiscoveredTokens int `json:"discovered_tokens"`

	// ContractStandard that is being scanned.
	Standard Standard `json:"standard"`

	// Contract that is being scanned including mathed functions and events.
	Contract *ContractMatcher `json:"contract"`
}

Discovery represents a contract standard discovery response.

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)

	// TokenCount returns the number of tokens associated with the Ethereum standard.
	TokenCount() 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 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 (
	EIP20   Standard = "EIP20"   // ERC-20 Token Standard.
	EIP721  Standard = "EIP721"  // ERC-721 Non-Fungible Token Standard.
	EIP1822 Standard = "EIP1822" // EIP-1822 Universal Proxy Standard (UPS).
	EIP1820 Standard = "EIP1820" // EIP-1820 Pseudo-introspection Registry Contract.
	EIP777  Standard = "EIP777"  // ERC-777 Token Standard.
	EIP1155 Standard = "EIP1155" // ERC-1155 Multi Token Standard.
	EIP1337 Standard = "EIP1337" // ERC-1337 Subscription Standard.
	EIP1400 Standard = "EIP1400" // ERC-1400 Security Token Standard.
	EIP1410 Standard = "EIP1410" // ERC-1410 Partially Fungible Token Standard.
	EIP165  Standard = "EIP165"  // ERC-165 Standard Interface Detection.
	EIP820  Standard = "EIP820"  // ERC-820 Registry Standard.
	EIP1014 Standard = "EIP1014" // ERC-1014 Create2 Standard.
	EIP1948 Standard = "EIP1948" // ERC-1948 Non-Fungible Data Token Standard.
	EIP1967 Standard = "EIP1967" // EIP-1967 Proxy Storage Slots Standard.
	EIP2309 Standard = "EIP2309" // ERC-2309 Consecutive Transfer Standard.
	EIP2535 Standard = "EIP2535" // ERC-2535 Diamond Standard.
	EIP2771 Standard = "EIP2771" // ERC-2771 Meta Transactions Standard.
	EIP2917 Standard = "EIP2917" // ERC-2917 Interest-Bearing Tokens Standard.
	EIP3156 Standard = "EIP3156" // ERC-3156 Flash Loans Standard.
	EIP3664 Standard = "EIP3664" // ERC-3664 BitWords Standard.
)

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