-
Notifications
You must be signed in to change notification settings - Fork 2
/
feeer.go
218 lines (175 loc) · 4.46 KB
/
feeer.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package types
import (
"bytes"
"encoding/binary"
"github.com/ProtoconNet/mitum-currency/v3/common"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/hint"
)
const (
FeeerNil = "nil"
FeeerFixed = "fixed"
FeeerRatio = "ratio"
)
var (
NilFeeerHint = hint.MustNewHint("mitum-currency-nil-feeer-v0.0.1")
FixedFeeerHint = hint.MustNewHint("mitum-currency-fixed-feeer-v0.0.1")
RatioFeeerHint = hint.MustNewHint("mitum-currency-ratio-feeer-v0.0.1")
)
var UnlimitedMaxFeeAmount = common.NewBig(-1)
type Feeer interface {
util.IsValider
hint.Hinter
Type() string
Bytes() []byte
Receiver() base.Address
Min() common.Big
Fee(common.Big) (common.Big, error)
}
type NilFeeer struct {
hint.BaseHinter
}
func NewNilFeeer() NilFeeer {
return NilFeeer{BaseHinter: hint.NewBaseHinter(NilFeeerHint)}
}
func (NilFeeer) Type() string {
return FeeerNil
}
func (NilFeeer) Bytes() []byte {
return nil
}
func (NilFeeer) Receiver() base.Address {
return nil
}
func (NilFeeer) Min() common.Big {
return common.ZeroBig
}
func (NilFeeer) Fee(common.Big) (common.Big, error) {
return common.ZeroBig, nil
}
func (fa NilFeeer) IsValid([]byte) error {
return fa.BaseHinter.IsValid(nil)
}
type FixedFeeer struct {
hint.BaseHinter
receiver base.Address
amount common.Big
}
func NewFixedFeeer(receiver base.Address, amount common.Big) FixedFeeer {
return FixedFeeer{
BaseHinter: hint.NewBaseHinter(FixedFeeerHint),
receiver: receiver,
amount: amount,
}
}
func (FixedFeeer) Type() string {
return FeeerFixed
}
func (fa FixedFeeer) Bytes() []byte {
return util.ConcatBytesSlice(fa.receiver.Bytes(), fa.amount.Bytes())
}
func (fa FixedFeeer) Receiver() base.Address {
return fa.receiver
}
func (fa FixedFeeer) Min() common.Big {
return fa.amount
}
func (fa FixedFeeer) Fee(common.Big) (common.Big, error) {
if fa.isZero() {
return common.ZeroBig, nil
}
return fa.amount, nil
}
func (fa FixedFeeer) IsValid([]byte) error {
if err := fa.BaseHinter.IsValid(nil); err != nil {
return err
}
if err := util.CheckIsValiders(nil, false, fa.receiver); err != nil {
return util.ErrInvalid.Errorf("Invalid receiver for fixed feeer: %v", err)
}
if !fa.amount.OverNil() {
return util.ErrInvalid.Errorf("Fixed feeer amount under zero")
}
return nil
}
func (fa FixedFeeer) isZero() bool {
return fa.amount.IsZero()
}
type RatioFeeer struct {
hint.BaseHinter
receiver base.Address
ratio float64 // 0 >=, or <= 1.0
min common.Big
max common.Big
}
func NewRatioFeeer(receiver base.Address, ratio float64, min, max common.Big) RatioFeeer {
return RatioFeeer{
BaseHinter: hint.NewBaseHinter(RatioFeeerHint),
receiver: receiver,
ratio: ratio,
min: min,
max: max,
}
}
func (RatioFeeer) Type() string {
return FeeerRatio
}
func (fa RatioFeeer) Bytes() []byte {
var rb bytes.Buffer
_ = binary.Write(&rb, binary.BigEndian, fa.ratio)
return util.ConcatBytesSlice(fa.receiver.Bytes(), rb.Bytes(), fa.min.Bytes(), fa.max.Bytes())
}
func (fa RatioFeeer) Receiver() base.Address {
return fa.receiver
}
func (fa RatioFeeer) Min() common.Big {
return fa.min
}
func (fa RatioFeeer) Fee(a common.Big) (common.Big, error) {
if fa.isZero() {
return common.ZeroBig, nil
} else if a.IsZero() {
return fa.min, nil
}
if fa.isOne() {
return a, nil
} else if f := a.MulFloat64(fa.ratio); f.Compare(fa.min) < 0 {
return fa.min, nil
} else {
if !fa.isUnlimited() && f.Compare(fa.max) > 0 {
return fa.max, nil
}
return f, nil
}
}
func (fa RatioFeeer) IsValid([]byte) error {
if err := fa.BaseHinter.IsValid(nil); err != nil {
return err
}
if err := util.CheckIsValiders(nil, false, fa.receiver); err != nil {
return util.ErrInvalid.Errorf("Invalid receiver for ratio feeer: %v", err)
}
if fa.ratio < 0 || fa.ratio > 1 {
return util.ErrInvalid.Errorf("Invalid ratio, %v; it should be 0 >=, <= 1", fa.ratio)
}
if !fa.min.OverNil() {
return util.ErrInvalid.Errorf("Ratio feeer min amount under zero")
} else if !fa.max.Equal(UnlimitedMaxFeeAmount) {
if !fa.max.OverNil() {
return util.ErrInvalid.Errorf("Ratio feeer max amount under zero")
} else if fa.min.Compare(fa.max) > 0 {
return util.ErrInvalid.Errorf("ratio feeer min should over max")
}
}
return nil
}
func (fa RatioFeeer) isUnlimited() bool {
return fa.max.Equal(UnlimitedMaxFeeAmount)
}
func (fa RatioFeeer) isZero() bool {
return fa.ratio == 0
}
func (fa RatioFeeer) isOne() bool {
return fa.ratio == 1
}