contracts

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 24 Imported by: 3

Documentation

Overview

Package integrates various Ethereum-related functionalities including interaction with blockchain clients, fetching contract metadata from external providers such as etherscan, token discovery, bytecode validation, auditing, etc...

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Contract

type Contract struct {
	// contains filtered or unexported fields
}

Contract represents an Ethereum smart contract within the context of a specific network. It encapsulates the contract's address, network information, and associated metadata, and provides methods to interact with the contract on the blockchain.

func NewContract

func NewContract(ctx context.Context, network utils.Network, clientPool *clients.ClientPool, stor *storage.Storage, bqp *bitquery.Provider, etherscan *etherscan.Provider, compiler *solc.Solc, bindManager *bindings.Manager, ipfsProvider metadata.Provider, addr common.Address) (*Contract, error)

NewContract creates a new instance of Contract for a given Ethereum address and network. It initializes the contract's context, metadata, and associated blockchain clients. The function validates the contract's existence and its bytecode before creation.

func (*Contract) Audit

func (c *Contract) Audit(ctx context.Context) error

Audit performs a security analysis of the contract using its associated detector, if available. It updates the contract descriptor with the audit results.

func (*Contract) DecodeLog

func (c *Contract) DecodeLog(ctx context.Context, log *types.Log) (*Log, error)

DecodeLog takes a raw Ethereum log entry and attempts to decode it into a structured Log instance using the contract's ABI. This method enables easier interpretation of log entries, which are crucial for monitoring contract events and state changes.

func (*Contract) DecodeTransaction

func (c *Contract) DecodeTransaction(ctx context.Context, data []byte) (*bytecode.Transaction, error)

DecodeTransaction attempts to decode a given transaction's data into a structured format by leveraging the contract's ABI (Application Binary Interface). This process is crucial for understanding the specifics of contract interactions encoded in transaction data.

func (*Contract) DiscoverChainInfo

func (c *Contract) DiscoverChainInfo(ctx context.Context, otsLookup bool) error

DiscoverChainInfo retrieves information about the contract's deployment chain, including transaction, receipt, and block details. If `otsLookup` is true, it queries the contract creator's information using the provided context. If `otsLookup` is false or if the creator's information is not available, it queries the contract creation transaction hash using etherscan. It then fetches the transaction, receipt, and block information associated with the contract deployment from the blockchain. This method populates the contract descriptor with the retrieved information.

func (*Contract) DiscoverConstructor

func (c *Contract) DiscoverConstructor(ctx context.Context) error

DiscoverConstructor discovers and decodes the constructor of the contract based on the provided context. It utilizes the contract's descriptor to gather information about the contract's bytecode, ABI, and transaction data. If a constructor is found in the bytecode, it decodes it using the provided ABI. The decoded constructor information is stored within the contract descriptor.

func (*Contract) DiscoverDeployedBytecode

func (c *Contract) DiscoverDeployedBytecode() error

DiscoverDeployedBytecode retrieves the deployed bytecode of the contract deployed at the specified address. It queries the blockchain using the provided client to fetch the bytecode associated with the contract address. The fetched bytecode is then stored in the contract descriptor for further processing.

func (*Contract) DiscoverMetadata

func (c *Contract) DiscoverMetadata(ctx context.Context) (*bytecode.Metadata, error)

DiscoverMetadata attempts to extract metadata from the contract's deployed bytecode. This method decodes the bytecode to retrieve metadata information, such as the compiler version used for deployment and the content of the deployed code. It is critical in understanding the capabilities and design of the contract on the blockchain.

func (*Contract) DiscoverOwner

func (c *Contract) DiscoverOwner(ctx context.Context) error

DiscoverOwner queries the blockchain to find the current owner of the contract and updates the contract's descriptor with the owner's address.

func (*Contract) DiscoverSourceCode

func (c *Contract) DiscoverSourceCode(ctx context.Context) error

DiscoverSourceCode queries Etherscan (or another configured source code provider) to retrieve the source code and metadata for the smart contract at the specified address. It attempts to enrich the contract's descriptor with details such as the source code, compiler version, optimization settings, and contract ABI, among others.

This method implements a retry mechanism to handle rate limiting by the provider. It logs and returns errors encountered during the process, except for cases where the contract source code is not found or not verified, which are considered non-critical errors.

func (*Contract) DiscoverToken

func (c *Contract) DiscoverToken(ctx context.Context, atBlock *big.Int) error

DiscoverToken retrieves token-related metadata for the contract at a specific block height.

func (*Contract) GetAddress

