forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_subscribe.go
56 lines (49 loc) · 1.4 KB
/
eth_subscribe.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
package transformer
import (
"github.com/kaonone/eth-rpc-gate/pkg/eth"
"github.com/kaonone/eth-rpc-gate/pkg/kaon"
"github.com/kaonone/eth-rpc-gate/pkg/notifier"
"github.com/labstack/echo"
)
// ETHSubscribe implements ETHProxy
type ETHSubscribe struct {
*kaon.Kaon
*notifier.Agent
}
func (p *ETHSubscribe) Method() string {
return "eth_subscribe"
}
func (p *ETHSubscribe) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, *eth.JSONRPCError) {
notifier := getNotifier(c)
if notifier == nil {
p.GetLogger().Log("msg", "eth_subscribe only supported over websocket")
/*
// TODO
{
"jsonrpc": "2.0",
"id": 580,
"error": {
"code": -32601,
"message": "The method eth_subscribe does not exist/is not available"
}
}
*/
return nil, eth.NewMethodNotFoundError("eth_subscribe")
}
var req eth.EthSubscriptionRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
return p.request(&req, notifier)
}
func (p *ETHSubscribe) request(req *eth.EthSubscriptionRequest, notifier *notifier.Notifier) (*eth.EthSubscriptionResponse, *eth.JSONRPCError) {
notifier.ResponseRequired()
id, err := p.NewSubscription(notifier, req)
response := eth.EthSubscriptionResponse(id)
if err == nil {
return &response, nil
} else {
return &response, eth.NewCallbackError(err.Error())
}
}