string2eth

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: Apache-2.0 Imports: 5 Imported by: 15

README

go-string2eth

Tag License GoDoc Travis CI codecov.io Go Report Card

Go utility library to convert strings to Ether values and vice versa.

When converting strings to numeric values the process is case-insensitive and doesn't care about whitespace, so input values such as "0.1 Ether", "0.1Ether" and "0.1ether" would all result in the same result. The standard unit denominations (Wei, Ether) are supported with or without SI prefixes (micro, milli, kilo, mega etc.), as are common names (Shannon, Babbage).

When converting numeric values to strings the user can select standard mode, in which case all values will be in units of Wei or Ether, or non-standard mode, in which case the full range of values will be used.

This also provides the ability to convert between string and GWei values.

Table of Contents

Install

go-string2eth is a standard Go module which can be installed with:

go get github.com/wealdtech/go-string2eth

Usage

go-string2eth converts from strings to Ether values and back again.

Example
package main

import (
	string2eth "github.com/wealdtech/go-string2eth"
)

func main() {

    // Convert a string value to a number of Wei
    value, err := string2eth.StringToWei("0.05 Ether")
    if err != nil {
        panic(err)
    }

    // Convert a number of Wei to a string value
    str := string2eth.WeiToString(value, true)

    fmt.Printf("0.05 Ether is %v Wei, is %s\n", value, str)
}

Maintainers

Jim McDonald: @mcdee.

Contribute

Contributions welcome. Please check out the issues.

License

Apache-2.0 © 2019 Weald Technology Trading Ltd

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GWeiToString added in v1.1.0

func GWeiToString(input uint64, standard bool) string

GWeiToString turns a number of GWei in to a string. See WeiToString for details.

func StringToGWei added in v1.1.0

func StringToGWei(input string) (uint64, error)

StringToGWei turns a string in to number of GWei. See StringToWei for details. Any part of the value below 1GWei in denomination is lost.

func StringToWei

func StringToWei(input string) (*big.Int, error)

StringToWei turns a string in to number of Wei. The string can be a simple number of Wei, e.g. "1000000000000000" or it can be a number followed by a unit, e.g. "10 ether". Unit names are case-insensitive, and can be either given names (e.g. "finney") or metric names (e.g. "mlliether"). Note that this function expects use of the period as the decimal separator.

func UnitToMultiplier

func UnitToMultiplier(unit string) (*big.Int, error)

UnitToMultiplier takes the name of an Ethereum unit and returns a multiplier.

Example
package main

import (
	"fmt"

	string2eth "github.com/wealdtech/go-string2eth"
)

func main() {
	multiplier, err := string2eth.UnitToMultiplier("ether")
	if err != nil {
		return
	}
	fmt.Println(multiplier.Text(10))
}
Output:

1000000000000000000

func WeiToGWeiString added in v1.2.0

func WeiToGWeiString(input *big.Int) string

WeiToGWeiSTring turns a number of wei in to a Gwei string.

func WeiToString

func WeiToString(input *big.Int, standard bool) string

WeiToString turns a number of Wei in to a string. If the 'standard' argument is true then this will display the value in either (KMG)Wei or Ether only.

Types

This section is empty.

Jump to

Keyboard shortcuts

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