github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/mobile/bind.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2016 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  //包含绑定包中的所有包装。
    26  
    27  package geth
    28  
    29  import (
    30  	"math/big"
    31  	"strings"
    32  
    33  	"github.com/ethereum/go-ethereum/accounts/abi"
    34  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    35  	"github.com/ethereum/go-ethereum/common"
    36  	"github.com/ethereum/go-ethereum/core/types"
    37  )
    38  
    39  //签名者是一个接口,当合同要求
    40  //提交前签署事务的方法。
    41  type Signer interface {
    42  	Sign(*Address, *Transaction) (tx *Transaction, _ error)
    43  }
    44  
    45  type signer struct {
    46  	sign bind.SignerFn
    47  }
    48  
    49  func (s *signer) Sign(addr *Address, unsignedTx *Transaction) (signedTx *Transaction, _ error) {
    50  	sig, err := s.sign(types.HomesteadSigner{}, addr.address, unsignedTx.tx)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return &Transaction{sig}, nil
    55  }
    56  
    57  //Callopts是对合同调用请求进行微调的选项集合。
    58  type CallOpts struct {
    59  	opts bind.CallOpts
    60  }
    61  
    62  //newcallopts为合同调用创建新的选项集。
    63  func NewCallOpts() *CallOpts {
    64  	return new(CallOpts)
    65  }
    66  
    67  func (opts *CallOpts) IsPending() bool    { return opts.opts.Pending }
    68  /*c(opts*callopts)getgaslimit()int64返回0/*todo(karalabe)*/
    69  
    70  //没有身份保护,getContext无法可靠实现(https://github.com/golang/go/issues/16876)
    71  即使是这样,将GO上下文的细微部分解压缩到Java也是很困难的。
    72  //func(opts*callopts)getContext()*context返回&context opts.opts.context
    73  
    74  func(opts*callopts)setpending(挂起bool)opts.opts.pending=挂起
    75  func(opts*callopts)setgaslimit(limit int64)/*todo(karalabe)*/ }
    76  
    77  func (opts *CallOpts) SetContext(context *Context) { opts.opts.Context = context.context }
    78  
    79  //TransactioOpts是创建
    80  //有效的以太坊事务。
    81  type TransactOpts struct {
    82  	opts bind.TransactOpts
    83  }
    84  
    85  func (opts *TransactOpts) GetFrom() *Address    { return &Address{opts.opts.From} }
    86  func (opts *TransactOpts) GetNonce() int64      { return opts.opts.Nonce.Int64() }
    87  func (opts *TransactOpts) GetValue() *BigInt    { return &BigInt{opts.opts.Value} }
    88  func (opts *TransactOpts) GetGasPrice() *BigInt { return &BigInt{opts.opts.GasPrice} }
    89  func (opts *TransactOpts) GetGasLimit() int64   { return int64(opts.opts.GasLimit) }
    90  
    91  //没有身份保护,getsigner无法可靠实现(https://github.com/golang/go/issues/16876)
    92  //func(opts*transactioopts)getsigner()签名者返回&签名者opts.opts.signer
    93  
    94  //没有身份保护,getContext无法可靠实现(https://github.com/golang/go/issues/16876)
    95  //即使这样,将GO上下文的细微部分解压缩到Java也是很困难的。
    96  //func(opts*transactionopts)getContext()*context返回&context opts.opts.context
    97  
    98  func (opts *TransactOpts) SetFrom(from *Address) { opts.opts.From = from.address }
    99  func (opts *TransactOpts) SetNonce(nonce int64)  { opts.opts.Nonce = big.NewInt(nonce) }
   100  func (opts *TransactOpts) SetSigner(s Signer) {
   101  	opts.opts.Signer = func(signer types.Signer, addr common.Address, tx *types.Transaction) (*types.Transaction, error) {
   102  		sig, err := s.Sign(&Address{addr}, &Transaction{tx})
   103  		if err != nil {
   104  			return nil, err
   105  		}
   106  		return sig.tx, nil
   107  	}
   108  }
   109  func (opts *TransactOpts) SetValue(value *BigInt)      { opts.opts.Value = value.bigint }
   110  func (opts *TransactOpts) SetGasPrice(price *BigInt)   { opts.opts.GasPrice = price.bigint }
   111  func (opts *TransactOpts) SetGasLimit(limit int64)     { opts.opts.GasLimit = uint64(limit) }
   112  func (opts *TransactOpts) SetContext(context *Context) { opts.opts.Context = context.context }
   113  
   114  //BoundContract是反映在
   115  //以太坊网络。它包含由
   116  //要操作的更高级别合同绑定。
   117  type BoundContract struct {
   118  	contract *bind.BoundContract
   119  	address  common.Address
   120  	deployer *types.Transaction
   121  }
   122  
   123  //DeployContract将合同部署到以太坊区块链上,并绑定
   124  //带有包装的部署地址。
   125  func DeployContract(opts *TransactOpts, abiJSON string, bytecode []byte, client *EthereumClient, args *Interfaces) (contract *BoundContract, _ error) {
   126  //将合同部署到网络
   127  	parsed, err := abi.JSON(strings.NewReader(abiJSON))
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  	addr, tx, bound, err := bind.DeployContract(&opts.opts, parsed, common.CopyBytes(bytecode), client.client, args.objects...)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	return &BoundContract{
   136  		contract: bound,
   137  		address:  addr,
   138  		deployer: tx,
   139  	}, nil
   140  }
   141  
   142  //bindcontact创建一个低级合同接口,通过该接口调用和
   143  //交易可以通过。
   144  func BindContract(address *Address, abiJSON string, client *EthereumClient) (contract *BoundContract, _ error) {
   145  	parsed, err := abi.JSON(strings.NewReader(abiJSON))
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	return &BoundContract{
   150  		contract: bind.NewBoundContract(address.address, parsed, client.client, client.client, client.client),
   151  		address:  address.address,
   152  	}, nil
   153  }
   154  
   155  func (c *BoundContract) GetAddress() *Address { return &Address{c.address} }
   156  func (c *BoundContract) GetDeployer() *Transaction {
   157  	if c.deployer == nil {
   158  		return nil
   159  	}
   160  	return &Transaction{c.deployer}
   161  }
   162  
   163  //调用调用(常量)contract方法,参数作为输入值,并且
   164  //将输出设置为结果。
   165  func (c *BoundContract) Call(opts *CallOpts, out *Interfaces, method string, args *Interfaces) error {
   166  	if len(out.objects) == 1 {
   167  		result := out.objects[0]
   168  		if err := c.contract.Call(&opts.opts, result, method, args.objects...); err != nil {
   169  			return err
   170  		}
   171  		out.objects[0] = result
   172  	} else {
   173  		results := make([]interface{}, len(out.objects))
   174  		copy(results, out.objects)
   175  		if err := c.contract.Call(&opts.opts, &results, method, args.objects...); err != nil {
   176  			return err
   177  		}
   178  		copy(out.objects, results)
   179  	}
   180  	return nil
   181  }
   182  
   183  //Transact使用参数作为输入值调用(付费)Contract方法。
   184  func (c *BoundContract) Transact(opts *TransactOpts, method string, args *Interfaces) (tx *Transaction, _ error) {
   185  	rawTx, err := c.contract.Transact(&opts.opts, method, args.objects...)
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  	return &Transaction{rawTx}, nil
   190  }
   191  
   192  //转账启动普通交易以将资金转移到合同,调用
   193  //它的默认方法(如果有)。
   194  func (c *BoundContract) Transfer(opts *TransactOpts) (tx *Transaction, _ error) {
   195  	rawTx, err := c.contract.Transfer(&opts.opts)
   196  	if err != nil {
   197  		return nil, err
   198  	}
   199  	return &Transaction{rawTx}, nil
   200  }