Skip to content

Commit

Permalink
Expose GRPC client so that consumers can subscribe to events
Browse files Browse the repository at this point in the history
  • Loading branch information
sequel21 committed Nov 16, 2024
1 parent 41ad1ac commit 963f062
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/go/iinft/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package iinft

import (
"errors"
"fmt"
"os"
"path"

"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/flow-go-sdk/access"
grpcAccess "github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/config"
"github.com/onflow/flowkit/v2/gateway"
"github.com/piprate/sequel-flow-contracts/lib/go/iinft/gwtf"
"github.com/spf13/afero"
"google.golang.org/grpc"
)

type (
Expand Down Expand Up @@ -61,6 +65,7 @@ func NewGoWithTheFlowError(baseLoader flowkit.ReaderWriter, network string, inMe

logger := NewFlowKitLogger()
var service *flowkit.Flowkit
var client access.Client

if inMemory {
// YAY, we can run it inline in memory!
Expand Down Expand Up @@ -91,11 +96,71 @@ func NewGoWithTheFlowError(baseLoader flowkit.ReaderWriter, network string, inMe
return nil, err
}
service = flowkit.NewFlowkit(state, *networkDef, gw, logger)

client, err = grpcAccess.NewClient(

Check failure on line 100 in lib/go/iinft/client.go

View workflow job for this annotation

GitHub Actions / lint

SA4023(related information): the lhs of the comparison gets its value from here and has a concrete type (staticcheck)
networkDef.Host,
grpcAccess.WithGRPCDialOptions(
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxGRPCMessageSize)),
),
)

if err != nil || client == nil {

Check failure on line 107 in lib/go/iinft/client.go

View workflow job for this annotation

GitHub Actions / lint

SA4023: this comparison is never true (staticcheck)
return nil, fmt.Errorf("failed to connect to host %s", networkDef.Host)
}
}
return &gwtf.GoWithTheFlow{
State: state,
Services: service,
Client: client,
Logger: logger,
PrependNetworkToAccountNames: true,
}, nil
}

// NewGrpcClientForNetworkFS creates a new local go with the flow client
func NewGrpcClientForNetworkFS(flowBasePath, network string) (access.Client, error) {
return NewGrpcClient(&fileLoader{
baseDir: flowBasePath,
fsLoader: &afero.Afero{Fs: afero.NewOsFs()},
}, network)
}

// NewGrpcClientForNetworkEmbedded creates a new test go with the flow client based on embedded setup
func NewGrpcClientForNetworkEmbedded(network string) (access.Client, error) {
return NewGrpcClient(&embeddedFileLoader{}, network)
}

// maxGRPCMessageSize 60mb
const maxGRPCMessageSize = 1024 * 1024 * 60

func NewGrpcClient(baseLoader flowkit.ReaderWriter, network string, opts ...grpcAccess.ClientOption) (access.Client, error) {
state, err := flowkit.Load([]string{"flow.json"}, baseLoader)
if err != nil {
return nil, err
}

networkDef, err := state.Networks().ByName(network)
if err != nil {
return nil, err
}

options := append(
[]grpcAccess.ClientOption{
grpcAccess.WithGRPCDialOptions(
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxGRPCMessageSize)),
),
},
opts...,
)

gClient, err := grpcAccess.NewClient(
networkDef.Host,
options...,
)

if err != nil || gClient == nil {
return nil, fmt.Errorf("failed to connect to host %s", networkDef.Host)
}

return gClient, nil
}
3 changes: 3 additions & 0 deletions lib/go/iinft/gwtf/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package gwtf
import (
"context"
"fmt"

"log"

"github.com/onflow/flow-go-sdk/access"
"github.com/onflow/flowkit/v2"
"github.com/onflow/flowkit/v2/accounts"
"github.com/onflow/flowkit/v2/config"
Expand All @@ -16,6 +18,7 @@ import (
// GoWithTheFlow Entire configuration to work with Go With the Flow
type GoWithTheFlow struct {
State *flowkit.State
Client access.Client
Services flowkit.Services
Logger output.Logger
PrependNetworkToAccountNames bool
Expand Down

0 comments on commit 963f062

Please sign in to comment.