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.

93 lines
2.4 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,
  56. cfg *ClientConfig) (*Client, error) {
  57. ethereumClient, err := NewEthereumClient(client, account, ks, &cfg.Ethereum)
  58. if err != nil {
  59. return nil, tracerr.Wrap(err)
  60. }
  61. auctionClient, err := NewAuctionClient(ethereumClient, cfg.Auction.Address,
  62. cfg.Auction.TokenHEZ)
  63. if err != nil {
  64. return nil, tracerr.Wrap(err)
  65. }
  66. rollupClient, err := NewRollupClient(ethereumClient, cfg.Rollup.Address,
  67. cfg.Auction.TokenHEZ)
  68. if err != nil {
  69. return nil, tracerr.Wrap(err)
  70. }
  71. wDelayerClient, err := NewWDelayerClient(ethereumClient, cfg.WDelayer.Address)
  72. if err != nil {
  73. return nil, tracerr.Wrap(err)
  74. }
  75. return &Client{
  76. EthereumClient: *ethereumClient,
  77. AuctionClient: *auctionClient,
  78. RollupClient: *rollupClient,
  79. WDelayerClient: *wDelayerClient,
  80. }, nil
  81. }