Skip to content

Commit

Permalink
Merge pull request #3784 from oasisprotocol/tjanez/symbol-and-txn-pprint
Browse files Browse the repository at this point in the history
Pretty-print: print token's symbol after the amount
  • Loading branch information
tjanez authored Mar 16, 2021
2 parents ac7e3d1 + 25828b4 commit 9e0190b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changelog/3784.breaking.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go/staking/api/token: Print token's symbol after the amount

This is more consistent with how others present token amounts.
4 changes: 4 additions & 0 deletions .changelog/3784.breaking.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go/consensus/api/transaction: Print txn's method and body before the rest

This is more intuitive and enables people to discover the transaction's main
information quicker.
7 changes: 3 additions & 4 deletions go/consensus/api/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,16 @@ func (t Transaction) PrettyPrintBody(ctx context.Context, prefix string, w io.Wr
// PrettyPrint writes a pretty-printed representation of the transaction to the
// given writer.
func (t Transaction) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%sMethod: %s\n", prefix, t.Method)
fmt.Fprintf(w, "%sBody:\n", prefix)
t.PrettyPrintBody(ctx, prefix+" ", w)
fmt.Fprintf(w, "%sNonce: %d\n", prefix, t.Nonce)
if t.Fee != nil {
fmt.Fprintf(w, "%sFee:\n", prefix)
t.Fee.PrettyPrint(ctx, prefix+" ", w)
} else {
fmt.Fprintf(w, "%sFee: none\n", prefix)
}
fmt.Fprintf(w, "%sMethod: %s\n", prefix, t.Method)
fmt.Fprintf(w, "%sBody:\n", prefix)
t.PrettyPrintBody(ctx, prefix+" ", w)

if genesisHash, ok := ctx.Value(prettyprint.ContextKeyGenesisHash).(hash.Hash); ok {
fmt.Println("Other info:")
fmt.Printf(" Genesis document's hash: %s\n", genesisHash)
Expand Down
2 changes: 1 addition & 1 deletion go/staking/api/token/prettyprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func PrettyPrintAmount(ctx context.Context, amount quantity.Quantity, w io.Write
if useBaseUnits {
fmt.Fprintf(w, "%s%s base units", sign, amount)
} else {
fmt.Fprintf(w, "%s %s%s", symbol, sign, tokenAmount)
fmt.Fprintf(w, "%s%s %s", sign, tokenAmount, symbol)
}
}
24 changes: 12 additions & 12 deletions go/staking/api/token/prettyprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,25 @@ func TestPrettyPrintAmount(t *testing.T) {
addSign bool
sign string
}{
{"CORE 10000000000.0", quantity.NewFromUint64(10000000000000000000), true, "CORE", true, 9, false, ""},
{"CORE 100.0", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, false, ""},
{"CORE 7999217230.1196", quantity.NewFromUint64(7999217230119600000), true, "CORE", true, 9, false, ""},
{"CORE 0.0", quantity.NewFromUint64(0), true, "CORE", true, 9, false, ""},
{"CORE -100.0", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "-"},
{"CORE +100.0", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "+"},
{"10000000000.0 CORE", quantity.NewFromUint64(10000000000000000000), true, "CORE", true, 9, false, ""},
{"100.0 CORE", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, false, ""},
{"7999217230.1196 CORE", quantity.NewFromUint64(7999217230119600000), true, "CORE", true, 9, false, ""},
{"0.0 CORE", quantity.NewFromUint64(0), true, "CORE", true, 9, false, ""},
{"-100.0 CORE", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "-"},
{"+100.0 CORE", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "+"},
// Check large and small token's value base-10 exponents.
{"BIG 10.0", quantity.NewFromUint64(10000000000000000000), true, "BIG", true, 18, false, ""},
{"SMALL -10000000000000000001.0", quantity.NewFromUint64(10000000000000000001), true, "SMALL", true, 0, true, "-"},
{"10.0 BIG", quantity.NewFromUint64(10000000000000000000), true, "BIG", true, 18, false, ""},
{"-10000000000000000001.0 SMALL", quantity.NewFromUint64(10000000000000000001), true, "SMALL", true, 0, true, "-"},
// Check invalid token's value base-10 exponent.
{"100000000 base units", quantity.NewFromUint64(100000000), true, "TOOBIG", true, 21, false, ""},
// Check invalid token's ticker symbol.
{"-100000 base units", quantity.NewFromUint64(100000), true, "SOMETHINGLONG", true, 6, true, "-"},
{"100000 base units", quantity.NewFromUint64(100000), true, "", true, 6, false, ""},
// Check invalid token's value sign.
{"CORE 100.0", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, ""},
{"CORE 100.0", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "--"},
{"CORE 100.0", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "++"},
{"CORE 100.0", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "?"},
{"100.0 CORE", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, ""},
{"100.0 CORE", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "--"},
{"100.0 CORE", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "++"},
{"100.0 CORE", quantity.NewFromUint64(100000000000), true, "CORE", true, 9, true, "?"},
// Check missing combinations of token's symbol, value exponent and value sign.
{"+100000 base units", quantity.NewFromUint64(100000), false, "MISSING", true, 6, true, "+"},
{"-100000 base units", quantity.NewFromUint64(100000), true, "NOEXP", false, 0, true, "-"},
Expand Down

0 comments on commit 9e0190b

Please sign in to comment.