-
Notifications
You must be signed in to change notification settings - Fork 4
/
currency_register.go
205 lines (171 loc) · 6.47 KB
/
currency_register.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
package cmds
import (
"github.com/pkg/errors"
"github.com/spikeekips/mitum-currency/currency"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/operation"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/isvalid"
)
type CurrencyFixedFeeerFlags struct {
Receiver AddressFlag `name:"receiver" help:"fee receiver account address"`
Amount BigFlag `name:"amount" help:"fee amount"`
feeer currency.Feeer
}
func (fl *CurrencyFixedFeeerFlags) IsValid([]byte) error {
if len(fl.Receiver.String()) < 1 {
return nil
}
var receiver base.Address
if a, err := fl.Receiver.Encode(jenc); err != nil {
return isvalid.InvalidError.Errorf("invalid receiver format, %q: %w", fl.Receiver.String(), err)
} else if err := a.IsValid(nil); err != nil {
return isvalid.InvalidError.Errorf("invalid receiver address, %q: %w", fl.Receiver.String(), err)
} else {
receiver = a
}
fl.feeer = currency.NewFixedFeeer(receiver, fl.Amount.Big)
return fl.feeer.IsValid(nil)
}
type CurrencyRatioFeeerFlags struct {
Receiver AddressFlag `name:"receiver" help:"fee receiver account address"`
Ratio float64 `name:"ratio" help:"fee ratio, multifly by operation amount"`
Min BigFlag `name:"min" help:"minimum fee"`
Max BigFlag `name:"max" help:"maximum fee"`
feeer currency.Feeer
}
func (fl *CurrencyRatioFeeerFlags) IsValid([]byte) error {
if len(fl.Receiver.String()) < 1 {
return nil
}
var receiver base.Address
if a, err := fl.Receiver.Encode(jenc); err != nil {
return isvalid.InvalidError.Errorf("invalid receiver format, %q: %w", fl.Receiver.String(), err)
} else if err := a.IsValid(nil); err != nil {
return isvalid.InvalidError.Errorf("invalid receiver address, %q: %w", fl.Receiver.String(), err)
} else {
receiver = a
}
fl.feeer = currency.NewRatioFeeer(receiver, fl.Ratio, fl.Min.Big, fl.Max.Big)
return fl.feeer.IsValid(nil)
}
type CurrencyPolicyFlags struct {
NewAccountMinBalance BigFlag `name:"new-account-min-balance" help:"minimum balance for new account"` // nolint lll
}
func (*CurrencyPolicyFlags) IsValid([]byte) error {
return nil
}
type CurrencyDesignFlags struct {
Currency CurrencyIDFlag `arg:"" name:"currency-id" help:"currency id" required:"true"`
GenesisAmount BigFlag `arg:"" name:"genesis-amount" help:"genesis amount" required:"true"`
GenesisAccount AddressFlag `arg:"" name:"genesis-account" help:"genesis-account address for genesis balance" required:"true"` // nolint lll
CurrencyPolicyFlags `prefix:"policy-" help:"currency policy" required:"true"`
FeeerString string `name:"feeer" help:"feeer type, {nil, fixed, ratio}" required:"true"`
CurrencyFixedFeeerFlags `prefix:"feeer-fixed-" help:"fixed feeer"`
CurrencyRatioFeeerFlags `prefix:"feeer-ratio-" help:"ratio feeer"`
currencyDesign currency.CurrencyDesign
}
func (fl *CurrencyDesignFlags) IsValid([]byte) error {
if err := fl.CurrencyPolicyFlags.IsValid(nil); err != nil {
return err
} else if err := fl.CurrencyFixedFeeerFlags.IsValid(nil); err != nil {
return err
} else if err := fl.CurrencyRatioFeeerFlags.IsValid(nil); err != nil {
return err
}
var feeer currency.Feeer
switch t := fl.FeeerString; t {
case currency.FeeerNil, "":
feeer = currency.NewNilFeeer()
case currency.FeeerFixed:
feeer = fl.CurrencyFixedFeeerFlags.feeer
case currency.FeeerRatio:
feeer = fl.CurrencyRatioFeeerFlags.feeer
default:
return isvalid.InvalidError.Errorf("unknown feeer type, %q", t)
}
if feeer == nil {
return isvalid.InvalidError.Errorf("empty feeer flags")
} else if err := feeer.IsValid(nil); err != nil {
return err
}
po := currency.NewCurrencyPolicy(fl.CurrencyPolicyFlags.NewAccountMinBalance.Big, feeer)
if err := po.IsValid(nil); err != nil {
return err
}
var genesisAccount base.Address
if a, err := fl.GenesisAccount.Encode(jenc); err != nil {
return isvalid.InvalidError.Errorf("invalid genesis-account format, %q: %w", fl.GenesisAccount.String(), err)
} else if err := a.IsValid(nil); err != nil {
return isvalid.InvalidError.Errorf("invalid genesis-account address, %q: %w", fl.GenesisAccount.String(), err)
} else {
genesisAccount = a
}
am := currency.NewAmount(fl.GenesisAmount.Big, fl.Currency.CID)
if err := am.IsValid(nil); err != nil {
return err
}
fl.currencyDesign = currency.NewCurrencyDesign(am, genesisAccount, po)
return fl.currencyDesign.IsValid(nil)
}
type CurrencyRegisterCommand struct {
*BaseCommand
OperationFlags
CurrencyDesignFlags
}
func NewCurrencyRegisterCommand() CurrencyRegisterCommand {
return CurrencyRegisterCommand{
BaseCommand: NewBaseCommand("currency-register-operation"),
}
}
func (cmd *CurrencyRegisterCommand) Run(version util.Version) error { // nolint:dupl
if err := cmd.Initialize(cmd, version); err != nil {
return errors.Wrap(err, "failed to initialize command")
}
if err := cmd.parseFlags(); err != nil {
return err
}
var op operation.Operation
if i, err := cmd.createOperation(); err != nil {
return errors.Wrap(err, "failed to create currency-register operation")
} else if err := i.IsValid([]byte(cmd.OperationFlags.NetworkID)); err != nil {
return errors.Wrap(err, "invalid currency-register operation")
} else {
cmd.Log().Debug().Interface("operation", i).Msg("operation loaded")
op = i
}
i, err := operation.NewBaseSeal(
cmd.OperationFlags.Privatekey,
[]operation.Operation{op},
[]byte(cmd.OperationFlags.NetworkID),
)
if err != nil {
return errors.Wrap(err, "failed to create operation.Seal")
}
cmd.Log().Debug().Interface("seal", i).Msg("seal loaded")
PrettyPrint(cmd.Out, cmd.Pretty, i)
return nil
}
func (cmd *CurrencyRegisterCommand) parseFlags() error {
if err := cmd.OperationFlags.IsValid(nil); err != nil {
return err
} else if err := cmd.CurrencyDesignFlags.IsValid(nil); err != nil {
return err
}
cmd.Log().Debug().Interface("currency-design", cmd.CurrencyDesignFlags.currencyDesign).Msg("currency design loaded")
return nil
}
func (cmd *CurrencyRegisterCommand) createOperation() (currency.CurrencyRegister, error) {
fact := currency.NewCurrencyRegisterFact([]byte(cmd.Token), cmd.currencyDesign)
var fs []base.FactSign
sig, err := base.NewFactSignature(
cmd.OperationFlags.Privatekey,
fact,
[]byte(cmd.OperationFlags.NetworkID),
)
if err != nil {
return currency.CurrencyRegister{}, err
}
fs = append(fs, base.NewBaseFactSign(cmd.OperationFlags.Privatekey.Publickey(), sig))
return currency.NewCurrencyRegister(fact, fs, cmd.OperationFlags.Memo)
}