backend.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bind
  17. import (
  18. "errors"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "golang.org/x/net/context"
  23. )
  24. // ErrNoCode is returned by call and transact operations for which the requested
  25. // recipient contract to operate on does not exist in the state db or does not
  26. // have any code associated with it (i.e. suicided).
  27. var ErrNoCode = errors.New("no contract code at given address")
  28. // ContractCaller defines the methods needed to allow operating with contract on a read
  29. // only basis.
  30. type ContractCaller interface {
  31. // HasCode checks if the contract at the given address has any code associated
  32. // with it or not. This is needed to differentiate between contract internal
  33. // errors and the local chain being out of sync.
  34. HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error)
  35. // ContractCall executes an Ethereum contract call with the specified data as
  36. // the input. The pending flag requests execution against the pending block, not
  37. // the stable head of the chain.
  38. ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error)
  39. }
  40. // ContractTransactor defines the methods needed to allow operating with contract
  41. // on a write only basis. Beside the transacting method, the remainder are helpers
  42. // used when the user does not provide some needed values, but rather leaves it up
  43. // to the transactor to decide.
  44. type ContractTransactor interface {
  45. // PendingAccountNonce retrieves the current pending nonce associated with an
  46. // account.
  47. PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error)
  48. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  49. // execution of a transaction.
  50. SuggestGasPrice(ctx context.Context) (*big.Int, error)
  51. // HasCode checks if the contract at the given address has any code associated
  52. // with it or not. This is needed to differentiate between contract internal
  53. // errors and the local chain being out of sync.
  54. HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error)
  55. // EstimateGasLimit tries to estimate the gas needed to execute a specific
  56. // transaction based on the current pending state of the backend blockchain.
  57. // There is no guarantee that this is the true gas limit requirement as other
  58. // transactions may be added or removed by miners, but it should provide a basis
  59. // for setting a reasonable default.
  60. EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error)
  61. // SendTransaction injects the transaction into the pending pool for execution.
  62. SendTransaction(ctx context.Context, tx *types.Transaction) error
  63. }
  64. // ContractBackend defines the methods needed to allow operating with contract
  65. // on a read-write basis.
  66. //
  67. // This interface is essentially the union of ContractCaller and ContractTransactor
  68. // but due to a bug in the Go compiler (https://github.com/golang/go/issues/6977),
  69. // we cannot simply list it as the two interfaces. The other solution is to add a
  70. // third interface containing the common methods, but that convolutes the user API
  71. // as it introduces yet another parameter to require for initialization.
  72. type ContractBackend interface {
  73. // HasCode checks if the contract at the given address has any code associated
  74. // with it or not. This is needed to differentiate between contract internal
  75. // errors and the local chain being out of sync.
  76. HasCode(ctx context.Context, contract common.Address, pending bool) (bool, error)
  77. // ContractCall executes an Ethereum contract call with the specified data as
  78. // the input. The pending flag requests execution against the pending block, not
  79. // the stable head of the chain.
  80. ContractCall(ctx context.Context, contract common.Address, data []byte, pending bool) ([]byte, error)
  81. // PendingAccountNonce retrieves the current pending nonce associated with an
  82. // account.
  83. PendingAccountNonce(ctx context.Context, account common.Address) (uint64, error)
  84. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  85. // execution of a transaction.
  86. SuggestGasPrice(ctx context.Context) (*big.Int, error)
  87. // EstimateGasLimit tries to estimate the gas needed to execute a specific
  88. // transaction based on the current pending state of the backend blockchain.
  89. // There is no guarantee that this is the true gas limit requirement as other
  90. // transactions may be added or removed by miners, but it should provide a basis
  91. // for setting a reasonable default.
  92. EstimateGasLimit(ctx context.Context, sender common.Address, contract *common.Address, value *big.Int, data []byte) (*big.Int, error)
  93. // SendTransaction injects the transaction into the pending pool for execution.
  94. SendTransaction(ctx context.Context, tx *types.Transaction) error
  95. }