You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB

  1. package eth
  2. import (
  3. "fmt"
  4. "github.com/ethereum/go-ethereum/accounts"
  5. ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"
  6. ethCommon "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/ethclient"
  8. "github.com/hermeznetwork/tracerr"
  9. )
  10. var errTODO = fmt.Errorf("TODO: Not implemented yet")
  11. // ClientInterface is the eth Client interface used by hermez-node modules to
  12. // interact with Ethereum Blockchain and smart contracts.
  13. type ClientInterface interface {
  14. EthereumInterface
  15. RollupInterface
  16. AuctionInterface
  17. WDelayerInterface
  18. }
  19. //
  20. // Implementation
  21. //
  22. // Client is used to interact with Ethereum and the Hermez smart contracts.
  23. type Client struct {
  24. EthereumClient
  25. AuctionClient
  26. RollupClient
  27. WDelayerClient
  28. }
  29. // TokenConfig is used to define the information about token
  30. type TokenConfig struct {
  31. Address ethCommon.Address
  32. Name string
  33. }
  34. // RollupConfig is the configuration for the Rollup smart contract interface
  35. type RollupConfig struct {
  36. Address ethCommon.Address
  37. }
  38. // AuctionConfig is the configuration for the Auction smart contract interface
  39. type AuctionConfig struct {
  40. Address ethCommon.Address
  41. TokenHEZ TokenConfig
  42. }
  43. // WDelayerConfig is the configuration for the WDelayer smart contract interface
  44. type WDelayerConfig struct {
  45. Address ethCommon.Address
  46. }
  47. // ClientConfig is the configuration of the Client
  48. type ClientConfig struct {
  49. Ethereum EthereumConfig
  50. Rollup RollupConfig
  51. Auction AuctionConfig
  52. WDelayer WDelayerConfig
  53. }
  54. // NewClient creates a new Client to interact with Ethereum and the Hermez smart contracts.
  55. func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore, cfg *ClientConfig) (*Client, error) {
  56. ethereumClient := NewEthereumClient(client, account, ks, &cfg.Ethereum)
  57. auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address, cfg.Auction.TokenHEZ)
  58. if err != nil {
  59. return nil, tracerr.Wrap(err)
  60. }
  61. rollupClient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address, cfg.Auction.TokenHEZ)
  62. if err != nil {
  63. return nil, tracerr.Wrap(err)
  64. }
  65. wDelayerClient, err := NewWDelayerClient(ethereumClient, cfg.WDelayer.Address)
  66. if err != nil {
  67. return nil, tracerr.Wrap(err)
  68. }
  69. return &Client{
  70. EthereumClient: *ethereumClient,
  71. AuctionClient: *auctionClient,
  72. RollupClient: *rollupClient,
  73. WDelayerClient: *wDelayerClient,
  74. }, nil
  75. }