forked from sero-cash/go-sero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gentx.go
292 lines (263 loc) · 5.97 KB
/
gentx.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package ethapi
import (
"fmt"
"math/big"
"github.com/sero-cash/go-sero/common/address"
"github.com/sero-cash/go-sero/zero/txs/stx"
"github.com/pkg/errors"
"github.com/sero-cash/go-czero-import/c_type"
"github.com/sero-cash/go-czero-import/superzk"
"github.com/sero-cash/go-sero/common"
"github.com/sero-cash/go-sero/common/hexutil"
"github.com/sero-cash/go-sero/zero/txs/assets"
"github.com/sero-cash/go-sero/zero/txtool/prepare"
"github.com/sero-cash/go-sero/zero/utils"
)
type PkgCloseArgs struct {
Id c_type.Uint256
Key c_type.Uint256
}
func (self *PkgCloseArgs) toCmd() *prepare.PkgCloseCmd {
if self == nil {
return nil
}
return &prepare.PkgCloseCmd{
self.Id,
self.Key,
}
}
type PkgTransferArgs struct {
Id c_type.Uint256
PKr AllMixedAddress
}
func (self *PkgTransferArgs) toCmd() *prepare.PkgTransferCmd {
if self == nil {
return nil
}
return &prepare.PkgTransferCmd{
self.Id,
self.PKr.ToPKr(),
}
}
type PkgCreateArgs struct {
Id c_type.Uint256
PKr AllMixedAddress
Currency Smbol
Value *Big
Memo c_type.Uint512
}
func (self *PkgCreateArgs) toCmd() *prepare.PkgCreateCmd {
if self == nil {
return nil
}
asset := assets.Asset{}
if !self.Currency.IsEmpty() && self.Value != nil {
asset.Tkn = &assets.Token{
utils.CurrencyToUint256(string(self.Currency)),
utils.U256(*self.Value.ToInt()),
}
}
return &prepare.PkgCreateCmd{
self.Id,
self.PKr.ToPKr(),
asset,
self.Memo,
}
}
type BuyShareArgs struct {
Value Big
Vote PKrAddress
Pool *c_type.Uint256
}
func (self *BuyShareArgs) toCmd() *stx.BuyShareCmd {
if self == nil {
return nil
}
return &stx.BuyShareCmd{
utils.U256(*self.Value.ToInt()),
*self.Vote.ToPKr(),
self.Pool,
}
}
type RegistPoolArgs struct {
Value utils.U256
Vote PKrAddress
FeeRate uint32
}
func (self *RegistPoolArgs) toCmd() *stx.RegistPoolCmd {
if self == nil {
return nil
}
return &stx.RegistPoolCmd{
utils.U256(*self.Value.ToInt()),
*self.Vote.ToPKr(),
self.FeeRate,
}
}
type ClosePoolArgs struct {
}
func (self *ClosePoolArgs) toCmd() *stx.ClosePoolCmd {
if self == nil {
return nil
}
return &stx.ClosePoolCmd{}
}
type ContractArgs struct {
Currency Smbol
Value *Big
Category Smbol
Tkt *common.Hash
To *ContractAddress
Data hexutil.Bytes
}
func (self *ContractArgs) toCmd() *stx.ContractCmd {
if self == nil {
return nil
}
asset := assets.Asset{}
if !self.Currency.IsEmpty() && self.Value != nil {
asset.Tkn = &assets.Token{
utils.CurrencyToUint256(string(self.Currency)),
utils.U256(*self.Value.ToInt()),
}
}
if !self.Category.IsEmpty() && self.Tkt != nil {
asset.Tkt = &assets.Ticket{
utils.CurrencyToUint256(string(self.Category)),
*self.Tkt.HashToUint256(),
}
}
var pkr *c_type.PKr
if self.To != nil {
temp := c_type.PKr(*self.To)
pkr = &temp
}
return &stx.ContractCmd{
asset,
pkr,
self.Data,
}
}
type CmdsArgs struct {
//Share
BuyShare *BuyShareArgs
//Pool
RegistPool *RegistPoolArgs
ClosePool *ClosePoolArgs
//Contract
Contract *ContractArgs
//Package
PkgCreate *PkgCreateArgs
PkgTransfer *PkgTransferArgs
PkgClose *PkgCloseArgs
}
func (self *CmdsArgs) toCmds() prepare.Cmds {
return prepare.Cmds{
self.BuyShare.toCmd(),
self.RegistPool.toCmd(),
self.ClosePool.toCmd(),
self.Contract.toCmd(),
self.PkgCreate.toCmd(),
self.PkgTransfer.toCmd(),
self.PkgClose.toCmd(),
}
}
type GenTxArgs struct {
From address.PKAddress
RefundTo *PKrAddress
Receptions []ReceptionArgs
Cmds *CmdsArgs
Gas uint64
GasPrice *Big
Roots []c_type.Uint256
}
func (args GenTxArgs) check() error {
if len(args.Receptions) == 0 && args.Cmds == nil {
return errors.New("have no receptions")
}
if args.GasPrice == nil {
return fmt.Errorf("gasPrice not specified")
}
if args.RefundTo != nil {
if !superzk.IsPKrValid(args.RefundTo.ToPKr()) {
return errors.New("RefundTo is not a valid pkr")
}
}
if args.Cmds != nil {
if args.Cmds.RegistPool != nil || args.Cmds.ClosePool != nil {
if args.RefundTo == nil {
return errors.New("Close | Regist StakingNode must need fixed refund address")
}
}
}
for _, rec := range args.Receptions {
_, err := validAddress(rec.Addr)
if err != nil {
return err
}
if rec.Currency.IsEmpty() && rec.Category.IsEmpty() {
return errors.Errorf("%v reception currency and categroy is nil", hexutil.Encode(rec.Addr[:]))
}
if rec.Value == nil && rec.TKt == nil {
return errors.Errorf("%v reception value or ticket is nil", hexutil.Encode(rec.Addr[:]))
}
}
return nil
}
func (args GenTxArgs) toTxParam() prepare.PreTxParam {
gasPrice := args.GasPrice.ToInt()
if gasPrice.Sign() == 0 {
gasPrice = new(big.Int).SetUint64(defaultGasPrice)
}
receptions := []prepare.Reception{}
for _, rec := range args.Receptions {
pkr := MixAdrressToPkr(rec.Addr)
var tkn *assets.Token
var tkt *assets.Ticket
if rec.Currency != "" && rec.Value != nil {
var currency c_type.Uint256
bytes := common.LeftPadBytes([]byte(string(rec.Currency)), 32)
copy(currency[:], bytes)
tkn = &assets.Token{
Currency: currency,
Value: utils.U256(*rec.Value.ToInt()),
}
}
if rec.Category != "" && rec.TKt != nil {
var category c_type.Uint256
var ticketValue c_type.Uint256
bytes := common.LeftPadBytes([]byte(string(rec.Category)), 32)
copy(category[:], bytes)
copy(ticketValue[:], rec.TKt[:])
tkt = &assets.Ticket{
Category: category,
Value: ticketValue,
}
}
assets := assets.Asset{tkn, tkt}
receptions = append(receptions, prepare.Reception{
pkr,
assets,
})
}
var refundPkr *c_type.PKr
if args.RefundTo != nil {
refundPkr = args.RefundTo.ToPKr()
}
cmds := prepare.Cmds{}
if args.Cmds != nil {
cmds = args.Cmds.toCmds()
}
return prepare.PreTxParam{
args.From.ToUint512(),
refundPkr,
receptions,
cmds,
assets.Token{
utils.CurrencyToUint256("SERO"),
utils.U256(*big.NewInt(0).Mul(big.NewInt(int64(args.Gas)), args.GasPrice.ToInt())),
},
gasPrice,
args.Roots,
}
}