I try to get the highest hexadecimal string and convert this to an uint64 basically. However, it doesn't always get the right hexadecimal string. How can i solve this issue?
A few things about the code, i'm using Find here instead of FindOne and i'll change this soon. For now it's Find.
Hexadecimal strings are like "0xf" for example.
func GetLatestBlockNumber() uint64 {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
filter := bson.D{}
options := options.Find().SetProjection(bson.M{"result.number": 1}).SetSort(bson.M{"result.number": -1}).SetLimit(1)
cursor, err := blocksCollections.Find(ctx, filter, options)
var results []bson.D
if err = cursor.All(context.TODO(), &results); err != nil {
panic(err)
}
var blockNumberHex string
for _, result := range results {
s := result[1].Value.(primitive.D)
for _, result := range s {
blockNumberHex = string(result.Value.(string))
}
}
n := new(big.Int)
n.SetString(blockNumberHex[2:], 16)
n.Uint64()
return n.Uint64()
}