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.

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