2023-04-04 11:52:04 +02:00

77 lines
1.6 KiB
Go

package main
import (
"encoding/json"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
godotenv.Load() //nolint:errcheck
wallets := strings.Split(os.Getenv("WALLETS"), ",")
for _, w := range wallets {
go run(w)
}
select {}
}
func run(w string) {
for {
url := "https://faucet." + strings.ToLower(os.Getenv("NETWORK")) + ".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")
}
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 {
switch {
case string(body) == "error code: 1015":
log.Info().Str("wallet", w).Msg("Claim already made!")
case string(body) == "error code: 1020":
log.Warn().Str("wallet", w).Msg("Network is down")
default:
log.Error().Str("wallet", w).Msg(string(body))
}
}
time.Sleep(6*time.Hour + 1*time.Minute)
}
}