-
Notifications
You must be signed in to change notification settings - Fork 88
/
encoder.go
51 lines (44 loc) · 1.62 KB
/
encoder.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
package wasm
import (
"encoding/json"
"github.com/CosmWasm/wasmd/x/wasm"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Encoder describes behavior for Stargaze smart contract message encoding.
// The contract address must ALWAYS be set as the Msg signer.
type Encoder func(contract sdk.AccAddress, data json.RawMessage, version string) ([]sdk.Msg, error)
// MessageEncoders provides stargaze custom encoder for contracts
func MessageEncoders(registry *EncoderRegistry) *wasm.MessageEncoders {
return &wasm.MessageEncoders{
Custom: customEncoders(registry),
}
}
type MessageEncodeRequest struct {
Route string `json:"route"`
MsgData json.RawMessage `json:"msg_data"`
Version string `json:"version"`
}
func customEncoders(registry *EncoderRegistry) wasm.CustomEncoder {
return func(sender sdk.AccAddress, m json.RawMessage) ([]sdk.Msg, error) {
encodeRequest := &MessageEncodeRequest{}
err := json.Unmarshal(m, encodeRequest)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
encode, exists := registry.encoders[encodeRequest.Route]
if !exists {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "encoder not found for route: %s", encodeRequest.Route)
}
msgs, err := encode(sender, encodeRequest.MsgData, encodeRequest.Version)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
for _, msg := range msgs {
if err := msg.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
}
return msgs, nil
}
}