func (c *Contract) GetAddress() common.Address

GetAddress returns the Ethereum address of the contract.

func (*Contract) GetBlock

func (c *Contract) GetBlock() *types.Header

GetBlock returns the blockchain block in which the contract was deployed or involved.

func (*Contract) GetDeployedBytecode

func (c *Contract) GetDeployedBytecode() []byte

GetDeployedBytecode returns the deployed bytecode of the contract. This bytecode is the compiled contract code that exists on the Ethereum blockchain.

func (*Contract) GetDescriptor

func (c *Contract) GetDescriptor() *Descriptor

GetDescriptor a public member to return back processed contract descriptor

func (*Contract) GetExecutionBytecode

func (c *Contract) GetExecutionBytecode() []byte

GetExecutionBytecode returns the runtime bytecode of the contract. This bytecode is used during the execution of contract calls and transactions.

func (*Contract) GetNetwork

func (c *Contract) GetNetwork() utils.Network

GetNetwork returns the network (e.g., Mainnet, Ropsten) on which the contract is deployed.

func (*Contract) GetReceipt

func (c *Contract) GetReceipt() *types.Receipt

GetReceipt returns the receipt of the transaction in which the contract was involved, providing details such as gas used and logs generated.

func (*Contract) GetSender

func (c *Contract) GetSender() (common.Address, error)

GetSender returns the Ethereum address of the sender of the contract's transaction. It extracts the sender's address using the transaction's signature.

func (*Contract) GetToken

func (c *Contract) GetToken() *tokens.Token

GetToken returns contract related discovered token (if found)

func (*Contract) GetTransaction

func (c *Contract) GetTransaction() *types.Transaction

GetTransaction returns the Ethereum transaction associated with the contract's deployment or a specific operation.

func (*Contract) IsRenounced

func (c *Contract) IsRenounced() bool

IsRenounced checks if the contract's ownership has been renounced by comparing the owner's address against a predefined list of "zero" addresses, which represent renounced ownership in various contexts. This method helps in identifying contracts that are deliberately made ownerless, usually as a measure of decentralization or to prevent further administrative intervention.

func (*Contract) IsValid

func (c *Contract) IsValid() (bool, error)

IsValid checks if the contract is valid by verifying its deployed bytecode. A contract is considered valid if it has non-empty deployed bytecode on the blockchain.

func (*Contract) Parse

func (c *Contract) Parse(ctx context.Context) error

Parse processes the source code of the contract to update its metadata, including detecting contract features and parsing its source to build an intermediate representation (IR).

func (*Contract) SetBlock

func (c *Contract) SetBlock(block *types.Header)

SetBlock sets the blockchain block in which the contract was deployed or involved.

func (*Contract) SetReceipt

func (c *Contract) SetReceipt(receipt *types.Receipt)

SetReceipt sets the receipt of the transaction in which the contract was involved, providing details such as gas used and logs generated.

func (*Contract) SetTransaction

func (c *Contract) SetTransaction(tx *types.Transaction)

SetTransaction sets the Ethereum transaction associated with the contract's deployment or a specific operation.

type Descriptor

type Descriptor struct {
	Network           utils.Network      `json:"network"`
	NetworkID         utils.NetworkID    `json:"network_id"`
	Address           common.Address     `json:"address"`
	ExecutionBytecode []byte             `json:"execution_bytecode"`
	DeployedBytecode  []byte             `json:"deployed_bytecode"`
	Block             *types.Header      `json:"block,omitempty"`
	Transaction       *types.Transaction `json:"transaction,omitempty"`
	Receipt           *types.Receipt     `json:"receipt,omitempty"`
	ABI               string             `json:"abi,omitempty"`
	Name              string             `json:"name,omitempty"`
	License           string             `json:"license,omitempty"`
	SolgoVersion      string             `json:"solgo_version,omitempty"`
	CompilerVersion   string             `json:"compiler_version,omitempty"`
	Optimized         bool               `json:"optimized,omitempty"`
	OptimizationRuns  uint64             `json:"optimization_runs,omitempty"`
	EVMVersion        string             `json:"evm_version,omitempty"`

	// Identity related fields
	Owner common.Address `json:"owner,omitempty"`

	// SourcesRaw is the raw sources from Etherscan|BscScan|etc. Should not be used anywhere except in
	// the contract discovery process.
	SourcesRaw      *etherscan.Contract `json:"-"`
	Sources         *solgo.Sources      `json:"sources,omitempty"`
	SourcesUnsorted *solgo.Sources      `json:"-"`
	SourceProvider  string              `json:"source_provider,omitempty"`

	// Source detection related fields.
	Detector    *detector.Detector    `json:"-"`
	IRRoot      *ir.RootSourceUnit    `json:"ir,omitempty"`
	Constructor *bytecode.Constructor `json:"constructor,omitempty"`
	Metadata    *bytecode.Metadata    `json:"metadata,omitempty"`

	// Auditing related fields.
	Verified             bool          `json:"verified,omitempty"`
	VerificationProvider string        `json:"verification_provider,omitempty"`
	Safe                 bool          `json:"safe,omitempty"`
	Audit                *audit.Report `json:"audit,omitempty"`

	// Proxy
	Proxy           bool             `json:"proxy"`
	Implementations []common.Address `json:"implementations"`

	// Token related fields.
	Token *tokens.Descriptor `json:"token,omitempty"`
}

