arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/eth/client.go
eth/client.goRaw
 1package eth
 2
 3import (
 4	"fmt"
 5
 6	"github.com/ethereum/go-ethereum/accounts"
 7	ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"
 8	ethCommon "github.com/ethereum/go-ethereum/common"
 9	"github.com/ethereum/go-ethereum/ethclient"
10	"github.com/hermeznetwork/tracerr"
11)
12
13var errTODO = fmt.Errorf("TODO: Not implemented yet")
14
15// ClientInterface is the eth Client interface used by hermez-node modules to
16// interact with Ethereum Blockchain and smart contracts.
17type ClientInterface interface {
18	EthereumInterface
19	RollupInterface
20	AuctionInterface
21	WDelayerInterface
22}
23
24//
25// Implementation
26//
27
28// Client is used to interact with Ethereum and the Hermez smart contracts.
29type Client struct {
30	EthereumClient
31	AuctionClient
32	RollupClient
33	WDelayerClient
34}
35
36// TokenConfig is used to define the information about token
37type TokenConfig struct {
38	Address ethCommon.Address
39	Name    string
40}
41
42// RollupConfig is the configuration for the Rollup smart contract interface
43type RollupConfig struct {
44	Address ethCommon.Address
45}
46
47// AuctionConfig is the configuration for the Auction smart contract interface
48type AuctionConfig struct {
49	Address  ethCommon.Address
50	TokenHEZ TokenConfig
51}
52
53// WDelayerConfig is the configuration for the WDelayer smart contract interface
54type WDelayerConfig struct {
55	Address ethCommon.Address
56}
57
58// ClientConfig is the configuration of the Client
59type ClientConfig struct {
60	Ethereum EthereumConfig
61	Rollup   RollupConfig
62	Auction  AuctionConfig
63	WDelayer WDelayerConfig
64}
65
66// NewClient creates a new Client to interact with Ethereum and the Hermez smart contracts.
67func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore,
68	cfg *ClientConfig) (*Client, error) {
69	ethereumClient, err := NewEthereumClient(client, account, ks, &cfg.Ethereum)
70	if err != nil {
71		return nil, tracerr.Wrap(err)
72	}
73	auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address,
74		cfg.Auction.TokenHEZ)
75	if err != nil {
76		return nil, tracerr.Wrap(err)
77	}
78	rollupClient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address,
79		cfg.Auction.TokenHEZ)
80	if err != nil {
81		return nil, tracerr.Wrap(err)
82	}
83	wDelayerClient, err := NewWDelayerClient(ethereumClient, cfg.WDelayer.Address)
84	if err != nil {
85		return nil, tracerr.Wrap(err)
86	}
87	return &Client{
88		EthereumClient: *ethereumClient,
89		AuctionClient:  *auctionClient,
90		RollupClient:   *rollupClient,
91		WDelayerClient: *wDelayerClient,
92	}, nil
93}