Skip to content

Commit

Permalink
refactor(e2e): refactored types package (#43)
Browse files Browse the repository at this point in the history
* refactor(e2e): started refactor

* refactor: continued

* refactor: improve refactor

* refactor: more refactor

* refactor: removing more

* fix: refactor

* fix: refactor

* refactor: remove more code

* fix: refactor

* refactor: removed more code

* refactor: removed some code

* refactor: starting to improve queries

* refactor: starting to remove query functions

* refactor: removed ica query helpers

* style: renamed a function

* refactor: removed unneeded code

* refactor: improve empty struct

* refactor: added owner/msg.go

* refactor: added more types

* refactor: started refactoring owner

* refactor: removing more code

* refactor: simplify further

* refactor: removed more code

* refactor: simplified further

* refactor: added godocs

* fix: gas

* refactor: done
  • Loading branch information
srdtrk authored Jan 22, 2024
1 parent c49028a commit 0359560
Show file tree
Hide file tree
Showing 24 changed files with 687 additions and 869 deletions.
258 changes: 189 additions & 69 deletions e2e/interchaintest/contract_test.go

Large diffs are not rendered by default.

59 changes: 41 additions & 18 deletions e2e/interchaintest/owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

mysuite "github.com/srdtrk/cw-ica-controller/interchaintest/v2/testsuite"
"github.com/srdtrk/cw-ica-controller/interchaintest/v2/types"
"github.com/srdtrk/cw-ica-controller/interchaintest/v2/types/icacontroller"
"github.com/srdtrk/cw-ica-controller/interchaintest/v2/types/owner"
)

