forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
85 lines (75 loc) · 2.22 KB
/
handler.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package contract
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/tendermint/tendermint/libs/bech32"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewHandler creates a new handler for contract module
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgStoreCode:
return handleMsgStoreCode(ctx, keeper, msg)
case MsgCreateContract:
return handleMsgCreateContract(ctx, keeper, msg)
case MsgSendContract:
return handleMsgSendContract(ctx, keeper, msg)
default:
errMsg := fmt.Sprintf("Unrecognized contract message type: %T", msg)
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
const (
Bech32CodePrefix = "code"
)
func EncodeBech32CodeID(id CodeID) (string, error) {
addr := make([]byte, binary.MaxVarintLen64+1)
n := binary.PutUvarint(addr, uint64(id))
bch, err := bech32.ConvertAndEncode(Bech32CodePrefix, addr[:n])
if err != nil {
return "", err
}
return bch, nil
}
func DecodeBech32CodeID(bch string) (CodeID, error) {
hrp, bz, err := bech32.DecodeAndConvert(bch)
if err != nil {
return 0, err
}
if hrp != Bech32CodePrefix {
return 0, fmt.Errorf("expected bech32 prefix %s, got %s", Bech32CodePrefix, hrp)
}
n, err := binary.ReadUvarint(bytes.NewBuffer(bz))
if err != nil {
return 0, err
}
return CodeID(n), nil
}
func handleMsgStoreCode(ctx sdk.Context, keeper Keeper, msg MsgStoreCode) sdk.Result {
id, err := keeper.StoreCode(ctx, msg.WASMByteCode)
if err != nil {
return err.Result()
}
bch, e := EncodeBech32CodeID(id)
if e != nil {
return sdk.ErrUnknownRequest(e.Error()).Result()
}
fmt.Printf("contract: %s\n", bch)
res := sdk.Result{Data: []byte(bch)}
res.Tags = res.Tags.AppendTag("contract.code-id", bch)
return res
}
func handleMsgCreateContract(ctx sdk.Context, keeper Keeper, msg MsgCreateContract) sdk.Result {
id, res := keeper.CreateContract(ctx, msg.Sender, msg.Code, msg.InitMsg, msg.InitFunds)
if !res.IsOK() {
return res
}
res.Tags = res.Tags.AppendTag("contract.address", id.String())
return res
}
func handleMsgSendContract(ctx sdk.Context, keeper Keeper, msg MsgSendContract) sdk.Result {
return keeper.SendContract(ctx, msg.Sender, msg.Contract, msg.Msg, msg.Payment)
}