From 69202baadb5e84d955aafab10f6761bf70a56be0 Mon Sep 17 00:00:00 2001 From: Mohammed Sohail Date: Tue, 14 Dec 2021 18:41:24 +0300 Subject: [PATCH] add: hex2uint64 util --- README.md | 2 +- internal/server/handlers.go | 9 +++++---- pkg/util/hex2int.go | 12 ++++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 pkg/util/hex2int.go diff --git a/README.md b/README.md index c19abf0..0e90f67 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A tiny stateless service to return health status and useful metrics from OpenEth #### Endpoints - `/health` (Expect 200 for health with an additional JSON object with more info) -- `/metrics` (Expect 200 for health with an additional JSON object with more info) +- `/metrics` (Expect 200 with an additional JSON object with more info) #### Development diff --git a/internal/server/handlers.go b/internal/server/handlers.go index bdad736..7a2d7ec 100644 --- a/internal/server/handlers.go +++ b/internal/server/handlers.go @@ -2,6 +2,7 @@ package server import ( "git.grassecon.org/grassrootseconomics/openethereum-node-status/pkg/rpc" + "git.grassecon.org/grassrootseconomics/openethereum-node-status/pkg/util" "github.com/rs/zerolog/log" "github.com/ybbus/jsonrpc/v2" ) @@ -17,8 +18,8 @@ type ( } BatchMetricsResponse struct { - GasPrice string - BlockNumber string + GasPrice uint64 + BlockNumber uint64 } ) @@ -85,7 +86,7 @@ func batchMetricsCall() (bool, BatchMetricsResponse) { return false, batchResponse } // If sync is complete the rpc result is false, however this call dones't gurantee all blocks have been imported - batchResponse.GasPrice = val + batchResponse.GasPrice = util.Hex2Int(val) } if blockNumber, ok := rpcResponses[1]; ok { @@ -93,7 +94,7 @@ func batchMetricsCall() (bool, BatchMetricsResponse) { if err != nil { return false, batchResponse } - batchResponse.BlockNumber = val + batchResponse.BlockNumber = util.Hex2Int(val) } return true, batchResponse diff --git a/pkg/util/hex2int.go b/pkg/util/hex2int.go new file mode 100644 index 0000000..b72aa7e --- /dev/null +++ b/pkg/util/hex2int.go @@ -0,0 +1,12 @@ +package util + +import ( + "strconv" + "strings" +) + +func Hex2Int(hexStr string) uint64 { + cleaned := strings.Replace(hexStr, "0x", "", -1) + result, _ := strconv.ParseUint(cleaned, 16, 64) + return uint64(result) +}