fix: same address in cron

This commit is contained in:
upong 2023-04-03 21:15:02 +02:00
parent da6ba29817
commit 05de71a2d6
2 changed files with 51 additions and 43 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.env

35
main.go
View File

@ -16,20 +16,28 @@ 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 {
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":"` + a + `"}}`
request := `
{"FixedAmountRequest":{"recipient":"` + w + `"}}
`
resp, err := http.Post(url, "application/json", strings.NewReader(request))
if err != nil {
log.Err(err).Msg("error sending request")
panic(err)
log.Err(err).Str("wallet", w).Msg("error sending request")
}
defer func() {
@ -38,8 +46,7 @@ func main() {
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Err(err).Msg("error reading response body")
panic(err)
log.Err(err).Str("wallet", w).Msg("error reading response body")
}
if resp.StatusCode == http.StatusCreated {
@ -54,15 +61,15 @@ func main() {
var d Data
err = json.Unmarshal(body, &d)
if err != nil {
log.Err(err).Msg("error unmarshalling data")
panic(err)
log.Err(err).Str("wallet", w).Msg("error unmarshalling data")
}
log.Info().Msg("Claim successful!")
log.Info().Str("wallet", w).Msg("Claim successful!")
} else {
log.Error().Msg(string(body))
if string(body) == "error code: 1015" {
log.Info().Str("wallet", w).Msg("Claim already made!")
} else {
log.Error().Str("wallet", w).Msg(string(body))
}
}
})
}
s.StartBlocking()
}