bind.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. // Contains all the wrappers from the bind package.
  17. package geth
  18. import (
  19. "math/big"
  20. "strings"
  21. "github.com/ethereum/go-ethereum/accounts/abi"
  22. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. )
  26. // Signer is an interaface defining the callback when a contract requires a
  27. // method to sign the transaction before submission.
  28. type Signer interface {
  29. Sign(*Address, *Transaction) (tx *Transaction, _ error)
  30. }
  31. type signer struct {
  32. sign bind.SignerFn
  33. }
  34. func (s *signer) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) {
  35. sig, err := s.sign(types.HomesteadSigner{}, addr.address, unsignedTx.tx)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &Transaction{sig}, nil
  40. }
  41. // CallOpts is the collection of options to fine tune a contract call request.
  42. type CallOpts struct {
  43. opts bind.CallOpts
  44. }
  45. // NewCallOpts creates a new option set for contract calls.
  46. func NewCallOpts() *CallOpts {
  47. return new(CallOpts)
  48. }
  49. func (opts *CallOpts) IsPending() bool { return opts.opts.Pending }
  50. func (opts *CallOpts) GetGasLimit() int64 { return 0 /* TODO(karalabe) */ }
  51. // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  52. // Even then it's awkward to unpack the subtleties of a Go context out to Java.
  53. // func (opts *CallOpts) GetContext() *Context { return &Context{opts.opts.Context} }
  54. func (opts *CallOpts) SetPending(pending bool) { opts.opts.Pending = pending }
  55. func (opts *CallOpts) SetGasLimit(limit int64) { /* TODO(karalabe) */ }
  56. func (opts *CallOpts) SetContext(context *Context) { opts.opts.Context = context.context }
  57. // TransactOpts is the collection of authorization data required to create a
  58. // valid Ethereum transaction.
  59. type TransactOpts struct {
  60. opts bind.TransactOpts
  61. }
  62. func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
  63. func (opts *TransactOpts) GetNonce() int64 { return opts.opts.Nonce.Int64() }
  64. func (opts *TransactOpts) GetValue() *BigInt { return &BigInt{opts.opts.Value} }
  65. func (opts *TransactOpts) GetGasPrice() *BigInt { return &BigInt{opts.opts.GasPrice} }
  66. func (opts *TransactOpts) GetGasLimit() int64 { return opts.opts.GasLimit.Int64() }
  67. // GetSigner cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  68. // func (opts *TransactOpts) GetSigner() Signer { return &signer{opts.opts.Signer} }
  69. // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  70. // Even then it's awkward to unpack the subtleties of a Go context out to Java.
  71. //func (opts *TransactOpts) GetContext() *Context { return &Context{opts.opts.Context} }
  72. func (opts *TransactOpts) SetFrom(from *Address) { opts.opts.From = from.address }
  73. func (opts *TransactOpts) SetNonce(nonce int64) { opts.opts.Nonce = big.NewInt(nonce) }
  74. func (opts *TransactOpts) SetSigner(s Signer) {
  75. opts.opts.Signer = func(signer types.Signer, addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
  76. sig, err := s.Sign(&Address{addr}, &Transaction{tx})
  77. if err != nil {
  78. return nil, err
  79. }
  80. return sig.tx, nil
  81. }
  82. }
  83. func (opts *TransactOpts) SetValue(value *BigInt) { opts.opts.Value = value.bigint }
  84. func (opts *TransactOpts) SetGasPrice(price *BigInt) { opts.opts.GasPrice = price.bigint }
  85. func (opts *TransactOpts) SetGasLimit(limit int64) { opts.opts.GasLimit = big.NewInt(limit) }
  86. func (opts *TransactOpts) SetContext(context *Context) { opts.opts.Context = context.context }
  87. // BoundContract is the base wrapper object that reflects a contract on the
  88. // Ethereum network. It contains a collection of methods that are used by the
  89. // higher level contract bindings to operate.
  90. type BoundContract struct {
  91. contract *bind.BoundContract
  92. address common.Address
  93. deployer *types.Transaction
  94. }
  95. // DeployContract deploys a contract onto the Ethereum blockchain and binds the
  96. // deployment address with a wrapper.
  97. func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (contract *BoundContract, _ error) {
  98. // Convert all the deployment parameters to Go types
  99. params := make([]interface{}, len(args.objects))
  100. for i, obj := range args.objects {
  101. params[i] = obj
  102. }
  103. // Deploy the contract to the network
  104. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  105. if err != nil {
  106. return nil, err
  107. }
  108. addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, bytecode, client.client, params...)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return &BoundContract{
  113. contract: bound,
  114. address: addr,
  115. deployer: tx,
  116. }, nil
  117. }
  118. // BindContract creates a low level contract interface through which calls and
  119. // transactions may be made through.
  120. func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) {
  121. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  122. if err != nil {
  123. return nil, err
  124. }
  125. return &BoundContract{
  126. contract: bind.NewBoundContract(address.address, parsed, client.client, client.client),
  127. address: address.address,
  128. }, nil
  129. }
  130. func (c *BoundContract) GetAddress() *Address { return &Address{c.address} }
  131. func (c *BoundContract) GetDeployer() *Transaction {
  132. if c.deployer == nil {
  133. return nil
  134. }
  135. return &Transaction{c.deployer}
  136. }
  137. // Call invokes the (constant) contract method with params as input values and
  138. // sets the output to result.
  139. func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error {
  140. // Convert all the input and output parameters to Go types
  141. params := make([]interface{}, len(args.objects))
  142. for i, obj := range args.objects {
  143. params[i] = obj
  144. }
  145. results := make([]interface{}, len(out.objects))
  146. for i, obj := range out.objects {
  147. results[i] = obj
  148. }
  149. // Execute the call to the contract and wrap any results
  150. if err := c.contract.Call(&opts.opts, &results, method, params...); err != nil {
  151. return err
  152. }
  153. for i, res := range results {
  154. out.objects[i] = res
  155. }
  156. return nil
  157. }
  158. // Transact invokes the (paid) contract method with params as input values.
  159. func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
  160. params := make([]interface{}, len(args.objects))
  161. for i, obj := range args.objects {
  162. params[i] = obj
  163. }
  164. rawTx, err := c.contract.Transact(&opts.opts, method, params)
  165. if err != nil {
  166. return nil, err
  167. }
  168. return &Transaction{rawTx}, nil
  169. }
  170. // Transfer initiates a plain transaction to move funds to the contract, calling
  171. // its default method if one is available.
  172. func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) {
  173. rawTx, err := c.contract.Transfer(&opts.opts)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return &Transaction{rawTx}, nil
  178. }