Descriptor encapsulates comprehensive metadata about an Ethereum contract. It includes network information, bytecode data, ownership details, and associated resources like ABI, source code, and audit reports. This type serves as a central repository for detailed insights into contract specifications and state.

func (*Descriptor) GetABI

func (d *Descriptor) GetABI() string

GetABI returns the contract's ABI (Application Binary Interface), essential for interacting with the contract.

func (*Descriptor) GetAddress

func (d *Descriptor) GetAddress() common.Address

GetAddress returns the Ethereum address of the contract.

func (*Descriptor) GetAudit

func (d *Descriptor) GetAudit() *audit.Report

GetAudit returns the audit report for the contract, if available. This report includes findings and security analysis details.

func (*Descriptor) GetBlock

func (d *Descriptor) GetBlock() *types.Header

GetBlock returns the block header in which the contract transaction was included, providing context such as block number and hash.

func (*Descriptor) GetCompilerVersion

func (d *Descriptor) GetCompilerVersion() string

GetCompilerVersion returns the version of the compiler used to compile the contract.

func (*Descriptor) GetConstructor

func (d *Descriptor) GetConstructor() *bytecode.Constructor

GetConstructor returns the constructor details of the contract, if available, including input parameters and bytecode.

func (*Descriptor) GetDeployedBytecode

func (d *Descriptor) GetDeployedBytecode() []byte

GetDeployedBytecode returns the deployed bytecode of the contract, which is the code stored on the blockchain.

func (*Descriptor) GetDetector

func (d *Descriptor) GetDetector() *detector.Detector

GetDetector returns the detector used for analyzing the contract, providing access to advanced analysis tools like IR generation.

func (*Descriptor) GetEVMVersion

func (d *Descriptor) GetEVMVersion() string

GetEVMVersion returns the EVM version target specified during the contract's compilation.

func (*Descriptor) GetExecutionBytecode

func (d *Descriptor) GetExecutionBytecode() []byte

GetExecutionBytecode returns the execution bytecode of the contract, which is the code that is executed on the Ethereum Virtual Machine.

func (*Descriptor) GetIrRoot

func (d *Descriptor) GetIrRoot() *ir.RootSourceUnit

GetIrRoot returns the IR root source unit, providing insights into the contract's structure.

func (*Descriptor) GetLicense

func (d *Descriptor) GetLicense() string

GetLicense returns the software license under which the contract source code is distributed.

func (*Descriptor) GetMetadata

func (d *Descriptor) GetMetadata() *bytecode.Metadata

GetMetadata returns the contract's metadata, including the compiler version, language, and settings used for compilation.

func (*Descriptor) GetName

func (d *Descriptor) GetName() string

GetName returns the name of the contract.

func (*Descriptor) GetNetwork

func (d *Descriptor) GetNetwork() utils.Network

GetNetwork returns the network (e.g., Mainnet, Ropsten) on which the contract is deployed.

func (*Descriptor) GetNetworkID

func (d *Descriptor) GetNetworkID() utils.NetworkID

GetNetworkID returns the unique identifier of the network on which the contract is deployed.

func (*Descriptor) GetOptimizationRuns

func (d *Descriptor) GetOptimizationRuns() uint64

GetOptimizationRuns returns the number of optimization runs performed during the contract's compilation.

func (*Descriptor) GetOwner

func (d *Descriptor) GetOwner() common.Address

GetOwner returns the address recognized as the owner of the contract, who has administrative privileges.

func (*Descriptor) GetReceipt

func (d *Descriptor) GetReceipt() *types.Receipt

GetReceipt returns the receipt of the transaction through which the contract was created or interacted with, detailing execution outcome.

func (*Descriptor) GetSolgoVersion

func (d *Descriptor) GetSolgoVersion() string

