backend.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 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. "context"
  19. "errors"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. )
  25. var (
  26. // ErrNoCode is returned by call and transact operations for which the requested
  27. // recipient contract to operate on does not exist in the state db or does not
  28. // have any code associated with it (i.e. suicided).
  29. ErrNoCode = errors.New("no contract code at given address")
  30. // ErrNoPendingState is raised when attempting to perform a pending state action
  31. // on a backend that doesn't implement PendingContractCaller.
  32. ErrNoPendingState = errors.New("backend does not support pending state")
  33. // ErrNoCodeAfterDeploy is returned by WaitDeployed if contract creation leaves
  34. // an empty contract behind.
  35. ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
  36. )
  37. // ContractCaller defines the methods needed to allow operating with a contract on a read
  38. // only basis.
  39. type ContractCaller interface {
  40. // CodeAt returns the code of the given account. This is needed to differentiate
  41. // between contract internal errors and the local chain being out of sync.
  42. CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error)
  43. // CallContract executes an Ethereum contract call with the specified data as the
  44. // input.
  45. CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error)
  46. }
  47. // PendingContractCaller defines methods to perform contract calls on the pending state.
  48. // Call will try to discover this interface when access to the pending state is requested.
  49. // If the backend does not support the pending state, Call returns ErrNoPendingState.
  50. type PendingContractCaller interface {
  51. // PendingCodeAt returns the code of the given account in the pending state.
  52. PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error)
  53. // PendingCallContract executes an Ethereum contract call against the pending state.
  54. PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
  55. }
  56. // ContractTransactor defines the methods needed to allow operating with a contract
  57. // on a write only basis. Besides the transacting method, the remainder are helpers
  58. // used when the user does not provide some needed values, but rather leaves it up
  59. // to the transactor to decide.
  60. type ContractTransactor interface {
  61. // HeaderByNumber returns a block header from the current canonical chain. If
  62. // number is nil, the latest known header is returned.
  63. HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
  64. // PendingCodeAt returns the code of the given account in the pending state.
  65. PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
  66. // PendingNonceAt retrieves the current pending nonce associated with an account.
  67. PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
  68. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  69. // execution of a transaction.
  70. SuggestGasPrice(ctx context.Context) (*big.Int, error)
  71. // SuggestGasTipCap retrieves the currently suggested 1559 priority fee to allow
  72. // a timely execution of a transaction.
  73. SuggestGasTipCap(ctx context.Context) (*big.Int, error)
  74. // EstimateGas tries to estimate the gas needed to execute a specific
  75. // transaction based on the current pending state of the backend blockchain.
  76. // There is no guarantee that this is the true gas limit requirement as other
  77. // transactions may be added or removed by miners, but it should provide a basis
  78. // for setting a reasonable default.
  79. EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
  80. // SendTransaction injects the transaction into the pending pool for execution.
  81. SendTransaction(ctx context.Context, tx *types.Transaction) error
  82. }
  83. // ContractFilterer defines the methods needed to access log events using one-off
  84. // queries or continuous event subscriptions.
  85. type ContractFilterer interface {
  86. // FilterLogs executes a log filter operation, blocking during execution and
  87. // returning all the results in one batch.
  88. //
  89. // TODO(karalabe): Deprecate when the subscription one can return past data too.
  90. FilterLogs(ctx context.Context, query ethereum.FilterQuery) ([]types.Log, error)
  91. // SubscribeFilterLogs creates a background log filtering operation, returning
  92. // a subscription immediately, which can be used to stream the found events.
  93. SubscribeFilterLogs(ctx context.Context, query ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
  94. }
  95. // DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
  96. type DeployBackend interface {
  97. TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
  98. CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
  99. }
  100. // ContractBackend defines the methods needed to work with contracts on a read-write basis.
  101. type ContractBackend interface {
  102. ContractCaller
  103. ContractTransactor
  104. ContractFilterer
  105. }