-
Notifications
You must be signed in to change notification settings - Fork 5
/
price.go
61 lines (46 loc) · 972 Bytes
/
price.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package price
import (
"fmt"
"math/big"
"github.com/benleb/gloomberg/internal/utils"
)
// Price represents the value/amount of (w)eth transferred in a transaction.
type Price struct {
valueWei *big.Int
}
func NewPrice(price *big.Int) *Price {
if price == nil {
return nil
}
return &Price{
valueWei: price,
}
}
func (p *Price) MarshalJSON() ([]byte, error) {
if p == nil {
return []byte("nilol"), nil
}
return []byte(p.String()), nil
}
func (p *Price) String() string {
return fmt.Sprintf("%5.3f", p.Ether())
}
func (p *Price) Add(itemPrice *Price) *Price {
return &Price{
valueWei: big.NewInt(0).Add(p.valueWei, itemPrice.valueWei),
}
}
func (p *Price) Wei() *big.Int {
if p.valueWei == nil {
return big.NewInt(0)
}
return p.valueWei
}
func (p *Price) Gwei() float64 {
gwei, _ := utils.WeiToGwei(p.Wei()).Float64()
return gwei
}
func (p *Price) Ether() float64 {
ether, _ := utils.WeiToEther(p.Wei()).Float64()
return ether
}