GetSolgoVersion returns the version of the Solgo tool used in processing the contract.

func (*Descriptor) GetSourceProvider

func (d *Descriptor) GetSourceProvider() string

GetSourceProvider returns the name of the source provider, indicating where the contract source was obtained from.

func (*Descriptor) GetSources

func (d *Descriptor) GetSources() *solgo.Sources

GetSources returns the parsed sources of the contract, providing a structured view of the contract's code.

func (*Descriptor) GetSourcesRaw

func (d *Descriptor) GetSourcesRaw() *etherscan.Contract

GetSourcesRaw returns the raw contract source as obtained from external providers like Etherscan.

func (*Descriptor) GetToken

func (d *Descriptor) GetToken() *tokens.Descriptor

GetToken returns the token descriptor associated with the contract, if available. This descriptor contains detailed token information such as name, symbol, and decimals.

func (*Descriptor) GetTransaction

func (d *Descriptor) GetTransaction() *types.Transaction

GetTransaction returns the transaction through which the contract was created or interacted with.

func (*Descriptor) GetUnsortedSources added in v0.3.5

func (d *Descriptor) GetUnsortedSources() *solgo.Sources

GetUnsortedSources returns the parsed sources of the contract, providing a structured view of the contract's code.

func (*Descriptor) GetVerificationProvider

func (d *Descriptor) GetVerificationProvider() string

GetVerificationProvider returns the name of the service provider where the contract's source code was verified.

func (*Descriptor) HasAudit

func (d *Descriptor) HasAudit() bool

HasAudit checks if an audit report is available for the contract.

func (*Descriptor) HasAuditReport

func (d *Descriptor) HasAuditReport() bool

HasAuditReport checks if an audit report is available for the contract, providing insights into security assessments.

func (*Descriptor) HasConstructor

func (d *Descriptor) HasConstructor() bool

HasConstructor checks if contract constructor details are available.

func (*Descriptor) HasContracts

func (d *Descriptor) HasContracts() bool

HasContracts checks if the contract or its components have been detected.

func (*Descriptor) HasDetector

func (d *Descriptor) HasDetector() bool

HasDetector checks if a detector for analyzing the contract is available.

func (*Descriptor) HasMetadata

func (d *Descriptor) HasMetadata() bool

HasMetadata checks if metadata is available for the contract.

func (*Descriptor) HasSources

func (d *Descriptor) HasSources() bool

HasSources checks if source code details are available for the contract.

func (*Descriptor) HasToken

func (d *Descriptor) HasToken() bool

HasToken checks if the contract has token details available.

func (*Descriptor) IsOptimized

func (d *Descriptor) IsOptimized() bool

IsOptimized checks whether the contract compilation was optimized, returning true if so.

func (*Descriptor) IsSafe

func (d *Descriptor) IsSafe() bool

IsSafe indicates whether the contract has been marked as safe based on audits or security reports.

func (*Descriptor) IsVerified

func (d *Descriptor) IsVerified() bool

IsVerified indicates whether the contract's source code has been verified on platforms like Etherscan.

func (*Descriptor) SetCompilerVersion

func (d *Descriptor) SetCompilerVersion(ver string)

SetCompilerVersion sets the compiler version used for the contract.

type Log

type Log struct {
	Log         *types.Log     // The original log entry from Ethereum.
	Address     common.Address // The address from which the log originated.
	Topics      []common.Hash  // Topics are essentially event signatures and optionally indexed event parameters.
	Data        []byte         // The data contains non-indexed event parameters.
	BlockNumber uint64         // The block number where this log was recorded.
	TxHash      common.Hash    // The hash of the transaction that generated this log.
	TxIndex     uint           // The index of the transaction in the block.
	BlockHash   common.Hash    // The hash of the block where this log was recorded.
	Index       uint           // The log's index position in the block.
	Removed     bool           // Flag indicating if the log was reverted due to a chain reorganization.
	DecodedLog  *bytecode.Log  // The decoded log information, providing a structured interpretation of the data.
}

Log represents a structured version of an Ethereum log entry, including both the raw log information and its decoded form if applicable. It serves as a comprehensive data structure for handling log data within the system.

type Metadata

type Metadata struct {
	RuntimeBytecode  []byte
	DeployedBytecode []byte
	Block            *types.Block
	Transaction      *types.Transaction
	Receipt          *types.Receipt
}

Metadata holds essential data related to an Ethereum smart contract. It includes information about the contract's bytecode, associated transactions, and blockchain context.

Jump to

Keyboard shortcuts

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