Skip to content

Commit

Permalink
Merge branch 'main' into test/TestStoreAndInstantiateContract
Browse files Browse the repository at this point in the history
  • Loading branch information
da1suk8 authored Aug 18, 2023
2 parents 4a6a16c + bb3a88f commit a347660
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* [\#73](https://github.com/Finschia/wasmd/pull/73) test: add the check for expPaginationTotal
* [\#72](https://github.com/Finschia/wasmd/pull/72) add pagination next key test in ContractHistory
* [\#75](https://github.com/Finschia/wasmd/pull/75) test: add the test case for InactiveContract
* [\#74](https://github.com/Finschia/wasmd/pull/74) add event checking to TestInstantiateContract2
* [\#79](https://github.com/Finschia/wasmd/pull/79) add an integration test for TestStoreAndInstantiateContract

### Bug Fixes
Expand Down
87 changes: 87 additions & 0 deletions x/wasm/keeper/msg_server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,90 @@ func TestInstantiateContract(t *testing.T) {
})
}
}

func TestInstantiateContract2(t *testing.T) {
wasmApp := app.Setup(false)
ctx := wasmApp.BaseApp.NewContext(false, tmproto.Header{Time: time.Now()})

var (
myAddress sdk.AccAddress = make([]byte, types.ContractAddrLen)
)

specs := map[string]struct {
addr string
permission *types.AccessConfig
salt string
expErr bool
}{
"address can instantiate a contract when permission is everybody": {
addr: myAddress.String(),
permission: &types.AllowEverybody,
salt: "salt1",
expErr: false,
},
"address cannot instantiate a contract when permission is nobody": {
addr: myAddress.String(),
permission: &types.AllowNobody,
salt: "salt2",
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
xCtx, _ := ctx.CacheContext()
// setup
_, _, sender := testdata.KeyTestPubAddr()
msg := types.MsgStoreCodeFixture(func(m *types.MsgStoreCode) {
m.WASMByteCode = wasmContract
m.Sender = sender.String()
m.InstantiatePermission = spec.permission
})

// store code
rsp, err := wasmApp.MsgServiceRouter().Handler(msg)(xCtx, msg)
require.NoError(t, err)
var result types.MsgStoreCodeResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &result))

// when
msgInstantiate := &types.MsgInstantiateContract2{
Sender: spec.addr,
Admin: myAddress.String(),
CodeID: result.CodeID,
Label: "test",
Msg: []byte(`{}`),
Funds: sdk.Coins{},
Salt: []byte(spec.salt),
FixMsg: true,
}
rsp, err = wasmApp.MsgServiceRouter().Handler(msgInstantiate)(xCtx, msgInstantiate)

//then
if spec.expErr {
require.Error(t, err)
return
}

var instantiateResponse types.MsgInstantiateContractResponse
require.NoError(t, wasmApp.AppCodec().Unmarshal(rsp.Data, &instantiateResponse))

// check event
events := rsp.Events
assert.Equal(t, 2, len(events))
assert.Equal(t, "message", events[0].Type)
assert.Equal(t, 2, len(events[0].Attributes))
assert.Equal(t, "module", string(events[0].Attributes[0].Key))
assert.Equal(t, "wasm", string(events[0].Attributes[0].Value))
assert.Equal(t, "sender", string(events[0].Attributes[1].Key))
assert.Equal(t, myAddress.String(), string(events[0].Attributes[1].Value))
assert.Equal(t, "instantiate", events[1].Type)
assert.Equal(t, 2, len(events[1].Attributes))
assert.Equal(t, "_contract_address", string(events[1].Attributes[0].Key))
assert.Equal(t, instantiateResponse.Address, string(events[1].Attributes[0].Value))
assert.Equal(t, "code_id", string(events[1].Attributes[1].Key))
assert.Equal(t, "1", string(events[1].Attributes[1].Value))

require.NoError(t, err)
})
}
}

0 comments on commit a347660

Please sign in to comment.