Skip to content

Commit

Permalink
Merge pull request #208 from InjectiveLabs/feat/add_distribution_modu…
Browse files Browse the repository at this point in the history
…le_examples

Feat/add distribution module examples
  • Loading branch information
aarmoa authored Mar 5, 2024
2 parents bd9ffbe + d64bc93 commit bb47a8f
Show file tree
Hide file tree
Showing 17 changed files with 987 additions and 0 deletions.
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 @@ import (
"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 @@ type ChainClient interface {
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 @@ type chainClient struct {
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 @@ func NewChainClient(
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 @@ type OrderCancelData struct {
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)
}

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

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

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)
}

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)
}

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

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

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

func (c *chainClient) FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error) {
req := &distributiontypes.QueryCommunityPoolRequest{}
return c.distributionQueryClient.CommunityPool(ctx, req)
}
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 @@ import (
"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) FetchDenomsFromCreator(ctx context.Context, creator st
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
}

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

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

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

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

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

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

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

func (c *MockChainClient) FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error) {
return &distributiontypes.QueryCommunityPoolResponse{}, nil
}
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,
)

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)
}

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

0 comments on commit bb47a8f

Please sign in to comment.