2023-04-03 21:00:36 +02:00

69 lines
1.3 KiB
Go

package main
import (
"encoding/json"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/go-co-op/gocron"
"github.com/joho/godotenv"
"github.com/rs/zerolog/log"
)
func main() {
godotenv.Load() //nolint:errcheck
address := strings.Split(os.Getenv("ADDRESS"), ",")
s := gocron.NewScheduler(time.UTC)
for _, a := range address {
s.Every(6).Hours().Do(func() { //nolint:errcheck
url := "https://faucet.devnet.sui.io/gas"
request := `{"FixedAmountRequest":{"recipient":"` + a + `"}}`
resp, err := http.Post(url, "application/json", strings.NewReader(request))
if err != nil {
log.Err(err).Msg("error sending request")
panic(err)
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Err(err).Msg("error reading response body")
panic(err)
}
if resp.StatusCode == http.StatusCreated {
type Data struct {
TransferredGasObjects []struct {
Amount uint `json:"amount"`
ID string `json:"id"`
TransferTxDigest string `json:"transferTxDigest"`
}
}
var d Data
err = json.Unmarshal(body, &d)
if err != nil {
log.Err(err).Msg("error unmarshalling data")
panic(err)
}
log.Info().Msg("Claim successful!")
} else {
log.Error().Msg(string(body))
}
})
}
s.StartBlocking()
}