ethtypes

package module
v0.0.0-...-ea6244e Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2021 License: GPL-3.0 Imports: 9 Imported by: 0

README

ethtypes

Description

Encapsulates commonly used data structures for EVM(Ethereum Virtual Machine) state variables.

Prerequisites

golang

Quick Start

package main

import (
	"fmt"

	"github.com/TheStarBoys/ethtypes"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/rawdb"
	"github.com/ethereum/go-ethereum/core/state"
)

func main() {
	// New stateDB in memory
	db := rawdb.NewMemoryDatabase()
	state, _ := state.New(common.Hash{}, state.NewDatabase(db), nil)

	// Your precompiled contract is at this address
	contractAddr := common.HexToAddress("123")
	tf, err := ethtypes.NewTypeFactory(state, contractAddr)
	if err != nil {
		panic(err)
	}

	arrayName := "Array"
	array := tf.NewArray(arrayName, 3, ethtypes.Int64Type)

	for i := 0; i < array.Len(); i++ {
		array.Set(i, int64(i+1))
	}

	for i := 0; i < array.Len(); i++ {
		var val int64
		array.Get(i, &val)
		fmt.Printf("index %d val: %d\n", i, val)
	}
	// index 0 val: 1
	// index 1 val: 2
	// index 2 val: 3

	fmt.Println("----------------")
	array.Del(0)
	for i := 0; i < array.Len(); i++ {
		var val int64
		array.Get(i, &val)
		fmt.Printf("index %d val: %d\n", i, val)
	}
}

License

The ethtypes library is licensed under the GNU General Public License v3.0, also included in our repository in the COPYING.LESSER file.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Token
	Wei        *big.Int = big.NewInt(1)
	Kwei       *big.Int = new(big.Int).Mul(Wei, big.NewInt(1000))
	Mwei       *big.Int = new(big.Int).Mul(Kwei, big.NewInt(1000))
	Gwei       *big.Int = new(big.Int).Mul(Mwei, big.NewInt(1000))
	Microether *big.Int = new(big.Int).Mul(Gwei, big.NewInt(1000))
	Milliether *big.Int = new(big.Int).Mul(Microether, big.NewInt(1000))
	Ether      *big.Int = new(big.Int).Mul(Milliether, big.NewInt(1000))
	Kether     *big.Int = new(big.Int).Mul(Ether, big.NewInt(1000))
	Mether     *big.Int = new(big.Int).Mul(Kether, big.NewInt(1000))
	Gether     *big.Int = new(big.Int).Mul(Mether, big.NewInt(1000))
)
View Source
var (
	// Basic Type
	StringType  = reflect.TypeOf("")
	IntType     = reflect.TypeOf(int(0))
	Int8Type    = reflect.TypeOf(int8(0))
	Int16Type   = reflect.TypeOf(int16(0))
	Int32Type   = reflect.TypeOf(int32(0))
	Int64Type   = reflect.TypeOf(int64(0))
	UintType    = reflect.TypeOf(uint(0))
	Uint8Type   = reflect.TypeOf(uint8(0))
	Uint16Type  = reflect.TypeOf(uint16(0))
	Uint32Type  = reflect.TypeOf(uint32(0))
	Uint64Type  = reflect.TypeOf(uint64(0))
	Float32Type = reflect.TypeOf(float32(0))
	Float64Type = reflect.TypeOf(float64(0))
	BoolType    = reflect.TypeOf(false)
	BytesType   = reflect.TypeOf([]byte{})
)
View Source
var (
	// Complex Type
	AddressType = reflect.TypeOf(common.Address{})
	BigIntType  = reflect.TypeOf(big.Int{})
)
View Source
var (
	ErrIndexOutOfRange = errors.New("index out of range")
)

Functions

func ArrayToStr

func ArrayToStr(a Array) string

func FromWei

func FromWei(wei, unit *big.Int) (*big.Int, error)

FromWei convert wei as Kwei, Mwei, ..., Ether..

func GetArrayElems

func GetArrayElems(a Array) []interface{}

func GetIterableMapElems

func GetIterableMapElems(m IterableMap) map[string]interface{}

func IterableMapToStr

func IterableMapToStr(m IterableMap) string

func SliceToStr

func SliceToStr(s Slice) string

func VariableToStr

func VariableToStr(v StateVariable) string

Types

type Array

type Array interface {
	// Get get val from index, val must
	// be pointer
	Get(index int, val interface{})
	Set(index int, val interface{})
	// Del deletes element in index, and
	// move all elements after index move forward by one position
	Del(index int)
	Len() int
	CopyFrom(src Array, dstFrom, srcFrom, srcTo int)
	// ElemType returns element type of the array
	ElemType() reflect.Type
	// Range(fn func(index int, val interface{}) bool)
	// Name returns the name of array
	Name() string
}

Array represents a simple array.

type BasicArray

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

func GetBasicArray

