/
lottery.go
executable file
·237 lines (214 loc) · 6.02 KB
/
lottery.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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import (
"encoding/json"
"reflect"
"github.com/33cn/chain33/common/address"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types"
)
var (
llog = log.New("module", "exectype."+LotteryX)
)
func init() {
types.AllowUserExec = append(types.AllowUserExec, []byte(LotteryX))
types.RegFork(LotteryX, InitFork)
types.RegExec(LotteryX, InitExecutor)
}
//InitFork ...
func InitFork(cfg *types.Chain33Config) {
cfg.RegisterDappFork(LotteryX, "Enable", 0)
}
//InitExecutor ...
func InitExecutor(cfg *types.Chain33Config) {
types.RegistorExecutor(LotteryX, NewType(cfg))
}
// LotteryType def
type LotteryType struct {
types.ExecTypeBase
}
// NewType method
func NewType(cfg *types.Chain33Config) *LotteryType {
c := &LotteryType{}
c.SetChild(c)
c.SetConfig(cfg)
return c
}
// GetName 获取执行器名称
func (lottery *LotteryType) GetName() string {
return LotteryX
}
// GetLogMap method
func (lottery *LotteryType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{
TyLogLotteryCreate: {Ty: reflect.TypeOf(ReceiptLottery{}), Name: "LogLotteryCreate"},
TyLogLotteryBuy: {Ty: reflect.TypeOf(ReceiptLottery{}), Name: "LogLotteryBuy"},
TyLogLotteryDraw: {Ty: reflect.TypeOf(ReceiptLottery{}), Name: "LogLotteryDraw"},
TyLogLotteryClose: {Ty: reflect.TypeOf(ReceiptLottery{}), Name: "LogLotteryClose"},
}
}
// GetPayload method
func (lottery *LotteryType) GetPayload() types.Message {
return &LotteryAction{}
}
// CreateTx method
func (lottery LotteryType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
llog.Debug("lottery.CreateTx", "action", action)
cfg := lottery.GetConfig()
if action == "LotteryCreate" {
var param LotteryCreateTx
err := json.Unmarshal(message, ¶m)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawLotteryCreateTx(cfg, ¶m)
} else if action == "LotteryBuy" {
var param LotteryBuyTx
err := json.Unmarshal(message, ¶m)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawLotteryBuyTx(cfg, ¶m)
} else if action == "LotteryDraw" {
var param LotteryDrawTx
err := json.Unmarshal(message, ¶m)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawLotteryDrawTx(cfg, ¶m)
} else if action == "LotteryClose" {
var param LotteryCloseTx
err := json.Unmarshal(message, ¶m)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawLotteryCloseTx(cfg, ¶m)
} else {
return nil, types.ErrNotSupport
}
}
// GetTypeMap method
func (lottery LotteryType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Create": LotteryActionCreate,
"Buy": LotteryActionBuy,
"Draw": LotteryActionDraw,
"Close": LotteryActionClose,
}
}
// CreateRawLotteryCreateTx method
func CreateRawLotteryCreateTx(cfg *types.Chain33Config, parm *LotteryCreateTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawLotteryCreateTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &LotteryCreate{
PurBlockNum: parm.PurBlockNum,
DrawBlockNum: parm.DrawBlockNum,
OpRewardRatio: parm.OpRewardRatio,
DevRewardRatio: parm.DevRewardRatio,
}
create := &LotteryAction{
Ty: LotteryActionCreate,
Value: &LotteryAction_Create{v},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(LotteryX)),
Payload: types.Encode(create),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(LotteryX)),
}
name := cfg.ExecName(LotteryX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawLotteryBuyTx method
func CreateRawLotteryBuyTx(cfg *types.Chain33Config, parm *LotteryBuyTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawLotteryBuyTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &LotteryBuy{
LotteryId: parm.LotteryID,
Amount: parm.Amount,
Number: parm.Number,
Way: parm.Way,
}
buy := &LotteryAction{
Ty: LotteryActionBuy,
Value: &LotteryAction_Buy{v},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(LotteryX)),
Payload: types.Encode(buy),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(LotteryX)),
}
name := cfg.ExecName(LotteryX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawLotteryDrawTx method
func CreateRawLotteryDrawTx(cfg *types.Chain33Config, parm *LotteryDrawTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawLotteryDrawTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &LotteryDraw{
LotteryId: parm.LotteryID,
}
draw := &LotteryAction{
Ty: LotteryActionDraw,
Value: &LotteryAction_Draw{v},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(LotteryX)),
Payload: types.Encode(draw),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(LotteryX)),
}
name := cfg.ExecName(LotteryX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawLotteryCloseTx method
func CreateRawLotteryCloseTx(cfg *types.Chain33Config, parm *LotteryCloseTx) (*types.Transaction, error) {
if parm == nil {
llog.Error("CreateRawLotteryCloseTx", "parm", parm)
return nil, types.ErrInvalidParam
}
v := &LotteryClose{
LotteryId: parm.LotteryID,
}
close := &LotteryAction{
Ty: LotteryActionClose,
Value: &LotteryAction_Close{v},
}
tx := &types.Transaction{
Execer: []byte(cfg.ExecName(LotteryX)),
Payload: types.Encode(close),
Fee: parm.Fee,
To: address.ExecAddress(cfg.ExecName(LotteryX)),
}
name := cfg.ExecName(LotteryX)
tx, err := types.FormatTx(cfg, name, tx)
if err != nil {
return nil, err
}
return tx, nil
}