-
Notifications
You must be signed in to change notification settings - Fork 51
/
staking.go
165 lines (146 loc) · 5.03 KB
/
staking.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
package main
import (
"crypto/ecdsa"
"encoding/hex"
"errors"
"fmt"
"math/big"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ethereum/go-ethereum/common"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/tendermint/tendermint/libs/cli"
"github.com/smartbch/smartbch/internal/bigutils"
"github.com/smartbch/smartbch/internal/ethutils"
"github.com/smartbch/smartbch/staking"
)
const (
flagRewardTo = "reward-to"
flagType = "type"
create = "create"
edit = "edit"
retire = "retire"
increaseMinGasPrice = "increase"
decreaseMinGasPrice = "decrease"
)
func StakingCmd(ctx *Context) *cobra.Command {
cmd := &cobra.Command{
Use: "staking",
Short: "call staking contract method",
Example: `
smartbchd staking \
--validator-key=07427a59913df1ae8af709f60f536ddba122b0afa8908291471ca58c603a7447 \
--consensus-pubkey=f7847ca2afd06fedcd2c404c6e99db4d5475e0746154a550fe72990657675dc9 \
--staking-coin=10000000000000 \
--introduction="my node" \
--reward-to=0x9887310499DB9E65411FC0A57689B4429755c372 \
--nonce=1 \
--chain-id=0x2710 \
--gas-price=1000 \
--type="create" \
--verbose
`,
RunE: func(_ *cobra.Command, args []string) error {
c := ctx.Config.NodeConfig
c.SetRoot(viper.GetString(cli.HomeFlag))
// get private key
priKey, _, err := ethutils.HexToPrivKey(viper.GetString(flagValKey))
if err != nil {
return fmt.Errorf("private key parse error: " + err.Error())
}
nonce := viper.GetUint64(flagNonce)
//todo: get chain id in config.toml
//chainID := ctx.Config.ChainID()
chainID, err := parseChainID(viper.GetString(flagChainId))
if err != nil {
return fmt.Errorf("parse chain id errpr: %s", err.Error())
}
fType := viper.GetString(flagType)
if fType == retire {
data := staking.PackRetire()
return printSignedTx(big.NewInt(0), data, nonce, priKey, chainID.ToBig())
} else if fType == increaseMinGasPrice {
data := staking.PackIncreaseMinGasPrice()
return printSignedTx(big.NewInt(0), data, nonce, priKey, chainID.ToBig())
} else if fType == decreaseMinGasPrice {
data := staking.PackDecreaseMinGasPrice()
return printSignedTx(big.NewInt(0), data, nonce, priKey, chainID.ToBig())
}
// get staking coin
sCoin, success := bigutils.ParseU256(viper.GetString(flagStakingCoin))
if !success {
return fmt.Errorf("staking coin parse failed")
}
// generate edit validator info
var intro [32]byte
copy(intro[:], viper.GetString(flagIntroduction))
rewardToString := viper.GetString(flagRewardTo)
rewardTo := common.HexToAddress(rewardToString)
if rewardToString == "" {
rewardTo = ethutils.PrivKeyToAddr(priKey)
}
if fType == edit {
data := staking.PackEditValidator(rewardTo, intro)
return printSignedTx(sCoin.ToBig(), data, nonce, priKey, chainID.ToBig())
}
if fType == create {
pubKeyHex := viper.GetString(flagConsPubKey)
if pubKeyHex == "" {
return errors.New(flagConsPubKey + " is missing")
}
pk, _, err := ethutils.HexToPubKey(pubKeyHex)
if err != nil {
return err
}
var pubkey [32]byte
copy(pubkey[:], pk)
data := staking.PackCreateValidator(rewardTo, intro, pubkey)
return printSignedTx(sCoin.ToBig(), data, nonce, priKey, chainID.ToBig())
}
return errors.New("invalid staking function type")
},
}
cmd.Flags().String(flagAddress, "", "validator address")
cmd.Flags().String(flagConsPubKey, "", "consensus pubkey")
cmd.Flags().Int64(flagVotingPower, 0, "voting power")
cmd.Flags().String(flagStakingCoin, "0", "staking coin")
cmd.Flags().String(flagRewardTo, "", "validator rewardTo address")
cmd.Flags().String(flagType, "", "validator function type, including create, edit, retire, increase, decrease")
cmd.Flags().String(flagIntroduction, "", "introduction")
cmd.Flags().Bool(flagVerbose, false, "display verbose information")
cmd.Flags().Uint64(flagGasPrice, 1500000000, "specify gas price")
cmd.Flags().String(flagChainId, "", "specify gas price")
cmd.Flags().Uint64(flagNonce, 1, "specify tx nonce")
cmd.Flags().String(flagValKey, "", "specify from address private key")
_ = cmd.MarkFlagRequired(flagType)
_ = cmd.MarkFlagRequired(flagValKey)
_ = cmd.MarkFlagRequired(flagChainId)
_ = cmd.MarkFlagRequired(flagNonce)
return cmd
}
func printSignedTx(value *big.Int, data []byte, nonce uint64, priKey *ecdsa.PrivateKey, chainID *big.Int) error {
to := common.Address(staking.StakingContractAddress)
txData := &gethtypes.LegacyTx{
Nonce: nonce,
GasPrice: big.NewInt(viper.GetInt64(flagGasPrice)),
Gas: staking.GasOfValidatorOp,
To: &to,
Value: value,
Data: data,
}
tx := gethtypes.NewTx(txData)
tx, e := ethutils.SignTx(tx, chainID, priKey)
if e != nil {
return fmt.Errorf("sign tx errpr: %s", e.Error())
}
txBytes, e := ethutils.EncodeTx(tx)
if e != nil {
return fmt.Errorf("encode tx errpr: %s", e.Error())
}
fmt.Println("0x" + hex.EncodeToString(txBytes))
if viper.GetBool(flagVerbose) {
out, _ := tx.MarshalJSON()
fmt.Println(string(out))
}
return nil
}