bind.go 8.2 KB

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