github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/tests/gen_tttransaction.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  //
    10  
    11  package tests
    12  
    13  import (
    14  	"encoding/json"
    15  	"errors"
    16  	"math/big"
    17  
    18  	"github.com/ethereum/go-ethereum/common"
    19  	"github.com/ethereum/go-ethereum/common/hexutil"
    20  	"github.com/ethereum/go-ethereum/common/math"
    21  )
    22  
    23  var _ = (*ttTransactionMarshaling)(nil)
    24  
    25  func (t ttTransaction) MarshalJSON() ([]byte, error) {
    26  	type ttTransaction struct {
    27  		Data     hexutil.Bytes         `gencodec:"required"`
    28  		GasLimit math.HexOrDecimal64   `gencodec:"required"`
    29  		GasPrice *math.HexOrDecimal256 `gencodec:"required"`
    30  		Nonce    math.HexOrDecimal64   `gencodec:"required"`
    31  		Value    *math.HexOrDecimal256 `gencodec:"required"`
    32  		R        *math.HexOrDecimal256 `gencodec:"required"`
    33  		S        *math.HexOrDecimal256 `gencodec:"required"`
    34  		V        *math.HexOrDecimal256 `gencodec:"required"`
    35  		To       common.Address        `gencodec:"required"`
    36  	}
    37  	var enc ttTransaction
    38  	enc.Data = t.Data
    39  	enc.GasLimit = math.HexOrDecimal64(t.GasLimit)
    40  	enc.GasPrice = (*math.HexOrDecimal256)(t.GasPrice)
    41  	enc.Nonce = math.HexOrDecimal64(t.Nonce)
    42  	enc.Value = (*math.HexOrDecimal256)(t.Value)
    43  	enc.R = (*math.HexOrDecimal256)(t.R)
    44  	enc.S = (*math.HexOrDecimal256)(t.S)
    45  	enc.V = (*math.HexOrDecimal256)(t.V)
    46  	enc.To = t.To
    47  	return json.Marshal(&enc)
    48  }
    49  
    50  func (t *ttTransaction) UnmarshalJSON(input []byte) error {
    51  	type ttTransaction struct {
    52  		Data     *hexutil.Bytes        `gencodec:"required"`
    53  		GasLimit *math.HexOrDecimal64  `gencodec:"required"`
    54  		GasPrice *math.HexOrDecimal256 `gencodec:"required"`
    55  		Nonce    *math.HexOrDecimal64  `gencodec:"required"`
    56  		Value    *math.HexOrDecimal256 `gencodec:"required"`
    57  		R        *math.HexOrDecimal256 `gencodec:"required"`
    58  		S        *math.HexOrDecimal256 `gencodec:"required"`
    59  		V        *math.HexOrDecimal256 `gencodec:"required"`
    60  		To       *common.Address       `gencodec:"required"`
    61  	}
    62  	var dec ttTransaction
    63  	if err := json.Unmarshal(input, &dec); err != nil {
    64  		return err
    65  	}
    66  	if dec.Data == nil {
    67  		return errors.New("missing required field 'data' for ttTransaction")
    68  	}
    69  	t.Data = *dec.Data
    70  	if dec.GasLimit == nil {
    71  		return errors.New("missing required field 'gasLimit' for ttTransaction")
    72  	}
    73  	t.GasLimit = uint64(*dec.GasLimit)
    74  	if dec.GasPrice == nil {
    75  		return errors.New("missing required field 'gasPrice' for ttTransaction")
    76  	}
    77  	t.GasPrice = (*big.Int)(dec.GasPrice)
    78  	if dec.Nonce == nil {
    79  		return errors.New("missing required field 'nonce' for ttTransaction")
    80  	}
    81  	t.Nonce = uint64(*dec.Nonce)
    82  	if dec.Value == nil {
    83  		return errors.New("missing required field 'value' for ttTransaction")
    84  	}
    85  	t.Value = (*big.Int)(dec.Value)
    86  	if dec.R == nil {
    87  		return errors.New("missing required field 'r' for ttTransaction")
    88  	}
    89  	t.R = (*big.Int)(dec.R)
    90  	if dec.S == nil {
    91  		return errors.New("missing required field 's' for ttTransaction")
    92  	}
    93  	t.S = (*big.Int)(dec.S)
    94  	if dec.V == nil {
    95  		return errors.New("missing required field 'v' for ttTransaction")
    96  	}
    97  	t.V = (*big.Int)(dec.V)
    98  	if dec.To == nil {
    99  		return errors.New("missing required field 'to' for ttTransaction")
   100  	}
   101  	t.To = *dec.To
   102  	return nil
   103  }