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

76 lines
1.5 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
wallets := strings.Split(os.Getenv("ADDRESS"), ",")
s := gocron.NewScheduler(time.UTC)
for _, w := range wallets {
run(s, w)
}
s.StartBlocking()
}
func run(s *gocron.Scheduler, w string) {
s.Every(6).Hours().Do(func() { //nolint:errcheck
url := "https://faucet.devnet.sui.io/gas"
request := `
{"FixedAmountRequest":{"recipient":"` + w + `"}}
`
resp, err := http.Post(url, "application/json", strings.NewReader(request))
if err != nil {
log.Err(err).Str("wallet", w).Msg("error sending request")
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Err(err).Str("wallet", w).Msg("error reading response body")
}
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).Str("wallet", w).Msg("error unmarshalling data")
}
log.Info().Str("wallet", w).Msg("Claim successful!")
} else {
if string(body) == "error code: 1015" {
log.Info().Str("wallet", w).Msg("Claim already made!")
} else {
log.Error().Str("wallet", w).Msg(string(body))
}
}
})
}