Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message mapping for gov proposals #295

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion x/twasm/contract/incoming_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package contract

import (
"encoding/json"

wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
Expand Down Expand Up @@ -196,6 +196,40 @@ func (p *GovProposal) UnmarshalJSON(b []byte) error {
}
return nil
},
"migrate_contract": func(b []byte) error {
var proxy = struct { // todo: better use wasmvmtypes.MigrateMsg when names match
alpe marked this conversation as resolved.
Show resolved Hide resolved
Contract string `json:"contract"`
CodeID uint64 `json:"code_id"`
Msg []byte `json:"msg"`
alpe marked this conversation as resolved.
Show resolved Hide resolved
}{}
if err := json.Unmarshal(b, &proxy); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
result.MigrateContract = &wasmtypes.MigrateContractProposal{
Contract: proxy.Contract,
CodeID: proxy.CodeID,
Msg: proxy.Msg,
}
return nil
}, "instantiate_contract": func(b []byte) error {
alpe marked this conversation as resolved.
Show resolved Hide resolved
var proxy = wasmvmtypes.InstantiateMsg{}
if err := json.Unmarshal(b, &proxy); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
funds, err := convertWasmCoinsToSdkCoins(proxy.Funds)
if err != nil {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
result.InstantiateContract = &wasmtypes.InstantiateContractProposal{
//RunAs: "",
Admin: proxy.Admin,
CodeID: proxy.CodeID,
Label: proxy.Label,
Msg: proxy.Msg,
Funds: funds,
}
return nil
},
}
for field, unmarshaler := range customUnmarshalers {
if bz, ok := raws[field]; ok {
Expand Down Expand Up @@ -331,3 +365,29 @@ func (p *EvidenceParams) ValidateBasic() error {
}
return nil
}

// copied from wasmd. should be public soon
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 = append(toSend, c)
}
return toSend, nil
}

// copied from wasmd. should be public soon
func convertWasmCoinToSdkCoin(coin wasmvmtypes.Coin) (sdk.Coin, error) {
amount, ok := sdk.NewIntFromString(coin.Amount)
if !ok {
return sdk.Coin{}, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, coin.Amount+coin.Denom)
}
r := sdk.Coin{
Denom: coin.Denom,
Amount: amount,
}
return r, r.Validate()
}
4 changes: 2 additions & 2 deletions x/twasm/contract/incoming_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestGetProposalContent(t *testing.T) {
"admin": "cosmos1g6chpwdke3kz69x67jkak5y7gynneqm3nulfrd",
"code_id": 1,
"funds": [{"denom": "ALX", "amount": "2"},{"denom": "BLX","amount": "3"}],
"msg": {},
"msg": "e30=",
"label": "testing"
}}}}`,
expGovProposal: &wasmtypes.InstantiateContractProposal{
Expand Down Expand Up @@ -155,7 +155,7 @@ func TestGetProposalContent(t *testing.T) {
"migrate_contract": {
"code_id": 1,
"contract": "cosmos1vtg95naqtvf99hj8pe0s9aevy622vt0jmupc09",
"msg": {}
"msg": "e30="
}}}}`,
expGovProposal: &wasmtypes.MigrateContractProposal{
Title: "foo",
Expand Down