-
Notifications
You must be signed in to change notification settings - Fork 113
/
token_handler.go
136 lines (118 loc) · 4.25 KB
/
token_handler.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
package database
import (
"encoding/json"
"errors"
"github.com/iost-official/go-iost/common"
"github.com/iost-official/go-iost/ilog"
)
// TokenContractName name of basic token contract
const TokenContractName = "token.iost"
// TokenHandler easy to get balance of token.iost
type TokenHandler struct {
db database
}
// FreezeItem represents freezed balance, will unfreeze after Ftime
type FreezeItem struct {
Amount int64
Ftime int64
}
// FreezeItemFixed ...
type FreezeItemFixed struct {
Amount common.Fixed
Ftime int64
}
func (m *TokenHandler) balanceKey(tokenName, acc string) string {
return "m-" + TokenContractName + "-" + "TB" + acc + "-" + tokenName
}
func (m *TokenHandler) freezedBalanceKey(tokenName, acc string) string {
return "m-" + TokenContractName + "-" + "TF" + acc + "-" + tokenName
}
func (m *TokenHandler) decimalKey(tokenName string) string {
key := "m-" + TokenContractName + "-" + "TI" + tokenName + "-" + "decimal"
return key
}
// TokenBalance get token balance of acc
func (m *TokenHandler) TokenBalance(tokenName, acc string) int64 {
currentRaw := m.db.Get(m.balanceKey(tokenName, acc))
balance := Unmarshal(currentRaw)
ib, ok := balance.(int64)
if !ok {
ib = 0
}
return ib
}
// TokenBalanceFixed get token balance of acc
func (m *TokenHandler) TokenBalanceFixed(tokenName, acc string) *common.Fixed {
ib := m.TokenBalance(tokenName, acc)
return &common.Fixed{Value: ib, Decimal: m.Decimal(tokenName)}
}
// FreezedTokenBalance get freezed token balance of acc
func (m *TokenHandler) FreezedTokenBalance(tokenName, acc string) int64 {
freezeJSON := Unmarshal(m.db.Get(m.freezedBalanceKey(tokenName, acc)))
if freezeJSON == nil {
return 0
}
freezeList := []FreezeItem{}
//fmt.Println(string(freezeJSON.(SerializedJSON)))
err := json.Unmarshal([]byte(freezeJSON.(SerializedJSON)), &freezeList)
if err != nil {
return 0
}
ilog.Debugf("FreezedTokenBalance is %v %v %v", tokenName, acc, string([]byte(freezeJSON.(SerializedJSON))))
ib := int64(0)
for _, item := range freezeList {
ib += item.Amount
}
return ib
}
// AllFreezedTokenBalance get freezed token balance of acc
func (m *TokenHandler) AllFreezedTokenBalance(tokenName, acc string) []FreezeItem {
freezeList := make([]FreezeItem, 0)
freezeJSON := Unmarshal(m.db.Get(m.freezedBalanceKey(tokenName, acc)))
if freezeJSON == nil {
return freezeList
}
//fmt.Println(string(freezeJSON.(SerializedJSON)))
err := json.Unmarshal([]byte(freezeJSON.(SerializedJSON)), &freezeList)
if err != nil {
ilog.Errorf("frozen token balance is invalid json %v %v", string(freezeJSON.(SerializedJSON)), err)
return freezeList
}
ilog.Debugf("FreezedTokenBalance is %v %v %v", tokenName, acc, string(freezeJSON.(SerializedJSON)))
return freezeList
}
// AllFreezedTokenBalanceFixed get freezed token balance of acc
func (m *TokenHandler) AllFreezedTokenBalanceFixed(tokenName, acc string) []FreezeItemFixed {
freezeList := m.AllFreezedTokenBalance(tokenName, acc)
result := make([]FreezeItemFixed, 0)
for _, item := range freezeList {
result = append(result, FreezeItemFixed{Amount: common.Fixed{Value: item.Amount, Decimal: m.Decimal(tokenName)}, Ftime: item.Ftime})
}
return result
}
// FreezedTokenBalanceFixed get token balance of acc
func (m *TokenHandler) FreezedTokenBalanceFixed(tokenName, acc string) *common.Fixed {
ib := m.FreezedTokenBalance(tokenName, acc)
return &common.Fixed{Value: ib, Decimal: m.Decimal(tokenName)}
}
// SetTokenBalance set token balance of acc, used for test
func (m *TokenHandler) SetTokenBalance(tokenName, acc string, amount int64) {
m.db.Put(m.balanceKey(tokenName, acc), MustMarshal(amount))
}
// SetTokenBalanceFixed set token balance of acc, used for test
func (m *TokenHandler) SetTokenBalanceFixed(tokenName, acc string, amountStr string) {
amountNumber, err := common.NewFixed(amountStr, m.Decimal(tokenName))
if err != nil {
panic(errors.New("construct Fixed number failed. str = " + amountStr + ", decimal = " + string(m.Decimal(tokenName))))
}
m.db.Put(m.balanceKey(tokenName, acc), MustMarshal(amountNumber.Value))
}
// Decimal get decimal in token info
func (m *TokenHandler) Decimal(tokenName string) int {
decimalRaw := m.db.Get(m.decimalKey(tokenName))
decimal, ok := Unmarshal(decimalRaw).(int64)
if !ok {
decimal = -1
}
return int(decimal)
}