/
cosmosclient.go
68 lines (55 loc) · 2.37 KB
/
cosmosclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package furya
import (
"context"
sdkclient "github.com/cosmos/cosmos-sdk/client"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/furychain/cosmosclient/cosmosclient"
rollapptypes "github.com/furychain/furya/x/rollapp/types"
sequencertypes "github.com/furychain/furya/x/sequencer/types"
"github.com/ignite/cli/ignite/pkg/cosmosaccount"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
// CosmosClient is an interface for interacting with cosmos client chains.
// It is a wrapper around the cosmos client in order to provide with an interface which can be implemented by
// other clients and can easily be mocked for testing purposes.
// Currently it contains only the methods that are used by the furya hub client.
type CosmosClient interface {
Context() sdkclient.Context
StartEventListener() error
StopEventListener() error
EventListenerQuit() <-chan struct{}
SubscribeToEvents(ctx context.Context, subscriber string, query string, outCapacity ...int) (out <-chan ctypes.ResultEvent, err error)
BroadcastTx(accountName string, msgs ...sdktypes.Msg) (cosmosclient.Response, error)
GetRollappClient() rollapptypes.QueryClient
GetSequencerClient() sequencertypes.QueryClient
GetAccount(accountName string) (cosmosaccount.Account, error)
}
type cosmosClient struct {
cosmosclient.Client
}
var _ CosmosClient = &cosmosClient{}
// NewCosmosClient creates a new cosmos client
func NewCosmosClient(client cosmosclient.Client) CosmosClient {
return &cosmosClient{client}
}
func (c *cosmosClient) StartEventListener() error {
return c.Client.RPC.WSEvents.Start()
}
func (c *cosmosClient) StopEventListener() error {
return c.Client.RPC.WSEvents.Stop()
}
func (c *cosmosClient) EventListenerQuit() <-chan struct{} {
return c.Client.RPC.GetWSClient().Quit()
}
func (c *cosmosClient) SubscribeToEvents(ctx context.Context, subscriber string, query string, outCapacity ...int) (out <-chan ctypes.ResultEvent, err error) {
return c.Client.RPC.WSEvents.Subscribe(ctx, subscriber, query, outCapacity...)
}
func (c *cosmosClient) GetRollappClient() rollapptypes.QueryClient {
return rollapptypes.NewQueryClient(c.Context())
}
func (c *cosmosClient) GetSequencerClient() sequencertypes.QueryClient {
return sequencertypes.NewQueryClient(c.Context())
}
func (c *cosmosClient) GetAccount(accountName string) (cosmosaccount.Account, error) {
return c.AccountRegistry.GetByName(accountName)
}