Skip to content

Commit

Permalink
Display fee per size in explorer transaction detail
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboehm committed Feb 1, 2023
1 parent 6edbc2d commit 84b931f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
7 changes: 2 additions & 5 deletions api/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down
14 changes: 14 additions & 0 deletions server/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions static/templates/tx.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,24 @@ <h3>Summary</h3>
<td>Total Output</td>
<td class="data">{{formatAmount $tx.ValueOutSat}} {{$cs}}</td>
</tr>
{{- if $tx.VSize -}}
<tr>
<td>Size / vSize</td>
<td class="data">{{$tx.Size}} / {{$tx.VSize}}</td>
</tr>
{{- else -}}
{{- if $tx.Size -}}
<tr>
<td>Size/VSize</td>
<td class="data">{{$tx.Size}}/{{$tx.VSize}}</td>
<td>Size</td>
<td class="data">{{$tx.Size}}</td>
</tr>
{{- end -}}
{{- end -}}
{{- end -}}
{{- if $tx.FeesSat -}}
<tr>
<td>Fees</td>
<td class="data">{{formatAmount $tx.FeesSat}} {{$cs}}</td>
<td class="data">{{formatAmount $tx.FeesSat}} {{$cs}}{{if $tx.Size}} ({{feePerByte $tx}}){{end}}</td>
</tr>{{end -}}
{{- if not $tx.Confirmations}}
<tr>
Expand Down

0 comments on commit 84b931f

Please sign in to comment.