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 WDelayerInterface
21}
22
23//
24// Implementation
25//
26
27// Client is used to interact with Ethereum and the Hermez smart contracts.
28type Client struct {
29 EthereumClient
30 AuctionClient
31 RollupClient
32 WDelayerClient
33}
34
35// TokenConfig is used to define the information about token
36type TokenConfig struct {
37 Address ethCommon.Address
38 Name string
39}
40
41// RollupConfig is the configuration for the Rollup smart contract interface
42type RollupConfig struct {
43 Address ethCommon.Address
44}
45
46// AuctionConfig is the configuration for the Auction smart contract interface
47type AuctionConfig struct {
48 Address ethCommon.Address
49 TokenHEZ TokenConfig
50}
51
52// WDelayerConfig is the configuration for the WDelayer smart contract interface
53type WDelayerConfig struct {
54 Address ethCommon.Address
55}
56
57// ClientConfig is the configuration of the Client
58type ClientConfig struct {
59 Ethereum EthereumConfig
60 Rollup RollupConfig
61 Auction AuctionConfig
62 WDelayer WDelayerConfig
63}
64
65// NewClient creates a new Client to interact with Ethereum and the Hermez smart contracts.
66func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, cfg *ClientConfig) (*Client, error) {
67 ethereumClient := NewEthereumClient(client, account, ks, &cfg.Ethereum)
68 auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address, cfg.Auction.TokenHEZ)
69 if err != nil {
70 return nil, err
71 }
72 rollupClient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address, cfg.Auction.TokenHEZ)
73 if err != nil {
74 return nil, err
75 }
76 wDelayerClient, err := NewWDelayerClient(ethereumClient, cfg.WDelayer.Address)
77 if err != nil {
78 return nil, err
79 }
80 return &Client{
81 EthereumClient: *ethereumClient,
82 AuctionClient: *auctionClient,
83 RollupClient: *rollupClient,
84 WDelayerClient: *wDelayerClient,
85 }, nil
86}