levm

package module
v0.0.0-...-591ae30 Latest Latest
Warning

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

Go to latest
Published: May 20, 2019 License: MIT Imports: 8 Imported by: 0

README

LEVM - Little Ethereum Virtual Machine

Run smart contracts on the Ethereum Virtual Machine (EVM) without the Ethereum blockchain.

I created this to make it a little easier to use the EVM in other projects, and for testing smart contracts.

levm.New(...) creates a small wrapper for the ethereum virtual machine, and sets up the EVM with default parameters and the required stateDB.

↓ Installation

This project only has two dependencies. (go-ethereum contains the EVM, and splashkeys is an ecdsa key library)

go get github.com/CryptoKass/levm

Dependencies
  • go get github.com/ethereum/go-ethereum
  • go get github.com/CryptoKass/splashkeys

Example

This is Example is located in the example sub-directory:

func main() {

	//make a new address evm
	fromAddr = tools.NewRandomAddress()

	//Load a contract from file
	abiObject, binData, err := tools.LoadContract("contract/example_sol_Example.abi", "contract/example_sol_Example.bin")
	fmt.Println("Abi\n", abiObject.Methods)

	// create new LEVM instance
	lvm := levm.New("./db", big.NewInt(0), fromAddr)

	// create a new account and set the balance
	// (needs enough balance to cover gas cost)
	lvm.NewAccount(fromAddr, big.NewInt(1e18))

	// deploy a contract
	code, addr, gas, err := lvm.DeployContract(fromAddr, binData)
	fmt.Println("contract code length:", len(code))
	fmt.Printf("contract address: %x\n", addr)
	fmt.Println("unused gas:", gas)
	fmt.Println("errors:", err)

}

contact: kasscrypto@gmail.com 👍

HELP WANTED would be great if you would create an issue if you find a bug.

Documentation

Overview

Package levm is a higher level wrapper for the EVM and the related StateDB. levm.go contains methods for creating the LEVM and deploying/calling contracts. db.go contains methods for interacting with the stateDB i.e. getting accounts and balances

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Account

type Account struct {
	Balance *big.Int
	Nonce   uint64
	Code    []byte
}

Account is a simple representation of an account in the stateDB, for further controls call DB() to retrieve the db reference itself. Almost as a snapshot

type LEVM

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

LEVM is a container for the go-ethereum EVM with methods to create and call contracts.

LEVM contains the two most important objects for interacting with the EVM: stateDB and vm.EVM. The LEVM should be created with the LEVM.New() method, unless you know what you doing.

func New

func New(dbPath string, blockNumber *big.Int, origin common.Address) *LEVM

New creates a new instace of the LEVM

func (*LEVM) CallContract

func (lvm *LEVM) CallContract(callerAddr, contractAddr common.Address, value *big.Int, inputs []byte) ([]byte, error)

CallContract - make a call to a Contract Method using prepacked Inputs. To use ABI directly try lvm.CallContractABI()

func (*LEVM) CallContractABI

func (lvm *LEVM) CallContractABI(callerAddr, contractAddr common.Address, value *big.Int, abiObject abi.ABI, funcName string, args ...interface{}) ([]byte, error)

CallContractABI - make a call to a Contract Method using the ABI.

func (*LEVM) DB

func (lvm *LEVM) DB() *state.StateDB

DB returns a pointer to the vm's state DB.

func (*LEVM) DeployContract

func (lvm *LEVM) DeployContract(fromAddr common.Address, contractData []byte) ([]byte, common.Address, uint64, error)

DeployContract will create and deploy a new contract from the contract data.

func (*LEVM) GetAccount

func (lvm *LEVM) GetAccount(addr common.Address) Account

GetAccount returns a copy of stateDB

func (*LEVM) NewAccount

func (lvm *LEVM) NewAccount(addr common.Address, balance *big.Int)

NewAccount Create a new Account and Set its balance

func (*LEVM) NewEVM

func (lvm *LEVM) NewEVM(blockNumber *big.Int, origin common.Address)

NewEVM creates a fresh evm instance with new origin and blocknumber and time. This method recreates the contained EVM while keeping the stateDB the same.

func (*LEVM) SetAccount

func (lvm *LEVM) SetAccount(addr common.Address, acc Account)

SetAccount will update the statedb with related values contained in the Account snapshot.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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