/
rpc.go
167 lines (149 loc) · 4.48 KB
/
rpc.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
// 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 rpc
import (
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
ty "github.com/33cn/plugin/plugin/dapp/ticket/types"
"golang.org/x/net/context"
)
func bindMiner(cfg *types.Chain33Config, param *ty.ReqBindMiner) (*ty.ReplyBindMiner, error) {
tBind := &ty.TicketBind{
MinerAddress: param.BindAddr,
ReturnAddress: param.OriginAddr,
}
data, err := types.CallCreateTx(cfg, cfg.ExecName(ty.TicketX), "Tbind", tBind)
if err != nil {
return nil, err
}
hex := common.ToHex(data)
return &ty.ReplyBindMiner{TxHex: hex}, nil
}
// CreateBindMiner 创建绑定挖矿
func (g *channelClient) CreateBindMiner(ctx context.Context, in *ty.ReqBindMiner) (*ty.ReplyBindMiner, error) {
if in.BindAddr != "" {
err := address.CheckAddress(in.BindAddr)
if err != nil {
return nil, err
}
}
err := address.CheckAddress(in.OriginAddr)
if err != nil {
return nil, err
}
cfg := g.GetConfig()
if in.CheckBalance {
header, err := g.GetLastHeader()
if err != nil {
return nil, err
}
price := ty.GetTicketMinerParam(cfg, header.Height).TicketPrice
if price == 0 {
return nil, types.ErrInvalidParam
}
if in.Amount%ty.GetTicketMinerParam(cfg, header.Height).TicketPrice != 0 || in.Amount < 0 {
return nil, types.ErrAmount
}
getBalance := &types.ReqBalance{Addresses: []string{in.OriginAddr}, Execer: cfg.GetCoinExec(), AssetSymbol: "bty", AssetExec: cfg.GetCoinExec()}
balances, err := g.GetCoinsAccountDB().GetBalance(g, getBalance)
if err != nil {
return nil, err
}
if len(balances) == 0 {
return nil, types.ErrInvalidParam
}
if balances[0].Balance < in.Amount+2*cfg.GetCoinPrecision() {
return nil, types.ErrNoBalance
}
}
return bindMiner(cfg, in)
}
// SetAutoMining set auto mining
func (g *channelClient) SetAutoMining(ctx context.Context, in *ty.MinerFlag) (*types.Reply, error) {
data, err := g.ExecWalletFunc(ty.TicketX, "WalletAutoMiner", in)
if err != nil {
return nil, err
}
return data.(*types.Reply), nil
}
// GetTicketCount get count
func (g *channelClient) GetTicketCount(ctx context.Context, in *types.ReqNil) (*types.Int64, error) {
data, err := g.QueryConsensusFunc(ty.TicketX, "GetTicketCount", &types.ReqNil{})
if err != nil {
return nil, err
}
return data.(*types.Int64), nil
}
// CloseTickets close ticket
func (g *channelClient) CloseTickets(ctx context.Context, in *ty.TicketClose) (*types.ReplyHashes, error) {
inn := *in
data, err := g.ExecWalletFunc(ty.TicketX, "CloseTickets", &inn)
if err != nil {
return nil, err
}
return data.(*types.ReplyHashes), nil
}
// CreateBindMiner create bind miner
func (c *Jrpc) CreateBindMiner(in *ty.ReqBindMiner, result *interface{}) error {
reply, err := c.cli.CreateBindMiner(context.Background(), in)
if err != nil {
return err
}
*result = reply
return nil
}
// GetTicketCount get ticket count
func (c *Jrpc) GetTicketCount(in *types.ReqNil, result *int64) error {
resp, err := c.cli.GetTicketCount(context.Background(), &types.ReqNil{})
if err != nil {
return err
}
*result = resp.GetData()
return nil
}
// CloseTickets close ticket
func (c *Jrpc) CloseTickets(in *ty.TicketClose, result *interface{}) error {
resp, err := c.cli.CloseTickets(context.Background(), in)
if err != nil {
return err
}
var hashes rpctypes.ReplyHashes
for _, has := range resp.Hashes {
hashes.Hashes = append(hashes.Hashes, common.ToHex(has))
}
*result = &hashes
return nil
}
// SetAutoMining set auto mining
func (c *Jrpc) SetAutoMining(in *ty.MinerFlag, result *rpctypes.Reply) error {
resp, err := c.cli.SetAutoMining(context.Background(), in)
if err != nil {
return err
}
var reply rpctypes.Reply
reply.IsOk = resp.GetIsOk()
reply.Msg = string(resp.GetMsg())
*result = reply
return nil
}
// GetTicketList get ticket list info
func (g *channelClient) GetTicketList(ctx context.Context, in *types.ReqNil) ([]*ty.Ticket, error) {
inn := *in
data, err := g.ExecWalletFunc(ty.TicketX, "WalletGetTickets", &inn)
if err != nil {
return nil, err
}
return data.(*ty.ReplyWalletTickets).Tickets, nil
}
// GetTicketList get ticket list info
func (c *Jrpc) GetTicketList(in *types.ReqNil, result *interface{}) error {
resp, err := c.cli.GetTicketList(context.Background(), &types.ReqNil{})
if err != nil {
return err
}
*result = resp
return nil
}