/
transaction_info.go
95 lines (86 loc) · 2.85 KB
/
transaction_info.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017 Sylvain 6120 Laurent
// This file is part of the gethitihteg library.
//
// The gethitihteg library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The gethitihteg library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the gethitihteg library. If not, see <http://www.gnu.org/licenses/>.
package blockchain
import (
"context"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
)
// ClientConnector defines typed wrappers for the Ethereum RPC API.
type ClientConnector struct {
c *rpc.Client
rawurl string
try_max int
}
// Dial connects a client to the given URL.
func (ec *ClientConnector) Dial() error {
c, err := rpc.Dial(ec.rawurl)
if err != nil { // TODO retry here ?
return err
}
ec.c = c
return nil
}
// NewClientConnector creates a client that uses the given RPC client.
func NewClientConnector(rawurl string, try_max int) (*ClientConnector, error) {
cc := ClientConnector{
c: nil,
rawurl: rawurl,
try_max: try_max,
}
err := cc.Dial()
if err != nil { // TODO retry here ?
return nil, err
}
return &cc, nil
}
// Close close the RPC connection.
func (ec *ClientConnector) Close() {
ec.Close()
}
// HeaderByHash returns the block header with the given hash.
func (ec *ClientConnector) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
var head *types.Header
err := ec.c.CallContext(ctx, &head, "eth_getBlockByHash", hash, false)
if err == nil && head == nil {
err = ethereum.NotFound
}
return head, err
}
// TransactionByHash returns the transaction with the given hash.
func (ec *ClientConnector) TransactionByHashFull(ctx context.Context, hash common.Hash) (tx *types.Transaction, blockhash *common.Hash, err error) {
var raw json.RawMessage
err = ec.c.CallContext(ctx, &raw, "eth_getTransactionByHash", hash)
if err != nil {
return nil, nil, err
} else if len(raw) == 0 {
return nil, nil, ethereum.NotFound
}
if err := json.Unmarshal(raw, &tx); err != nil {
return nil, nil, err
} else if _, r, _ := tx.RawSignatureValues(); r == nil {
return nil, nil, fmt.Errorf("server returned transaction without signature")
}
var block struct{ BlockHash *common.Hash }
if err := json.Unmarshal(raw, &block); err != nil {
return nil, nil, err
}
return tx, block.BlockHash, nil
}