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, cfg *ClientConfig) (*Client, error) {
68	ethereumClient := NewEthereumClient(client, account, ks, &cfg.Ethereum)
69	auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address, cfg.Auction.TokenHEZ)
70	if err != nil {
71		return nil, tracerr.Wrap(err)
72	}
73	rollupClient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address, cfg.Auction.TokenHEZ)
74	if err != nil {
75		return nil, tracerr.Wrap(err)
76	}
77	wDelayerClient, err := NewWDelayerClient(ethereumClient, cfg.WDelayer.Address)
78	if err != nil {
79		return nil, tracerr.Wrap(err)
80	}
81	return &Client{
82		EthereumClient: *ethereumClient,
83		AuctionClient:  *auctionClient,
84		RollupClient:   *rollupClient,
85		WDelayerClient: *wDelayerClient,
86	}, nil
87}