github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/mobile/bind.go (about)

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