github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/tests/gen_tttransaction.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:50</date>
    10  //</624342685590753280>
    11  
    12  //
    13  
    14  package tests
    15  
    16  import (
    17  	"encoding/json"
    18  	"errors"
    19  	"math/big"
    20  
    21  	"github.com/ethereum/go-ethereum/common"
    22  	"github.com/ethereum/go-ethereum/common/hexutil"
    23  	"github.com/ethereum/go-ethereum/common/math"
    24  )
    25  
    26  var _ = (*ttTransactionMarshaling)(nil)
    27  
    28  func (t ttTransaction) MarshalJSON() ([]byte, error) {
    29  	type ttTransaction struct {
    30  		Data     hexutil.Bytes         `gencodec:"required"`
    31  		GasLimit math.HexOrDecimal64   `gencodec:"required"`
    32  		GasPrice *math.HexOrDecimal256 `gencodec:"required"`
    33  		Nonce    math.HexOrDecimal64   `gencodec:"required"`
    34  		Value    *math.HexOrDecimal256 `gencodec:"required"`
    35  		R        *math.HexOrDecimal256 `gencodec:"required"`
    36  		S        *math.HexOrDecimal256 `gencodec:"required"`
    37  		V        *math.HexOrDecimal256 `gencodec:"required"`
    38  		To       common.Address        `gencodec:"required"`
    39  	}
    40  	var enc ttTransaction
    41  	enc.Data = t.Data
    42  	enc.GasLimit = math.HexOrDecimal64(t.GasLimit)
    43  	enc.GasPrice = (*math.HexOrDecimal256)(t.GasPrice)
    44  	enc.Nonce = math.HexOrDecimal64(t.Nonce)
    45  	enc.Value = (*math.HexOrDecimal256)(t.Value)
    46  	enc.R = (*math.HexOrDecimal256)(t.R)
    47  	enc.S = (*math.HexOrDecimal256)(t.S)
    48  	enc.V = (*math.HexOrDecimal256)(t.V)
    49  	enc.To = t.To
    50  	return json.Marshal(&enc)
    51  }
    52  
    53  func (t *ttTransaction) UnmarshalJSON(input []byte) error {
    54  	type ttTransaction struct {
    55  		Data     *hexutil.Bytes        `gencodec:"required"`
    56  		GasLimit *math.HexOrDecimal64  `gencodec:"required"`
    57  		GasPrice *math.HexOrDecimal256 `gencodec:"required"`
    58  		Nonce    *math.HexOrDecimal64  `gencodec:"required"`
    59  		Value    *math.HexOrDecimal256 `gencodec:"required"`
    60  		R        *math.HexOrDecimal256 `gencodec:"required"`
    61  		S        *math.HexOrDecimal256 `gencodec:"required"`
    62  		V        *math.HexOrDecimal256 `gencodec:"required"`
    63  		To       *common.Address       `gencodec:"required"`
    64  	}
    65  	var dec ttTransaction
    66  	if err := json.Unmarshal(input, &dec); err != nil {
    67  		return err
    68  	}
    69  	if dec.Data == nil {
    70  		return errors.New("missing required field 'data' for ttTransaction")
    71  	}
    72  	t.Data = *dec.Data
    73  	if dec.GasLimit == nil {
    74  		return errors.New("missing required field 'gasLimit' for ttTransaction")
    75  	}
    76  	t.GasLimit = uint64(*dec.GasLimit)
    77  	if dec.GasPrice == nil {
    78  		return errors.New("missing required field 'gasPrice' for ttTransaction")
    79  	}
    80  	t.GasPrice = (*big.Int)(dec.GasPrice)
    81  	if dec.Nonce == nil {
    82  		return errors.New("missing required field 'nonce' for ttTransaction")
    83  	}
    84  	t.Nonce = uint64(*dec.Nonce)
    85  	if dec.Value == nil {
    86  		return errors.New("missing required field 'value' for ttTransaction")
    87  	}
    88  	t.Value = (*big.Int)(dec.Value)
    89  	if dec.R == nil {
    90  		return errors.New("missing required field 'r' for ttTransaction")
    91  	}
    92  	t.R = (*big.Int)(dec.R)
    93  	if dec.S == nil {
    94  		return errors.New("missing required field 's' for ttTransaction")
    95  	}
    96  	t.S = (*big.Int)(dec.S)
    97  	if dec.V == nil {
    98  		return errors.New("missing required field 'v' for ttTransaction")
    99  	}
   100  	t.V = (*big.Int)(dec.V)
   101  	if dec.To == nil {
   102  		return errors.New("missing required field 'to' for ttTransaction")
   103  	}
   104  	t.To = *dec.To
   105  	return nil
   106  }
   107