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. "math/big"
  19. "github.com/ethereum/go-ethereum"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/internal/ethapi"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. "github.com/ethereum/go-ethereum/rpc"
  25. "golang.org/x/net/context"
  26. )
  27. // ContractBackend implements bind.ContractBackend with direct calls to Ethereum
  28. // internals to support operating on contracts within subprotocols like eth and
  29. // swarm.
  30. //
  31. // Internally this backend uses the already exposed API endpoints of the Ethereum
  32. // object. These should be rewritten to internal Go method calls when the Go API
  33. // is refactored to support a clean library use.
  34. type ContractBackend struct {
  35. eapi *ethapi.PublicEthereumAPI // Wrapper around the Ethereum object to access metadata
  36. bcapi *ethapi.PublicBlockChainAPI // Wrapper around the blockchain to access chain data
  37. txapi *ethapi.PublicTransactionPoolAPI // Wrapper around the transaction pool to access transaction data
  38. }
  39. // NewContractBackend creates a new native contract backend using an existing
  40. // Etheruem object.
  41. func NewContractBackend(apiBackend ethapi.Backend) *ContractBackend {
  42. return &ContractBackend{
  43. eapi: ethapi.NewPublicEthereumAPI(apiBackend),
  44. bcapi: ethapi.NewPublicBlockChainAPI(apiBackend),
  45. txapi: ethapi.NewPublicTransactionPoolAPI(apiBackend),
  46. }
  47. }
  48. // CodeAt retrieves any code associated with the contract from the local API.
  49. func (b *ContractBackend) CodeAt(ctx context.Context, contract common.Address, blockNum *big.Int) ([]byte, error) {
  50. out, err := b.bcapi.GetCode(ctx, contract, toBlockNumber(blockNum))
  51. return common.FromHex(out), err
  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. out, err := b.bcapi.GetCode(ctx, contract, rpc.PendingBlockNumber)
  56. return common.FromHex(out), err
  57. }
  58. // ContractCall implements bind.ContractCaller executing an Ethereum contract
  59. // call with the specified data as the input. The pending flag requests execution
  60. // against the pending block, not the stable head of the chain.
  61. func (b *ContractBackend) CallContract(ctx context.Context, msg ethereum.CallMsg, blockNum *big.Int) ([]byte, error) {
  62. out, err := b.bcapi.Call(ctx, toCallArgs(msg), toBlockNumber(blockNum))
  63. return common.FromHex(out), err
  64. }
  65. // ContractCall implements bind.ContractCaller executing an Ethereum contract
  66. // call with the specified data as the input. The pending flag requests execution
  67. // against the pending block, not the stable head of the chain.
  68. func (b *ContractBackend) PendingCallContract(ctx context.Context, msg ethereum.CallMsg) ([]byte, error) {
  69. out, err := b.bcapi.Call(ctx, toCallArgs(msg), rpc.PendingBlockNumber)
  70. return common.FromHex(out), err
  71. }
  72. func toCallArgs(msg ethereum.CallMsg) ethapi.CallArgs {
  73. args := ethapi.CallArgs{
  74. To: msg.To,
  75. From: msg.From,
  76. Data: common.ToHex(msg.Data),
  77. }
  78. if msg.Gas != nil {
  79. args.Gas = *rpc.NewHexNumber(msg.Gas)
  80. }
  81. if msg.GasPrice != nil {
  82. args.GasPrice = *rpc.NewHexNumber(msg.GasPrice)
  83. }
  84. if msg.Value != nil {
  85. args.Value = *rpc.NewHexNumber(msg.Value)
  86. }
  87. return args
  88. }
  89. func toBlockNumber(num *big.Int) rpc.BlockNumber {
  90. if num == nil {
  91. return rpc.LatestBlockNumber
  92. }
  93. return rpc.BlockNumber(num.Int64())
  94. }
  95. // PendingAccountNonce implements bind.ContractTransactor retrieving the current
  96. // pending nonce associated with an account.
  97. func (b *ContractBackend) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) {
  98. out, err := b.txapi.GetTransactionCount(ctx, account, rpc.PendingBlockNumber)
  99. return out.Uint64(), 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) (*big.Int, error) {
  112. out, err := b.bcapi.EstimateGas(ctx, toCallArgs(msg))
  113. return out.BigInt(), 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, common.ToHex(raw))
  120. return err
  121. }