2021-11-23 16:44:46 +00:00
|
|
|
package requests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-03-14 13:59:51 +00:00
|
|
|
"strconv"
|
2021-11-23 16:44:46 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func post(client *http.Client, url, request string, response interface{}) error {
|
|
|
|
start := time.Now()
|
|
|
|
r, err := client.Post(url, "application/json", strings.NewReader(request))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
2022-03-16 22:21:05 +00:00
|
|
|
|
2021-11-23 16:44:46 +00:00
|
|
|
if r.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("status %s", r.Status)
|
|
|
|
}
|
2022-03-16 22:21:05 +00:00
|
|
|
|
2021-11-23 16:44:46 +00:00
|
|
|
decoder := json.NewDecoder(r.Body)
|
|
|
|
err = decoder.Decode(response)
|
|
|
|
log.Info("Got in", "time", time.Since(start).Seconds())
|
|
|
|
return err
|
|
|
|
}
|
2022-03-14 13:59:51 +00:00
|
|
|
|
|
|
|
func HexToInt(hexStr string) uint64 {
|
|
|
|
// Remove the 0x prefix
|
|
|
|
cleaned := strings.ReplaceAll(hexStr, "0x", "")
|
|
|
|
|
|
|
|
result, _ := strconv.ParseUint(cleaned, 16, 64)
|
|
|
|
return result
|
|
|
|
}
|