Skip to content

Commit

Permalink
go/consensus/api/transaction: Implement PrettyPrinter interface for Fee
Browse files Browse the repository at this point in the history
Use it in Transaction's PrettyPrinter to print a transaction's fee.
  • Loading branch information
tjanez committed Aug 5, 2020
1 parent cf38b9a commit 0dc0c40
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changelog/3151.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/consensus/api/transaction: Pretty-print transaction's fee amount in tokens
1 change: 1 addition & 0 deletions .changelog/3167.feature.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/consensus/api/transaction: Implement `PrettyPrinter` interface for `Fee`
28 changes: 28 additions & 0 deletions go/consensus/api/transaction/gas.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package transaction

import (
"context"
"fmt"
"io"

"github.com/oasisprotocol/oasis-core/go/common/errors"
"github.com/oasisprotocol/oasis-core/go/common/prettyprint"
"github.com/oasisprotocol/oasis-core/go/common/quantity"

"github.com/oasisprotocol/oasis-core/go/staking/api/token"
)

var (
Expand All @@ -12,6 +19,8 @@ var (

// ErrGasPriceTooLow is the error returned when the gas price is too low.
ErrGasPriceTooLow = errors.New(moduleName, 3, "transaction: gas price too low")

_ prettyprint.PrettyPrinter = (*Fee)(nil)
)

// Gas is the consensus gas representation.
Expand All @@ -26,6 +35,25 @@ type Fee struct {
Gas Gas `json:"gas"`
}

// PrettyPrint writes a pretty-printed representation of the fee to the given
// writer.
func (f Fee) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%sAmount: ", prefix)
token.PrettyPrintAmount(ctx, f.Amount, w)
fmt.Fprintln(w)

fmt.Fprintf(w, "%sGas limit: %d\n", prefix, f.Gas)
fmt.Fprintf(w, "%s(gas price: ", prefix)
token.PrettyPrintAmount(ctx, *f.GasPrice(), w)
fmt.Fprintln(w, " per gas unit)")
}

// PrettyType returns a representation of Fee that can be used for pretty
// printing.
func (f Fee) PrettyType() (interface{}, error) {
return f, nil
}

// GasPrice returns the gas price implied by the amount and gas.
func (f Fee) GasPrice() *quantity.Quantity {
if f.Amount.IsZero() || f.Gas == 0 {
Expand Down
3 changes: 2 additions & 1 deletion go/consensus/api/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (t Transaction) PrettyPrintBody(ctx context.Context, prefix string, w io.Wr
func (t Transaction) PrettyPrint(ctx context.Context, prefix string, w io.Writer) {
fmt.Fprintf(w, "%sNonce: %d\n", prefix, t.Nonce)
if t.Fee != nil {
fmt.Fprintf(w, "%sFee: %s (gas limit: %d, gas price: %s)\n", prefix, t.Fee.Amount, t.Fee.Gas, t.Fee.GasPrice())
fmt.Fprintf(w, "%sFee:\n", prefix)
t.Fee.PrettyPrint(ctx, prefix+" ", w)
} else {
fmt.Fprintf(w, "%sFee: none\n", prefix)
}
Expand Down

0 comments on commit 0dc0c40

Please sign in to comment.