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