forked from dcb9/janus
-
Notifications
You must be signed in to change notification settings - Fork 26
/
eth_getTransactionByBlockNumberAndIndex.go
64 lines (54 loc) · 2.02 KB
/
eth_getTransactionByBlockNumberAndIndex.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
package transformer
import (
"context"
"encoding/json"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
)
// ProxyETHGetTransactionByBlockNumberAndIndex implements ETHProxy
type ProxyETHGetTransactionByBlockNumberAndIndex struct {
*qtum.Qtum
}
func (p *ProxyETHGetTransactionByBlockNumberAndIndex) Method() string {
return "eth_getTransactionByBlockNumberAndIndex"
}
func (p *ProxyETHGetTransactionByBlockNumberAndIndex) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var req eth.GetTransactionByBlockNumberAndIndex
if err := json.Unmarshal(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError("couldn't unmarshal request")
}
if req.BlockNumber == "" {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError("invalid argument 0: empty hex string")
}
return p.request(c.Request().Context(), &req)
}
func (p *ProxyETHGetTransactionByBlockNumberAndIndex) request(ctx context.Context, req *eth.GetTransactionByBlockNumberAndIndex) (interface{}, eth.JSONRPCError) {
// Decoded by ProxyETHGetTransactionByBlockHashAndIndex, quickly decode so we can fail cheaply without making any calls
_, decodeErr := hexutil.DecodeUint64(req.TransactionIndex)
if decodeErr != nil {
return nil, eth.NewInvalidParamsError("invalid argument 1")
}
blockNum, err := getBlockNumberByParam(ctx, p.Qtum, req.BlockNumber, false)
if err != nil {
return nil, eth.NewCallbackError("couldn't get block number by parameter")
}
blockHash, err := proxyETHGetBlockByHash(ctx, p, p.Qtum, blockNum)
if err != nil {
return nil, err
}
if blockHash == nil {
return nil, nil
}
var (
getBlockByHashReq = ð.GetTransactionByBlockHashAndIndex{
BlockHash: string(*blockHash),
TransactionIndex: req.TransactionIndex,
}
proxy = &ProxyETHGetTransactionByBlockHashAndIndex{Qtum: p.Qtum}
)
return proxy.request(ctx, getBlockByHashReq)
}