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
 15const (
 16	blocksPerDay = (3600 * 24) / 15
 17)
 18
 19func max(x, y int64) int64 {
 20	if x > y {
 21		return x
 22	}
 23	return y
 24}
 25
 26// ClientInterface is the eth Client interface used by hermez-node modules to
 27// interact with Ethereum Blockchain and smart contracts.
 28type ClientInterface interface {
 29	EthereumInterface
 30	RollupInterface
 31	AuctionInterface
 32	WDelayerInterface
 33}
 34
 35//
 36// Implementation
 37//
 38
 39// Client is used to interact with Ethereum and the Hermez smart contracts.
 40type Client struct {
 41	EthereumClient
 42	AuctionClient
 43	RollupClient
 44	WDelayerClient
 45}
 46
 47// TokenConfig is used to define the information about token
 48type TokenConfig struct {
 49	Address ethCommon.Address
 50	Name    string
 51}
 52
 53// RollupConfig is the configuration for the Rollup smart contract interface
 54type RollupConfig struct {
 55	Address ethCommon.Address
 56}
 57
 58// AuctionConfig is the configuration for the Auction smart contract interface
 59type AuctionConfig struct {
 60	Address  ethCommon.Address
 61	TokenHEZ TokenConfig
 62}
 63
 64// WDelayerConfig is the configuration for the WDelayer smart contract interface
 65type WDelayerConfig struct {
 66	Address ethCommon.Address
 67}
 68
 69// ClientConfig is the configuration of the Client
 70type ClientConfig struct {
 71	Ethereum EthereumConfig
 72	Rollup   RollupConfig
 73	Auction  AuctionConfig
 74	WDelayer WDelayerConfig
 75}
 76
 77// NewClient creates a new Client to interact with Ethereum and the Hermez smart contracts.
 78func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore,
 79	cfg *ClientConfig) (*Client, error) {
 80	ethereumClient, err := NewEthereumClient(client, account, ks, &cfg.Ethereum)
 81	if err != nil {
 82		return nil, tracerr.Wrap(err)
 83	}
 84	auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address,
 85		cfg.Auction.TokenHEZ)
 86	if err != nil {
 87		return nil, tracerr.Wrap(err)
 88	}
 89	rollupClient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address,
 90		cfg.Auction.TokenHEZ)
 91	if err != nil {
 92		return nil, tracerr.Wrap(err)
 93	}
 94	wDelayerClient, err := NewWDelayerClient(ethereumClient, cfg.WDelayer.Address)
 95	if err != nil {
 96		return nil, tracerr.Wrap(err)
 97	}
 98	return &Client{
 99		EthereumClient: *ethereumClient,
100		AuctionClient:  *auctionClient,
101		RollupClient:   *rollupClient,
102		WDelayerClient: *wDelayerClient,
103	}, nil
104}