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

93
main.go
View File

@ -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))
}
}
})
}