bind.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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(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, chainID *big.Int) (*TransactOpts, error) {
  71. key, err := keystore.DecryptKey(keyJson, passphrase)
  72. if err != nil {
  73. return nil, err
  74. }
  75. auth, err := bind.NewKeyedTransactorWithChainID(key.PrivateKey, chainID)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return &TransactOpts{*auth}, nil
  80. }
  81. func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
  82. func (opts *TransactOpts) GetNonce() int64 { return opts.opts.Nonce.Int64() }
  83. func (opts *TransactOpts) GetValue() *BigInt { return &BigInt{opts.opts.Value} }
  84. func (opts *TransactOpts) GetGasPrice() *BigInt { return &BigInt{opts.opts.GasPrice} }
  85. func (opts *TransactOpts) GetGasLimit() int64 { return int64(opts.opts.GasLimit) }
  86. // GetSigner cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  87. // func (opts *TransactOpts) GetSigner() Signer { return &signer{opts.opts.Signer} }
  88. // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
  89. // Even then it's awkward to unpack the subtleties of a Go context out to Java.
  90. //func (opts *TransactOpts) GetContext() *Context { return &Context{opts.opts.Context} }
  91. func (opts *TransactOpts) SetFrom(from *Address) { opts.opts.From = from.address }
  92. func (opts *TransactOpts) SetNonce(nonce int64) { opts.opts.Nonce = big.NewInt(nonce) }
  93. func (opts *TransactOpts) SetSigner(s Signer) {
  94. opts.opts.Signer = func(addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
  95. sig, err := s.Sign(&Address{addr}, &Transaction{tx})
  96. if err != nil {
  97. return nil, err
  98. }
  99. return sig.tx, nil
  100. }
  101. }
  102. func (opts *TransactOpts) SetValue(value *BigInt) { opts.opts.Value = value.bigint }
  103. func (opts *TransactOpts) SetGasPrice(price *BigInt) { opts.opts.GasPrice = price.bigint }
  104. func (opts *TransactOpts) SetGasLimit(limit int64) { opts.opts.GasLimit = uint64(limit) }
  105. func (opts *TransactOpts) SetContext(context *Context) { opts.opts.Context = context.context }
  106. // BoundContract is the base wrapper object that reflects a contract on the
  107. // Ethereum network. It contains a collection of methods that are used by the
  108. // higher level contract bindings to operate.
  109. type BoundContract struct {
  110. contract *bind.BoundContract
  111. address common.Address
  112. deployer *types.Transaction
  113. }
  114. // DeployContract deploys a contract onto the Ethereum blockchain and binds the
  115. // deployment address with a wrapper.
  116. func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (contract *BoundContract, _ error) {
  117. // Deploy the contract to the network
  118. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  119. if err != nil {
  120. return nil, err
  121. }
  122. addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return &BoundContract{
  127. contract: bound,
  128. address: addr,
  129. deployer: tx,
  130. }, nil
  131. }
  132. // BindContract creates a low level contract interface through which calls and
  133. // transactions may be made through.
  134. func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) {
  135. parsed, err := abi.JSON(strings.NewReader(abiJSON))
  136. if err != nil {
  137. return nil, err
  138. }
  139. return &BoundContract{
  140. contract: bind.NewBoundContract(address.address, parsed, client.client, client.client, client.client),
  141. address: address.address,
  142. }, nil
  143. }
  144. func (c *BoundContract) GetAddress() *Address { return &Address{c.address} }
  145. func (c *BoundContract) GetDeployer() *Transaction {
  146. if c.deployer == nil {
  147. return nil
  148. }
  149. return &Transaction{c.deployer}
  150. }
  151. // Call invokes the (constant) contract method with params as input values and
  152. // sets the output to result.
  153. func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error {
  154. results := make([]interface{}, len(out.objects))
  155. copy(results, out.objects)
  156. if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil {
  157. return err
  158. }
  159. copy(out.objects, results)
  160. return nil
  161. }
  162. // Transact invokes the (paid) contract method with params as input values.
  163. func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
  164. rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...)
  165. if err != nil {
  166. return nil, err
  167. }
  168. return &Transaction{rawTx}, nil
  169. }
  170. // RawTransact invokes the (paid) contract method with raw calldata as input values.
  171. func (c *BoundContract) RawTransact(opts *TransactOpts, calldata []byte) (tx *Transaction, _ error) {
  172. rawTx, err := c.contract.RawTransact(&opts.opts, calldata)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return &Transaction{rawTx}, nil
  177. }
  178. // Transfer initiates a plain transaction to move funds to the contract, calling
  179. // its default method if one is available.
  180. func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) {
  181. rawTx, err := c.contract.Transfer(&opts.opts)
  182. if err != nil {
  183. return nil, err
  184. }
  185. return &Transaction{rawTx}, nil
  186. }