func GetBasicArray(state *ContractState, name string, typ reflect.Type) (*BasicArray, error)

func NewBasicArray

func NewBasicArray(state *ContractState, name string, len int, typ reflect.Type) (*BasicArray, error)

func (*BasicArray) CopyFrom

func (a *BasicArray) CopyFrom(src Array, dstFrom, srcFrom, srcTo int)

func (*BasicArray) Del

func (a *BasicArray) Del(index int)

func (*BasicArray) ElemType

func (a *BasicArray) ElemType() reflect.Type

func (*BasicArray) Get

func (a *BasicArray) Get(index int, val interface{})

func (*BasicArray) Len

func (a *BasicArray) Len() int

func (*BasicArray) Name

func (a *BasicArray) Name() string

func (*BasicArray) Set

func (a *BasicArray) Set(index int, val interface{})

type BasicIterableMap

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

func GetBasicIterableMap

func GetBasicIterableMap(state *ContractState, name string, keyType, valType reflect.Type) (*BasicIterableMap, error)

func NewBasicIterableMap

func NewBasicIterableMap(state *ContractState, name string, keyType, valType reflect.Type) (*BasicIterableMap, error)

func (*BasicIterableMap) Contains

func (im *BasicIterableMap) Contains(key interface{}) bool

func (*BasicIterableMap) Del

func (im *BasicIterableMap) Del(key interface{})

func (*BasicIterableMap) Get

func (im *BasicIterableMap) Get(key interface{}, val interface{}) (ok bool)

func (*BasicIterableMap) GetKVType

func (im *BasicIterableMap) GetKVType() (key, val reflect.Type)

func (*BasicIterableMap) Index

func (im *BasicIterableMap) Index(i int, key, val interface{})

func (*BasicIterableMap) Len

func (im *BasicIterableMap) Len() int

func (*BasicIterableMap) Name

func (im *BasicIterableMap) Name() string

func (*BasicIterableMap) Range

func (im *BasicIterableMap) Range(fn func(key, val interface{}) bool)

func (*BasicIterableMap) Set

func (im *BasicIterableMap) Set(key, val interface{})

type BasicMap

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

func GetBasicMap

func GetBasicMap(state *ContractState, name string, keyType, valType reflect.Type) (*BasicMap, error)

func NewBasicMap

func NewBasicMap(state *ContractState, name string, keyType, valType reflect.Type) (*BasicMap, error)

func (*BasicMap) Contains

func (m *BasicMap) Contains(key interface{}) bool

func (*BasicMap) Del

func (m *BasicMap) Del(key interface{})

func (*BasicMap) Get

func (m *BasicMap) Get(key interface{}, val interface{}) bool

func (*BasicMap) GetKVType

func (m *BasicMap) GetKVType() (key, val reflect.Type)

func (*BasicMap) Name

func (m *BasicMap) Name() string

func (*BasicMap) Set

func (m *BasicMap) Set(key, val interface{})

type BasicSlice

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

func GetBasicSlice

func GetBasicSlice(state *ContractState, name string, typ reflect.Type) (*BasicSlice, error)

func NewBasicSlice

func NewBasicSlice(state *ContractState, name string, len, cap int, typ reflect.Type) (*BasicSlice, error)

func (*BasicSlice) Append

func (s *BasicSlice) Append(vals ...interface{})

func (*BasicSlice) Cap

func (s *BasicSlice) Cap() int

func (*BasicSlice) CopyFrom

func (s *BasicSlice) CopyFrom(src Array, dstFrom, srcFrom, srcTo int)

func (*BasicSlice) Del

func (s *BasicSlice) Del(index int)

func (*BasicSlice) ElemType

func (s *BasicSlice) ElemType() reflect.Type

func (*BasicSlice) Get

func (s *BasicSlice) Get(index int, val interface{})

func (*BasicSlice) Len

func (s *BasicSlice) Len() int

func (*BasicSlice) Name

func (s *BasicSlice) Name() string

func (*BasicSlice) Pop

func (s *BasicSlice) Pop(val interface{})

func (*BasicSlice) Set

func (s *BasicSlice) Set(index int, val interface{})

type BasicStateVariable

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

func GetBasicStateVariable

func GetBasicStateVariable(state *ContractState, variableName string, typ reflect.Type) (*BasicStateVariable, error)

func NewBasicStateVariable

func NewBasicStateVariable(state *ContractState, variableName string, initialVal interface{}) (*BasicStateVariable, error)

func (*BasicStateVariable) Addr

func (sv *BasicStateVariable) Addr() common.Hash

func (*BasicStateVariable) CopyFrom

func (sv *BasicStateVariable) CopyFrom(src StateVariable)

func (*BasicStateVariable) Del

func (sv *BasicStateVariable) Del()

func (*BasicStateVariable) Get

func (sv *BasicStateVariable) Get(val interface{}) bool

func (*BasicStateVariable) IsAssigned

