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/add distribution module examples #208

Merged
merged 3 commits into from
Mar 5, 2024
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
8 changes: 8 additions & 0 deletions .coderabbit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
reviews:
auto_review:
base_branches:
- "master"
- "dev"
- "feat/.*"
chat:
auto_reply: true
81 changes: 81 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"sync/atomic"
"time"

distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/cosmos/cosmos-sdk/types/query"

"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -167,6 +169,17 @@
FetchDenomsFromCreator(ctx context.Context, creator string) (*tokenfactorytypes.QueryDenomsFromCreatorResponse, error)
FetchTokenfactoryModuleState(ctx context.Context) (*tokenfactorytypes.QueryModuleStateResponse, error)

// distribution module
FetchValidatorDistributionInfo(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorDistributionInfoResponse, error)
FetchValidatorOutstandingRewards(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorOutstandingRewardsResponse, error)
FetchValidatorCommission(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorCommissionResponse, error)
FetchValidatorSlashes(ctx context.Context, validatorAddress string, startingHeight uint64, endingHeight uint64, pagination *query.PageRequest) (*distributiontypes.QueryValidatorSlashesResponse, error)
FetchDelegationRewards(ctx context.Context, delegatorAddress string, validatorAddress string) (*distributiontypes.QueryDelegationRewardsResponse, error)
FetchDelegationTotalRewards(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegationTotalRewardsResponse, error)
FetchDelegatorValidators(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorValidatorsResponse, error)
FetchDelegatorWithdrawAddress(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorWithdrawAddressResponse, error)
FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error)

Close()
}

Expand Down Expand Up @@ -202,6 +215,7 @@
wasmQueryClient wasmtypes.QueryClient
chainStreamClient chainstreamtypes.StreamClient
tokenfactoryQueryClient tokenfactorytypes.QueryClient
distributionQueryClient distributiontypes.QueryClient
subaccountToNonce map[ethcommon.Hash]uint32

closed int64
Expand Down Expand Up @@ -296,6 +310,7 @@
wasmQueryClient: wasmtypes.NewQueryClient(conn),
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
distributionQueryClient: distributiontypes.NewQueryClient(conn),
subaccountToNonce: make(map[ethcommon.Hash]uint32),
}

Expand Down Expand Up @@ -1494,3 +1509,69 @@
OrderHash string
Cid string
}

// Distribution module
func (c *chainClient) FetchValidatorDistributionInfo(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorDistributionInfoResponse, error) {
req := &distributiontypes.QueryValidatorDistributionInfoRequest{
ValidatorAddress: validatorAddress,
}
return c.distributionQueryClient.ValidatorDistributionInfo(ctx, req)

Check warning on line 1518 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1514-L1518

Added lines #L1514 - L1518 were not covered by tests
}

func (c *chainClient) FetchValidatorOutstandingRewards(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorOutstandingRewardsResponse, error) {
req := &distributiontypes.QueryValidatorOutstandingRewardsRequest{
ValidatorAddress: validatorAddress,
}
return c.distributionQueryClient.ValidatorOutstandingRewards(ctx, req)

Check warning on line 1525 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1521-L1525

Added lines #L1521 - L1525 were not covered by tests
}

func (c *chainClient) FetchValidatorCommission(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorCommissionResponse, error) {
req := &distributiontypes.QueryValidatorCommissionRequest{
ValidatorAddress: validatorAddress,
}
return c.distributionQueryClient.ValidatorCommission(ctx, req)

Check warning on line 1532 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1528-L1532

Added lines #L1528 - L1532 were not covered by tests
}

func (c *chainClient) FetchValidatorSlashes(ctx context.Context, validatorAddress string, startingHeight uint64, endingHeight uint64, pagination *query.PageRequest) (*distributiontypes.QueryValidatorSlashesResponse, error) {
req := &distributiontypes.QueryValidatorSlashesRequest{
ValidatorAddress: validatorAddress,
StartingHeight: startingHeight,
EndingHeight: endingHeight,
Pagination: pagination,
}
return c.distributionQueryClient.ValidatorSlashes(ctx, req)

Check warning on line 1542 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1535-L1542

Added lines #L1535 - L1542 were not covered by tests
}

