bind.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "math/big"
  19. "github.com/ethereum/go-ethereum"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/hexutil"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/internal/ethapi"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. "github.com/ethereum/go-ethereum/rpc"
  26. "golang.org/x/net/context"
  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. // Etheruem 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),
  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. out, err := b.bcapi.GetCode(ctx, contract, toBlockNumber(blockNum))
  52. return common.FromHex(out), err
  53. }
  54. // CodeAt retrieves any code associated with the contract from the local API.
  55. func (b *ContractBackend) PendingCodeAt(ctx context.Context, contract common.Address) ([]byte, error) {
  56. out, err := b.bcapi.GetCode(ctx, contract, rpc.PendingBlockNumber)
  57. return common.FromHex(out), err
  58. }
  59. // ContractCall implements bind.ContractCaller executing an Ethereum contract
  60. // call with the specified data as the input. The pending flag requests execution
  61. // against the pending block, not the stable head of the chain.
  62. func (b *ContractBackend) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNum *big.Int) ([]byte, error) {
  63. out, err := b.bcapi.Call(ctx, toCallArgs(msg), toBlockNumber(blockNum))
  64. return out, err
  65. }
  66. // ContractCall implements bind.ContractCaller executing an Ethereum contract
  67. // call with the specified data as the input. The pending flag requests execution
  68. // against the pending block, not the stable head of the chain.
  69. func (b *ContractBackend) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
  70. out, err := b.bcapi.Call(ctx, toCallArgs(msg), rpc.PendingBlockNumber)
  71. return out, err
  72. }
  73. func toCallArgs(msg ethereum.CallMsg) ethapi.CallArgs {
  74. args := ethapi.CallArgs{
  75. To: msg.To,
  76. From: msg.From,
  77. Data: msg.Data,
  78. }
  79. if msg.Gas != nil {
  80. args.Gas = hexutil.Big(*msg.Gas)
  81. }
  82. if msg.GasPrice != nil {
  83. args.GasPrice = hexutil.Big(*msg.GasPrice)
  84. }
  85. if msg.Value != nil {
  86. args.Value = hexutil.Big(*msg.Value)
  87. }
  88. return args
  89. }
  90. func toBlockNumber(num *big.Int) rpc.BlockNumber {
  91. if num == nil {
  92. return rpc.LatestBlockNumber
  93. }
  94. return rpc.BlockNumber(num.Int64())
  95. }
  96. // PendingAccountNonce implements bind.ContractTransactor retrieving the current
  97. // pending nonce associated with an account.
  98. func (b *ContractBackend) PendingNonceAt(ctx context.Context, account common.Address) (nonce uint64, err error) {
  99. out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber)
  100. if out != nil {
  101. nonce = uint64(*out)
  102. }
  103. return nonce, err
  104. }
  105. // SuggestGasPrice implements bind.ContractTransactor retrieving the currently
  106. // suggested gas price to allow a timely execution of a transaction.
  107. func (b *ContractBackend) SuggestGasPrice(ctx context.Context) (*big.Int, error) {
  108. return b.eapi.GasPrice(ctx)
  109. }
  110. // EstimateGasLimit implements bind.ContractTransactor triing to estimate the gas
  111. // needed to execute a specific transaction based on the current pending state of
  112. // the backend blockchain. There is no guarantee that this is the true gas limit
  113. // requirement as other transactions may be added or removed by miners, but it
  114. // should provide a basis for setting a reasonable default.
  115. func (b *ContractBackend) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (*big.Int, error) {
  116. out, err := b.bcapi.EstimateGas(ctx, toCallArgs(msg))
  117. return out.ToInt(), err
  118. }
  119. // SendTransaction implements bind.ContractTransactor injects the transaction
  120. // into the pending pool for execution.
  121. func (b *ContractBackend) SendTransaction(ctx context.Context, tx *types.Transaction) error {
  122. raw, _ := rlp.EncodeToBytes(tx)
  123. _, err := b.txapi.SendRawTransaction(ctx, raw)
  124. return err
  125. }