/
doc_token.go
93 lines (79 loc) · 1.98 KB
/
doc_token.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
package digest
import (
"github.com/ProtoconNet/mitum-currency/v3/common"
mongodbstorage "github.com/ProtoconNet/mitum-currency/v3/digest/mongodb"
bsonenc "github.com/ProtoconNet/mitum-currency/v3/digest/util/bson"
crcystate "github.com/ProtoconNet/mitum-currency/v3/state"
"github.com/ProtoconNet/mitum-token/state"
"github.com/ProtoconNet/mitum-token/types"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util/encoder"
)
type TokenDoc struct {
mongodbstorage.BaseDoc
st base.State
de types.Design
}
func NewTokenDoc(st base.State, enc encoder.Encoder) (TokenDoc, error) {
de, err := state.StateDesignValue(st)
if err != nil {
return TokenDoc{}, err
}
b, err := mongodbstorage.NewBaseDoc(nil, st, enc)
if err != nil {
return TokenDoc{}, err
}
return TokenDoc{
BaseDoc: b,
st: st,
de: *de,
}, nil
}
func (doc TokenDoc) MarshalBSON() ([]byte, error) {
m, err := doc.BaseDoc.M()
if err != nil {
return nil, err
}
stateKeys, err := crcystate.ParseStateKey(doc.st.Key(), state.TokenPrefix, 3)
if err != nil {
return nil, err
}
m["contract"] = stateKeys[1]
m["height"] = doc.st.Height()
m["design"] = doc.de
return bsonenc.Marshal(m)
}
type TokenBalanceDoc struct {
mongodbstorage.BaseDoc
st base.State
amount common.Big
}
func NewTokenBalanceDoc(st base.State, enc encoder.Encoder) (*TokenBalanceDoc, error) {
balance, err := state.StateTokenBalanceValue(st)
if err != nil {
return nil, err
}
b, err := mongodbstorage.NewBaseDoc(nil, st, enc)
if err != nil {
return nil, err
}
return &TokenBalanceDoc{
BaseDoc: b,
st: st,
amount: balance,
}, nil
}
func (doc TokenBalanceDoc) MarshalBSON() ([]byte, error) {
m, err := doc.BaseDoc.M()
if err != nil {
return nil, err
}
stateKeys, err := crcystate.ParseStateKey(doc.st.Key(), state.TokenPrefix, 4)
if err != nil {
return nil, err
}
m["contract"] = stateKeys[1]
m["address"] = stateKeys[2]
m["height"] = doc.st.Height()
return bsonenc.Marshal(m)
}