-
Notifications
You must be signed in to change notification settings - Fork 0
/
deposit_stake.go
110 lines (96 loc) · 3.6 KB
/
deposit_stake.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
package tx
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/thetatoken/theta/cmd/thetacli/cmd/utils"
"github.com/thetatoken/theta/common"
"github.com/thetatoken/theta/core"
"github.com/thetatoken/theta/ledger/types"
"github.com/thetatoken/theta/rpc"
rpcc "github.com/ybbus/jsonrpc"
)
// depositStakeCmd represents the deposit stake command
// Example:
// thetacli tx deposit --chain="privatenet" --source=2E833968E5bB786Ae419c4d13189fB081Cc43bab --holder=2E833968E5bB786Ae419c4d13189fB081Cc43bab --stake=6000000 --purpose=0 --seq=7
var depositStakeCmd = &cobra.Command{
Use: "deposit",
Short: "Deposit stake to a validator or guardian",
Example: `thetacli tx deposit --chain="privatenet" --source=2E833968E5bB786Ae419c4d13189fB081Cc43bab --holder=2E833968E5bB786Ae419c4d13189fB081Cc43bab --stake=6000000 --purpose=0 --seq=7`,
Run: doDepositStakeCmd,
}
func doDepositStakeCmd(cmd *cobra.Command, args []string) {
wallet, sourceAddress, err := walletUnlockWithPath(cmd, sourceFlag, pathFlag)
if err != nil {
return
}
defer wallet.Lock(sourceAddress)
fee, ok := types.ParseCoinAmount(feeFlag)
if !ok {
utils.Error("Failed to parse fee")
}
stake, ok := types.ParseCoinAmount(stakeInThetaFlag)
if !ok {
utils.Error("Failed to parse stake")
}
if stake.Cmp(core.Zero) < 0 {
utils.Error("Invalid input: stake must be positive\n")
}
source := types.TxInput{
Address: sourceAddress,
Coins: types.Coins{
ThetaWei: stake,
TFuelWei: new(big.Int).SetUint64(0),
},
Sequence: uint64(seqFlag),
}
holder := types.TxOutput{
Address: common.HexToAddress(holderFlag),
}
depositStakeTx := &types.DepositStakeTx{
Fee: types.Coins{
ThetaWei: new(big.Int).SetUint64(0),
TFuelWei: fee,
},
Source: source,
Holder: holder,
Purpose: purposeFlag,
}
sig, err := wallet.Sign(sourceAddress, depositStakeTx.SignBytes(chainIDFlag))
if err != nil {
utils.Error("Failed to sign transaction: %v\n", err)
}
depositStakeTx.SetSignature(sourceAddress, sig)
raw, err := types.TxToBytes(depositStakeTx)
if err != nil {
utils.Error("Failed to encode transaction: %v\n", err)
}
signedTx := hex.EncodeToString(raw)
client := rpcc.NewRPCClient(viper.GetString(utils.CfgRemoteRPCEndpoint))
res, err := client.Call("theta.BroadcastRawTransaction", rpc.BroadcastRawTransactionArgs{TxBytes: signedTx})
if err != nil {
utils.Error("Failed to broadcast transaction: %v\n", err)
}
if res.Error != nil {
utils.Error("Server returned error: %v\n", res.Error)
}
fmt.Printf("Successfully broadcasted transaction.\n")
}
func init() {
depositStakeCmd.Flags().StringVar(&chainIDFlag, "chain", "", "Chain ID")
depositStakeCmd.Flags().StringVar(&sourceFlag, "source", "", "Source of the stake")
depositStakeCmd.Flags().StringVar(&holderFlag, "holder", "", "Holder of the stake")
depositStakeCmd.Flags().StringVar(&pathFlag, "path", "", "Wallet derivation path")
depositStakeCmd.Flags().StringVar(&feeFlag, "fee", fmt.Sprintf("%dwei", types.MinimumTransactionFeeTFuelWei), "Fee")
depositStakeCmd.Flags().Uint64Var(&seqFlag, "seq", 0, "Sequence number of the transaction")
depositStakeCmd.Flags().StringVar(&stakeInThetaFlag, "stake", "5000000", "Theta amount to stake")
depositStakeCmd.Flags().Uint8Var(&purposeFlag, "purpose", 0, "Purpose of staking")
depositStakeCmd.Flags().StringVar(&walletFlag, "wallet", "soft", "Wallet type (soft|nano)")
depositStakeCmd.MarkFlagRequired("chain")
depositStakeCmd.MarkFlagRequired("source")
depositStakeCmd.MarkFlagRequired("holder")
depositStakeCmd.MarkFlagRequired("seq")
depositStakeCmd.MarkFlagRequired("stake")
}