func (c *chainClient) FetchDelegationRewards(ctx context.Context, delegatorAddress string, validatorAddress string) (*distributiontypes.QueryDelegationRewardsResponse, error) {
req := &distributiontypes.QueryDelegationRewardsRequest{
DelegatorAddress: delegatorAddress,
ValidatorAddress: validatorAddress,
}
return c.distributionQueryClient.DelegationRewards(ctx, req)

Check warning on line 1550 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1545-L1550

Added lines #L1545 - L1550 were not covered by tests
}

func (c *chainClient) FetchDelegationTotalRewards(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegationTotalRewardsResponse, error) {
req := &distributiontypes.QueryDelegationTotalRewardsRequest{
DelegatorAddress: delegatorAddress,
}
return c.distributionQueryClient.DelegationTotalRewards(ctx, req)

Check warning on line 1557 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1553-L1557

Added lines #L1553 - L1557 were not covered by tests
}

func (c *chainClient) FetchDelegatorValidators(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorValidatorsResponse, error) {
req := &distributiontypes.QueryDelegatorValidatorsRequest{
DelegatorAddress: delegatorAddress,
}
return c.distributionQueryClient.DelegatorValidators(ctx, req)

Check warning on line 1564 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1560-L1564

Added lines #L1560 - L1564 were not covered by tests
}

func (c *chainClient) FetchDelegatorWithdrawAddress(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorWithdrawAddressResponse, error) {
req := &distributiontypes.QueryDelegatorWithdrawAddressRequest{
DelegatorAddress: delegatorAddress,
}
return c.distributionQueryClient.DelegatorWithdrawAddress(ctx, req)

Check warning on line 1571 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1567-L1571

Added lines #L1567 - L1571 were not covered by tests
}

func (c *chainClient) FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error) {
req := &distributiontypes.QueryCommunityPoolRequest{}
return c.distributionQueryClient.CommunityPool(ctx, req)

Check warning on line 1576 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1574-L1576

Added lines #L1574 - L1576 were not covered by tests
}
39 changes: 39 additions & 0 deletions client/chain/chain_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"errors"
"time"

distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
Expand Down Expand Up @@ -286,3 +288,40 @@
func (c *MockChainClient) FetchTokenfactoryModuleState(ctx context.Context) (*tokenfactorytypes.QueryModuleStateResponse, error) {
return &tokenfactorytypes.QueryModuleStateResponse{}, nil
}

// Distribution module
func (c *MockChainClient) FetchValidatorDistributionInfo(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorDistributionInfoResponse, error) {
return &distributiontypes.QueryValidatorDistributionInfoResponse{}, nil

Check warning on line 294 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L293-L294

Added lines #L293 - L294 were not covered by tests
}

func (c *MockChainClient) FetchValidatorOutstandingRewards(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorOutstandingRewardsResponse, error) {
return &distributiontypes.QueryValidatorOutstandingRewardsResponse{}, nil

Check warning on line 298 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L297-L298

Added lines #L297 - L298 were not covered by tests
}

func (c *MockChainClient) FetchValidatorCommission(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorCommissionResponse, error) {
return &distributiontypes.QueryValidatorCommissionResponse{}, nil

Check warning on line 302 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L301-L302

Added lines #L301 - L302 were not covered by tests
}

func (c *MockChainClient) FetchValidatorSlashes(ctx context.Context, validatorAddress string, startingHeight uint64, endingHeight uint64, pagination *query.PageRequest) (*distributiontypes.QueryValidatorSlashesResponse, error) {
return &distributiontypes.QueryValidatorSlashesResponse{}, nil

Check warning on line 306 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L305-L306

Added lines #L305 - L306 were not covered by tests
}

