wallet: refresh,auto-refresh,get-height
Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
This commit is contained in:
parent
8058e3dfe0
commit
78745598d8
85
cmd/monero/commands/daemon/start_mining.go
Normal file
85
cmd/monero/commands/daemon/start_mining.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cirocosta/go-monero/cmd/monero/display"
|
||||
"github.com/cirocosta/go-monero/cmd/monero/options"
|
||||
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
|
||||
)
|
||||
|
||||
type startMiningCommand struct {
|
||||
MinerAddress string
|
||||
BackgroundMining bool
|
||||
IgnoreBattery bool
|
||||
ThreadsCount uint
|
||||
|
||||
JSON bool
|
||||
}
|
||||
|
||||
func (c *startMiningCommand) Cmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "start-mining",
|
||||
Short: "start mining on the daemon",
|
||||
RunE: c.RunE,
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&c.JSON, "json",
|
||||
false, "whether or not to output the result as json")
|
||||
|
||||
cmd.Flags().StringVar(&c.MinerAddress, "address",
|
||||
"", "address to send coinbase transactions to")
|
||||
_ = cmd.MarkFlagRequired("address")
|
||||
|
||||
cmd.Flags().UintVar(&c.ThreadsCount, "threads",
|
||||
1, "number of threads to dedicate to mining")
|
||||
|
||||
cmd.Flags().BoolVar(&c.BackgroundMining, "background-mining",
|
||||
false, "whether the miner should run in the background or "+
|
||||
"foreground")
|
||||
|
||||
cmd.Flags().BoolVar(&c.IgnoreBattery, "ignore-battery",
|
||||
true, "if laptop battery state should be ignore or not")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *startMiningCommand) RunE(_ *cobra.Command, _ []string) error {
|
||||
ctx, cancel := options.RootOptions.Context()
|
||||
defer cancel()
|
||||
|
||||
client, err := options.RootOptions.Client()
|
||||
if err != nil {
|
||||
return fmt.Errorf("client: %w", err)
|
||||
}
|
||||
|
||||
params := daemon.StartMiningRequestParameters{
|
||||
MinerAddress: c.MinerAddress,
|
||||
BackgroundMining: c.BackgroundMining,
|
||||
IgnoreBattery: c.IgnoreBattery,
|
||||
ThreadsCount: c.ThreadsCount,
|
||||
}
|
||||
resp, err := client.StartMining(ctx, params)
|
||||
if err != nil {
|
||||
return fmt.Errorf("start mining: %w", err)
|
||||
}
|
||||
if c.JSON {
|
||||
return display.JSON(resp)
|
||||
}
|
||||
|
||||
c.pretty(resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *startMiningCommand) pretty(v *daemon.StartMiningResult) {
|
||||
table := display.NewTable()
|
||||
table.AddRow("Status:", v.Status)
|
||||
fmt.Println(table)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCommand.AddCommand((&startMiningCommand{}).Cmd())
|
||||
}
|
65
cmd/monero/commands/daemon/stop_mining.go
Normal file
65
cmd/monero/commands/daemon/stop_mining.go
Normal file
|
@ -0,0 +1,65 @@
|
|||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cirocosta/go-monero/cmd/monero/display"
|
||||
"github.com/cirocosta/go-monero/cmd/monero/options"
|
||||
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
|
||||
)
|
||||
|
||||
type stopMiningCommand struct {
|
||||
MinerAddress string
|
||||
BackgroundMining bool
|
||||
IgnoreBattery bool
|
||||
ThreadsCount uint
|
||||
|
||||
JSON bool
|
||||
}
|
||||
|
||||
func (c *stopMiningCommand) Cmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "stop-mining",
|
||||
Short: "stop mining on the daemon",
|
||||
RunE: c.RunE,
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&c.JSON, "json",
|
||||
false, "whether or not to output the result as json")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *stopMiningCommand) RunE(_ *cobra.Command, _ []string) error {
|
||||
ctx, cancel := options.RootOptions.Context()
|
||||
defer cancel()
|
||||
|
||||
client, err := options.RootOptions.Client()
|
||||
if err != nil {
|
||||
return fmt.Errorf("client: %w", err)
|
||||
}
|
||||
|
||||
resp, err := client.StopMining(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("stop mining: %w", err)
|
||||
}
|
||||
if c.JSON {
|
||||
return display.JSON(resp)
|
||||
}
|
||||
|
||||
c.pretty(resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *stopMiningCommand) pretty(v *daemon.StopMiningResult) {
|
||||
table := display.NewTable()
|
||||
table.AddRow("Status:", v.Status)
|
||||
fmt.Println(table)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCommand.AddCommand((&stopMiningCommand{}).Cmd())
|
||||
}
|
59
cmd/monero/commands/wallet/auto_refresh.go
Normal file
59
cmd/monero/commands/wallet/auto_refresh.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package wallet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cirocosta/go-monero/cmd/monero/options"
|
||||
"github.com/cirocosta/go-monero/pkg/rpc/wallet"
|
||||
)
|
||||
|
||||
type autoRefreshCommand struct {
|
||||
Enable bool
|
||||
Period time.Duration
|
||||
}
|
||||
|
||||
func (c *autoRefreshCommand) Cmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "auto-refresh",
|
||||
Short: "configure wallet-rpc to automatically refresh or not",
|
||||
RunE: c.RunE,
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&c.Enable, "enable",
|
||||
true, "enable the ability to auto-refresh")
|
||||
|
||||
cmd.Flags().DurationVar(&c.Period, "period",
|
||||
90*time.Second, "how often to automatically refresh")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *autoRefreshCommand) RunE(_ *cobra.Command, _ []string) error {
|
||||
ctx, cancel := options.RootOptions.Context()
|
||||
defer cancel()
|
||||
|
||||
client, err := options.RootOptions.WalletClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("client: %w", err)
|
||||
}
|
||||
|
||||
resp, err := client.AutoRefresh(ctx, c.Enable, int64(c.Period.Seconds()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("auto refresh: %w", err)
|
||||
}
|
||||
|
||||
c.pretty(resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *autoRefreshCommand) pretty(v *wallet.AutoRefreshResult) {
|
||||
fmt.Println("OK")
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCommand.AddCommand((&autoRefreshCommand{}).Cmd())
|
||||
}
|
|
@ -57,7 +57,6 @@ func (c *getBalanceCommand) RunE(_ *cobra.Command, _ []string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *getBalanceCommand) pretty(v *wallet.GetBalanceResult) {
|
||||
c.prettyTotal(v)
|
||||
|
||||
|
@ -66,6 +65,7 @@ func (c *getBalanceCommand) pretty(v *wallet.GetBalanceResult) {
|
|||
}
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *getBalanceCommand) prettyTotal(v *wallet.GetBalanceResult) {
|
||||
table := display.NewTable()
|
||||
|
||||
|
@ -87,6 +87,7 @@ func (c *getBalanceCommand) prettyTotal(v *wallet.GetBalanceResult) {
|
|||
fmt.Println(table)
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *getBalanceCommand) prettySubAddress(saddr wallet.SubAddress) {
|
||||
table := display.NewTable()
|
||||
|
||||
|
|
63
cmd/monero/commands/wallet/get_height.go
Normal file
63
cmd/monero/commands/wallet/get_height.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package wallet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cirocosta/go-monero/cmd/monero/display"
|
||||
"github.com/cirocosta/go-monero/cmd/monero/options"
|
||||
"github.com/cirocosta/go-monero/pkg/rpc/wallet"
|
||||
)
|
||||
|
||||
type getHeightCommand struct {
|
||||
JSON bool
|
||||
}
|
||||
|
||||
func (c *getHeightCommand) Cmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "get-height",
|
||||
Short: "get the wallet's current block height",
|
||||
RunE: c.RunE,
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&c.JSON, "json",
|
||||
false, "whether or not to output the result as json")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *getHeightCommand) RunE(_ *cobra.Command, _ []string) error {
|
||||
ctx, cancel := options.RootOptions.Context()
|
||||
defer cancel()
|
||||
|
||||
client, err := options.RootOptions.WalletClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("client: %w", err)
|
||||
}
|
||||
|
||||
resp, err := client.GetHeight(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("refresh: %w", err)
|
||||
}
|
||||
|
||||
if c.JSON {
|
||||
return display.JSON(resp)
|
||||
}
|
||||
|
||||
c.pretty(resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *getHeightCommand) pretty(v *wallet.GetHeightResult) {
|
||||
table := display.NewTable()
|
||||
|
||||
table.AddRow("Height:", v.Height)
|
||||
|
||||
fmt.Println(table)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCommand.AddCommand((&getHeightCommand{}).Cmd())
|
||||
}
|
67
cmd/monero/commands/wallet/refresh.go
Normal file
67
cmd/monero/commands/wallet/refresh.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package wallet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/cirocosta/go-monero/cmd/monero/display"
|
||||
"github.com/cirocosta/go-monero/cmd/monero/options"
|
||||
"github.com/cirocosta/go-monero/pkg/rpc/wallet"
|
||||
)
|
||||
|
||||
type refreshCommand struct {
|
||||
StartHeight uint64
|
||||
|
||||
JSON bool
|
||||
}
|
||||
|
||||
func (c *refreshCommand) Cmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "refresh",
|
||||
Short: "refresh the wallet openned by the wallet-rpc server",
|
||||
RunE: c.RunE,
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&c.JSON, "json",
|
||||
false, "whether or not to output the result as json")
|
||||
|
||||
cmd.Flags().Uint64Var(&c.StartHeight, "start-height",
|
||||
0, "block height from which to start refreshing")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *refreshCommand) RunE(_ *cobra.Command, _ []string) error {
|
||||
ctx, cancel := options.RootOptions.Context()
|
||||
defer cancel()
|
||||
|
||||
client, err := options.RootOptions.WalletClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("client: %w", err)
|
||||
}
|
||||
|
||||
resp, err := client.Refresh(ctx, c.StartHeight)
|
||||
if err != nil {
|
||||
return fmt.Errorf("refresh: %w", err)
|
||||
}
|
||||
|
||||
if c.JSON {
|
||||
return display.JSON(resp)
|
||||
}
|
||||
|
||||
c.pretty(resp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// nolint:forbidigo
|
||||
func (c *refreshCommand) pretty(v *wallet.RefreshResult) {
|
||||
table := display.NewTable()
|
||||
table.AddRow("Blocks Fetched:", v.BlocksFetched)
|
||||
table.AddRow("Received Money:", v.ReceivedMoney)
|
||||
fmt.Println(table)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCommand.AddCommand((&refreshCommand{}).Cmd())
|
||||
}
|
|
@ -40,7 +40,6 @@ func (o *options) initializeFromEnv() {
|
|||
if address := os.Getenv("MONERO_ADDRESS"); address != "" {
|
||||
o.address = address
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Client instantiates a new daemon RPC client based on the options filled.
|
||||
|
|
|
@ -7,17 +7,45 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
endpointMiningStatus = "/mining_status"
|
||||
endpointGetHeight = "/get_height"
|
||||
endpointGetOuts = "/get_outs"
|
||||
endpointGetNetStats = "/get_net_stats"
|
||||
endpointGetOuts = "/get_outs"
|
||||
endpointGetPeerList = "/get_peer_list"
|
||||
endpointGetPublicNodes = "/get_public_nodes"
|
||||
endpointGetTransactionPool = "/get_transaction_pool"
|
||||
endpointGetTransactionPoolStats = "/get_transaction_pool_stats"
|
||||
endpointGetTransactions = "/get_transactions"
|
||||
endpointMiningStatus = "/mining_status"
|
||||
endpointStartMining = "/start_mining"
|
||||
endpointStopMining = "/stop_mining"
|
||||
)
|
||||
|
||||
func (c *Client) StopMining(
|
||||
ctx context.Context,
|
||||
) (*StopMiningResult, error) {
|
||||
resp := &StopMiningResult{}
|
||||
|
||||
err := c.RawRequest(ctx, endpointStopMining, nil, resp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("raw request: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) StartMining(
|
||||
ctx context.Context, params StartMiningRequestParameters,
|
||||
) (*StartMiningResult, error) {
|
||||
resp := &StartMiningResult{}
|
||||
|
||||
err := c.RawRequest(ctx, endpointStartMining, params, resp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("raw request: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) MiningStatus(
|
||||
ctx context.Context,
|
||||
) (*MiningStatusResult, error) {
|
||||
|
|
|
@ -840,3 +840,18 @@ type GetTransactionPoolResult struct {
|
|||
} `json:"transactions"`
|
||||
Untrusted bool `json:"untrusted"`
|
||||
}
|
||||
|
||||
type StartMiningRequestParameters struct {
|
||||
MinerAddress string `json:"miner_address"`
|
||||
BackgroundMining bool `json:"background_mining"`
|
||||
IgnoreBattery bool `json:"ignore_battery"`
|
||||
ThreadsCount uint `json:"threads_count"`
|
||||
}
|
||||
|
||||
type StartMiningResult struct {
|
||||
RPCResultFooter `json:",inline"`
|
||||
}
|
||||
|
||||
type StopMiningResult struct {
|
||||
RPCResultFooter `json:",inline"`
|
||||
}
|
||||
|
|
|
@ -6,8 +6,11 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
methodGetBalance = "get_balance"
|
||||
methodCreateAddress = "create_address"
|
||||
methodGetBalance = "get_balance"
|
||||
methodGetHeight = "get_height"
|
||||
methodRefresh = "refresh"
|
||||
methodAutoRefresh = "auto_refresh"
|
||||
)
|
||||
|
||||
// GetBalance gets the balance of the wallet configured for the wallet rpc
|
||||
|
@ -41,3 +44,44 @@ func (c *Client) CreateAddress(
|
|||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) AutoRefresh(
|
||||
ctx context.Context, enable bool, period int64,
|
||||
) (*AutoRefreshResult, error) {
|
||||
resp := &AutoRefreshResult{}
|
||||
|
||||
params := map[string]interface{}{
|
||||
"enable": enable,
|
||||
"period": period,
|
||||
}
|
||||
if err := c.JSONRPC(ctx, methodAutoRefresh, params, resp); err != nil {
|
||||
return nil, fmt.Errorf("jsonrpc: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) Refresh(
|
||||
ctx context.Context, startHeight uint64,
|
||||
) (*RefreshResult, error) {
|
||||
resp := &RefreshResult{}
|
||||
|
||||
params := map[string]interface{}{
|
||||
"start_height": startHeight,
|
||||
}
|
||||
if err := c.JSONRPC(ctx, methodRefresh, params, resp); err != nil {
|
||||
return nil, fmt.Errorf("jsonrpc: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetHeight(ctx context.Context) (*GetHeightResult, error) {
|
||||
resp := &GetHeightResult{}
|
||||
|
||||
if err := c.JSONRPC(ctx, methodGetHeight, nil, resp); err != nil {
|
||||
return nil, fmt.Errorf("jsonrpc: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
|
|
@ -75,3 +75,15 @@ type CreateAddressResult struct {
|
|||
AddressIndices []uint `json:"address_indices"`
|
||||
Addresses []string `json:"addresses"`
|
||||
}
|
||||
|
||||
type RefreshResult struct {
|
||||
BlocksFetched uint64 `json:"blocks_fetched"`
|
||||
ReceivedMoney bool `json:"received_money"`
|
||||
}
|
||||
|
||||
type AutoRefreshResult struct {
|
||||
}
|
||||
|
||||
type GetHeightResult struct {
|
||||
Height uint64 `json:"height"`
|
||||
}
|
||||
|
|
Reference in a new issue