unit

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2021 License: MIT Imports: 3 Imported by: 6

README

Github CI/CD Go Report Repository Top Language GitHub go.mod Go version Github Repository Size Github Open Issues Lines of code License GitHub last commit GitHub contributors

Ethereum Unit Converter

It is a simple library for conversion units between themselves. The library is written in Golang.

Install

go get github.com/DeOne4eg/eth-unit-converter

Usage

// first
amount := ethUnit.NewWei(big.NewInt(1000000000000000000))

fmt.Printf("Wei: %v\n", amount.Wei())
fmt.Printf("GWei: %v\n", amount.GWei())
fmt.Printf("Ether: %v\n", amount.Ether())

// second (the same of first)
amount, err := ethUnit.ParseUnit(big.NewFloat(1000000000000000000), "wei")
if err != nil {
	log.Fatal(err)
}

fmt.Printf("Wei: %v\n", amount.Wei())
fmt.Printf("GWei: %v\n", amount.GWei())
fmt.Printf("Ether: %v\n", amount.Ether())

Tests

  • run with make test

Documentation

Index

Constants

View Source
const (
	Ether = 1
)
View Source
const (
	Finney = 1e3
)
View Source
const (
	GWei = 1e9
)
View Source
const (
	KWei = 1e15
)
View Source
const (
	MWei = 1e12
)
View Source
const (
	Szabo = 1e6
)
View Source
const (
	Wei = 1e18
)

Variables

This section is empty.

Functions

func SetPrecision

func SetPrecision(prec uint)

SetPrecision set the precision value for big.Float. Decrease value for faster conversion. Increase value if you need to work with very small or very large numbers. It is very easy to understand that you need to increase this value. For example: if you convert 1e1000 number and receive not zeros in the end of number then increase value.

Types

type Unit

type Unit struct {
	Value *big.Int
}

Unit store value in Wei unit. Wei should be always big.Int.

func NewEther

func NewEther(value *big.Float) *Unit

NewEther create instance of Unit with convert Ether to Wei units and returns pointer to it. Then you can use Unit for get the value in supported units.

func NewFinney

func NewFinney(value *big.Float) *Unit

NewFinney create instance of Unit with convert Finney to Wei units and returns pointer to it. Then you can use Unit for get the value in supported units.

func NewGWei

func NewGWei(value *big.Float) *Unit

NewGWei create instance of Unit with convert GWei to Wei units and returns pointer to it. Then you can use Unit for get the value in supported units.

func NewKWei

func NewKWei(value *big.Float) *Unit

NewKWei create instance of Unit with convert KWei to Wei units and returns pointer to it. Then you can use Unit for get the value in supported units.

func NewMWei

func NewMWei(value *big.Float) *Unit

NewMWei create instance of Unit with convert MWei to Wei units and returns pointer to it. Then you can use Unit for get the value in supported units.

func NewSzabo

func NewSzabo(value *big.Float) *Unit

NewSzabo create instance of Unit with convert Szabo to Wei units and returns pointer to it. Then you can use Unit for get the value in supported units.

func NewWei

func NewWei(value *big.Int) *Unit

func ParseUnit

func ParseUnit(value *big.Float, unit string) (*Unit, error)

ParseUnit create instance of Unit from passed value and unit name. Support units: wei, kwei, mwei, gwei, szabo, finney, ether. You may use it for convert units between themselves.

func (Unit) Ether

func (u Unit) Ether() float64

Ether returns the value in Ether units.

func (Unit) Finney

func (u Unit) Finney() *big.Float

Finney returns the value in Finney units.

func (Unit) GWei

func (u Unit) GWei() *big.Float

GWei returns the value in GWei units.

func (Unit) KWei

func (u Unit) KWei() *big.Float

KWei returns the value in KWei units.

func (Unit) MWei

func (u Unit) MWei() *big.Float

MWei returns the value in MWei units.

func (Unit) String

func (u Unit) String() string

String return value of Unit in string format.

func (Unit) Szabo

func (u Unit) Szabo() *big.Float

Szabo returns the value in Szabo units.

func (Unit) Wei

func (u Unit) Wei() *big.Int

Jump to

Keyboard shortcuts

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