base.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2016 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. "errors"
  19. "fmt"
  20. "math/big"
  21. "github.com/ethereum/go-ethereum/accounts/abi"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. )
  26. // SignerFn is a signer function callback when a contract requires a method to
  27. // sign the transaction before submission.
  28. type SignerFn func(common.Address, *types.Transaction) (*types.Transaction, error)
  29. // CallOpts is the collection of options to fine tune a contract call request.
  30. type CallOpts struct {
  31. Pending bool // Whether to operate on the pending state or the last known one
  32. }
  33. // TransactOpts is the collection of authorization data required to create a
  34. // valid Ethereum transaction.
  35. type TransactOpts struct {
  36. From common.Address // Ethereum account to send the transaction from
  37. Nonce *big.Int // Nonce to use for the transaction execution (nil = use pending state)
  38. Signer SignerFn // Method to use for signing the transaction (mandatory)
  39. Value *big.Int // Funds to transfer along along the transaction (nil = 0 = no funds)
  40. GasPrice *big.Int // Gas price to use for the transaction execution (nil = gas price oracle)
  41. GasLimit *big.Int // Gas limit to set for the transaction execution (nil = estimate + 10%)
  42. }
  43. // BoundContract is the base wrapper object that reflects a contract on the
  44. // Ethereum network. It contains a collection of methods that are used by the
  45. // higher level contract bindings to operate.
  46. type BoundContract struct {
  47. address common.Address // Deployment address of the contract on the Ethereum blockchain
  48. abi abi.ABI // Reflect based ABI to access the correct Ethereum methods
  49. caller ContractCaller // Read interface to interact with the blockchain
  50. transactor ContractTransactor // Write interface to interact with the blockchain
  51. }
  52. // NewBoundContract creates a low level contract interface through which calls
  53. // and transactions may be made through.
  54. func NewBoundContract(address common.Address, abi abi.ABI, caller ContractCaller, transactor ContractTransactor) *BoundContract {
  55. return &BoundContract{
  56. address: address,
  57. abi: abi,
  58. caller: caller,
  59. transactor: transactor,
  60. }
  61. }
  62. // DeployContract deploys a contract onto the Ethereum blockchain and binds the
  63. // deployment address with a Go wrapper.
  64. func DeployContract(opts *TransactOpts, abi abi.ABI, bytecode []byte, backend ContractBackend, params ...interface{}) (common.Address, *types.Transaction, *BoundContract, error) {
  65. // Otherwise try to deploy the contract
  66. c := NewBoundContract(common.Address{}, abi, backend, backend)
  67. input, err := c.abi.Pack("", params...)
  68. if err != nil {
  69. return common.Address{}, nil, nil, err
  70. }
  71. tx, err := c.transact(opts, nil, append(bytecode, input...))
  72. if err != nil {
  73. return common.Address{}, nil, nil, err
  74. }
  75. c.address = crypto.CreateAddress(opts.From, tx.Nonce())
  76. return c.address, tx, c, nil
  77. }
  78. // Call invokes the (constant) contract method with params as input values and
  79. // sets the output to result. The result type might be a single field for simple
  80. // returns, a slice of interfaces for anonymous returns and a struct for named
  81. // returns.
  82. func (c *BoundContract) Call(opts *CallOpts, result interface{}, method string, params ...interface{}) error {
  83. // Don't crash on a lazy user
  84. if opts == nil {
  85. opts = new(CallOpts)
  86. }
  87. // Pack the input, call and unpack the results
  88. input, err := c.abi.Pack(method, params...)
  89. if err != nil {
  90. return err
  91. }
  92. output, err := c.caller.ContractCall(c.address, input, opts.Pending)
  93. if err != nil {
  94. return err
  95. }
  96. return c.abi.Unpack(result, method, output)
  97. }
  98. // Transact invokes the (paid) contract method with params as input values.
  99. func (c *BoundContract) Transact(opts *TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
  100. // Otherwise pack up the parameters and invoke the contract
  101. input, err := c.abi.Pack(method, params...)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return c.transact(opts, &c.address, input)
  106. }
  107. // Transfer initiates a plain transaction to move funds to the contract, calling
  108. // its default method if one is available.
  109. func (c *BoundContract) Transfer(opts *TransactOpts) (*types.Transaction, error) {
  110. return c.transact(opts, &c.address, nil)
  111. }
  112. // transact executes an actual transaction invocation, first deriving any missing
  113. // authorization fields, and then scheduling the transaction for execution.
  114. func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error) {
  115. var err error
  116. // Ensure a valid value field and resolve the account nonce
  117. value := opts.Value
  118. if value == nil {
  119. value = new(big.Int)
  120. }
  121. nonce := uint64(0)
  122. if opts.Nonce == nil {
  123. nonce, err = c.transactor.PendingAccountNonce(opts.From)
  124. if err != nil {
  125. return nil, fmt.Errorf("failed to retrieve account nonce: %v", err)
  126. }
  127. } else {
  128. nonce = opts.Nonce.Uint64()
  129. }
  130. // Figure out the gas allowance and gas price values
  131. gasPrice := opts.GasPrice
  132. if gasPrice == nil {
  133. gasPrice, err = c.transactor.SuggestGasPrice()
  134. if err != nil {
  135. return nil, fmt.Errorf("failed to suggest gas price: %v", err)
  136. }
  137. }
  138. gasLimit := opts.GasLimit
  139. if gasLimit == nil {
  140. gasLimit, err = c.transactor.EstimateGasLimit(opts.From, contract, value, input)
  141. if err != nil {
  142. return nil, fmt.Errorf("failed to exstimate gas needed: %v", err)
  143. }
  144. }
  145. // Create the transaction, sign it and schedule it for execution
  146. var rawTx *types.Transaction
  147. if contract == nil {
  148. rawTx = types.NewContractCreation(nonce, value, gasLimit, gasPrice, input)
  149. } else {
  150. rawTx = types.NewTransaction(nonce, c.address, value, gasLimit, gasPrice, input)
  151. }
  152. if opts.Signer == nil {
  153. return nil, errors.New("no signer to authorize the transaction with")
  154. }
  155. signedTx, err := opts.Signer(opts.From, rawTx)
  156. if err != nil {
  157. return nil, err
  158. }
  159. if err := c.transactor.SendTransaction(signedTx); err != nil {
  160. return nil, err
  161. }
  162. return signedTx, nil
  163. }