From 05de71a2d6e2d7aa31a0986e6c0e203c6d648c84 Mon Sep 17 00:00:00 2001 From: upong Date: Mon, 3 Apr 2023 21:15:02 +0200 Subject: [PATCH] fix: same address in cron --- .gitignore | 1 + main.go | 93 +++++++++++++++++++++++++++++------------------------- 2 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/main.go b/main.go index c455b1e..e1e3b21 100644 --- a/main.go +++ b/main.go @@ -16,53 +16,60 @@ import ( func main() { godotenv.Load() //nolint:errcheck - address := strings.Split(os.Getenv("ADDRESS"), ",") + wallets := 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)) - } - }) + 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)) + } + } + }) +}