/
account_value.go
96 lines (76 loc) · 2.1 KB
/
account_value.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
package digest
import (
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/state"
"github.com/spikeekips/mitum/util/hint"
"github.com/spikeekips/mitum-currency/currency"
currencydigest "github.com/spikeekips/mitum-currency/digest"
)
var (
AccountValueType = hint.Type("mitum-currency-account-value")
AccountValueHint = hint.NewHint(AccountValueType, "v0.0.1")
)
type AccountValue struct {
ac currency.Account
balance []currency.Amount
owner base.Address
isActiveContractAccount bool
height base.Height
previousHeight base.Height
}
func NewAccountValue(st state.State) (AccountValue, error) {
var ac currency.Account
switch a, ok, err := currencydigest.IsAccountState(st); {
case err != nil:
return AccountValue{}, err
case !ok:
return AccountValue{}, errors.Errorf("not state for currency.Account, %T", st.Value().Interface())
default:
ac = a
}
return AccountValue{
ac: ac,
height: st.Height(),
previousHeight: st.PreviousHeight(),
}, nil
}
func (AccountValue) Hint() hint.Hint {
return AccountValueHint
}
func (va AccountValue) Account() currency.Account {
return va.ac
}
func (va AccountValue) Balance() []currency.Amount {
return va.balance
}
func (va AccountValue) Owner() base.Address {
return va.owner
}
func (va AccountValue) Height() base.Height {
return va.height
}
func (va AccountValue) SetHeight(height base.Height) AccountValue {
if int64(height) > int64(va.height) {
va.height = height
}
return va
}
func (va AccountValue) PreviousHeight() base.Height {
return va.previousHeight
}
func (va AccountValue) SetPreviousHeight(height base.Height) AccountValue {
if int64(height) > int64(va.previousHeight) {
va.previousHeight = height
}
return va
}
func (va AccountValue) SetBalance(balance []currency.Amount) AccountValue {
va.balance = balance
return va
}
func (va AccountValue) SetContractAccountStatus(owner base.Address, active bool) AccountValue {
va.owner = owner
va.isActiveContractAccount = active
return va
}