@@ -13,86 +13,121 @@ import (
1313 "go.uber.org/zap/zaptest"
1414)
1515
16+ // GetCountResponse represents the response structure from the CosmWasm contract's get_count query
1617type GetCountResponse struct {
1718 // {"data":{"count":0}}
1819 Data * GetCountObj `json:"data"`
1920}
2021
22+ // GetCountObj holds the actual count value from the contract's state
2123type GetCountObj struct {
2224 Count int64 `json:"count"`
2325}
2426
27+ // TestCosmWasmIntegration verifies that CosmWasm smart contracts can be deployed and executed on the chain.
28+ // This test ensures that:
29+ // 1. A chain with CosmWasm support can be properly initialized
30+ // 2. A CosmWasm contract can be stored on the chain
31+ // 3. The contract can be instantiated with initial state
32+ // 4. Contract execution (state changes) works correctly
33+ // 5. Contract queries return the expected results
2534func TestCosmWasmIntegration (t * testing.T ) {
2635 t .Parallel ()
2736 ctx := context .Background ()
2837 rep := testreporter .NewNopReporter ()
2938 eRep := rep .RelayerExecReporter (t )
39+ // Set up Docker environment for the test
3040 client , network := interchaintest .DockerSetup (t )
3141
42+ // Create a chain factory with our default chain specification
3243 cf := interchaintest .NewBuiltinChainFactory (zaptest .NewLogger (t ), []* interchaintest.ChainSpec {
3344 & DefaultChainSpec ,
3445 })
3546
47+ // Initialize the chain from the factory
3648 chains , err := cf .Chains (t .Name ())
3749 require .NoError (t , err )
3850
51+ // Get the chain from the list (we only have one in this test)
3952 chain := chains [0 ].(* cosmos.CosmosChain )
4053
41- // Setup Interchain
54+ // Setup Interchain environment with our single chain
4255 ic := interchaintest .NewInterchain ().
4356 AddChain (chain )
4457
58+ // Build the interchain environment
4559 require .NoError (t , ic .Build (ctx , eRep , interchaintest.InterchainBuildOptions {
4660 TestName : t .Name (),
4761 Client : client ,
4862 NetworkID : network ,
4963 SkipPathCreation : false ,
5064 }))
65+ // Clean up resources when the test completes
5166 t .Cleanup (func () {
5267 _ = ic .Close ()
5368 })
5469
70+ // Create and fund a test user
5571 users := interchaintest .GetAndFundTestUsers (t , ctx , t .Name (), GenesisFundsAmount , chain )
5672 user := users [0 ]
5773
74+ // Execute the standard CosmWasm test flow
5875 StdExecute (t , ctx , chain , user )
5976}
6077
78+ // StdExecute performs a standard flow of CosmWasm operations:
79+ // 1. Uploads a contract to the chain
80+ // 2. Instantiates the contract with initial state
81+ // 3. Executes a transaction on the contract to increment the counter
82+ // 4. Queries the contract to verify the state change
6183func StdExecute (t * testing.T , ctx context.Context , chain * cosmos.CosmosChain , user ibc.Wallet ) (contractAddr string ) {
84+ // Upload and instantiate the contract with initial count of 0
6285 _ , contractAddr = SetupContract (t , ctx , chain , user .KeyName (), "contracts/cw_template.wasm" , `{"count":0}` )
86+
87+ // Execute the increment operation on the contract
6388 chain .ExecuteContract (ctx , user .KeyName (), contractAddr , `{"increment":{}}` , "--fees" , "10000" + chain .Config ().Denom )
6489
90+ // Query the contract to verify the count was incremented
6591 var res GetCountResponse
6692 err := SmartQueryString (t , ctx , chain , contractAddr , `{"get_count":{}}` , & res )
6793 require .NoError (t , err )
6894
95+ // Verify the count is now 1 after the increment operation
6996 require .Equal (t , int64 (1 ), res .Data .Count )
7097
7198 return contractAddr
7299}
73100
101+ // SmartQueryString performs a query on a CosmWasm contract and unmarshals the result into the provided response object
74102func SmartQueryString (t * testing.T , ctx context.Context , chain * cosmos.CosmosChain , contractAddr , queryMsg string , res interface {}) error {
103+ // Convert the query string to a JSON map for the chain's QueryContract method
75104 var jsonMap map [string ]interface {}
76105 if err := json .Unmarshal ([]byte (queryMsg ), & jsonMap ); err != nil {
77106 t .Fatal (err )
78107 }
108+ // Execute the query and unmarshal the result into the provided response object
79109 err := chain .QueryContract (ctx , contractAddr , jsonMap , & res )
80110 return err
81111}
82112
113+ // SetupContract uploads a CosmWasm contract to the chain and instantiates it
114+ // Returns the code ID and contract address
83115func SetupContract (t * testing.T , ctx context.Context , chain * cosmos.CosmosChain , keyname string , fileLoc string , message string , extraFlags ... string ) (codeId , contract string ) {
116+ // Store the contract on the chain
84117 codeId , err := chain .StoreContract (ctx , keyname , fileLoc )
85118 if err != nil {
86119 t .Fatal (err )
87120 }
88121
122+ // Determine if we need to add the --no-admin flag
89123 needsNoAdminFlag := true
90124 for _ , flag := range extraFlags {
91125 if flag == "--admin" {
92126 needsNoAdminFlag = false
93127 }
94128 }
95129
130+ // Instantiate the contract with the provided message
96131 contractAddr , err := chain .InstantiateContract (ctx , keyname , codeId , message , needsNoAdminFlag , extraFlags ... )
97132 if err != nil {
98133 t .Fatal (err )
0 commit comments