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

     1  package watcher
     2  
     3  import (
     4  	"github.com/ethereum/go-ethereum/common"
     5  	"github.com/ethereum/go-ethereum/common/hexutil"
     6  	ethtypes "github.com/ethereum/go-ethereum/core/types"
     7  	prototypes "github.com/fibonacci-chain/fbc/x/evm/watcher/proto"
     8  )
     9  
    10  func transactionToProto(tr *Transaction) *prototypes.Transaction {
    11  	var to []byte
    12  	if tr.To != nil {
    13  		to = tr.To.Bytes()
    14  	}
    15  	return &prototypes.Transaction{
    16  		BlockHash:        tr.BlockHash.Bytes(),
    17  		BlockNumber:      tr.BlockNumber.String(),
    18  		From:             tr.From.Bytes(),
    19  		Gas:              uint64(tr.Gas),
    20  		GasPrice:         tr.GasPrice.String(),
    21  		Hash:             tr.Hash.Bytes(),
    22  		Input:            tr.Input,
    23  		Nonce:            uint64(tr.Nonce),
    24  		To:               to,
    25  		TransactionIndex: uint64(*tr.TransactionIndex),
    26  		Value:            tr.Value.String(),
    27  		V:                tr.V.String(),
    28  		R:                tr.R.String(),
    29  		S:                tr.S.String(),
    30  	}
    31  }
    32  
    33  func protoToTransaction(tr *prototypes.Transaction) *Transaction {
    34  	blockHash := common.BytesToHash(tr.BlockHash)
    35  	blockNum := hexutil.MustDecodeBig(tr.BlockNumber)
    36  	gasPrice := hexutil.MustDecodeBig(tr.GasPrice)
    37  	var to *common.Address
    38  	if len(tr.To) > 0 {
    39  		addr := common.BytesToAddress(tr.To)
    40  		to = &addr
    41  	}
    42  	index := hexutil.Uint64(tr.TransactionIndex)
    43  	value := hexutil.MustDecodeBig(tr.Value)
    44  	v := hexutil.MustDecodeBig(tr.V)
    45  	r := hexutil.MustDecodeBig(tr.R)
    46  	s := hexutil.MustDecodeBig(tr.S)
    47  	return &Transaction{
    48  		BlockHash:        &blockHash,
    49  		BlockNumber:      (*hexutil.Big)(blockNum),
    50  		From:             common.BytesToAddress(tr.From),
    51  		Gas:              hexutil.Uint64(tr.Gas),
    52  		GasPrice:         (*hexutil.Big)(gasPrice),
    53  		Hash:             common.BytesToHash(tr.Hash),
    54  		Input:            tr.Input,
    55  		Nonce:            hexutil.Uint64(tr.Nonce),
    56  		To:               to,
    57  		TransactionIndex: &index,
    58  		Value:            (*hexutil.Big)(value),
    59  		V:                (*hexutil.Big)(v),
    60  		R:                (*hexutil.Big)(r),
    61  		S:                (*hexutil.Big)(s),
    62  	}
    63  }
    64  
    65  func receiptToProto(tr *TransactionReceipt) *prototypes.TransactionReceipt {
    66  	logs := make([]*prototypes.Log, len(tr.Logs))
    67  	for i, log := range tr.Logs {
    68  		topics := make([][]byte, len(log.Topics))
    69  		for j, topic := range log.Topics {
    70  			topics[j] = topic.Bytes()
    71  		}
    72  		logs[i] = &prototypes.Log{
    73  			Address:     log.Address.Bytes(),
    74  			Topics:      topics,
    75  			Data:        log.Data,
    76  			BlockNumber: log.BlockNumber,
    77  			TxHash:      log.TxHash.Bytes(),
    78  			TxIndex:     uint64(log.TxIndex),
    79  			BlockHash:   log.BlockHash.Bytes(),
    80  			Index:       uint64(log.Index),
    81  			Removed:     log.Removed,
    82  		}
    83  	}
    84  	var contractAddr []byte
    85  	if tr.ContractAddress != nil {
    86  		contractAddr = tr.ContractAddress.Bytes()
    87  	}
    88  	var to []byte
    89  	if tr.To != nil {
    90  		to = tr.To.Bytes()
    91  	}
    92  	return &prototypes.TransactionReceipt{
    93  		Status:            uint64(tr.Status),
    94  		CumulativeGasUsed: uint64(tr.CumulativeGasUsed),
    95  		LogsBloom:         tr.LogsBloom.Bytes(),
    96  		Logs:              logs,
    97  		TransactionHash:   tr.TransactionHash,
    98  		ContractAddress:   contractAddr,
    99  		GasUsed:           uint64(tr.GasUsed),
   100  		BlockHash:         tr.BlockHash,
   101  		BlockNumber:       uint64(tr.BlockNumber),
   102  		TransactionIndex:  uint64(tr.TransactionIndex),
   103  		From:              tr.From,
   104  		To:                to,
   105  	}
   106  }
   107  
   108  func protoToReceipt(tr *prototypes.TransactionReceipt) *TransactionReceipt {
   109  	logs := make([]*ethtypes.Log, len(tr.Logs))
   110  	for i, log := range tr.Logs {
   111  		topics := make([]common.Hash, len(log.Topics))
   112  		for j, topic := range log.Topics {
   113  			topics[j] = common.BytesToHash(topic)
   114  		}
   115  		logs[i] = &ethtypes.Log{
   116  			Address:     common.BytesToAddress(log.Address),
   117  			Topics:      topics,
   118  			Data:        log.Data,
   119  			BlockNumber: log.BlockNumber,
   120  			TxHash:      common.BytesToHash(log.TxHash),
   121  			TxIndex:     uint(log.TxIndex),
   122  			BlockHash:   common.BytesToHash(log.BlockHash),
   123  			Index:       uint(log.Index),
   124  			Removed:     log.Removed,
   125  		}
   126  	}
   127  	var contractAddr *common.Address
   128  	if len(tr.ContractAddress) > 0 {
   129  		addr := common.BytesToAddress(tr.ContractAddress)
   130  		contractAddr = &addr
   131  	}
   132  	var to *common.Address
   133  	if len(tr.To) > 0 {
   134  		addr := common.BytesToAddress(tr.To)
   135  		to = &addr
   136  	}
   137  	return &TransactionReceipt{
   138  		Status:            hexutil.Uint64(tr.Status),
   139  		CumulativeGasUsed: hexutil.Uint64(tr.CumulativeGasUsed),
   140  		LogsBloom:         ethtypes.BytesToBloom(tr.LogsBloom),
   141  		Logs:              logs,
   142  		TransactionHash:   tr.TransactionHash,
   143  		ContractAddress:   contractAddr,
   144  		GasUsed:           hexutil.Uint64(tr.GasUsed),
   145  		BlockHash:         tr.BlockHash,
   146  		BlockNumber:       hexutil.Uint64(tr.BlockNumber),
   147  		TransactionIndex:  hexutil.Uint64(tr.TransactionIndex),
   148  		From:              tr.From,
   149  		To:                to,
   150  	}
   151  }