This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alph.go
141 lines (123 loc) · 3.29 KB
/
alph.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package alephium
import (
"bytes"
"fmt"
"github.com/willf/pad"
"math/big"
"math/rand"
"strings"
)
type ALPH struct {
Amount *big.Int
}
var (
OneQuintillionString = "1000000000000000000"
OneQuintillionInt64 = int64(1000000000000000000)
OneBillionInt64 = int64(1000000000)
CoinInOneALPH = new(big.Int).SetInt64(OneQuintillionInt64)
CoinInNanoALPH = new(big.Int).SetInt64(OneBillionInt64)
//N = "א"
N = "ALPH"
)
// TODO: should the ALPH parsing function return a typed error instead of a bool?
func ALPHFromALPHString(amount string) (ALPH, bool) {
split := strings.Split(amount, ".")
if len(split) == 1 {
alphAmount, ok := new(big.Int).SetString(amount, 10)
if ok {
coinAmount := new(big.Int).Mul(alphAmount, CoinInOneALPH)
return ALPH{Amount: coinAmount}, true
}
} else if len(split) == 2 {
decimals := pad.Right(split[1], 18, "0")
coinAmount, ok := new(big.Int).SetString(strings.Join([]string{split[0], decimals}, ""), 10)
if ok {
return ALPH{Amount: coinAmount}, true
}
}
return ALPH{}, false
}
func ALPHFromCoinString(amount string) (ALPH, bool) {
alphAmount, ok := new(big.Int).SetString(amount, 10)
if !ok {
return ALPH{}, false
}
return ALPH{Amount: alphAmount}, true
}
func (alph ALPH) Add(other ALPH) ALPH {
c := new(big.Int)
c.Add(alph.Amount, other.Amount)
return ALPH{Amount: c}
}
func (alph ALPH) Subtract(other ALPH) ALPH {
c := new(big.Int)
c.Sub(alph.Amount, other.Amount)
return ALPH{Amount: c}
}
func (alph ALPH) Multiply(multiplier int64) ALPH {
c := new(big.Int)
m := new(big.Int).SetInt64(multiplier)
c.Mul(alph.Amount, m)
return ALPH{Amount: c}
}
func (alph ALPH) Divide(divider int64) ALPH {
c := new(big.Int)
m := new(big.Int).SetInt64(divider)
c.Div(alph.Amount, m)
return ALPH{Amount: c}
}
func (alph ALPH) Cmp(other ALPH) int {
return alph.Amount.Cmp(other.Amount)
}
func (alph ALPH) String() string {
if alph.Amount == nil {
return "0"
}
return alph.Amount.String()
}
func (alph ALPH) PrettyString() string {
if alph.Amount == nil {
return "0"
}
if alph.Amount.Cmp(CoinInNanoALPH) > 0 {
return fmt.Sprintf("%s%s", strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.9f", alph.FloatALPH()), "0"), "."), N)
}
return alph.Amount.String()
}
func (alph ALPH) FloatALPH() float64 {
c := new(big.Int)
nanoAFL := c.Div(alph.Amount, CoinInNanoALPH).Int64()
return float64(nanoAFL) / float64(OneBillionInt64)
}
func RandomALPHAmount(upperLimit int) ALPH {
unit := rand.Intn(upperLimit)
decimals := rand.Intn(int(OneBillionInt64))
rAmountStr := fmt.Sprintf("%d.%d", unit, decimals)
alph, _ := ALPHFromALPHString(rAmountStr)
return alph
}
func RandomNanoALPHAmount(upperLimit int) ALPH {
nanoALPH := rand.Intn(upperLimit)
c := new(big.Int).SetInt64(int64(nanoALPH))
m := new(big.Int).Mul(c, CoinInNanoALPH)
return ALPH{Amount: m}
}
func (alph ALPH) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
str := alph.Amount.String()
buffer.WriteString(str)
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (alph *ALPH) UnmarshalJSON(b []byte) error {
if len(b) < 2 {
return fmt.Errorf("NaN")
}
alph.Amount = new(big.Int)
err := alph.Amount.UnmarshalJSON(b[1 : len(b)-1])
return err
}
func ToNanoALPH(alph ALPH) int {
m := new(big.Int).Div(alph.Amount, CoinInNanoALPH)
return int(m.Int64())
}