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

Add tests for submessages with cw721 contract #1736

Draft
wants to merge 4 commits into
base: releases/v0.4x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
Binary file added tests/e2e/testdata/cw721_base.wasm.gz
Binary file not shown.
Binary file added tests/e2e/testdata/cw721_receiver.wasm.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/e2e/testdata/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cw721_base - https://github.com/CosmWasm/cw-nfts/releases/download/v0.17.0/cw721_base.wasm
cw721-receiver - https://github.com/CosmWasm/cw-nfts/pull/144
68 changes: 68 additions & 0 deletions tests/e2e/wasm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package e2e

import (
"encoding/base64"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/CosmWasm/wasmd/x/wasm/types"

"github.com/CosmWasm/wasmd/x/wasm/ibctesting"
)

func TestNFTSubmessages(t *testing.T) {
// Given a cw721_base contract and a compatible contract as receiver
// and a nft minted
// when a send_nft is executed on the base contract, a submessage is emitted
// and handled by the receiver contract
specs := map[string]struct {
submsgPayload string
expErr bool
}{
"succeed": {
submsgPayload: `"succeed"`,
},
"fail": {
submsgPayload: `"fail"`,
expErr: true,
},
}
for name, spec := range specs {
t.Run(name, func(t *testing.T) {
coord := ibctesting.NewCoordinator(t, 1)
chain := coord.GetChain(ibctesting.GetChainID(1))
minterAddress := chain.SenderAccount.GetAddress()

codeID := chain.StoreCodeFile("testdata/cw721_base.wasm.gz").CodeID
senderContractAddr := chain.InstantiateContract(codeID, []byte(fmt.Sprintf(`{"name":"Reece #00001", "symbol":"juno-reece-test-#00001", "minter":"%s"}`, minterAddress.String())))

codeID = chain.StoreCodeFile("testdata/cw721_receiver.wasm.gz").CodeID
receiverContractAddr := chain.InstantiateContract(codeID, []byte(`{}`))

// and token minted
execMsg := types.MsgExecuteContract{
Sender: minterAddress.String(),
Contract: senderContractAddr.String(),
Msg: []byte(fmt.Sprintf(`{"mint":{"token_id":"00000", "owner":"%s"}}`, minterAddress.String())),
}
_, err := chain.SendMsgs(&execMsg)
require.NoError(t, err)

// when submessages is emitted
execMsg = types.MsgExecuteContract{
Sender: minterAddress.String(),
Contract: senderContractAddr.String(),
Msg: []byte(fmt.Sprintf(`{"send_nft": { "contract": "%s", "token_id": "00000", "msg": "%s" }}`, receiverContractAddr.String(), base64.RawStdEncoding.EncodeToString([]byte(spec.submsgPayload)))),
}
_, gotErr := chain.SendMsgs(&execMsg)
// then
if spec.expErr {
require.Error(t, gotErr)
return
}
require.NoError(t, err)
})
}
}
1 change: 1 addition & 0 deletions tests/system/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/testnet
/binaries
35 changes: 35 additions & 0 deletions tests/system/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,38 @@ func TestMultiContract(t *testing.T) {
assert.Equal(t, int64(0), cli.QueryBalance(hackatomContractAddr, "stake"))
assert.Equal(t, int64(70), cli.QueryBalance(bobAddr, "stake"))
}

func TestNFTSubmessages(t *testing.T) {
// Given a cw721_base contract and a compatible contract as receiver
// and a nft minted
// when a send_nft is executed on the base contract, a submessage is emitted
// and handled by the receiver contract

sut.ResetChain(t)
sut.StartChain(t)

cli := NewWasmdCLI(t, sut, verbose)

minterAddress := cli.GetKeyAddr(defaultSrcAddr)

t.Log("Upload cw721-base code")
codeID := cli.WasmStore("../e2e/testdata/cw721_base.wasm.gz", "--from=node0", "--gas=auto", "--gas-adjustment=1.4", "--fees=4stake")
t.Log("Instantiate cw721-base code")
msg := fmt.Sprintf(`{"name":"Reece #00001", "symbol":"juno-reece-test-#00001", "minter":"%s"}`, minterAddress)
senderContractAddr := cli.WasmInstantiate(codeID, msg, "--admin="+defaultSrcAddr, "--label=reflect_contract", "--from="+defaultSrcAddr, "--amount=100stake")

t.Log("Upload cw721-receiver code")
codeID = cli.WasmStore("../e2e/testdata/cw721_receiver.wasm.gz", "--from=node0", "--gas=auto", "--gas-adjustment=1.4", "--fees=4stake")
t.Log("Instantiate cw721-receiver code")
receiverContractAddr := cli.WasmInstantiate(codeID, "{}", "--admin="+defaultSrcAddr, "--label=reflect_contract", "--from="+defaultSrcAddr, "--amount=100stake")

// and token minted
msg = fmt.Sprintf(`{"mint":{"token_id":"00000", "owner":"%s"}}`, minterAddress)
rsp := cli.WasmExecute(senderContractAddr, msg, defaultSrcAddr, "--gas=auto", "--gas-adjustment=1.4", "--fees=4stake")
RequireTxSuccess(t, rsp)

// when submessages is emitted
msg = fmt.Sprintf(`{"send_nft": { "contract": "%s", "token_id": "00000", "msg": "%s" }}`, receiverContractAddr, base64.RawStdEncoding.EncodeToString([]byte(`"succeed"`)))
rsp = cli.WasmExecute(senderContractAddr, msg, defaultSrcAddr, "--gas=auto", "--gas-adjustment=1.4", "--fees=4stake")
RequireTxSuccess(t, rsp)
}