-
Notifications
You must be signed in to change notification settings - Fork 180
/
api.go
48 lines (41 loc) · 1.34 KB
/
api.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
package web3
import (
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/okex/exchain/app/rpc/monitor"
"github.com/okex/exchain/libs/cosmos-sdk/version"
"github.com/okex/exchain/libs/tendermint/libs/log"
"github.com/spf13/viper"
)
const (
NameSpace = "web3"
)
// PublicWeb3API is the web3_ prefixed set of APIs in the Web3 JSON-RPC spec.
type PublicWeb3API struct {
logger log.Logger
Metrics *monitor.RpcMetrics
}
// NewAPI creates an instance of the Web3 API.
func NewAPI(log log.Logger) *PublicWeb3API {
api := &PublicWeb3API{
logger: log.With("module", "json-rpc", "namespace", NameSpace),
}
if viper.GetBool(monitor.FlagEnableMonitor) {
api.Metrics = monitor.MakeMonitorMetrics(NameSpace)
}
return api
}
// ClientVersion returns the client version in the Web3 user agent format.
func (api *PublicWeb3API) ClientVersion() string {
monitor := monitor.GetMonitor("web3_clientVersion", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd()
info := version.NewInfo()
return fmt.Sprintf("%s-%s", info.Name, info.Version)
}
// Sha3 returns the keccak-256 hash of the passed-in input.
func (api *PublicWeb3API) Sha3(input hexutil.Bytes) hexutil.Bytes {
monitor := monitor.GetMonitor("web3_sha3", api.logger, api.Metrics).OnBegin()
defer monitor.OnEnd()
return crypto.Keccak256(input)
}