forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheth_getTransactionByBlockHashAndIndex.go
60 lines (48 loc) · 1.81 KB
/
eth_getTransactionByBlockHashAndIndex.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
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"
)
// ProxyETHGetTransactionByBlockHashAndIndex implements ETHProxy
type ProxyETHGetTransactionByBlockHashAndIndex struct {
*qtum.Qtum
}
func (p *ProxyETHGetTransactionByBlockHashAndIndex) Method() string {
return "eth_getTransactionByBlockHashAndIndex"
}
func (p *ProxyETHGetTransactionByBlockHashAndIndex) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var req eth.GetTransactionByBlockHashAndIndex
if err := json.Unmarshal(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
if req.BlockHash == "" {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError("invalid argument 0: empty hex string")
}
return p.request(c.Request().Context(), &req)
}
func (p *ProxyETHGetTransactionByBlockHashAndIndex) request(ctx context.Context, req *eth.GetTransactionByBlockHashAndIndex) (interface{}, eth.JSONRPCError) {
transactionIndex, err := hexutil.DecodeUint64(req.TransactionIndex)
if err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError("invalid argument 1")
}
// Proxy eth_getBlockByHash and return the transaction at requested index
getBlockByNumber := ProxyETHGetBlockByHash{p.Qtum}
blockByNumber, jsonErr := getBlockByNumber.request(ctx, ð.GetBlockByHashRequest{BlockHash: req.BlockHash, FullTransaction: true})
if jsonErr != nil {
return nil, jsonErr
}
if blockByNumber == nil {
return nil, nil
}
if len(blockByNumber.Transactions) <= int(transactionIndex) {
return nil, nil
}
return blockByNumber.Transactions[int(transactionIndex)], nil
}