cmd: minor unit and flag improvements

- `get-transaction` on miner tx's now reveal the outputs in `m`
- `get-block` now includes a `--last` to allow one to easily say "get me
the last block", or "get me the last-1 block" ...
- `get-last-block-header` now shows rewards in precise `m`

Signed-off-by: Ciro S. Costa <utxobr@protonmail.com>
This commit is contained in:
Ciro S. Costa 2021-07-31 06:42:08 -04:00
parent dcbefcada8
commit ad498be8f1
5 changed files with 38 additions and 16 deletions

View file

@ -18,6 +18,7 @@ import (
type getBlockCommand struct {
Height uint64
Hash string
Last int64
BlockJSON bool
JSON bool
@ -31,6 +32,9 @@ func (c *getBlockCommand) Cmd() *cobra.Command {
RunE: c.RunE,
}
cmd.Flags().Int64Var(&c.Last, "last",
-1, "get the last Nth block")
cmd.Flags().Uint64Var(&c.Height, "height",
0, "height of the block to retrieve the information of")
@ -39,6 +43,7 @@ func (c *getBlockCommand) Cmd() *cobra.Command {
cmd.Flags().BoolVar(&c.JSON, "json",
false, "whether or not to output the result as json")
cmd.Flags().BoolVar(&c.BlockJSON, "block-json",
false, "display just the block json (from the `json` field)")
@ -54,6 +59,15 @@ func (c *getBlockCommand) RunE(_ *cobra.Command, _ []string) error {
return fmt.Errorf("client: %w", err)
}
if c.Last >= 0 {
lastBlockHeaderResp, err := client.GetLastBlockHeader(ctx)
if err != nil {
return fmt.Errorf("get last block header: %w", err)
}
c.Height = lastBlockHeaderResp.BlockHeader.Height - uint64(c.Last)
}
if c.Hash == "" && c.Height == 0 {
return fmt.Errorf("hash or height must be set")
}

View file

@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/dustin/go-humanize"
@ -148,7 +147,7 @@ func (c *getTransactionCommand) prettyOutputs(
for idx, vout := range txnDetails.Vout {
amount := "?"
if vout.Amount != 0 {
amount = strconv.Itoa(vout.Amount)
amount = display.PreciseXMR(vout.Amount)
}
var outIdx interface{} = "?"
@ -164,18 +163,6 @@ func (c *getTransactionCommand) prettyOutputs(
return nil
}
func decodeOffsets(offsets []uint) []uint {
accum := uint(0)
res := make([]uint, len(offsets))
for idx, offset := range offsets {
accum += offset
res[idx] = accum
}
return res
}
// nolint:forbidigo
func (c *getTransactionCommand) prettyInputs(
ctx context.Context,
@ -212,6 +199,18 @@ func (c *getTransactionCommand) prettyInputs(
return nil
}
func decodeOffsets(offsets []uint) []uint {
accum := uint(0)
res := make([]uint, len(offsets))
for idx, offset := range offsets {
accum += offset
res[idx] = accum
}
return res
}
func init() {
RootCommand.AddCommand((&getTransactionCommand{}).Cmd())
}

View file

@ -7,6 +7,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/gosuri/uitable"
"github.com/cirocosta/go-monero/cmd/monero/display"
"github.com/cirocosta/go-monero/pkg/rpc/daemon"
)
@ -31,7 +32,7 @@ func prettyBlockHeader(table *uitable.Table, header daemon.BlockHeader) {
table.AddRow("Orphan Status:", header.OrphanStatus)
table.AddRow("Proof-of-Work Hash:", header.PowHash)
table.AddRow("Previous Hash:", header.PrevHash)
table.AddRow("Reward:", header.Reward)
table.AddRow("Reward:", display.PreciseXMR(header.Reward))
table.AddRow("Timestamp:", fmt.Sprintf("%s (%s)", timestamp, humanize.Time(timestamp)))
table.AddRow("Wide Cumulative Difficulty:", header.WideCumulativeDifficulty)
table.AddRow("Wide Difficulty:", header.WideDifficulty)

View file

@ -34,6 +34,14 @@ func NewTable() *uitable.Table {
return table
}
func MicroXMR(v uint64) string {
return fmt.Sprintf("%.2f uɱ", float64(v)/float64(constant.MicroXMR))
}
func PreciseXMR(v uint64) string {
return fmt.Sprintf("%.6f ɱ", float64(v)/float64(constant.XMR))
}
func XMR(v uint64) string {
return fmt.Sprintf("%.2f ɱ", float64(v)/float64(constant.XMR))
}

View file

@ -774,7 +774,7 @@ type TransactionJSON struct {
} `json:"key"`
} `json:"vin"`
Vout []struct {
Amount int `json:"amount"`
Amount uint64 `json:"amount"`
Target struct {
Key string `json:"key"`
} `json:"target"`