-
Notifications
You must be signed in to change notification settings - Fork 2
/
doc_currency.go
48 lines (40 loc) · 1.13 KB
/
doc_currency.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
package digest
import (
mongodbstorage "github.com/ProtoconNet/mitum-currency/v3/digest/mongodb"
bsonenc "github.com/ProtoconNet/mitum-currency/v3/digest/util/bson"
"github.com/ProtoconNet/mitum-currency/v3/state/currency"
"github.com/ProtoconNet/mitum-currency/v3/types"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util/encoder"
"github.com/pkg/errors"
)
type CurrencyDoc struct {
mongodbstorage.BaseDoc
st base.State
cd types.CurrencyDesign
}
// NewBalanceDoc gets the State of Amount
func NewCurrencyDoc(st base.State, enc encoder.Encoder) (CurrencyDoc, error) {
cd, err := currency.StateCurrencyDesignValue(st)
if err != nil {
return CurrencyDoc{}, errors.Wrap(err, "CurrencyDoc needs CurrencyDesign state")
}
b, err := mongodbstorage.NewBaseDoc(nil, st, enc)
if err != nil {
return CurrencyDoc{}, err
}
return CurrencyDoc{
BaseDoc: b,
st: st,
cd: cd,
}, nil
}
func (doc CurrencyDoc) MarshalBSON() ([]byte, error) {
m, err := doc.BaseDoc.M()
if err != nil {
return nil, err
}
m["currency"] = doc.cd.Currency().String()
m["height"] = doc.st.Height()
return bsonenc.Marshal(m)
}