/
rpc.go
137 lines (124 loc) · 3.72 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
// 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/PhenixChain/devchain/common"
"github.com/PhenixChain/devchain/common/address"
rpctypes "github.com/PhenixChain/devchain/rpc/types"
"github.com/PhenixChain/devchain/types"
ty "github.com/PhenixChain/devchain/plugin/dapp/ticket/types"
context "golang.org/x/net/context"
)
func bindMiner(param *ty.ReqBindMiner) (*ty.ReplyBindMiner, error) {
tBind := &ty.TicketBind{
MinerAddress: param.BindAddr,
ReturnAddress: param.OriginAddr,
}
data, err := types.CallCreateTx(types.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) {
header, err := g.GetLastHeader()
if err != nil {
return nil, err
}
if in.Amount%ty.GetTicketMinerParam(header.Height).TicketPrice != 0 || in.Amount < 0 {
return nil, types.ErrAmount
}
err = address.CheckAddress(in.BindAddr)
if err != nil {
return nil, err
}
err = address.CheckAddress(in.OriginAddr)
if err != nil {
return nil, err
}
if in.CheckBalance {
getBalance := &types.ReqBalance{Addresses: []string{in.OriginAddr}, Execer: "coins", AssetSymbol: "bty", AssetExec: "coins"}
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*types.Coin {
return nil, types.ErrNoBalance
}
}
return bindMiner(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
}