-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
168 lines (130 loc) · 5.22 KB
/
backend.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package backend
import (
"context"
"github.com/bcdevtools/block-explorer-rpc-cosmos/be_rpc/config"
berpctypes "github.com/bcdevtools/block-explorer-rpc-cosmos/be_rpc/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/tendermint/tendermint/libs/log"
)
type BackendI interface {
// Chain
// GetChainInfo returns the chain information.
GetChainInfo() (berpctypes.GenericBackendResponse, error)
// GetModuleParams returns the module parameters by module name.
GetModuleParams(moduleName string) (berpctypes.GenericBackendResponse, error)
// Account
GetAccountBalances(accountAddressStr string, denom *string) (berpctypes.GenericBackendResponse, error)
GetAccount(accountAddressStr string) (berpctypes.GenericBackendResponse, error)
GetValidatorAccount(consOrValAddr string) (berpctypes.GenericBackendResponse, error)
// Block
// GetLatestBlockNumber returns the latest block number, along with the epoch UTC seconds.
GetLatestBlockNumber() (berpctypes.GenericBackendResponse, error)
// GetRecentBlocks returns the recent blocks, paginated.
GetRecentBlocks(pageNo, pageSize int) (berpctypes.GenericBackendResponse, error)
// GetBlockByNumber returns a block by its height.
GetBlockByNumber(height int64) (berpctypes.GenericBackendResponse, error)
// Transactions
// GetTransactionsInBlockRange returns the list transaction info within a block range.
// The range is inclusive, specified clearly.
GetTransactionsInBlockRange(fromHeightIncluded, toHeightIncluded int64) (berpctypes.GenericBackendResponse, error)
// GetTransactionByHash returns a transaction by its hash.
GetTransactionByHash(hash string) (berpctypes.GenericBackendResponse, error)
// Staking
// GetStakingInfo returns the staking information, includes:
// - Delegator's staking information
// - Validator's commission & outstanding rewards
GetStakingInfo(delegatorAddr string) (berpctypes.GenericBackendResponse, error)
GetValidators() (berpctypes.GenericBackendResponse, error)
// Gov
GetGovProposal(proposalId uint64) (berpctypes.GenericBackendResponse, error)
GetGovProposals(pageNo int) (berpctypes.GenericBackendResponse, error)
// Misc
GetDenomMetadata(base string) (berpctypes.GenericBackendResponse, error)
GetDenomsMetadata(pageNo int) (berpctypes.GenericBackendResponse, error)
GetTotalSupply(pageNo int) (berpctypes.GenericBackendResponse, error)
GetBankDenomsMetadata(coins sdk.Coins) map[string]banktypes.Metadata
// Export fields
GetContext() context.Context
GetClientContext() client.Context
GetQueryClient() *berpctypes.QueryClient
GetLogger() log.Logger
GetConfig() config.BeJsonRpcConfig
GetExternalServices() berpctypes.ExternalServices
}
var _ BackendI = (*Backend)(nil)
// Backend implements the BackendI interface
type Backend struct {
ctx context.Context
clientCtx client.Context
queryClient *berpctypes.QueryClient // gRPC query client
logger log.Logger
cfg config.BeJsonRpcConfig
interceptor RequestInterceptor
messageParsers map[string]berpctypes.MessageParser
messageInvolversExtractors map[string]berpctypes.MessageInvolversExtractor
externalServices berpctypes.ExternalServices
// cache
bech32Cfg berpctypes.Bech32Config
tendermintValidatorsCache *tendermintValidatorsCache
stakingValidatorsCache *stakingValidatorsCache
}
// NewBackend creates a new Backend instance for RollApp Block Explorer
func NewBackend(
ctx *server.Context,
logger log.Logger,
clientCtx client.Context,
messageParsers map[string]berpctypes.MessageParser,
messageInvolversExtractors map[string]berpctypes.MessageInvolversExtractor,
externalServices berpctypes.ExternalServices,
) *Backend {
appConf, err := config.GetConfig(ctx.Viper)
if err != nil {
panic(err)
}
queryClient := berpctypes.NewQueryClient(clientCtx)
return &Backend{
ctx: context.Background(),
clientCtx: clientCtx,
queryClient: queryClient,
logger: logger.With("module", "be_rpc"),
cfg: appConf,
messageParsers: messageParsers,
messageInvolversExtractors: messageInvolversExtractors,
externalServices: externalServices,
// cache
bech32Cfg: berpctypes.NewBech32Config(),
tendermintValidatorsCache: NewTendermintValidatorsCache(
clientCtx.Client,
),
stakingValidatorsCache: NewStakingValidatorsCache(
clientCtx.Client,
queryClient.StakingQueryClient,
clientCtx.Codec,
),
}
}
func (m *Backend) WithInterceptor(interceptor RequestInterceptor) *Backend {
m.interceptor = interceptor
return m
}
func (m *Backend) GetContext() context.Context {
return m.ctx
}
func (m *Backend) GetClientContext() client.Context {
return m.clientCtx
}
func (m *Backend) GetQueryClient() *berpctypes.QueryClient {
return m.queryClient
}
func (m *Backend) GetLogger() log.Logger {
return m.logger
}
func (m *Backend) GetConfig() config.BeJsonRpcConfig {
return m.cfg
}
func (m *Backend) GetExternalServices() berpctypes.ExternalServices {
return m.externalServices
}