bind.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 int64(opts.opts.GasLimit) }
  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 = uint64(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. // Deploy the contract to the network
  99. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  100. if err != nil {
  101. return nil, err
  102. }
  103. addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return &BoundContract{
  108. contract: bound,
  109. address: addr,
  110. deployer: tx,
  111. }, nil
  112. }
  113. // BindContract creates a low level contract interface through which calls and
  114. // transactions may be made through.
  115. func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) {
  116. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  117. if err != nil {
  118. return nil, err
  119. }
  120. return &BoundContract{
  121. contract: bind.NewBoundContract(address.address, parsed, client.client, client.client, client.client),
  122. address: address.address,
  123. }, nil
  124. }
  125. func (c *BoundContract) GetAddress() *Address { return &Address{c.address} }
  126. func (c *BoundContract) GetDeployer() *Transaction {
  127. if c.deployer == nil {
  128. return nil
  129. }
  130. return &Transaction{c.deployer}
  131. }
  132. // Call invokes the (constant) contract method with params as input values and
  133. // sets the output to result.
  134. func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error {
  135. if len(out.objects) == 1 {
  136. result := out.objects[0]
  137. if err := c.contract.Call(&opts.opts, result, method, args.objects...); err != nil {
  138. return err
  139. }
  140. out.objects[0] = result
  141. } else {
  142. results := make([]interface{}, len(out.objects))
  143. copy(results, out.objects)
  144. if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil {
  145. return err
  146. }
  147. copy(out.objects, results)
  148. }
  149. return nil
  150. }
  151. // Transact invokes the (paid) contract method with params as input values.
  152. func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
  153. rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...)
  154. if err != nil {
  155. return nil, err
  156. }
  157. return &Transaction{rawTx}, nil
  158. }
  159. // Transfer initiates a plain transaction to move funds to the contract, calling
  160. // its default method if one is available.
  161. func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) {
  162. rawTx, err := c.contract.Transfer(&opts.opts)
  163. if err != nil {
  164. return nil, err
  165. }
  166. return &Transaction{rawTx}, nil
  167. }