github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/watcher/utils.go (about)

     1  package watcher
     2  
     3  import (
     4  	"math/big"
     5  	"time"
     6  
     7  	clientcontext "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    10  	authtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/auth/types"
    11  	ctypes "github.com/fibonacci-chain/fbc/libs/tendermint/rpc/core/types"
    12  
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethereum/go-ethereum/common/hexutil"
    15  	evmtypes "github.com/fibonacci-chain/fbc/x/evm/types"
    16  )
    17  
    18  // NewTransaction returns a transaction that will serialize to the RPC
    19  // representation, with the given location metadata set (if available).
    20  func NewTransaction(tx *evmtypes.MsgEthereumTx, txHash, blockHash common.Hash, blockNumber, index uint64) (*Transaction, error) {
    21  	// Verify signature and retrieve sender address
    22  	err := tx.VerifySig(tx.ChainID(), int64(blockNumber))
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	rpcTx := &Transaction{
    28  		From:     common.HexToAddress(tx.GetFrom()),
    29  		Gas:      hexutil.Uint64(tx.Data.GasLimit),
    30  		GasPrice: (*hexutil.Big)(tx.Data.Price),
    31  		Hash:     txHash,
    32  		Input:    hexutil.Bytes(tx.Data.Payload),
    33  		Nonce:    hexutil.Uint64(tx.Data.AccountNonce),
    34  		To:       tx.To(),
    35  		Value:    (*hexutil.Big)(tx.Data.Amount),
    36  		V:        (*hexutil.Big)(tx.Data.V),
    37  		R:        (*hexutil.Big)(tx.Data.R),
    38  		S:        (*hexutil.Big)(tx.Data.S),
    39  	}
    40  
    41  	if blockHash != (common.Hash{}) {
    42  		rpcTx.BlockHash = &blockHash
    43  		rpcTx.BlockNumber = (*hexutil.Big)(new(big.Int).SetUint64(blockNumber))
    44  		rpcTx.TransactionIndex = (*hexutil.Uint64)(&index)
    45  	}
    46  
    47  	return rpcTx, nil
    48  }
    49  
    50  func RawTxResultToStdResponse(clientCtx clientcontext.CLIContext,
    51  	tr *ctypes.ResultTx, tx sdk.Tx, timestamp time.Time) (*TransactionResult, error) {
    52  
    53  	var err error
    54  	if tx == nil {
    55  		tx, err = evmtypes.TxDecoder(clientCtx.CodecProy)(tr.Tx, tr.Height)
    56  		if err != nil {
    57  			return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
    58  		}
    59  	}
    60  
    61  	var realTx *authtypes.StdTx
    62  	switch tx.(type) {
    63  	case *authtypes.IbcTx:
    64  		realTx, err = authtypes.FromProtobufTx(clientCtx.CodecProy, tx.(*authtypes.IbcTx))
    65  		if nil != err {
    66  			return nil, err
    67  		}
    68  	default:
    69  		err = clientCtx.Codec.UnmarshalBinaryLengthPrefixed(tr.Tx, &realTx)
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  	}
    74  
    75  	response := sdk.NewResponseResultTx(tr, realTx, timestamp.Format(time.RFC3339))
    76  	wrappedR := &WrappedResponseWithCodec{Response: response, Codec: clientCtx.Codec}
    77  
    78  	return &TransactionResult{TxType: hexutil.Uint64(StdResponse), Response: wrappedR}, nil
    79  }