backend.go 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // This error 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. // This error is returned by WaitDeployed if contract creation leaves an
  34. // empty contract behind.
  35. ErrNoCodeAfterDeploy = errors.New("no contract code after deployment")
  36. )
  37. // ContractCaller defines the methods needed to allow operating with 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. // ContractCall 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. // DeployBackend wraps the operations needed by WaitMined and WaitDeployed.
  48. type DeployBackend interface {
  49. TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
  50. CodeAt(ctx context.Context, account common.Address, blockNumber *big.Int) ([]byte, error)
  51. }
  52. // PendingContractCaller defines methods to perform contract calls on the pending state.
  53. // Call will try to discover this interface when access to the pending state is requested.
  54. // If the backend does not support the pending state, Call returns ErrNoPendingState.
  55. type PendingContractCaller interface {
  56. // PendingCodeAt returns the code of the given account in the pending state.
  57. PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error)
  58. // PendingCallContract executes an Ethereum contract call against the pending state.
  59. PendingCallContract(ctx context.Context, call ethereum.CallMsg) ([]byte, error)
  60. }
  61. // ContractTransactor defines the methods needed to allow operating with contract
  62. // on a write only basis. Beside the transacting method, the remainder are helpers
  63. // used when the user does not provide some needed values, but rather leaves it up
  64. // to the transactor to decide.
  65. type ContractTransactor interface {
  66. // PendingCodeAt returns the code of the given account in the pending state.
  67. PendingCodeAt(ctx context.Context, account common.Address) ([]byte, error)
  68. // PendingNonceAt retrieves the current pending nonce associated with an account.
  69. PendingNonceAt(ctx context.Context, account common.Address) (uint64, error)
  70. // SuggestGasPrice retrieves the currently suggested gas price to allow a timely
  71. // execution of a transaction.
  72. SuggestGasPrice(ctx context.Context) (*big.Int, error)
  73. // EstimateGas tries to estimate the gas needed to execute a specific
  74. // transaction based on the current pending state of the backend blockchain.
  75. // There is no guarantee that this is the true gas limit requirement as other
  76. // transactions may be added or removed by miners, but it should provide a basis
  77. // for setting a reasonable default.
  78. EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error)
  79. // SendTransaction injects the transaction into the pending pool for execution.
  80. SendTransaction(ctx context.Context, tx *types.Transaction) error
  81. }
  82. // ContractBackend defines the methods needed to work with contracts on a read-write basis.
  83. type ContractBackend interface {
  84. ContractCaller
  85. ContractTransactor
  86. }