Skip to content

Commit

Permalink
Merge pull request #537 from CosmWasm/check_coin
Browse files Browse the repository at this point in the history
Verify converted coin
  • Loading branch information
alpe authored Jun 14, 2021
2 parents e7526cc + 110da0f commit 3b82807
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
5 changes: 3 additions & 2 deletions x/wasm/keeper/handler_plugin_encoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,9 @@ func convertWasmCoinToSdkCoin(coin wasmvmtypes.Coin) (sdk.Coin, error) {
if !ok {
return sdk.Coin{}, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, coin.Amount+coin.Denom)
}
return sdk.Coin{
r := sdk.Coin{
Denom: coin.Denom,
Amount: amount,
}, nil
}
return r, r.Validate()
}
56 changes: 56 additions & 0 deletions x/wasm/keeper/handler_plugin_encoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,59 @@ func TestEncoding(t *testing.T) {
})
}
}

func TestConvertWasmCoinToSdkCoin(t *testing.T) {
specs := map[string]struct {
src wasmvmtypes.Coin
expErr bool
expVal sdk.Coin
}{
"all good": {
src: wasmvmtypes.Coin{
Denom: "foo",
Amount: "1",
},
expVal: sdk.NewCoin("foo", sdk.NewIntFromUint64(1)),
},
"negative amount": {
src: wasmvmtypes.Coin{
Denom: "foo",
Amount: "-1",
},
expErr: true,
},
"denom to short": {
src: wasmvmtypes.Coin{
Denom: "f",
Amount: "1",
},
expErr: true,
},
"invalid demum char": {
src: wasmvmtypes.Coin{
Denom: "&fff",
Amount: "1",
},
expErr: true,
},
"not a number amount": {
src: wasmvmtypes.Coin{
Denom: "foo",
Amount: "bar",
},
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
gotVal, gotErr := convertWasmCoinToSdkCoin(spec.src)
if spec.expErr {
require.Error(t, gotErr)
return
}
require.NoError(t, gotErr)
assert.Equal(t, spec.expVal, gotVal)
})
}

}

0 comments on commit 3b82807

Please sign in to comment.