github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/tx/tx.go (about)

     1  package tx
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
     8  	anytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types"
     9  	sdktypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  	types "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/ibc-adapter"
    11  	"github.com/golang/protobuf/ptypes/wrappers"
    12  )
    13  
    14  var (
    15  	_                   context.TxRequest      = (*BroadcastTxRequest)(nil)
    16  	_                   context.TxResponse     = (*BroadcastTxResponse)(nil)
    17  	_                   context.CodecSensitive = (*BroadcastTxResponse)(nil)
    18  	errMarshalSensitive                        = errors.New("error for Marshal  Sensitive")
    19  )
    20  
    21  func (t *BroadcastTxRequest) GetModeDetail() int32 {
    22  	return int32(t.Mode)
    23  }
    24  
    25  func (t *BroadcastTxRequest) GetData() []byte {
    26  	return t.GetTxBytes()
    27  }
    28  
    29  func (t *BroadcastTxResponse) HandleResponse(codec *codec.CodecProxy, data interface{}) interface{} {
    30  	resp := data.(sdktypes.TxResponse)
    31  	logs := convOkcLogs2Proto(resp.Logs)
    32  	t.TxResponse = &types.TxResponse{
    33  		Height:    resp.Height,
    34  		TxHash:    resp.TxHash,
    35  		Codespace: resp.Codespace,
    36  		Code:      resp.Code,
    37  		Data:      resp.Data,
    38  		RawLog:    resp.RawLog,
    39  		Logs:      logs,
    40  		Info:      resp.Info,
    41  		GasWanted: resp.GasWanted,
    42  		GasUsed:   resp.GasUsed,
    43  		Timestamp: resp.Timestamp,
    44  	}
    45  
    46  	respWrapper := AminoBroadCastTxResponse{resp}
    47  	dataBytes, err := codec.GetCdc().MarshalJSON(respWrapper)
    48  	if nil == err {
    49  		bytesWrapper := &wrappers.BytesValue{Value: dataBytes}
    50  		if any, err := anytypes.NewAnyWithValue(bytesWrapper); nil == err {
    51  			t.TxResponse.Tx = any
    52  		}
    53  	}
    54  	return t
    55  }
    56  
    57  func convOkcLogs2Proto(ls sdktypes.ABCIMessageLogs) types.ABCIMessageLogs {
    58  	logs := make(types.ABCIMessageLogs, 0)
    59  	for _, l := range ls {
    60  		es := make(types.StringEvents, 0)
    61  		for _, e := range l.Events {
    62  			attrs := make([]types.Attribute, 0)
    63  			for _, a := range e.Attributes {
    64  				attrs = append(attrs, types.Attribute{
    65  					Key:   a.Key,
    66  					Value: a.Value,
    67  				})
    68  			}
    69  			es = append(es, types.StringEvent{
    70  				Type:       e.Type,
    71  				Attributes: attrs,
    72  			})
    73  		}
    74  		logs = append(logs, types.ABCIMessageLog{
    75  			MsgIndex: uint32(l.MsgIndex),
    76  			Log:      l.Log,
    77  			Events:   es,
    78  		})
    79  	}
    80  	return logs
    81  }
    82  
    83  type AminoBroadCastTxResponse struct {
    84  	TxResponse sdktypes.TxResponse `protobuf:"bytes,1,opt,name=tx_response,json=txResponse,proto3" json:"tx_response,omitempty"`
    85  }
    86  
    87  func (t *BroadcastTxResponse) MarshalSensitive(proxy *codec.CodecProxy) ([]byte, error) {
    88  	if t.TxResponse == nil {
    89  		return nil, errMarshalSensitive
    90  	}
    91  	internalV := t.TxResponse.Tx.GetCachedValue()
    92  	if nil == internalV {
    93  		return nil, errMarshalSensitive
    94  	}
    95  	resp, ok := internalV.(*wrappers.BytesValue)
    96  	if !ok {
    97  		return nil, errMarshalSensitive
    98  	}
    99  	return resp.Value, nil
   100  }