Skip to content

Commit

Permalink
More examples in the go builder
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrit committed Jul 9, 2024
1 parent 541b2a4 commit 702e629
Show file tree
Hide file tree
Showing 6 changed files with 1,136 additions and 20 deletions.
50 changes: 50 additions & 0 deletions go/builder/examples/broadcast_requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package examples

import (
"context"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
pubsub "github.com/peggyjv/sommelier/v7/x/pubsub/types"
)

// Requests to schedule a contract call must be sent to many Steward instances. This is required because calls that do not have enough votes of approval from the validator set will not be
// executed. Right now there is no single endpoint that handles broadcastic the call to each
// Steward instance, so we must query the endpoints and then send the request to each one.
//
// This example simple sends the requests sequentially in a loop. In production it would be better to
// send the requests concurrently.
func ExampleBroadcastRequests() {
// Query the Sommelier chain to get the list of Steward instances
sommCtx := client.Context{}.WithNodeURI("http://localhost:26657")
sommQueryClient := pubsub.NewQueryClient(sommCtx)

res, err := sommQueryClient.QuerySubscribers(context.Background(), &pubsub.QuerySubscribersRequest{})
if err != nil {
panic(err)
}

subscribers := res.Subscribers

// Get client
client, err := CreateTlsClient()
if err != nil {
panic(err)
}

// Build request
request, err := BuildMulticallRequest()
if err != nil {
panic(err)
}

// Send request to each subscriber
for _, subscriber := range subscribers {
response, err := client.Schedule(context.Background(), request)
if err != nil {
fmt.Printf("Error sending request to %s: %s\n", subscriber, err)
}

fmt.Print("Sent request to ", subscriber, " with response: ", response, "\n")
}
}
52 changes: 52 additions & 0 deletions go/builder/examples/check_steward_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package examples

import (
"context"
"fmt"
"time"

"github.com/peggyjv/steward/steward_proto_go/steward_proto"
"google.golang.org/grpc"
)

// When running the production server, Steward exposes a Status/Version method that shows the current
// running version of Steward. It can be useful for verifying connectivity and compatability. TLS authentication
// is required to access this endpoint.
func CreateTlsStatusClient() (steward_proto.StatusServiceClient, error) {
// This example uses fake file paths for the auth materials
creds, err := buildCredentials("client.crt", "client.key", "server_ca.crt")
if err != nil {
return nil, err
}

addr := "localhost:5734"
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(creds))
if err != nil {
return nil, err
}

defer conn.Close()

client := steward_proto.NewStatusServiceClient(conn)

return client, nil
}

func ExampleCheckStewardStatus() {
// Get client and context
client, err := CreateTlsStatusClient()
if err != nil {
panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Send request
response, err := client.Version(ctx, &steward_proto.VersionRequest{})
if err != nil {
panic(err)
}

fmt.Print(response)
}
33 changes: 19 additions & 14 deletions go/builder/examples/multicall.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,46 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/steward/go/builder"
"github.com/peggyjv/steward/steward_proto_go/steward_proto"
)

func ExampleMulticall() {
// Get client and context
client, err := CreateTlsClient()
if err != nil {
panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Build call data
func BuildMulticallRequest() (*steward_proto.ScheduleRequest, error) {
index := uint32(0)
positionId := uint32(1)
inDebtArray := false
configurationData := []byte{0x1}

// A multicall is used implicitly when more than one function call is added to the CellarCallDataBuilder
callData, err := builder.NewCallDataBuilder().
callData, err := builder.NewCellarCallDataBuilder().
AddPositionToCatalogue(positionId).
AddPosition(index, positionId, configurationData, inDebtArray).
Build()
if err != nil {
panic(err)
return nil, err
}

// Build request
cellarId := common.HexToAddress("0x0")
request, err := builder.NewScheduleRequestBuilder().
return builder.NewScheduleRequestBuilder().
WithCellarID(cellarId).
WithChainID(1).
WithCallData(callData).
WithBlockHeight(100).
Build()
}

func ExampleMulticall() {
// Get client and context
client, err := CreateTlsClient()
if err != nil {
panic(err)
}

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

// Build call data
request, err := BuildMulticallRequest()
if err != nil {
panic(err)
}
Expand Down
98 changes: 95 additions & 3 deletions go/builder/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,112 @@ require (
github.com/ethereum/go-ethereum v1.14.5
github.com/peggyjv/steward/steward_proto_go/steward_proto v0.1.0
github.com/stretchr/testify v1.8.4
google.golang.org/grpc v1.64.0
)

require (
cosmossdk.io/errors v1.0.0-beta.7 // indirect
cosmossdk.io/math v1.0.0-rc.0 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/confio/ics23/go v0.9.0 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-proto v1.0.0-alpha8 // indirect
github.com/cosmos/cosmos-sdk v0.46.14 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.6 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.4 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/dvsekhvalnov/jose2go v1.5.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/protobuf v1.3.3 // indirect
github.com/golang/glog v1.2.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/btree v1.1.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/peggyjv/sommelier/v7 v7.0.1
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.14.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
github.com/tendermint/go-amino v0.16.0 // indirect
github.com/tendermint/tendermint v0.34.29 // indirect
github.com/tendermint/tm-db v0.6.7 // indirect
github.com/tidwall/btree v1.5.0 // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/peggyjv/steward/steward_proto_go/steward_proto => ../../steward_proto_go/steward_proto
replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

replace github.com/peggyjv/steward/steward_proto_go/steward_proto => ../steward_proto_go/steward_proto

replace github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.28
Loading

0 comments on commit 702e629

Please sign in to comment.