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

Better to sdk coin convertion #1164

Merged
merged 2 commits into from
Jan 24, 2023
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
3 changes: 3 additions & 0 deletions x/wasm/keeper/handler_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ func NewBurnCoinMessageHandler(burner types.Burner) MessageHandlerFunc {
if err != nil {
return nil, nil, err
}
if coins.IsZero() {
return nil, nil, types.ErrEmpty.Wrap("amount")
}
if err := burner.SendCoinsFromAccountToModule(ctx, contractAddr, types.ModuleName, coins); err != nil {
return nil, nil, sdkerrors.Wrap(err, "transfer to module")
}
Expand Down
5 changes: 2 additions & 3 deletions x/wasm/keeper/handler_plugin_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,9 @@ func ConvertWasmCoinsToSdkCoins(coins []wasmvmtypes.Coin) (sdk.Coins, error) {
if err != nil {
return nil, err
}
toSend = append(toSend, c)
toSend = toSend.Add(c)
}
toSend.Sort()
return toSend, nil
return toSend.Sort(), nil
}

// ConvertWasmCoinToSdkCoin converts a wasm vm type coin to sdk type coin
Expand Down
75 changes: 75 additions & 0 deletions x/wasm/keeper/handler_plugin_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,78 @@ func TestConvertWasmCoinToSdkCoin(t *testing.T) {
})
}
}

func TestConvertWasmCoinsToSdkCoins(t *testing.T) {
specs := map[string]struct {
src []wasmvmtypes.Coin
exp sdk.Coins
expErr bool
}{
"empty": {
src: []wasmvmtypes.Coin{},
exp: nil,
},
"single coin": {
src: []wasmvmtypes.Coin{{Denom: "foo", Amount: "1"}},
exp: sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(1))),
},
"multiple coins": {
src: []wasmvmtypes.Coin{
{Denom: "foo", Amount: "1"},
{Denom: "bar", Amount: "2"},
},
exp: sdk.NewCoins(
sdk.NewCoin("bar", sdk.NewInt(2)),
sdk.NewCoin("foo", sdk.NewInt(1)),
),
},
"sorted": {
src: []wasmvmtypes.Coin{
{Denom: "foo", Amount: "1"},
{Denom: "other", Amount: "1"},
{Denom: "bar", Amount: "1"},
},
exp: []sdk.Coin{
sdk.NewCoin("bar", sdk.NewInt(1)),
sdk.NewCoin("foo", sdk.NewInt(1)),
sdk.NewCoin("other", sdk.NewInt(1)),
},
},
"zero amounts dropped": {
src: []wasmvmtypes.Coin{
{Denom: "foo", Amount: "1"},
{Denom: "bar", Amount: "0"},
},
exp: sdk.NewCoins(
sdk.NewCoin("foo", sdk.NewInt(1)),
),
},
"duplicate denoms merged": {
src: []wasmvmtypes.Coin{
{Denom: "foo", Amount: "1"},
{Denom: "foo", Amount: "1"},
},
exp: []sdk.Coin{sdk.NewCoin("foo", sdk.NewInt(2))},
},
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
"empty denom rejected": {
src: []wasmvmtypes.Coin{{Denom: "", Amount: "1"}},
expErr: true,
},
"invalid denom rejected": {
src: []wasmvmtypes.Coin{{Denom: "!%&", Amount: "1"}},
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
gotCoins, gotErr := ConvertWasmCoinsToSdkCoins(spec.src)
if spec.expErr {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
assert.Equal(t, spec.exp, gotCoins)
assert.NoError(t, gotCoins.Validate())
})
}
}