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