utils

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 18 Imported by: 18

Documentation

Index

Constants

View Source
const (
	// FormatBytes encodes the output as bytes
	FormatBytes = "bytes"
	// FormatPreformatted encodes the output, assumed to be hex, as bytes.
	FormatPreformatted = "preformatted"
	// FormatUint256 encodes the output as bytes containing a uint256
	FormatUint256 = "uint256"
	// FormatInt256 encodes the output as bytes containing an int256
	FormatInt256 = "int256"
	// FormatBool encodes the output as bytes containing a bool
	FormatBool = "bool"
)
View Source
const EVMWordByteLen = 32

EVMWordByteLen the length of an EVM Word Byte

Variables

View Source
var (

	// MaxUint256 represents the largest number represented by an EVM word
	MaxUint256 = &big.Int{}
	// MaxInt256 represents the largest number represented by an EVM word using
	// signed encoding.
	MaxInt256 = &big.Int{}
	// MinInt256 represents the smallest number represented by an EVM word using
	// signed encoding.
	MinInt256 = &big.Int{}
)

"Constants" used by EVM words

View Source
var EmptyHash = common.Hash{}

EmptyHash is a hash of all zeroes, otherwise in Ethereum as 0x0000000000000000000000000000000000000000000000000000000000000000

View Source
var ZeroAddress = common.Address{}

ZeroAddress is an address of all zeroes, otherwise in Ethereum as 0x0000000000000000000000000000000000000000

Functions

func ABIDecode

func ABIDecode(abiStr string, data []byte) ([]interface{}, error)

ABIEncode is the equivalent of abi.decode. See a full set of examples https://github.com/ethereum/go-ethereum/blob/420b78659bef661a83c5c442121b13f13288c09f/accounts/abi/packing_test.go#L31

func ABIEncode

func ABIEncode(abiStr string, values ...interface{}) ([]byte, error)

ABIEncode is the equivalent of abi.encode. See a full set of examples https://github.com/ethereum/go-ethereum/blob/420b78659bef661a83c5c442121b13f13288c09f/accounts/abi/packing_test.go#L31

func Bytes32FromString

func Bytes32FromString(s string) [32]byte

Bytes32FromString returns a 32 byte array filled from the given string, which may be of any length.

func Bytes32ToSlice

func Bytes32ToSlice(a [32]byte) (r []byte)

func Bytes4FromString

func Bytes4FromString(s string) [4]byte

Bytes4FromString returns a 4 byte array filled from the given string, which may be of any length.

func CheckUint256

func CheckUint256(n *big.Int) error

CheckUint256 returns an error if n is out of bounds for a uint256

func ConcatBytes

func ConcatBytes(bufs ...[]byte) []byte

ConcatBytes appends a bunch of byte arrays into a single byte array

func EIP55CapitalizedAddress

func EIP55CapitalizedAddress(possibleAddressString string) bool

EIP55CapitalizedAddress returns true iff possibleAddressString has the correct capitalization for an Ethereum address, per EIP 55

func EVMEncodeBytes

func EVMEncodeBytes(input []byte) []byte

EVMEncodeBytes encodes arbitrary bytes as bytes expected by the EVM

func EVMTranscodeBool

func EVMTranscodeBool(value gjson.Result) ([]byte, error)

EVMTranscodeBool converts a json input to an EVM bool

func EVMTranscodeInt256

func EVMTranscodeInt256(value gjson.Result) ([]byte, error)

EVMTranscodeInt256 converts a json input to an EVM int256

func EVMTranscodeUint256

func EVMTranscodeUint256(value gjson.Result) ([]byte, error)

EVMTranscodeUint256 converts a json input to an EVM uint256

func EVMWordBigInt

func EVMWordBigInt(val *big.Int) ([]byte, error)

EVMWordBigInt returns a big.Int as an EVM word byte array, with support for a signed representation. Returns error on overflow.

func EVMWordSignedBigInt

