Skip to content

Commit

Permalink
add cw-hooks e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Oct 2, 2023
1 parent dad9bf2 commit f9277ba
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 14 deletions.
1 change: 1 addition & 0 deletions interchaintest/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ A list of the contracts here which are pre-compiled in other repos.
> cw_unity_prop -> <https://github.com/CosmosContracts/cw-unity-prop> v0.3.4-alpha
> ibchooks_counter.wasm -> <https://github.com/osmosis-labs/osmosis/blob/64393a14e18b2562d72a3892eec716197a3716c7/tests/ibc-hooks/bytecode/counter.wasm>
> clock_example.wasm -> <https://github.com/Reecepbcups/cw-clock-example>
> juno_staking_hooks_example.wasm -> <https://github.com/Reecepbcups/cw-juno-staking-hooks-example>
Binary file not shown.
11 changes: 11 additions & 0 deletions interchaintest/helpers/cosmwasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"context"
"encoding/json"
"testing"

"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
Expand All @@ -12,6 +13,16 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keyring"
)

// TODO: Upstream this to interchaintest
func SmartQueryString(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, contractAddr, queryMsg string, res interface{}) error {
var jsonMap map[string]interface{}
if err := json.Unmarshal([]byte(queryMsg), &jsonMap); err != nil {
t.Fatal(err)
}
err := chain.QueryContract(ctx, contractAddr, jsonMap, &res)
return err
}

func SetupContract(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, keyname string, fileLoc string, message string) (codeId, contract string) {
codeId, err := chain.StoreContract(ctx, keyname, fileLoc)
if err != nil {
Expand Down
40 changes: 36 additions & 4 deletions interchaintest/helpers/cwhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,42 @@ import (
"github.com/stretchr/testify/require"
)

// Register
func RegisterCwHooksStaking(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, contractAddr string) {
cwHooksCmd(t, ctx, chain, user, "register-staking", contractAddr)
}
func RegisterCwHooksGovernance(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, contractAddr string) {
cwHooksCmd(t, ctx, chain, user, "register-governance", contractAddr)
}

// UnRegister
func UnregisterCwHooksStaking(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, contractAddr string) {
cwHooksCmd(t, ctx, chain, user, "unregister-staking", contractAddr)
}
func UnregisterCwHooksGovernance(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, contractAddr string) {
cwHooksCmd(t, ctx, chain, user, "unregister-governance", contractAddr)
}

// Get Contracts
func GetCwHooksStakingContracts(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain) []string {
return getContracts(t, ctx, chain, "staking-contracts")
}
func GetCwHooksGovernanceContracts(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain) []string {
return getContracts(t, ctx, chain, "governance-contracts")
}

// Contract specific
func GetCwStakingHookLastDelegationChange(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, contract string, uaddr string) GetCwHooksDelegationResponse {
var res GetCwHooksDelegationResponse
err := SmartQueryString(t, ctx, chain, contract, `{"last_delegation_change":{}}`, &res)
require.NoError(t, err)
return res
}

// helpers
func cwHooksCmd(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, registerModule, contractAddr string) {
cmd := []string{
"junod", "tx", "cw-hooks", "register-staking", contractAddr,
"junod", "tx", "cw-hooks", registerModule, contractAddr,
"--node", chain.GetRPCAddress(),
"--home", chain.HomeDir(),
"--chain-id", chain.Config().ChainID,
Expand All @@ -34,15 +67,14 @@ func RegisterCwHooksStaking(t *testing.T, ctx context.Context, chain *cosmos.Cos
}
}

func GetCwHooksStakingContracts(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain) []string {
func getContracts(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, subCmd string) []string {
cmd := []string{
"junod", "query", "cw-hooks", "staking-contracts",
"junod", "query", "cw-hooks", subCmd,
"--node", chain.GetRPCAddress(),
"--chain-id", chain.Config().ChainID,
"--output", "json",
}

// This query does not return a type, just prints the string.
stdout, _, err := chain.Exec(ctx, cmd, nil)
require.NoError(t, err)

Expand Down
10 changes: 10 additions & 0 deletions interchaintest/helpers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ type ClockContractResponse struct {
type ClockContractObj struct {
Val uint32 `json:"val"`
}

type GetCwHooksDelegationResponse struct {
// {"data":{"validator_address":"%s","delegator_address":"%s","shares":"%s"}}
Data *GetDelegationObj `json:"data"`
}
type GetDelegationObj struct {
ValidatorAddress string `json:"validator_address"`
DelegatorAddress string `json:"delegator_address"`
Shares string `json:"shares"`
}
36 changes: 26 additions & 10 deletions interchaintest/module_cwhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

"github.com/strangelove-ventures/interchaintest/v7"
"github.com/strangelove-ventures/interchaintest/v7/chain/cosmos"
"github.com/stretchr/testify/require"

helpers "github.com/CosmosContracts/juno/tests/interchaintest/helpers"
)

// TestJunoCwHooks
// from x/cw-hooks/keeper/msg_server_test.go -> TestContractExecution
func TestJunoCwHooks(t *testing.T) {
t.Parallel()

Expand All @@ -28,18 +30,32 @@ func TestJunoCwHooks(t *testing.T) {
user := users[0]

// Upload & init contract payment to another address
// TODO: convert this to a contract with staking sudo msgs
_, contractAddr := helpers.SetupContract(t, ctx, juno, user.KeyName(), "contracts/clock_example.wasm", `{}`)

// register contract addr with the command
_, contractAddr := helpers.SetupContract(t, ctx, juno, user.KeyName(), "contracts/juno_staking_hooks_example.wasm", `{}`)

// register staking contract (to be tested)
helpers.RegisterCwHooksStaking(t, ctx, juno, user, contractAddr)

c := helpers.GetCwHooksStakingContracts(t, ctx, juno)
fmt.Printf("c: %v\n", c)

// do a staking action here, and confirm it works and is modified in the contract.
// TODO: do for all actions, staking and gov.
sc := helpers.GetCwHooksStakingContracts(t, ctx, juno)
require.Equal(t, 1, len(sc))
require.Equal(t, contractAddr, sc[0])

// Validate that governance contract is added
helpers.RegisterCwHooksGovernance(t, ctx, juno, user, contractAddr)
gc := helpers.GetCwHooksGovernanceContracts(t, ctx, juno)
require.Equal(t, 1, len(gc))
require.Equal(t, contractAddr, gc[0])

// Perform a Staking Action
vals := helpers.GetValidators(t, ctx, juno)
valoper := vals.Validators[0].OperatorAddress

stakeAmt := 1_000_000
helpers.StakeTokens(t, ctx, juno, user, valoper, fmt.Sprintf("%d%s", stakeAmt, juno.Config().Denom))

// Query the smart contract to validate it saw the fire-and-forget update
res := helpers.GetCwStakingHookLastDelegationChange(t, ctx, juno, contractAddr, user.FormattedAddress())
require.Equal(t, valoper, res.Data.ValidatorAddress)
require.Equal(t, user.FormattedAddress(), res.Data.DelegatorAddress)
require.Equal(t, fmt.Sprintf("%d.000000000000000000", stakeAmt), res.Data.Shares)

t.Cleanup(func() {
_ = ic.Close()
Expand Down

0 comments on commit f9277ba

Please sign in to comment.