From 84b931f42b442422a1972f9c225b1c13c459d70e Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Sun, 28 Aug 2022 22:30:04 +0200 Subject: [PATCH] Display fee per size in explorer transaction detail --- api/worker.go | 7 ++----- server/public.go | 14 ++++++++++++++ static/templates/tx.html | 13 ++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/api/worker.go b/api/worker.go index 3011f5de..f95ca93e 100644 --- a/api/worker.go +++ b/api/worker.go @@ -415,6 +415,8 @@ func (w *Worker) getTransactionFromBchainTx(bchainTx *bchain.Tx, height int, spe ValueInSat: (*Amount)(pValInSat), ValueOutSat: (*Amount)(&valOutSat), Version: bchainTx.Version, + Size: len(bchainTx.Hex) >> 1, + VSize: int(bchainTx.VSize), Hex: bchainTx.Hex, Rbf: rbf, Vin: vins, @@ -423,11 +425,6 @@ func (w *Worker) getTransactionFromBchainTx(bchainTx *bchain.Tx, height int, spe TokenTransfers: tokens, EthereumSpecific: ethSpecific, } - if w.chainParser.SupportsVSize() { - r.Size = len(bchainTx.Hex) >> 1 - r.VSize = int(bchainTx.VSize) - - } return r, nil } diff --git a/server/public.go b/server/public.go index b92e0983..4147d241 100644 --- a/server/public.go +++ b/server/public.go @@ -454,6 +454,7 @@ func (s *PublicServer) parseTemplates() []*template.Template { "formatAmount": s.formatAmount, "formatAmountWithDecimals": formatAmountWithDecimals, "setTxToTemplateData": setTxToTemplateData, + "feePerByte": feePerByte, "isOwnAddress": isOwnAddress, "toJSON": toJSON, "tokenTransfersCount": tokenTransfersCount, @@ -556,6 +557,19 @@ func setTxToTemplateData(td *TemplateData, tx *api.Tx) *TemplateData { return td } +// feePerByte returns fee per vByte or Byte if vsize is unknown +func feePerByte(tx *api.Tx) string { + if tx.FeesSat != nil { + if tx.VSize > 0 { + return fmt.Sprintf("%.2f sat/vByte", float64(tx.FeesSat.AsInt64())/float64(tx.VSize)) + } + if tx.Size > 0 { + return fmt.Sprintf("%.2f sat/Byte", float64(tx.FeesSat.AsInt64())/float64(tx.Size)) + } + } + return "" +} + // isOwnAddress returns true if the address is the one that is being shown in the explorer func isOwnAddress(td *TemplateData, a string) bool { return a == td.AddrStr diff --git a/static/templates/tx.html b/static/templates/tx.html index f139cba1..b78df2f9 100644 --- a/static/templates/tx.html +++ b/static/templates/tx.html @@ -59,17 +59,24 @@ Total Output {{formatAmount $tx.ValueOutSat}} {{$cs}} + {{- if $tx.VSize -}} + + Size / vSize + {{$tx.Size}} / {{$tx.VSize}} + + {{- else -}} {{- if $tx.Size -}} - Size/VSize - {{$tx.Size}}/{{$tx.VSize}} + Size + {{$tx.Size}} {{- end -}} {{- end -}} + {{- end -}} {{- if $tx.FeesSat -}} Fees - {{formatAmount $tx.FeesSat}} {{$cs}} + {{formatAmount $tx.FeesSat}} {{$cs}}{{if $tx.Size}} ({{feePerByte $tx}}){{end}} {{end -}} {{- if not $tx.Confirmations}}