diff --git a/cmd/monitor/monitor.go b/cmd/monitor/monitor.go index 0e0ccc17..2395bf0c 100644 --- a/cmd/monitor/monitor.go +++ b/cmd/monitor/monitor.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/big" + "strconv" "strings" "sync" "time" @@ -567,8 +568,16 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu } ms.BlocksLock.RUnlock() renderedBlocks = renderedBlocksTemp + renderedBlocksMeanGasPrice := metrics.GetMeanGasPricePerBlock(renderedBlocks) + // First initialization will render no gas price because the GasPriceChart will have no data. + if renderedBlocksMeanGasPrice == nil { + skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, "--", ms.PeerCount, ms.ChainID, rpcUrl) + } else { + // Under normal cases, the gas price will be derived from the last element of the GasPriceChart with 2 decimal places precision. + gasPriceStr := strconv.FormatFloat(renderedBlocksMeanGasPrice[len(renderedBlocksMeanGasPrice)-1]/1000000000, 'f', 2, 64) + skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, gasPriceStr, ms.PeerCount, ms.ChainID, rpcUrl) + } - skeleton.Current.Text = ui.GetCurrentText(skeleton.Current, ms.HeadBlock, ms.GasPrice, ms.PeerCount, ms.ChainID, rpcUrl) if txPoolStatusSupported { skeleton.TxPool.Text = ui.GetTxPoolText(skeleton.TxPool, ms.TxPoolStatus.pending, ms.TxPoolStatus.queued) } @@ -578,7 +587,7 @@ func renderMonitorUI(ctx context.Context, ec *ethclient.Client, ms *monitorStatu } skeleton.TxPerBlockChart.Data = metrics.GetTxsPerBlock(renderedBlocks) - skeleton.GasPriceChart.Data = metrics.GetMeanGasPricePerBlock(renderedBlocks) + skeleton.GasPriceChart.Data = renderedBlocksMeanGasPrice // equivalent to metrics.GetMeanGasPricePerBlock(renderedBlocks) skeleton.BlockSizeChart.Data = metrics.GetSizePerBlock(renderedBlocks) // skeleton.pendingTxChart.Data = metrics.GetUnclesPerBlock(renderedBlocks) skeleton.PendingTxChart.Data = observedPendingTxs.getValues(25) diff --git a/cmd/monitor/ui/ui.go b/cmd/monitor/ui/ui.go index d705ec9f..3ebfde9e 100644 --- a/cmd/monitor/ui/ui.go +++ b/cmd/monitor/ui/ui.go @@ -36,11 +36,11 @@ type UiSkeleton struct { Receipts *widgets.List } -func GetCurrentText(widget *widgets.Paragraph, headBlock, gasPrice *big.Int, peerCount uint64, chainID *big.Int, rpcURL string) string { +func GetCurrentText(widget *widgets.Paragraph, headBlock *big.Int, gasPrice string, peerCount uint64, chainID *big.Int, rpcURL string) string { // First column height := fmt.Sprintf("Height: %s", headBlock.String()) timeInfo := fmt.Sprintf("Time: %s", time.Now().Format("02 Jan 06 15:04:05 MST")) - gasPriceString := fmt.Sprintf("Gas Price: %s gwei", new(big.Int).Div(gasPrice, metrics.UnitShannon).String()) + gasPriceString := fmt.Sprintf("Gas Price: %s gwei", gasPrice) peers := fmt.Sprintf("Peers: %d", peerCount) // Second column