func (sv *BasicStateVariable) IsAssigned() bool

func (*BasicStateVariable) Name

func (sv *BasicStateVariable) Name() string

func (*BasicStateVariable) Set

func (sv *BasicStateVariable) Set(val interface{})

func (*BasicStateVariable) Type

func (sv *BasicStateVariable) Type() reflect.Type

type ContractState

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

func NewContractState

func NewContractState(db vm.StateDB, addr common.Address) *ContractState

func (*ContractState) Delete

func (s *ContractState) Delete(hash common.Hash)

func (*ContractState) Exists

func (s *ContractState) Exists(hash common.Hash) bool

Exists - check data has been saved or not

func (*ContractState) Read

func (s *ContractState) Read(hash common.Hash) []byte

func (*ContractState) Write

func (s *ContractState) Write(hash common.Hash, data []byte)

type IterableMap

type IterableMap interface {
	Map
	// Len returns the number of elems
	Len() int
	// Index get key and val by index i,
	// key and val must be pointer
	Index(i int, key, val interface{})
	// Range iterate all key-value pair, it
	// will stop if fn returns false
	Range(fn func(key, val interface{}) bool)
}

IterableMap represents a iterable mapping

type Map

type Map interface {
	// Get val from key, val must be pointer,
	// returns false if element not exsit.
	Get(key interface{}, val interface{}) (ok bool)
	Set(key, val interface{})
	Contains(key interface{}) bool
	Del(key interface{})
	// GetKVType returns key-value pair type
	GetKVType() (key, val reflect.Type)
	// Name returns the name of map
	Name() string
}

Map represents key-value pair mapping

type Slice

type Slice interface {
	Array
	// Cap returns capacity of the Slice
	Cap() int
	// Append append elements in the tail of the slice
	Append(vals ...interface{})
	// Pop remove elements in the tail of the slice
	Pop(val interface{})
}

Slice represents a slice like slice in golang

type StateVariable

type StateVariable interface {
	// Del delete this state variable
	Del()
	// Set set state variable as val
	Set(val interface{})
	// Get get state variable, val must be pointer.
	// The value of state variable will store into val.
	// Return true if the initial value has been assigned.
	Get(val interface{}) bool
	// CopyFrom copy the value of src into
	// this state variable
	CopyFrom(src StateVariable)
	// Type returns type of this state variable
	Type() reflect.Type
	// IsAssigned returns true if the initial value
	// has been assigned.
	IsAssigned() bool
	// Addr returns the location of this state variable
	Addr() common.Hash
	// Name returns the name of state varialbe
	Name() string
}

StateVariable represents global variable and stores in chain db

type TypeFactory

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

func NewTypeFactory

func NewTypeFactory(db vm.StateDB, contractAddr common.Address) (*TypeFactory, error)

func (*TypeFactory) GetArray

func (t *TypeFactory) GetArray(name string, length int, typ reflect.Type) Array

func (*TypeFactory) GetIterableMap

func (t *TypeFactory) GetIterableMap(name string, keyType, valType reflect.Type) IterableMap

func (*TypeFactory) GetMap

func (t *TypeFactory) GetMap(name string, keyType, valType reflect.Type) Map

func (*TypeFactory) GetSlice

func (t *TypeFactory) GetSlice(name string, length, cap int, typ reflect.Type) Slice

func (*TypeFactory) GetVariable

func (t *TypeFactory) GetVariable(name string, typ reflect.Type) StateVariable

func (*TypeFactory) NewArray

func (t *TypeFactory) NewArray(name string, length int, typ reflect.Type) Array

func (*TypeFactory) NewFloat64

func (t *TypeFactory) NewFloat64(name string, initialVal float64) StateVariable

func (*TypeFactory) NewInt

func (t *TypeFactory) NewInt(name string, initialVal int) StateVariable

func (*TypeFactory) NewIterableMap

func (t *TypeFactory) NewIterableMap(name string, keyType, valType reflect.Type) IterableMap

func (*TypeFactory) NewMap

func (t *TypeFactory) NewMap(name string, keyType, valType reflect.Type) Map

func (*TypeFactory) NewSlice

func (t *TypeFactory) NewSlice(name string, length, cap int, typ reflect.Type) Slice

func (*TypeFactory) NewString

func (t *TypeFactory) NewString(name, initialVal string) StateVariable

func (*TypeFactory) NewStringArray

func (t *TypeFactory) NewStringArray(name string, length int, initialData []string) Array

func (*TypeFactory) NewStringSlice

func (t *TypeFactory) NewStringSlice(name string, length, cap int, initialData []string) Slice

func (*TypeFactory) NewUint

func (t *TypeFactory) NewUint(name string, initialVal uint) StateVariable

func (*TypeFactory) NewVariable

func (t *TypeFactory) NewVariable(name string, initialVal interface{}) StateVariable

Jump to

Keyboard shortcuts

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