/
client.go
63 lines (49 loc) · 1.42 KB
/
client.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
package deployer
import (
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/pkg/errors"
log "github.com/xlab/suplog"
)
type Client struct {
*ethclient.Client
rc *rpc.Client
}
func NewClient(rc *rpc.Client) *Client {
return &Client{
Client: ethclient.NewClient(rc),
rc: rc,
}
}
func (ec *Client) SendTransactionWithRet(ctx context.Context, tx *types.Transaction) (txHash common.Hash, err error) {
data, err := rlp.EncodeToBytes(tx)
if err != nil {
return common.Hash{}, err
}
if err := ec.rc.CallContext(ctx, &txHash, "eth_sendRawTransaction", hexutil.Encode(data)); err != nil {
return tx.Hash(), err
}
return txHash, nil
}
var ErrClientNotAvailable = errors.New("EVM RPC client is not available due to connection issue")
func (d *deployer) Backend() (*Client, error) {
d.initClientOnce.Do(func() {
dialCtx, cancelFn := context.WithTimeout(context.Background(), d.options.RPCTimeout)
defer cancelFn()
rc, err := rpc.DialContext(dialCtx, d.options.EVMRPCEndpoint)
if err != nil {
log.WithError(err).Errorln("failed to dial EVM RPC endpoint")
return
}
d.client = NewClient(rc)
})
if d.client == nil {
return nil, ErrClientNotAvailable
}
return d.client, nil
}