Documentation
¶
Overview ¶
Package w3vm provides a VM for executing EVM messages.
Index ¶
- Variables
- func RandA() (addr common.Address)
- func Slot(pos, key common.Hash) common.Hash
- func Slot2(pos, key, key2 common.Hash) common.Hash
- func WETHAllowanceSlot(owner, spender common.Address) common.Hash
- func WETHBalanceSlot(addr common.Address) common.Hash
- type CallFuncFactory
- type Fetcher
- type Option
- func WithBlockContext(ctx *vm.BlockContext) Option
- func WithChainConfig(cfg *params.ChainConfig) Option
- func WithFetcher(fetcher Fetcher) Option
- func WithFork(client *w3.Client, blockNumber *big.Int) Option
- func WithHeader(header *types.Header) Option
- func WithNoBaseFee() Option
- func WithState(state w3types.State) Option
- func WithTB(tb testing.TB) Option
- type Receipt
- type VM
- func (vm *VM) Apply(msg *w3types.Message, tracers ...vm.EVMLogger) (*Receipt, error)
- func (vm *VM) ApplyTx(tx *types.Transaction, tracers ...vm.EVMLogger) (*Receipt, error)
- func (vm *VM) Balance(addr common.Address) (*big.Int, error)
- func (vm *VM) Call(msg *w3types.Message, tracers ...vm.EVMLogger) (*Receipt, error)
- func (vm *VM) CallFunc(contract common.Address, f w3types.Func, args ...any) *CallFuncFactory
- func (vm *VM) Code(addr common.Address) ([]byte, error)
- func (vm *VM) Nonce(addr common.Address) (uint64, error)
- func (vm *VM) StorageAt(addr common.Address, slot common.Hash) (common.Hash, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrFetch = errors.New("fetching failed") ErrRevert = errors.New("execution reverted") )
var ErrMissingFunc = errors.New("missing function")
Functions ¶
func WETHAllowanceSlot ¶
WETHAllowanceSlot returns the storage slot that stores the WETH allowance of the given owner and spender.
func WETHBalanceSlot ¶
WETHBalanceSlot returns the storage slot that stores the WETH balance of the given addr.
Example ¶
package main import ( "fmt" "math/big" "github.com/ethereum/go-ethereum/common" "github.com/zfesd/w3" "github.com/zfesd/w3/w3types" "github.com/zfesd/w3/w3vm" ) func main() { client := w3.MustDial("https://rpc.ankr.com/eth") defer client.Close() addrC0fe := w3.A("0x000000000000000000000000000000000000c0Fe") addrWETH := w3.A("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2") funcBalanceOf := w3.MustNewFunc("balanceOf(address)", "uint256") vm, err := w3vm.New( w3vm.WithFork(client, nil), w3vm.WithState(w3types.State{ addrWETH: { Storage: map[common.Hash]common.Hash{ w3vm.WETHBalanceSlot(addrC0fe): common.BigToHash(w3.I("100 ether")), }, }, }), ) if err != nil { // ... } var balance *big.Int err = vm.CallFunc(addrWETH, funcBalanceOf, addrC0fe).Returns(&balance) if err != nil { // ... } fmt.Printf("%s: %s WETH", addrC0fe, w3.FromWei(balance, 18)) }
Output: 0x000000000000000000000000000000000000c0Fe: 100 WETH
Types ¶
type CallFuncFactory ¶
type CallFuncFactory struct {
// contains filtered or unexported fields
}
func (*CallFuncFactory) Returns ¶
func (cff *CallFuncFactory) Returns(returns ...any) error
type Fetcher ¶
type Fetcher interface { // Nonce fetches the nonce of the given address. Nonce(common.Address) (uint64, error) // Balance fetches the balance of the given address. Balance(common.Address) (*big.Int, error) // Code fetches the code of the given address. Code(common.Address) ([]byte, error) // StorageAt fetches the state of the given address and storage slot. StorageAt(common.Address, common.Hash) (common.Hash, error) // HeaderHash fetches the hash of the header with the given number. HeaderHash(*big.Int) (common.Hash, error) }
Fetcher is the interface to access account state of a blockchain.
func NewRPCFetcher ¶
NewRPCFetcher returns a new Fetcher that fetches account state from the given RPC client for the given block number.
Note, that the returned state for a given block number is the state after the execution of that block.
type Option ¶
type Option func(*VM)
An Option configures a VM.
func WithBlockContext ¶
func WithBlockContext(ctx *vm.BlockContext) Option
WithBlockContext sets the block context for the VM.
func WithChainConfig ¶
func WithChainConfig(cfg *params.ChainConfig) Option
WithChainConfig sets the chain config for the VM.
func WithFetcher ¶
WithFetcher sets the fetcher for the VM.
func WithFork ¶
WithFork sets the client and block number to fetch state from and sets the block context for the VM. If the block number is nil, the latest state is fetched and the pending block is used for constructing the block context.
If used together with WithTB, fetched state is stored in the testdata directory of the tests package.
func WithHeader ¶
WithHeader sets the block context for the VM based on the given header
func WithNoBaseFee ¶
func WithNoBaseFee() Option
WithNoBaseFee forces the EIP-1559 base fee to 0 for the VM.
type Receipt ¶
type Receipt struct { GasUsed uint64 // Gas used for executing the message GasRefund uint64 // Gas refunded after executing the message GasLimit uint64 // Deprecated: Minimum required gas limit (gas used without refund) Logs []*types.Log // Logs emitted by the message Output []byte // Output bytes of the applied message ContractAddress *common.Address // Contract address created by a contract creation transaction Err error // Revert reason // contains filtered or unexported fields }
Receipt represents the result of an applied w3types.Message.
func (Receipt) DecodeReturns ¶
DecodeReturns is like w3types.Func.DecodeReturns, but returns ErrMissingFunc if the underlying w3types.Message.Func is nil.
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
func (*VM) Apply ¶
Apply the given message to the VM and return its receipt. Multiple tracers can be given to trace the execution of the message.
func (*VM) Call ¶
Call calls the given message on the VM and returns a receipt. Any state changes of a call are reverted. Multiple tracers can be passed to trace the execution of the message.
func (*VM) CallFunc ¶
CallFunc is a utility function for VM.Call that calls the given function on the given contract address with the given arguments and parses the output into the given returns.
Example:
funcBalanceOf := w3.MustNewFunc("balanceOf(address)", "uint256") var balance *big.Int err := vm.CallFunc(contractAddr, funcBalanceOf, addr).Returns(&balance) if err != nil { // ... }