/
wei.go
32 lines (27 loc) · 899 Bytes
/
wei.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package ethereum
import (
"github.com/keep-network/keep-common/pkg/chain/ethlike"
"math/big"
)
// Units defines denominations of the Ether token.
var Units = map[string]float64{
"wei": 1, // default unit
"gwei": 1e9,
"ether": 1e18,
}
// Wei is a custom type to handle Ether value parsing in configuration files
// using BurntSushi/toml package. It supports wei, Gwei and ether units. The
// Ether value is kept as `wei` and `wei` is the default unit.
// The value can be provided in the text file as e.g.: `1 wei`, `200 Gwei` or
// `0.5 ether`.
type Wei struct {
ethlike.Token
}
// WrapWei wraps the given integer value in order to represent it as Wei value.
func WrapWei(value *big.Int) *Wei {
return &Wei{ethlike.Token{value}}
}
// UnmarshalText is a function used to parse a value of Ethers.
func (w *Wei) UnmarshalText(text []byte) error {
return w.UnmarshalToken(text, Units)
}