func EVMWordSignedBigInt(val *big.Int) ([]byte, error)

EVMWordSignedBigInt returns a big.Int as an EVM word byte array, with support for a signed representation. Returns error on overflow.

func EVMWordUint128

func EVMWordUint128(val *big.Int) ([]byte, error)

EVMWordUint128 returns a uint128 as an EVM word byte array.

func EVMWordUint32

func EVMWordUint32(val uint32) []byte

EVMWordUint32 returns a uint32 as an EVM word byte array.

func EVMWordUint64

func EVMWordUint64(val uint64) []byte

EVMWordUint64 returns a uint64 as an EVM word byte array.

func HexToUint256

func HexToUint256(s string) (*big.Int, error)

HexToUint256 returns the uint256 represented by s, or an error if it doesn't represent one.

func IsEmptyAddress

func IsEmptyAddress(addr common.Address) bool

IsEmptyAddress checks that the address is empty, synonymous with the zero account/address. No logs can come from this address, as there is no contract present there.

See https://stackoverflow.com/questions/48219716/what-is-address0-in-solidity for the more info on the zero address.

func Keccak256

func Keccak256(in []byte) ([]byte, error)

Keccak256 is a simplified interface for the legacy SHA3 implementation that Ethereum uses.

func Keccak256Fixed

func Keccak256Fixed(in []byte) [32]byte

func MustAbiType

func MustAbiType(ts string, components []abi.ArgumentMarshaling) abi.Type

func MustHash

func MustHash(in string) common.Hash

MustHash returns the keccak256 hash, or panics on failure.

func NewHash

func NewHash() common.Hash

NewHash return random Keccak256

func NewRedialBackoff

func NewRedialBackoff() backoff.Backoff

NewRedialBackoff is a standard backoff to use for redialling or reconnecting to unreachable network endpoints

func PadByteToHash

func PadByteToHash(b byte) common.Hash

PadByteToHash returns a hash with zeros padded on the left of the given byte.

func ParseEthereumAddress

func ParseEthereumAddress(addressString string) (common.Address, error)

ParseEthereumAddress returns addressString as a go-ethereum Address, or an error if it's invalid, e.g. if EIP 55 capitalization check fails

func RandUint256

func RandUint256() *big.Int

RandUint256 generates a random bigNum up to 2 ** 256 - 1

func RandomAddress

func RandomAddress() common.Address

func RandomBytes32

func RandomBytes32() (r [32]byte)

func RandomHash

func RandomHash() common.Hash

func RetryWithBackoff

func RetryWithBackoff(ctx context.Context, fn func() (retry bool))

RetryWithBackoff retries the sleeper and backs off if not Done

func Uint256ToBytes

func Uint256ToBytes(x *big.Int) (uint256 []byte, err error)

Uint256ToBytes is x represented as the bytes of a uint256

func Uint256ToBytes32

func Uint256ToBytes32(n *big.Int) []byte

Uint256ToBytes32 returns the bytes32 encoding of the big int provided

Types

type BackoffSleeper

type BackoffSleeper struct {
	backoff.Backoff
	// contains filtered or unexported fields
}

BackoffSleeper is a sleeper that backs off on subsequent attempts.

func NewBackoffSleeper

func NewBackoffSleeper() *BackoffSleeper

NewBackoffSleeper returns a BackoffSleeper that is configured to sleep for 0 seconds initially, then backs off from 1 second minimum to 10 seconds maximum.

func (*BackoffSleeper) After

func (bs *BackoffSleeper) After() time.Duration

After returns the duration for the next stop, and increments the backoff.

func (*BackoffSleeper) Duration

func (bs *BackoffSleeper) Duration() time.Duration

Duration returns the current duration value.

func (*BackoffSleeper) Reset

func (bs *BackoffSleeper) Reset()

Reset resets the backoff intervals.

func (*BackoffSleeper) Sleep

func (bs *BackoffSleeper) Sleep()

Sleep waits for the given duration, incrementing the back off.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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