github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/events.go (about)

     1  package keeper
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	wasmvmtypes "github.com/CosmWasm/wasmvm/types"
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    10  
    11  	"github.com/fibonacci-chain/fbc/x/wasm/types"
    12  )
    13  
    14  // newWasmModuleEvent creates with wasm module event for interacting with the given contract. Adds custom attributes
    15  // to this event.
    16  func newWasmModuleEvent(customAttributes []wasmvmtypes.EventAttribute, contractAddr sdk.AccAddress) (sdk.Events, error) {
    17  	attrs, err := contractSDKEventAttributes(customAttributes, contractAddr)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	// each wasm invocation always returns one sdk.Event
    23  	return sdk.Events{sdk.NewEvent(types.WasmModuleEventType, attrs...)}, nil
    24  }
    25  
    26  const eventTypeMinLength = 2
    27  
    28  // newCustomEvents converts wasmvm events from a contract response to sdk type events
    29  func newCustomEvents(evts wasmvmtypes.Events, contractAddr sdk.AccAddress) (sdk.Events, error) {
    30  	events := make(sdk.Events, 0, len(evts))
    31  	for _, e := range evts {
    32  		typ := strings.TrimSpace(e.Type)
    33  		if len(typ) <= eventTypeMinLength {
    34  			return nil, sdkerrors.Wrap(types.ErrInvalidEvent, fmt.Sprintf("Event type too short: '%s'", typ))
    35  		}
    36  		attributes, err := contractSDKEventAttributes(e.Attributes, contractAddr)
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  		events = append(events, sdk.NewEvent(fmt.Sprintf("%s%s", types.CustomContractEventPrefix, typ), attributes...))
    41  	}
    42  	return events, nil
    43  }
    44  
    45  // convert and add contract address issuing this event
    46  func contractSDKEventAttributes(customAttributes []wasmvmtypes.EventAttribute, contractAddr sdk.AccAddress) ([]sdk.Attribute, error) {
    47  	attrs := []sdk.Attribute{sdk.NewAttribute(types.AttributeKeyContractAddr, contractAddr.String())}
    48  	// append attributes from wasm to the sdk.Event
    49  	for _, l := range customAttributes {
    50  		// ensure key and value are non-empty (and trim what is there)
    51  		key := strings.TrimSpace(l.Key)
    52  		if len(key) == 0 {
    53  			return nil, sdkerrors.Wrap(types.ErrInvalidEvent, fmt.Sprintf("Empty attribute key. Value: %s", l.Value))
    54  		}
    55  		value := strings.TrimSpace(l.Value)
    56  		// TODO: check if this is legal in the SDK - if it is, we can remove this check
    57  		if len(value) == 0 {
    58  			return nil, sdkerrors.Wrap(types.ErrInvalidEvent, fmt.Sprintf("Empty attribute value. Key: %s", key))
    59  		}
    60  		// and reserve all _* keys for our use (not contract)
    61  		if strings.HasPrefix(key, types.AttributeReservedPrefix) {
    62  			return nil, sdkerrors.Wrap(types.ErrInvalidEvent, fmt.Sprintf("Attribute key starts with reserved prefix %s: '%s'", types.AttributeReservedPrefix, key))
    63  		}
    64  		attrs = append(attrs, sdk.NewAttribute(key, value))
    65  	}
    66  	return attrs, nil
    67  }