Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gateway: improving client script #10744

Merged
merged 6 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions core/scripts/gateway/client/send_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"fmt"
"io"
"net/http"
"os"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/joho/godotenv"

"github.com/smartcontractkit/chainlink/v2/core/services/gateway/api"
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers/functions"
Expand All @@ -27,8 +29,19 @@ func main() {
s4SetVersion := flag.Uint64("s4_set_version", 0, "S4 set version")
s4SetExpirationPeriod := flag.Int64("s4_set_expiration_period", 60*60*1000, "S4 how long until the entry expires from now (in milliseconds)")
s4SetPayload := flag.String("s4_set_payload", "", "S4 set payload")
repeat := flag.Bool("repeat", false, "Repeat sending the request every 10 seconds")
flag.Parse()

if privateKey == nil || *privateKey == "" {
if err := godotenv.Load(); err != nil {
panic(err)
}

privateKeyEnvVar := os.Getenv("PRIVATE_KEY")
privateKey = &privateKeyEnvVar
fmt.Println("Loaded private key from .env")
}

// validate key and extract address
key, err := crypto.HexToECDSA(*privateKey)
if err != nil {
Expand Down Expand Up @@ -77,8 +90,7 @@ func main() {
},
}

err = msg.Sign(key)
if err != nil {
if err = msg.Sign(key); err != nil {
fmt.Println("error signing message", err)
return
}
Expand All @@ -88,26 +100,44 @@ func main() {
fmt.Println("error JSON-RPC encoding", err)
return
}
req, err := http.NewRequestWithContext(context.Background(), "POST", *gatewayURL, bytes.NewBuffer(rawMsg))
if err != nil {
fmt.Println("error creating an HTTP request", err)

createRequest := func() (req *http.Request, err error) {
req, err = http.NewRequestWithContext(context.Background(), "POST", *gatewayURL, bytes.NewBuffer(rawMsg))
if err == nil {
req.Header.Set("Content-Type", "application/json")
}
return
}
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("error sending a request", err)
return
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("error sending a request", err)
return
sendRequest := func() {
req, err := createRequest()
if err != nil {
fmt.Println("error creating a request", err)
return
}

resp, err := client.Do(req)
if err != nil {
fmt.Println("error sending a request", err)
return
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("error sending a request", err)
return
}

fmt.Println(string(body))
}

fmt.Println(string(body))
sendRequest()

for *repeat {
time.Sleep(10 * time.Second)
sendRequest()
}
}
1 change: 1 addition & 0 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/ethereum/go-ethereum v1.12.0
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.1
github.com/joho/godotenv v1.4.0
github.com/manyminds/api2go v0.0.0-20171030193247-e7b693844a6f
github.com/montanaflynn/stats v0.7.1
github.com/olekukonko/tablewriter v0.0.5
Expand Down
Loading