github.com/cgcardona/r-subnet-evm@v0.1.5/signer/core/apitypes/types.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2018 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package apitypes
    28  
    29  import (
    30  	"encoding/json"
    31  	"fmt"
    32  	"math/big"
    33  	"strings"
    34  
    35  	"github.com/cgcardona/r-subnet-evm/core/types"
    36  	"github.com/ethereum/go-ethereum/common"
    37  	"github.com/ethereum/go-ethereum/common/hexutil"
    38  )
    39  
    40  type ValidationInfo struct {
    41  	Typ     string `json:"type"`
    42  	Message string `json:"message"`
    43  }
    44  type ValidationMessages struct {
    45  	Messages []ValidationInfo
    46  }
    47  
    48  const (
    49  	WARN = "WARNING"
    50  	CRIT = "CRITICAL"
    51  	INFO = "Info"
    52  )
    53  
    54  func (vs *ValidationMessages) Crit(msg string) {
    55  	vs.Messages = append(vs.Messages, ValidationInfo{CRIT, msg})
    56  }
    57  func (vs *ValidationMessages) Warn(msg string) {
    58  	vs.Messages = append(vs.Messages, ValidationInfo{WARN, msg})
    59  }
    60  func (vs *ValidationMessages) Info(msg string) {
    61  	vs.Messages = append(vs.Messages, ValidationInfo{INFO, msg})
    62  }
    63  
    64  // getWarnings returns an error with all messages of type WARN of above, or nil if no warnings were present
    65  func (v *ValidationMessages) GetWarnings() error {
    66  	var messages []string
    67  	for _, msg := range v.Messages {
    68  		if msg.Typ == WARN || msg.Typ == CRIT {
    69  			messages = append(messages, msg.Message)
    70  		}
    71  	}
    72  	if len(messages) > 0 {
    73  		return fmt.Errorf("validation failed: %s", strings.Join(messages, ","))
    74  	}
    75  	return nil
    76  }
    77  
    78  // SendTxArgs represents the arguments to submit a transaction
    79  // This struct is identical to ethapi.TransactionArgs, except for the usage of
    80  // common.MixedcaseAddress in From and To
    81  type SendTxArgs struct {
    82  	From                 common.MixedcaseAddress  `json:"from"`
    83  	To                   *common.MixedcaseAddress `json:"to"`
    84  	Gas                  hexutil.Uint64           `json:"gas"`
    85  	GasPrice             *hexutil.Big             `json:"gasPrice"`
    86  	MaxFeePerGas         *hexutil.Big             `json:"maxFeePerGas"`
    87  	MaxPriorityFeePerGas *hexutil.Big             `json:"maxPriorityFeePerGas"`
    88  	Value                hexutil.Big              `json:"value"`
    89  	Nonce                hexutil.Uint64           `json:"nonce"`
    90  
    91  	// We accept "data" and "input" for backwards-compatibility reasons.
    92  	// "input" is the newer name and should be preferred by clients.
    93  	// Issue detail: https://github.com/ethereum/go-ethereum/issues/15628
    94  	Data  *hexutil.Bytes `json:"data"`
    95  	Input *hexutil.Bytes `json:"input,omitempty"`
    96  
    97  	// For non-legacy transactions
    98  	AccessList *types.AccessList `json:"accessList,omitempty"`
    99  	ChainID    *hexutil.Big      `json:"chainId,omitempty"`
   100  }
   101  
   102  func (args SendTxArgs) String() string {
   103  	s, err := json.Marshal(args)
   104  	if err == nil {
   105  		return string(s)
   106  	}
   107  	return err.Error()
   108  }
   109  
   110  // ToTransaction converts the arguments to a transaction.
   111  func (args *SendTxArgs) ToTransaction() *types.Transaction {
   112  	// Add the To-field, if specified
   113  	var to *common.Address
   114  	if args.To != nil {
   115  		dstAddr := args.To.Address()
   116  		to = &dstAddr
   117  	}
   118  
   119  	var input []byte
   120  	if args.Input != nil {
   121  		input = *args.Input
   122  	} else if args.Data != nil {
   123  		input = *args.Data
   124  	}
   125  
   126  	var data types.TxData
   127  	switch {
   128  	case args.MaxFeePerGas != nil:
   129  		al := types.AccessList{}
   130  		if args.AccessList != nil {
   131  			al = *args.AccessList
   132  		}
   133  		data = &types.DynamicFeeTx{
   134  			To:         to,
   135  			ChainID:    (*big.Int)(args.ChainID),
   136  			Nonce:      uint64(args.Nonce),
   137  			Gas:        uint64(args.Gas),
   138  			GasFeeCap:  (*big.Int)(args.MaxFeePerGas),
   139  			GasTipCap:  (*big.Int)(args.MaxPriorityFeePerGas),
   140  			Value:      (*big.Int)(&args.Value),
   141  			Data:       input,
   142  			AccessList: al,
   143  		}
   144  	case args.AccessList != nil:
   145  		data = &types.AccessListTx{
   146  			To:         to,
   147  			ChainID:    (*big.Int)(args.ChainID),
   148  			Nonce:      uint64(args.Nonce),
   149  			Gas:        uint64(args.Gas),
   150  			GasPrice:   (*big.Int)(args.GasPrice),
   151  			Value:      (*big.Int)(&args.Value),
   152  			Data:       input,
   153  			AccessList: *args.AccessList,
   154  		}
   155  	default:
   156  		data = &types.LegacyTx{
   157  			To:       to,
   158  			Nonce:    uint64(args.Nonce),
   159  			Gas:      uint64(args.Gas),
   160  			GasPrice: (*big.Int)(args.GasPrice),
   161  			Value:    (*big.Int)(&args.Value),
   162  			Data:     input,
   163  		}
   164  	}
   165  	return types.NewTx(data)
   166  }