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

feat: adding query to fetch x/gov module address #831

Merged
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
24 changes: 24 additions & 0 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,30 @@ func (tn *ChainNode) QueryClientContractCode(ctx context.Context, codeHash strin
return err
}

// GetModuleAddress performs a query to get the address of the specified chain module
func (tn *ChainNode) GetModuleAddress(ctx context.Context, moduleName string) (string, error) {
queryRes, err := tn.GetModuleAccount(ctx, moduleName)
if err != nil {
return "", err
}
return queryRes.Account.BaseAccount.Address, nil
}

// GetModuleAccount performs a query to get the account details of the specified chain module
func (tn *ChainNode) GetModuleAccount(ctx context.Context, moduleName string) (QueryModuleAccountResponse, error) {
stdout, _, err := tn.ExecQuery(ctx, "auth", "module-account", moduleName)
if err != nil {
return QueryModuleAccountResponse{}, err
}

queryRes := QueryModuleAccountResponse{}
err = json.Unmarshal(stdout, &queryRes)
if err != nil {
return QueryModuleAccountResponse{}, err
}
return queryRes, nil
}

// VoteOnProposal submits a vote for the specified proposal.
func (tn *ChainNode) VoteOnProposal(ctx context.Context, keyName string, proposalID string, vote string) error {
_, err := tn.ExecTx(ctx, keyName,
Expand Down
10 changes: 10 additions & 0 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,16 @@ func (c *CosmosChain) SendIBCTransfer(
return tx, nil
}

// GetGovernanceAddress performs a query to get the address of the chain's x/gov module
func (c *CosmosChain) GetGovernanceAddress(ctx context.Context) (string, error) {
return c.GetModuleAddress(ctx, govtypes.ModuleName)
}

// GetModuleAddress performs a query to get the address of the specified chain module
func (c *CosmosChain) GetModuleAddress(ctx context.Context, moduleName string) (string, error) {
return c.getFullNode().GetModuleAddress(ctx, moduleName)
}

// QueryProposal returns the state and details of a governance proposal.
func (c *CosmosChain) QueryProposal(ctx context.Context, proposalID string) (*ProposalResponse, error) {
return c.getFullNode().QueryProposal(ctx, proposalID)
Expand Down
12 changes: 12 additions & 0 deletions chain/cosmos/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,15 @@ type DenomAuthorityMetadata struct {
// Can be empty for no admin, or a valid address
Admin string `protobuf:"bytes,1,opt,name=admin,proto3" json:"admin,omitempty" yaml:"admin"`
}

type QueryModuleAccountResponse struct {
Account struct {
BaseAccount struct {
AccountNumber string `json:"account_number"`
Address string `json:"address"`
PubKey string `json:"pub_key"`
Sequence string `json:"sequence"`
} `json:"base_account"`
Name string `json:"name"`
} `json:"account"`
}
8 changes: 8 additions & 0 deletions examples/cosmos/chain_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
testHasCommand(ctx, t, chain)
testTokenFactory(ctx, t, chain, users)
testAddingNode(ctx, t, chain)
testGetGovernanceAddress(ctx, t, chain)
}

func testBuildDependencies(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain) {
Expand Down Expand Up @@ -376,6 +377,13 @@ func testTokenFactory(ctx context.Context, t *testing.T, chain *cosmos.CosmosCha

}

func testGetGovernanceAddress(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain) {
govAddr, err := chain.GetGovernanceAddress(ctx)
require.NoError(t, err)
_, err = sdk.AccAddressFromBech32(govAddr)
require.NoError(t, err)
}

// helpers
func sendTokens(ctx context.Context, chain *cosmos.CosmosChain, from, to ibc.Wallet, token string, amount int64) (ibc.WalletAmount, error) {
if token == "" {
Expand Down
Loading