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