/
coin_type.go
88 lines (72 loc) · 2.33 KB
/
coin_type.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
package types
import (
"strings"
)
type Unit struct {
Denom string `json:"denom"` //denom of unit
Scale uint8 `json:"scale"` //scale of unit
}
func NewUnit(denom string, scale uint8) Unit {
return Unit{
Denom: denom,
Scale: scale,
}
}
//GetScaleFactor return 1 * 10^scale
func (u Unit) GetScaleFactor() Int {
return NewIntWithDecimal(1, int(u.Scale))
}
type CoinType struct {
Name string `json:"name"` //description name of CoinType
MinUnit Unit `json:"min_unit"` //the min unit of CoinType
MainUnit Unit `json:"main_unit"` //the max unit of CoinType
Desc string `json:"desc"` //the description of CoinType
}
//ToMainCoin return the main denom coin from args
func (ct CoinType) ConvertToMainCoin(coin Coin) (DecCoin, error) {
if !ct.hasUnit(coin.Denom) {
return DecCoin{
Amount: NewDecFromInt(coin.Amount),
Denom: coin.Denom,
}, nil
//return DecCoin{}, errors.New("coinType unit (%s) not defined" + coin.Denom)
}
if ct.isMainUnit(coin.Denom) {
return DecCoin{}, nil
}
// dest amount = src amount * (10^(dest scale) / 10^(src scale))
dstScale := NewDecFromInt(ct.MainUnit.GetScaleFactor())
srcScale := NewDecFromInt(ct.MinUnit.GetScaleFactor())
amount := NewDecFromInt(coin.Amount)
amt := amount.Mul(dstScale).Quo(srcScale)
return NewDecCoinFromDec(ct.MainUnit.Denom, amt), nil
}
//ToMinCoin return the min denom coin from args
func (ct CoinType) ConvertToMinCoin(coin DecCoin) (newCoin Coin, err error) {
if !ct.hasUnit(coin.Denom) {
return Coin{
Amount: coin.Amount.TruncateInt(),
Denom: coin.Denom,
}, nil
//return newCoin, errors.New("coinType unit (%s) not defined" + coin.Denom)
}
if ct.isMinUnit(coin.Denom) {
newCoin, _ := coin.TruncateDecimal()
return newCoin, nil
}
// dest amount = src amount * (10^(dest scale) / 10^(src scale))
srcScale := NewDecFromInt(ct.MainUnit.GetScaleFactor())
dstScale := NewDecFromInt(ct.MinUnit.GetScaleFactor())
amount := coin.Amount
amt := amount.Mul(dstScale).Quo(srcScale)
return NewCoin(ct.MinUnit.Denom, amt.RoundInt()), nil
}
func (ct CoinType) isMainUnit(name string) bool {
return ct.MainUnit.Denom == strings.TrimSpace(name)
}
func (ct CoinType) isMinUnit(name string) bool {
return ct.MinUnit.Denom == strings.TrimSpace(name)
}
func (ct CoinType) hasUnit(name string) bool {
return ct.isMainUnit(name) || ct.isMinUnit(name)
}