-
Notifications
You must be signed in to change notification settings - Fork 412
/
handler_plugin_encoders.go
408 lines (381 loc) · 13.1 KB
/
handler_plugin_encoders.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package keeper
import (
"encoding/json"
"fmt"
wasmvmtypes "github.com/CosmWasm/wasmvm/v2/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
type (
BankEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.BankMsg) ([]sdk.Msg, error)
CustomEncoder func(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error)
DistributionEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.DistributionMsg) ([]sdk.Msg, error)
StakingEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk.Msg, error)
AnyEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.AnyMsg) ([]sdk.Msg, error)
WasmEncoder func(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg, error)
IBCEncoder func(ctx sdk.Context, sender sdk.AccAddress, contractIBCPortID string, msg *wasmvmtypes.IBCMsg) ([]sdk.Msg, error)
)
type MessageEncoders struct {
Bank func(sender sdk.AccAddress, msg *wasmvmtypes.BankMsg) ([]sdk.Msg, error)
Custom func(sender sdk.AccAddress, msg json.RawMessage) ([]sdk.Msg, error)
Distribution func(sender sdk.AccAddress, msg *wasmvmtypes.DistributionMsg) ([]sdk.Msg, error)
IBC func(ctx sdk.Context, sender sdk.AccAddress, contractIBCPortID string, msg *wasmvmtypes.IBCMsg) ([]sdk.Msg, error)
Staking func(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk.Msg, error)
Any func(sender sdk.AccAddress, msg *wasmvmtypes.AnyMsg) ([]sdk.Msg, error)
Wasm func(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg, error)
Gov func(sender sdk.AccAddress, msg *wasmvmtypes.GovMsg) ([]sdk.Msg, error)
}
func DefaultEncoders(unpacker codectypes.AnyUnpacker, portSource types.ICS20TransferPortSource) MessageEncoders {
return MessageEncoders{
Bank: EncodeBankMsg,
Custom: NoCustomMsg,
Distribution: EncodeDistributionMsg,
IBC: EncodeIBCMsg(portSource),
Staking: EncodeStakingMsg,
Any: EncodeAnyMsg(unpacker),
Wasm: EncodeWasmMsg,
Gov: EncodeGovMsg,
}
}
func (e MessageEncoders) Merge(o *MessageEncoders) MessageEncoders {
if o == nil {
return e
}
if o.Bank != nil {
e.Bank = o.Bank
}
if o.Custom != nil {
e.Custom = o.Custom
}
if o.Distribution != nil {
e.Distribution = o.Distribution
}
if o.IBC != nil {
e.IBC = o.IBC
}
if o.Staking != nil {
e.Staking = o.Staking
}
if o.Any != nil {
e.Any = o.Any
}
if o.Wasm != nil {
e.Wasm = o.Wasm
}
if o.Gov != nil {
e.Gov = o.Gov
}
return e
}
func (e MessageEncoders) Encode(ctx sdk.Context, contractAddr sdk.AccAddress, contractIBCPortID string, msg wasmvmtypes.CosmosMsg) ([]sdk.Msg, error) {
switch {
case msg.Bank != nil:
return e.Bank(contractAddr, msg.Bank)
case msg.Custom != nil:
return e.Custom(contractAddr, msg.Custom)
case msg.Distribution != nil:
return e.Distribution(contractAddr, msg.Distribution)
case msg.IBC != nil:
return e.IBC(ctx, contractAddr, contractIBCPortID, msg.IBC)
case msg.Staking != nil:
return e.Staking(contractAddr, msg.Staking)
case msg.Any != nil:
return e.Any(contractAddr, msg.Any)
case msg.Wasm != nil:
return e.Wasm(contractAddr, msg.Wasm)
case msg.Gov != nil:
return EncodeGovMsg(contractAddr, msg.Gov)
}
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "unknown variant of Wasm")
}
func EncodeBankMsg(sender sdk.AccAddress, msg *wasmvmtypes.BankMsg) ([]sdk.Msg, error) {
if msg.Send == nil {
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "unknown variant of Bank")
}
if len(msg.Send.Amount) == 0 {
return nil, nil
}
toSend, err := ConvertWasmCoinsToSdkCoins(msg.Send.Amount)
if err != nil {
return nil, err
}
sdkMsg := banktypes.MsgSend{
FromAddress: sender.String(),
ToAddress: msg.Send.ToAddress,
Amount: toSend,
}
return []sdk.Msg{&sdkMsg}, nil
}
func NoCustomMsg(_ sdk.AccAddress, _ json.RawMessage) ([]sdk.Msg, error) {
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "custom variant not supported")
}
func EncodeDistributionMsg(sender sdk.AccAddress, msg *wasmvmtypes.DistributionMsg) ([]sdk.Msg, error) {
switch {
case msg.SetWithdrawAddress != nil:
setMsg := distributiontypes.MsgSetWithdrawAddress{
DelegatorAddress: sender.String(),
WithdrawAddress: msg.SetWithdrawAddress.Address,
}
return []sdk.Msg{&setMsg}, nil
case msg.WithdrawDelegatorReward != nil:
withdrawMsg := distributiontypes.MsgWithdrawDelegatorReward{
DelegatorAddress: sender.String(),
ValidatorAddress: msg.WithdrawDelegatorReward.Validator,
}
return []sdk.Msg{&withdrawMsg}, nil
case msg.FundCommunityPool != nil:
amt, err := ConvertWasmCoinsToSdkCoins(msg.FundCommunityPool.Amount)
if err != nil {
return nil, err
}
fundMsg := distributiontypes.MsgFundCommunityPool{
Depositor: sender.String(),
Amount: amt,
}
return []sdk.Msg{&fundMsg}, nil
default:
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "unknown variant of Distribution")
}
}
func EncodeStakingMsg(sender sdk.AccAddress, msg *wasmvmtypes.StakingMsg) ([]sdk.Msg, error) {
switch {
case msg.Delegate != nil:
coin, err := ConvertWasmCoinToSdkCoin(msg.Delegate.Amount)
if err != nil {
return nil, err
}
sdkMsg := stakingtypes.MsgDelegate{
DelegatorAddress: sender.String(),
ValidatorAddress: msg.Delegate.Validator,
Amount: coin,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.Redelegate != nil:
coin, err := ConvertWasmCoinToSdkCoin(msg.Redelegate.Amount)
if err != nil {
return nil, err
}
sdkMsg := stakingtypes.MsgBeginRedelegate{
DelegatorAddress: sender.String(),
ValidatorSrcAddress: msg.Redelegate.SrcValidator,
ValidatorDstAddress: msg.Redelegate.DstValidator,
Amount: coin,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.Undelegate != nil:
coin, err := ConvertWasmCoinToSdkCoin(msg.Undelegate.Amount)
if err != nil {
return nil, err
}
sdkMsg := stakingtypes.MsgUndelegate{
DelegatorAddress: sender.String(),
ValidatorAddress: msg.Undelegate.Validator,
Amount: coin,
}
return []sdk.Msg{&sdkMsg}, nil
default:
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "unknown variant of Staking")
}
}
func EncodeAnyMsg(unpacker codectypes.AnyUnpacker) AnyEncoder {
return func(sender sdk.AccAddress, msg *wasmvmtypes.AnyMsg) ([]sdk.Msg, error) {
codecAny := codectypes.Any{
TypeUrl: msg.TypeURL,
Value: msg.Value,
}
var sdkMsg sdk.Msg
if err := unpacker.UnpackAny(&codecAny, &sdkMsg); err != nil {
return nil, errorsmod.Wrap(types.ErrInvalidMsg, fmt.Sprintf("Cannot unpack proto message with type URL: %s", msg.TypeURL))
}
if err := codectypes.UnpackInterfaces(sdkMsg, unpacker); err != nil {
return nil, errorsmod.Wrap(types.ErrInvalidMsg, fmt.Sprintf("UnpackInterfaces inside msg: %s", err))
}
return []sdk.Msg{sdkMsg}, nil
}
}
func EncodeWasmMsg(sender sdk.AccAddress, msg *wasmvmtypes.WasmMsg) ([]sdk.Msg, error) {
switch {
case msg.Execute != nil:
coins, err := ConvertWasmCoinsToSdkCoins(msg.Execute.Funds)
if err != nil {
return nil, err
}
sdkMsg := types.MsgExecuteContract{
Sender: sender.String(),
Contract: msg.Execute.ContractAddr,
Msg: msg.Execute.Msg,
Funds: coins,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.Instantiate != nil:
coins, err := ConvertWasmCoinsToSdkCoins(msg.Instantiate.Funds)
if err != nil {
return nil, err
}
sdkMsg := types.MsgInstantiateContract{
Sender: sender.String(),
CodeID: msg.Instantiate.CodeID,
Label: msg.Instantiate.Label,
Msg: msg.Instantiate.Msg,
Admin: msg.Instantiate.Admin,
Funds: coins,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.Instantiate2 != nil:
coins, err := ConvertWasmCoinsToSdkCoins(msg.Instantiate2.Funds)
if err != nil {
return nil, err
}
sdkMsg := types.MsgInstantiateContract2{
Sender: sender.String(),
Admin: msg.Instantiate2.Admin,
CodeID: msg.Instantiate2.CodeID,
Label: msg.Instantiate2.Label,
Msg: msg.Instantiate2.Msg,
Funds: coins,
Salt: msg.Instantiate2.Salt,
// FixMsg is discouraged, see: https://medium.com/cosmwasm/dev-note-3-limitations-of-instantiate2-and-how-to-deal-with-them-a3f946874230
FixMsg: false,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.Migrate != nil:
sdkMsg := types.MsgMigrateContract{
Sender: sender.String(),
Contract: msg.Migrate.ContractAddr,
CodeID: msg.Migrate.NewCodeID,
Msg: msg.Migrate.Msg,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.ClearAdmin != nil:
sdkMsg := types.MsgClearAdmin{
Sender: sender.String(),
Contract: msg.ClearAdmin.ContractAddr,
}
return []sdk.Msg{&sdkMsg}, nil
case msg.UpdateAdmin != nil:
sdkMsg := types.MsgUpdateAdmin{
Sender: sender.String(),
Contract: msg.UpdateAdmin.ContractAddr,
NewAdmin: msg.UpdateAdmin.Admin,
}
return []sdk.Msg{&sdkMsg}, nil
default:
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "unknown variant of Wasm")
}
}
func EncodeIBCMsg(portSource types.ICS20TransferPortSource) func(ctx sdk.Context, sender sdk.AccAddress, contractIBCPortID string, msg *wasmvmtypes.IBCMsg) ([]sdk.Msg, error) {
return func(ctx sdk.Context, sender sdk.AccAddress, contractIBCPortID string, msg *wasmvmtypes.IBCMsg) ([]sdk.Msg, error) {
switch {
case msg.CloseChannel != nil:
return []sdk.Msg{&channeltypes.MsgChannelCloseInit{
PortId: PortIDForContract(sender),
ChannelId: msg.CloseChannel.ChannelID,
Signer: sender.String(),
}}, nil
case msg.Transfer != nil:
amount, err := ConvertWasmCoinToSdkCoin(msg.Transfer.Amount)
if err != nil {
return nil, errorsmod.Wrap(err, "amount")
}
msg := &ibctransfertypes.MsgTransfer{
SourcePort: portSource.GetPort(ctx),
SourceChannel: msg.Transfer.ChannelID,
Token: amount,
Sender: sender.String(),
Receiver: msg.Transfer.ToAddress,
TimeoutHeight: ConvertWasmIBCTimeoutHeightToCosmosHeight(msg.Transfer.Timeout.Block),
TimeoutTimestamp: msg.Transfer.Timeout.Timestamp,
Memo: msg.Transfer.Memo,
}
return []sdk.Msg{msg}, nil
default:
return nil, errorsmod.Wrap(types.ErrUnknownMsg, "unknown variant of IBC")
}
}
}
func EncodeGovMsg(sender sdk.AccAddress, msg *wasmvmtypes.GovMsg) ([]sdk.Msg, error) {
switch {
case msg.Vote != nil:
voteOption, err := convertVoteOption(msg.Vote.Option)
if err != nil {
return nil, errorsmod.Wrap(err, "vote option")
}
m := v1.NewMsgVote(sender, msg.Vote.ProposalId, voteOption, "")
return []sdk.Msg{m}, nil
case msg.VoteWeighted != nil:
opts := make([]*v1.WeightedVoteOption, len(msg.VoteWeighted.Options))
for i, v := range msg.VoteWeighted.Options {
weight, err := sdkmath.LegacyNewDecFromStr(v.Weight)
if err != nil {
return nil, errorsmod.Wrapf(err, "weight for vote %d", i+1)
}
voteOption, err := convertVoteOption(v.Option)
if err != nil {
return nil, errorsmod.Wrap(err, "vote option")
}
opts[i] = &v1.WeightedVoteOption{Option: voteOption, Weight: weight.String()}
}
m := v1.NewMsgVoteWeighted(sender, msg.VoteWeighted.ProposalId, opts, "")
return []sdk.Msg{m}, nil
default:
return nil, types.ErrUnknownMsg.Wrap("unknown variant of gov")
}
}
func convertVoteOption(s interface{}) (v1.VoteOption, error) {
var option v1.VoteOption
switch s {
case wasmvmtypes.Yes:
option = v1.OptionYes
case wasmvmtypes.No:
option = v1.OptionNo
case wasmvmtypes.NoWithVeto:
option = v1.OptionNoWithVeto
case wasmvmtypes.Abstain:
option = v1.OptionAbstain
default:
return v1.OptionEmpty, types.ErrInvalid
}
return option, nil
}
// ConvertWasmIBCTimeoutHeightToCosmosHeight converts a wasmvm type ibc timeout height to ibc module type height
func ConvertWasmIBCTimeoutHeightToCosmosHeight(ibcTimeoutBlock *wasmvmtypes.IBCTimeoutBlock) ibcclienttypes.Height {
if ibcTimeoutBlock == nil {
return ibcclienttypes.NewHeight(0, 0)
}
return ibcclienttypes.NewHeight(ibcTimeoutBlock.Revision, ibcTimeoutBlock.Height)
}
// ConvertWasmCoinsToSdkCoins converts the wasm vm type coins to sdk type coins
func ConvertWasmCoinsToSdkCoins(coins []wasmvmtypes.Coin) (sdk.Coins, error) {
var toSend sdk.Coins
for _, coin := range coins {
c, err := ConvertWasmCoinToSdkCoin(coin)
if err != nil {
return nil, err
}
toSend = toSend.Add(c)
}
return toSend.Sort(), nil
}
// ConvertWasmCoinToSdkCoin converts a wasm vm type coin to sdk type coin
func ConvertWasmCoinToSdkCoin(coin wasmvmtypes.Coin) (sdk.Coin, error) {
amount, ok := sdkmath.NewIntFromString(coin.Amount)
if !ok {
return sdk.Coin{}, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, coin.Amount+coin.Denom)
}
r := sdk.Coin{
Denom: coin.Denom,
Amount: amount,
}
return r, r.Validate()
}