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.

215 lines
6.7 KiB

  1. package eth
  2. import (
  3. "context"
  4. "fmt"
  5. "math/big"
  6. "time"
  7. "github.com/ethereum/go-ethereum/accounts"
  8. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  9. ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/ethereum/go-ethereum/core/types"
  12. "github.com/ethereum/go-ethereum/ethclient"
  13. "github.com/hermeznetwork/hermez-node/common"
  14. "github.com/hermeznetwork/hermez-node/log"
  15. )
  16. var (
  17. ErrAccountNil = fmt.Errorf("Authorized calls can't be made when the account is nil")
  18. // ErrReceiptStatusFailed when receiving a failed transaction
  19. ErrReceiptStatusFailed = fmt.Errorf("receipt status is failed")
  20. // ErrReceiptNotRecieved when unable to retrieve a transaction
  21. ErrReceiptNotReceived = fmt.Errorf("receipt not available")
  22. )
  23. const (
  24. errStrDeploy = "deployment of %s failed: %w"
  25. errStrWaitReceipt = "wait receipt of %s deploy failed: %w"
  26. )
  27. // Client is an ethereum client to call Smart Contract methods.
  28. type Client struct {
  29. client *ethclient.Client
  30. account *accounts.Account
  31. ks *ethKeystore.KeyStore
  32. ReceiptTimeout time.Duration
  33. }
  34. // NewClient creates a Client instance. The account is not mandatory (it can
  35. // be nil). If the account is nil, CallAuth will fail with ErrAccountNil.
  36. func NewClient(client *ethclient.Client, account *accounts.Account, ks *ethKeystore.KeyStore) *Client {
  37. return &Client{client: client, account: account, ks: ks, ReceiptTimeout: 60 * time.Second}
  38. }
  39. // BalanceAt retieves information about the default account
  40. func (c *Client) BalanceAt(addr ethCommon.Address) (*big.Int, error) {
  41. return c.client.BalanceAt(context.TODO(), addr, nil)
  42. }
  43. // Account returns the underlying ethereum account
  44. func (c *Client) Account() *accounts.Account {
  45. return c.account
  46. }
  47. // CallAuth performs a Smart Contract method call that requires authorization.
  48. // This call requires a valid account with Ether that can be spend during the
  49. // call.
  50. func (c *Client) CallAuth(gasLimit uint64,
  51. fn func(*ethclient.Client, *bind.TransactOpts) (*types.Transaction, error)) (*types.Transaction, error) {
  52. if c.account == nil {
  53. return nil, ErrAccountNil
  54. }
  55. gasPrice, err := c.client.SuggestGasPrice(context.Background())
  56. if err != nil {
  57. return nil, err
  58. }
  59. inc := new(big.Int).Set(gasPrice)
  60. inc.Div(inc, new(big.Int).SetUint64(100))
  61. gasPrice.Add(gasPrice, inc)
  62. log.Debug("Transaction metadata", "gasPrice", gasPrice)
  63. auth, err := bind.NewKeyStoreTransactor(c.ks, *c.account)
  64. if err != nil {
  65. return nil, err
  66. }
  67. auth.Value = big.NewInt(0) // in wei
  68. if gasLimit == 0 {
  69. auth.GasLimit = uint64(300000) // in units
  70. } else {
  71. auth.GasLimit = gasLimit // in units
  72. }
  73. auth.GasPrice = gasPrice
  74. tx, err := fn(c.client, auth)
  75. if tx != nil {
  76. log.Debug("Transaction", "tx", tx.Hash().Hex(), "nonce", tx.Nonce())
  77. }
  78. return tx, err
  79. }
  80. type ContractData struct {
  81. Address ethCommon.Address
  82. Tx *types.Transaction
  83. Receipt *types.Receipt
  84. }
  85. // Deploy a smart contract. `name` is used to log deployment information. fn
  86. // is a wrapper to the deploy function generated by abigen. In case of error,
  87. // the returned `ContractData` may have some parameters filled depending on the
  88. // kind of error that ocurred.
  89. // successfull.
  90. func (c *Client) Deploy(name string,
  91. fn func(c *ethclient.Client, auth *bind.TransactOpts) (ethCommon.Address, *types.Transaction, interface{}, error)) (ContractData, error) {
  92. var contractData ContractData
  93. log.Info("Deploying", "contract", name)
  94. tx, err := c.CallAuth(
  95. 1000000,
  96. func(client *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  97. addr, tx, _, err := fn(client, auth)
  98. if err != nil {
  99. return nil, err
  100. }
  101. contractData.Address = addr
  102. return tx, nil
  103. },
  104. )
  105. if err != nil {
  106. return contractData, fmt.Errorf(errStrDeploy, name, err)
  107. }
  108. log.Info("Waiting receipt", "tx", tx.Hash().Hex(), "contract", name)
  109. contractData.Tx = tx
  110. receipt, err := c.WaitReceipt(tx)
  111. if err != nil {
  112. return contractData, fmt.Errorf(errStrWaitReceipt, name, err)
  113. }
  114. contractData.Receipt = receipt
  115. return contractData, nil
  116. }
  117. // Call performs a read only Smart Contract method call.
  118. func (c *Client) Call(fn func(*ethclient.Client) error) error {
  119. return fn(c.client)
  120. }
  121. // WaitReceipt will block until a transaction is confirmed. Internally it
  122. // polls the state every 200 milliseconds.
  123. func (c *Client) WaitReceipt(tx *types.Transaction) (*types.Receipt, error) {
  124. return c.waitReceipt(tx, context.TODO(), c.ReceiptTimeout)
  125. }
  126. // GetReceipt will check if a transaction is confirmed and return
  127. // immediately, waiting at most 1 second and returning error if the transaction
  128. // is still pending.
  129. func (c *Client) GetReceipt(tx *types.Transaction) (*types.Receipt, error) {
  130. ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
  131. defer cancel()
  132. return c.waitReceipt(tx, ctx, 0)
  133. }
  134. func (c *Client) waitReceipt(tx *types.Transaction, ctx context.Context, timeout time.Duration) (*types.Receipt, error) {
  135. var err error
  136. var receipt *types.Receipt
  137. txid := tx.Hash()
  138. log.Debug("Waiting for receipt", "tx", txid.Hex())
  139. start := time.Now()
  140. for {
  141. receipt, err = c.client.TransactionReceipt(ctx, txid)
  142. if receipt != nil || time.Since(start) >= timeout {
  143. break
  144. }
  145. time.Sleep(200 * time.Millisecond)
  146. }
  147. if receipt != nil && receipt.Status == types.ReceiptStatusFailed {
  148. log.Error("Failed transaction", "tx", txid.Hex())
  149. return receipt, ErrReceiptStatusFailed
  150. }
  151. if receipt == nil {
  152. log.Debug("Pendingtransaction / Wait receipt timeout", "tx", txid.Hex(), "lasterr", err)
  153. return receipt, ErrReceiptNotReceived
  154. }
  155. log.Debug("Successfull transaction", "tx", txid.Hex())
  156. return receipt, err
  157. }
  158. // CurrentBlock returns the current block number in the blockchain
  159. func (c *Client) CurrentBlock() (*big.Int, error) {
  160. ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second)
  161. defer cancel()
  162. header, err := c.client.HeaderByNumber(ctx, nil)
  163. if err != nil {
  164. return nil, err
  165. }
  166. return header.Number, nil
  167. }
  168. // HeaderByNumber internally calls ethclient.Client HeaderByNumber
  169. func (c *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
  170. return c.client.HeaderByNumber(ctx, number)
  171. }
  172. // BlockByNumber internally calls ethclient.Client BlockByNumber and returns *common.Block
  173. func (c *Client) BlockByNumber(ctx context.Context, number *big.Int) (*common.Block, error) {
  174. block, err := c.client.BlockByNumber(ctx, number)
  175. if err != nil {
  176. return nil, err
  177. }
  178. b := &common.Block{
  179. EthBlockNum: block.Number().Uint64(),
  180. Timestamp: time.Unix(int64(block.Time()), 0),
  181. Hash: block.Hash(),
  182. }
  183. return b, nil
  184. }
  185. func (c *Client) ForgeCall(callData *common.CallDataForge) ([]byte, error) {
  186. // TODO this depends on the smart contracts, once are ready this will be updated
  187. return nil, nil
  188. }