github.com/energicryptocurrency/go-energi@v1.1.7/mobile/bind.go (about)

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