Skip to content

Commit

Permalink
Merge pull request #1170 from CosmWasm/mergify/bp/develop_sdk47/pr-1164
Browse files Browse the repository at this point in the history
Better to sdk coin convertion (backport #1164)
  • Loading branch information
alpe authored Jan 24, 2023
2 parents 22880a8 + cc28399 commit 650af69
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
3 changes: 3 additions & 0 deletions x/wasm/keeper/handler_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,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 @@ -330,10 +330,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
82 changes: 82 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,85 @@ 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))},
},
"duplicate denoms with one 0 amount does not fail": {
src: []wasmvmtypes.Coin{
{Denom: "foo", Amount: "0"},
{Denom: "foo", Amount: "1"},
},
exp: []sdk.Coin{sdk.NewCoin("foo", sdk.NewInt(1))},
},
"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())
})
}
}

0 comments on commit 650af69

Please sign in to comment.