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)
11
12var errTODO = fmt.Errorf("TODO: Not implemented yet")
13
14// ClientInterface is the eth Client interface used by hermez-node modules to
15// interact with Ethereum Blockchain and smart contracts.
16type ClientInterface interface {
17	EthereumInterface
18	RollupInterface
19	AuctionInterface
20}
21
22//
23// Implementation
24//
25
26// Client is used to interact with Ethereum and the Hermez smart contracts.
27type Client struct {
28	EthereumClient
29	AuctionClient
30	RollupClient
31}
32
33// RollupConfig is the configuration for the Rollup smart contract interface
34type RollupConfig struct {
35	Address ethCommon.Address
36}
37
38// AuctionConfig is the configuration for the Auction smart contract interface
39type AuctionConfig struct {
40	Address         ethCommon.Address
41	TokenHEZAddress ethCommon.Address
42}
43
44// ClientConfig is the configuration of the Client
45type ClientConfig struct {
46	Ethereum EthereumConfig
47	Rollup   RollupConfig
48	Auction  AuctionConfig
49}
50
51// NewClient creates a new Client to interact with Ethereum and the Hermez smart contracts.
52func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, cfg *ClientConfig) (*Client, error) {
53	ethereumClient := NewEthereumClient(client, account, ks, &cfg.Ethereum)
54	auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address, cfg.Auction.TokenHEZAddress)
55	if err != nil {
56		return nil, err
57	}
58	rollupCient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address, cfg.Auction.TokenHEZAddress)
59	if err != nil {
60		return nil, err
61	}
62	return &Client{
63		EthereumClient: *ethereumClient,
64		AuctionClient:  *auctionClient,
65		RollupClient:   *rollupCient,
66	}, nil
67}