forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_getCode.go
53 lines (44 loc) · 1.29 KB
/
eth_getCode.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
package transformer
import (
"context"
"github.com/kaonone/eth-rpc-gate/pkg/eth"
"github.com/kaonone/eth-rpc-gate/pkg/kaon"
"github.com/kaonone/eth-rpc-gate/pkg/utils"
"github.com/labstack/echo"
)
// ProxyETHGetCode implements ETHProxy
type ProxyETHGetCode struct {
*kaon.Kaon
}
func (p *ProxyETHGetCode) Method() string {
return "eth_getCode"
}
func (p *ProxyETHGetCode) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, *eth.JSONRPCError) {
var req eth.GetCodeRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
return p.request(c.Request().Context(), &req)
}
func (p *ProxyETHGetCode) request(ctx context.Context, ethreq *eth.GetCodeRequest) (eth.GetCodeResponse, *eth.JSONRPCError) {
kaonreq := kaon.GetAccountInfoRequest(utils.RemoveHexPrefix(ethreq.Address))
kaonresp, err := p.GetAccountInfo(ctx, &kaonreq)
if err != nil {
if err == kaon.ErrInvalidAddress {
/**
// correct response for an invalid address
{
"jsonrpc": "2.0",
"id": 123,
"result": "0x"
}
**/
return "0x", nil
} else {
return "", eth.NewCallbackError(err.Error())
}
}
// kaon res -> eth res
return eth.GetCodeResponse(utils.AddHexPrefix(kaonresp.Code)), nil
}