bind.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 eth
  17. import (
  18. "context"
  19. "math/big"
  20. "github.com/ethereum/go-ethereum"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/hexutil"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/internal/ethapi"
  25. "github.com/ethereum/go-ethereum/rlp"
  26. "github.com/ethereum/go-ethereum/rpc"
  27. )
  28. // ContractBackend implements bind.ContractBackend with direct calls to Ethereum
  29. // internals to support operating on contracts within subprotocols like eth and
  30. // swarm.
  31. //
  32. // Internally this backend uses the already exposed API endpoints of the Ethereum
  33. // object. These should be rewritten to internal Go method calls when the Go API
  34. // is refactored to support a clean library use.
  35. type ContractBackend struct {
  36. eapi *ethapi.PublicEthereumAPI // Wrapper around the Ethereum object to access metadata
  37. bcapi *ethapi.PublicBlockChainAPI // Wrapper around the blockchain to access chain data
  38. txapi *ethapi.PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data
  39. }
  40. // NewContractBackend creates a new native contract backend using an existing
  41. // Ethereum object.
  42. func NewContractBackend(apiBackend ethapi.Backend) *ContractBackend {
  43. return &ContractBackend{
  44. eapi: ethapi.NewPublicEthereumAPI(apiBackend),
  45. bcapi: ethapi.NewPublicBlockChainAPI(apiBackend),
  46. txapi: ethapi.NewPublicTransactionPoolAPI(apiBackend, new(ethapi.AddrLocker)),
  47. }
  48. }
  49. // CodeAt retrieves any code associated with the contract from the local API.
  50. func (b *ContractBackend) CodeAt(ctx context.Context, contract common.Address, blockNum *big.Int) ([]byte, error) {
  51. return b.bcapi.GetCode(ctx, contract, toBlockNumber(blockNum))
  52. }
  53. // CodeAt retrieves any code associated with the contract from the local API.
  54. func (b *ContractBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
  55. return b.bcapi.GetCode(ctx, contract, rpc.PendingBlockNumber)
  56. }
  57. // ContractCall implements bind.ContractCaller executing an Ethereum contract
  58. // call with the specified data as the input. The pending flag requests execution
  59. // against the pending block, not the stable head of the chain.
  60. func (b *ContractBackend) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNum *big.Int) ([]byte, error) {
  61. out, err := b.bcapi.Call(ctx, toCallArgs(msg), toBlockNumber(blockNum))
  62. return out, err
  63. }
  64. // ContractCall implements bind.ContractCaller executing an Ethereum contract
  65. // call with the specified data as the input. The pending flag requests execution
  66. // against the pending block, not the stable head of the chain.
  67. func (b *ContractBackend) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
  68. out, err := b.bcapi.Call(ctx, toCallArgs(msg), rpc.PendingBlockNumber)
  69. return out, err
  70. }
  71. func toCallArgs(msg ethereum.CallMsg) ethapi.CallArgs {
  72. args := ethapi.CallArgs{
  73. To: msg.To,
  74. From: msg.From,
  75. Data: msg.Data,
  76. Gas: hexutil.Uint64(msg.Gas),
  77. }
  78. if msg.GasPrice != nil {
  79. args.GasPrice = hexutil.Big(*msg.GasPrice)
  80. }
  81. if msg.Value != nil {
  82. args.Value = hexutil.Big(*msg.Value)
  83. }
  84. return args
  85. }
  86. func toBlockNumber(num *big.Int) rpc.BlockNumber {
  87. if num == nil {
  88. return rpc.LatestBlockNumber
  89. }
  90. return rpc.BlockNumber(num.Int64())
  91. }
  92. // PendingAccountNonce implements bind.ContractTransactor retrieving the current
  93. // pending nonce associated with an account.
  94. func (b *ContractBackend) PendingNonceAt(ctx context.Context, account common.Address) (nonce uint64, err error) {
  95. out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber)
  96. if out != nil {
  97. nonce = uint64(*out)
  98. }
  99. return nonce, err
  100. }
  101. // SuggestGasPrice implements bind.ContractTransactor retrieving the currently
  102. // suggested gas price to allow a timely execution of a transaction.
  103. func (b *ContractBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
  104. return b.eapi.GasPrice(ctx)
  105. }
  106. // EstimateGasLimit implements bind.ContractTransactor triing to estimate the gas
  107. // needed to execute a specific transaction based on the current pending state of
  108. // the backend blockchain. There is no guarantee that this is the true gas limit
  109. // requirement as other transactions may be added or removed by miners, but it
  110. // should provide a basis for setting a reasonable default.
  111. func (b *ContractBackend) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) {
  112. gas, err := b.bcapi.EstimateGas(ctx, toCallArgs(msg))
  113. return uint64(gas), err
  114. }
  115. // SendTransaction implements bind.ContractTransactor injects the transaction
  116. // into the pending pool for execution.
  117. func (b *ContractBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
  118. raw, _ := rlp.EncodeToBytes(tx)
  119. _, err := b.txapi.SendRawTransaction(ctx, raw)
  120. return err
  121. }