func (c *MockChainClient) FetchDelegationRewards(ctx context.Context, delegatorAddress string, validatorAddress string) (*distributiontypes.QueryDelegationRewardsResponse, error) {
return &distributiontypes.QueryDelegationRewardsResponse{}, nil

Check warning on line 310 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L309-L310

Added lines #L309 - L310 were not covered by tests
}

func (c *MockChainClient) FetchDelegationTotalRewards(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegationTotalRewardsResponse, error) {
return &distributiontypes.QueryDelegationTotalRewardsResponse{}, nil

Check warning on line 314 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L313-L314

Added lines #L313 - L314 were not covered by tests
}

func (c *MockChainClient) FetchDelegatorValidators(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorValidatorsResponse, error) {
return &distributiontypes.QueryDelegatorValidatorsResponse{}, nil

Check warning on line 318 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L317-L318

Added lines #L317 - L318 were not covered by tests
}

func (c *MockChainClient) FetchDelegatorWithdrawAddress(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorWithdrawAddressResponse, error) {
return &distributiontypes.QueryDelegatorWithdrawAddressResponse{}, nil

Check warning on line 322 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L321-L322

Added lines #L321 - L322 were not covered by tests
}

func (c *MockChainClient) FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error) {
return &distributiontypes.QueryCommunityPoolResponse{}, nil

Check warning on line 326 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L325-L326

Added lines #L325 - L326 were not covered by tests
}
75 changes: 75 additions & 0 deletions examples/chain/distribution/1_SetWithdrawAddress/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)
Comment on lines +23 to +31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding sensitive information such as the mnemonic and keyring passphrase ("12345678") in the source code is a security risk. Consider using environment variables or secure vaults for sensitive data.


if err != nil {
panic(err)
}

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}

withdrawAddress := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"

msg := &types.MsgSetWithdrawAddress{
DelegatorAddress: senderAddress.String(),
WithdrawAddress: withdrawAddress,
}

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
response, err := chainClient.AsyncBroadcastMsg(msg)

if err != nil {
panic(err)
}
Comment on lines +67 to +71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error handling for AsyncBroadcastMsg is done by panicking. While this might be acceptable for an example script, in production code, it's better to handle errors gracefully. Consider logging the error or returning it to the caller.


str, _ := json.MarshalIndent(response, "", " ")
fmt.Print(string(str))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}

validatorAddress := "injvaloper1ultw9r29l8nxy5u6thcgusjn95vsy2caw722q5"

msg := &types.MsgWithdrawValidatorCommission{
ValidatorAddress: validatorAddress,
}

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
response, err := chainClient.AsyncBroadcastMsg(msg)

if err != nil {
panic(err)
}

str, _ := json.MarshalIndent(response, "", " ")
fmt.Print(string(str))
}
76 changes: 76 additions & 0 deletions examples/chain/distribution/4_FundCommunityPool/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/types"
distriutiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/InjectiveLabs/sdk-go/client"
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
)

func main() {
network := common.LoadNetwork("testnet", "lb")
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
if err != nil {
panic(err)
}

senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
os.Getenv("HOME")+"/.injectived",
"injectived",
"file",
"inj-user",
"12345678",
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
false,
)

if err != nil {
panic(err)
}

clientCtx, err := chainclient.NewClientContext(
network.ChainId,
senderAddress.String(),
cosmosKeyring,
)

if err != nil {
panic(err)
}

clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)

chainClient, err := chainclient.NewChainClient(
clientCtx,
network,
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
)

if err != nil {
panic(err)
}

amount := types.NewCoin("inj", types.NewInt(1))

msg := &distriutiontypes.MsgFundCommunityPool{
Amount: []types.Coin{amount},
Depositor: senderAddress.String(),
}

//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
response, err := chainClient.AsyncBroadcastMsg(msg)

if err != nil {
panic(err)
}

str, _ := json.MarshalIndent(response, "", " ")
fmt.Print(string(str))
}
Loading
Loading