type OwnerTestSuite struct {
Expand All @@ -38,19 +40,26 @@ func (s *OwnerTestSuite) SetupOwnerTestSuite(ctx context.Context) {
s.IcaContractCodeId, err = strconv.ParseUint(codeId, 10, 64)
s.Require().NoError(err)

s.OwnerContract, err = types.StoreAndInstantiateNewOwnerContract(
ctx, s.ChainA, s.UserA.KeyName(), "../../artifacts/cw_ica_owner.wasm", s.IcaContractCodeId,
)
codeId, err = s.ChainA.StoreContract(ctx, s.UserA.KeyName(), "../../artifacts/cw_ica_owner.wasm")
s.Require().NoError(err)

instantiateMsg := owner.InstantiateMsg{IcaControllerCodeId: s.IcaContractCodeId}
contractAddr, err := s.ChainA.InstantiateContract(ctx, s.UserA.KeyName(), codeId, instantiateMsg.ToString(), true)
s.Require().NoError(err)

s.OwnerContract = types.NewOwnerContract(types.NewContract(contractAddr, codeId, s.ChainA))
s.NumOfIcaContracts = 0

// Create the ICA Contract
channelOpenInitOptions := types.ChannelOpenInitOptions{
ConnectionId: s.ChainAConnID,
CounterpartyConnectionId: s.ChainBConnID,
createMsg := owner.ExecuteMsg{
CreateIcaContract: &owner.ExecuteMsg_CreateIcaContract{
Salt: nil,
ChannelOpenInitOptions: &icacontroller.ChannelOpenInitOptions{
ConnectionId: s.ChainAConnID,
CounterpartyConnectionId: s.ChainBConnID,
},
},
}
createMsg := types.NewOwnerCreateIcaContractMsg(nil, &channelOpenInitOptions)

err = s.OwnerContract.Execute(ctx, s.UserA.KeyName(), createMsg, "--gas", "500000")
s.Require().NoError(err)
Expand All @@ -74,7 +83,8 @@ func (s *OwnerTestSuite) TestOwnerCreateIcaContract() {
s.SetupOwnerTestSuite(ctx)
wasmd, simd := s.ChainA, s.ChainB

icaState, err := s.OwnerContract.QueryIcaContractState(ctx, 0)
icaStateRequest := owner.QueryMsg{GetIcaContractState: &owner.QueryMsg_GetIcaContractState{IcaId: 0}}
icaState, err := types.QueryAnyMsg[owner.IcaContractState](ctx, &s.OwnerContract.Contract, icaStateRequest)
s.Require().NoError(err)
s.Require().NotNil(icaState.IcaState)

Expand Down Expand Up @@ -109,7 +119,7 @@ func (s *OwnerTestSuite) TestOwnerCreateIcaContract() {
s.Require().Equal(channeltypes.OPEN.String(), simdChannel.State)

// Check contract's channel state
contractChannelState, err := icaContract.QueryChannelState(ctx)
contractChannelState, err := types.QueryAnyMsg[icacontroller.ContractChannelState](ctx, &icaContract.Contract, icacontroller.GetChannelRequest)
s.Require().NoError(err)

s.T().Logf("contract's channel store after handshake: %s", toJSONString(contractChannelState))
Expand All @@ -124,16 +134,19 @@ func (s *OwnerTestSuite) TestOwnerCreateIcaContract() {
s.Require().Equal(wasmdChannel.Ordering, contractChannelState.Channel.Order)

// Check contract state
contractState, err := icaContract.QueryContractState(ctx)
contractState, err := types.QueryAnyMsg[icacontroller.ContractState](
ctx, &icaContract.Contract,
icacontroller.GetContractStateRequest,
)
s.Require().NoError(err)
s.Require().Equal(wasmdChannel.ChannelID, contractState.IcaInfo.ChannelID)
s.Require().Equal(false, contractState.AllowChannelOpenInit)

ownerResponse, err := icaContract.QueryOwnership(ctx)
ownershipResponse, err := types.QueryAnyMsg[icacontroller.OwnershipResponse](ctx, &icaContract.Contract, icacontroller.OwnershipRequest)
s.Require().NoError(err)
s.Require().Equal(s.OwnerContract.Address, ownerResponse.Owner)
s.Require().Nil(ownerResponse.PendingOwner)
s.Require().Nil(ownerResponse.PendingExpiry)
s.Require().Equal(s.OwnerContract.Address, ownershipResponse.Owner)
s.Require().Nil(ownershipResponse.PendingOwner)
s.Require().Nil(ownershipResponse.PendingExpiry)
})
}

Expand All @@ -146,13 +159,17 @@ func (s *OwnerTestSuite) TestOwnerPredefinedAction() {
wasmd, simd := s.ChainA, s.ChainB
wasmdUser, simdUser := s.UserA, s.UserB

icaState, err := s.OwnerContract.QueryIcaContractState(ctx, 0)
icaStateRequest := owner.QueryMsg{GetIcaContractState: &owner.QueryMsg_GetIcaContractState{IcaId: 0}}
icaState, err := types.QueryAnyMsg[owner.IcaContractState](ctx, &s.OwnerContract.Contract, icaStateRequest)
s.Require().NoError(err)

icaContract := types.NewIcaContract(types.NewContract(icaState.ContractAddr, strconv.FormatUint(s.IcaContractCodeId, 10), wasmd))

// Check contract state
contractState, err := icaContract.QueryContractState(ctx)
contractState, err := types.QueryAnyMsg[icacontroller.ContractState](
ctx, &icaContract.Contract,
icacontroller.GetContractStateRequest,
)
s.Require().NoError(err)

icaAddress := contractState.IcaInfo.IcaAddress
Expand All @@ -161,7 +178,13 @@ func (s *OwnerTestSuite) TestOwnerPredefinedAction() {
s.FundAddressChainB(ctx, icaAddress)

s.Run("TestSendPredefinedActionSuccess", func() {
err := s.OwnerContract.ExecSendPredefinedAction(ctx, wasmdUser.KeyName(), 0, simdUser.FormattedAddress())
execPredefinedActionMsg := owner.ExecuteMsg{
SendPredefinedAction: &owner.ExecuteMsg_SendPredefinedAction{
IcaId: 0,
ToAddress: simdUser.FormattedAddress(),
},
}
err := s.OwnerContract.Execute(ctx, wasmdUser.KeyName(), execPredefinedActionMsg)
s.Require().NoError(err)

err = testutil.WaitForBlocks(ctx, 6, wasmd, simd)
Expand All @@ -172,7 +195,7 @@ func (s *OwnerTestSuite) TestOwnerPredefinedAction() {
s.Require().Equal(sdkmath.NewInt(1000000000-100), icaBalance)

// Check if contract callbacks were executed:
callbackCounter, err := icaContract.QueryCallbackCounter(ctx)
callbackCounter, err := types.QueryAnyMsg[icacontroller.CallbackCounter](ctx, &icaContract.Contract, icacontroller.GetCallbackCounterRequest)
s.Require().NoError(err)

s.Require().Equal(uint64(1), callbackCounter.Success)
Expand Down
26 changes: 18 additions & 8 deletions e2e/interchaintest/types/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,52 @@ package types

import (
"context"
"encoding/json"

"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
)

type Contract struct {
Address string
CodeID string
chain *cosmos.CosmosChain
Chain *cosmos.CosmosChain
}

// NewContract creates a new Contract instance
func NewContract(address string, codeId string, chain *cosmos.CosmosChain) Contract {
return Contract{
Address: address,
CodeID: codeId,
chain: chain,
Chain: chain,
}
}

func (c *Contract) Port() string {
return "wasm." + c.Address
}

func (c *Contract) Execute(ctx context.Context, callerKeyName string, execMsg string, extraExecTxArgs ...string) error {
_, err := c.chain.ExecuteContract(ctx, callerKeyName, c.Address, execMsg, extraExecTxArgs...)
// ExecAnyMsg executes the contract with the given exec message.
func (c *Contract) ExecAnyMsg(ctx context.Context, callerKeyName string, execMsg string, extraExecTxArgs ...string) error {
_, err := c.Chain.ExecuteContract(ctx, callerKeyName, c.Address, execMsg, extraExecTxArgs...)
return err
}

func QueryContract[T any](ctx context.Context, chain *cosmos.CosmosChain, contractAddr string, queryMsg string) (*T, error) {
queryResp := QueryResponse[T]{}
err := chain.QueryContract(ctx, contractAddr, queryMsg, &queryResp)
// QueryAnyMsg queries the contract with the given query message and returns the response.
func QueryAnyMsg[T any](ctx context.Context, c *Contract, queryMsg any) (*T, error) {
// QueryResponse is used to represent the response of a query.
// It may contain different types of data, so we need to unmarshal it
type QueryResponse struct {
Response json.RawMessage `json:"data"`
}

queryResp := QueryResponse{}
err := c.Chain.QueryContract(ctx, c.Address, queryMsg, &queryResp)
if err != nil {
return nil, err
}

resp, err := queryResp.GetResp()
var resp T
err = json.Unmarshal(queryResp.Response, &resp)
if err != nil {
return nil, err
}
Expand Down
32 changes: 0 additions & 32 deletions e2e/interchaintest/types/cosmwasm.go

This file was deleted.

20 changes: 0 additions & 20 deletions e2e/interchaintest/types/counter_msg.go

This file was deleted.

37 changes: 0 additions & 37 deletions e2e/interchaintest/types/export_test.go

This file was deleted.

Loading

0 comments on commit 0359560

Please sign in to comment.