github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/tests/gen_sttransaction.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  //</624342685506867200>
    11  
    12  //
    13  
    14  package tests
    15  
    16  import (
    17  	"encoding/json"
    18  	"math/big"
    19  
    20  	"github.com/ethereum/go-ethereum/common/hexutil"
    21  	"github.com/ethereum/go-ethereum/common/math"
    22  )
    23  
    24  var _ = (*stTransactionMarshaling)(nil)
    25  
    26  func (s stTransaction) MarshalJSON() ([]byte, error) {
    27  	type stTransaction struct {
    28  		GasPrice   *math.HexOrDecimal256 `json:"gasPrice"`
    29  		Nonce      math.HexOrDecimal64   `json:"nonce"`
    30  		To         string                `json:"to"`
    31  		Data       []string              `json:"data"`
    32  		GasLimit   []math.HexOrDecimal64 `json:"gasLimit"`
    33  		Value      []string              `json:"value"`
    34  		PrivateKey hexutil.Bytes         `json:"secretKey"`
    35  	}
    36  	var enc stTransaction
    37  	enc.GasPrice = (*math.HexOrDecimal256)(s.GasPrice)
    38  	enc.Nonce = math.HexOrDecimal64(s.Nonce)
    39  	enc.To = s.To
    40  	enc.Data = s.Data
    41  	if s.GasLimit != nil {
    42  		enc.GasLimit = make([]math.HexOrDecimal64, len(s.GasLimit))
    43  		for k, v := range s.GasLimit {
    44  			enc.GasLimit[k] = math.HexOrDecimal64(v)
    45  		}
    46  	}
    47  	enc.Value = s.Value
    48  	enc.PrivateKey = s.PrivateKey
    49  	return json.Marshal(&enc)
    50  }
    51  
    52  func (s *stTransaction) UnmarshalJSON(input []byte) error {
    53  	type stTransaction struct {
    54  		GasPrice   *math.HexOrDecimal256 `json:"gasPrice"`
    55  		Nonce      *math.HexOrDecimal64  `json:"nonce"`
    56  		To         *string               `json:"to"`
    57  		Data       []string              `json:"data"`
    58  		GasLimit   []math.HexOrDecimal64 `json:"gasLimit"`
    59  		Value      []string              `json:"value"`
    60  		PrivateKey *hexutil.Bytes        `json:"secretKey"`
    61  	}
    62  	var dec stTransaction
    63  	if err := json.Unmarshal(input, &dec); err != nil {
    64  		return err
    65  	}
    66  	if dec.GasPrice != nil {
    67  		s.GasPrice = (*big.Int)(dec.GasPrice)
    68  	}
    69  	if dec.Nonce != nil {
    70  		s.Nonce = uint64(*dec.Nonce)
    71  	}
    72  	if dec.To != nil {
    73  		s.To = *dec.To
    74  	}
    75  	if dec.Data != nil {
    76  		s.Data = dec.Data
    77  	}
    78  	if dec.GasLimit != nil {
    79  		s.GasLimit = make([]uint64, len(dec.GasLimit))
    80  		for k, v := range dec.GasLimit {
    81  			s.GasLimit[k] = uint64(v)
    82  		}
    83  	}
    84  	if dec.Value != nil {
    85  		s.Value = dec.Value
    86  	}
    87  	if dec.PrivateKey != nil {
    88  		s.PrivateKey = *dec.PrivateKey
    89  	}
    90  	return nil
    91  }
    92