github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/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 gvnt
    20  
    21  import (
    22  	"math/big"
    23  	"strings"
    24  
    25  	"github.com/vntchain/go-vnt/accounts/abi"
    26  	"github.com/vntchain/go-vnt/accounts/abi/bind"
    27  	"github.com/vntchain/go-vnt/common"
    28  	"github.com/vntchain/go-vnt/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  type signer struct {
    38  	sign bind.SignerFn
    39  }
    40  
    41  func (s *signer) Sign(addr *Address, unsignedTx *Transaction, chainID *big.Int) (signedTx *Transaction, _ error) {
    42  	si := types.NewHubbleSigner(chainID)
    43  	sig, err := s.sign(si, addr.address, unsignedTx.tx)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  	return &Transaction{sig}, nil
    48  }
    49  
    50  // CallOpts is the collection of options to fine tune a contract call request.
    51  type CallOpts struct {
    52  	opts bind.CallOpts
    53  }
    54  
    55  // NewCallOpts creates a new option set for contract calls.
    56  func NewCallOpts() *CallOpts {
    57  	return new(CallOpts)
    58  }
    59  
    60  func (opts *CallOpts) IsPending() bool    { return opts.opts.Pending }
    61  func (opts *CallOpts) GetGasLimit() int64 { return 0 /* TODO(karalabe) */ }
    62  
    63  // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
    64  // Even then it's awkward to unpack the subtleties of a Go context out to Java.
    65  // func (opts *CallOpts) GetContext() *Context { return &Context{opts.opts.Context} }
    66  
    67  func (opts *CallOpts) SetPending(pending bool)     { opts.opts.Pending = pending }
    68  func (opts *CallOpts) SetGasLimit(limit int64)     { /* TODO(karalabe) */ }
    69  func (opts *CallOpts) SetContext(context *Context) { opts.opts.Context = context.context }
    70  
    71  // TransactOpts is the collection of authorization data required to create a
    72  // valid VNT transaction.
    73  type TransactOpts struct {
    74  	opts bind.TransactOpts
    75  }
    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  func (opts *TransactOpts) GetChainID() *BigInt  { return &BigInt{opts.opts.ChainID} }
    83  
    84  // GetSigner cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
    85  // func (opts *TransactOpts) GetSigner() Signer { return &signer{opts.opts.Signer} }
    86  
    87  // GetContext cannot be reliably implemented without identity preservation (https://github.com/golang/go/issues/16876)
    88  // Even then it's awkward to unpack the subtleties of a Go context out to Java.
    89  //func (opts *TransactOpts) GetContext() *Context { return &Context{opts.opts.Context} }
    90  
    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(signer types.Signer, 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  func (opts *TransactOpts) SetChainID(chainID *BigInt)  { opts.opts.ChainID = chainID.bigint }
   107  
   108  // BoundContract is the base wrapper object that reflects a contract on the
   109  // VNT network. It contains a collection of methods that are used by the
   110  // higher level contract bindings to operate.
   111  type BoundContract struct {
   112  	contract *bind.BoundContract
   113  	address  common.Address
   114  	deployer *types.Transaction
   115  }
   116  
   117  // DeployContract deploys a contract onto the VNT blockchain and binds the
   118  // deployment address with a wrapper.
   119  func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *VNTClient, args *Interfaces) (contract *BoundContract, _ error) {
   120  	// Deploy the contract to the network
   121  	parsed, err := abi.JSON(strings.NewReader(abiJSON))
   122  	if err != nil {
   123  		return nil, err
   124  	}
   125  	addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  	return &BoundContract{
   130  		contract: bound,
   131  		address:  addr,
   132  		deployer: tx,
   133  	}, nil
   134  }
   135  
   136  // BindContract creates a low level contract interface through which calls and
   137  // transactions may be made through.
   138  func BindContract(address *Address, abiJSON string, client *VNTClient) (contract *BoundContract, _ error) {
   139  	parsed, err := abi.JSON(strings.NewReader(abiJSON))
   140  	if err != nil {
   141  		return nil, err
   142  	}
   143  	return &BoundContract{
   144  		contract: bind.NewBoundContract(address.address, parsed, client.client, client.client, client.client),
   145  		address:  address.address,
   146  	}, nil
   147  }
   148  
   149  func (c *BoundContract) GetAddress() *Address { return &Address{c.address} }
   150  func (c *BoundContract) GetDeployer() *Transaction {
   151  	if c.deployer == nil {
   152  		return nil
   153  	}
   154  	return &Transaction{c.deployer}
   155  }
   156  
   157  // Call invokes the (constant) contract method with params as input values and
   158  // sets the output to result.
   159  func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error {
   160  	if len(out.objects) == 1 {
   161  		result := out.objects[0]
   162  		if err := c.contract.Call(&opts.opts, result, method, args.objects...); err != nil {
   163  			return err
   164  		}
   165  		out.objects[0] = result
   166  	} else {
   167  		results := make([]interface{}, len(out.objects))
   168  		copy(results, out.objects)
   169  		if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil {
   170  			return err
   171  		}
   172  		copy(out.objects, results)
   173  	}
   174  	return nil
   175  }
   176  
   177  // Transact invokes the (paid) contract method with params as input values.
   178  func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
   179  	rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...)
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	return &Transaction{rawTx}, nil
   184  }
   185  
   186  // Transfer initiates a plain transaction to move funds to the contract, calling
   187  // its default method if one is available.
   188  func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) {
   189  	rawTx, err := c.contract.Transfer(&opts.opts)
   190  	if err != nil {
   191  		return nil, err
   192  	}
   193  	return &Transaction{rawTx}, nil
   194  }