Skip to content

Commit

Permalink
(feat) Added example script for all queries and messages in the chain…
Browse files Browse the repository at this point in the history
… `distribution` module
  • Loading branch information
abel committed Mar 5, 2024
1 parent bd9ffbe commit 0e0b7a2
Show file tree
Hide file tree
Showing 16 changed files with 973 additions and 0 deletions.
80 changes: 80 additions & 0 deletions client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"math"
"os"
"strconv"
Expand Down Expand Up @@ -167,6 +168,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 +214,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 +309,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 +1508,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)

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

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1513-L1517

Added lines #L1513 - L1517 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 1524 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1520-L1524

Added lines #L1520 - L1524 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 1531 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1527-L1531

Added lines #L1527 - L1531 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 1541 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1534-L1541

Added lines #L1534 - L1541 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 1549 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1544-L1549

Added lines #L1544 - L1549 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 1556 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1552-L1556

Added lines #L1552 - L1556 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 1563 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1559-L1563

Added lines #L1559 - L1563 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 1570 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1566-L1570

Added lines #L1566 - L1570 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 1575 in client/chain/chain.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain.go#L1573-L1575

Added lines #L1573 - L1575 were not covered by tests
}
38 changes: 38 additions & 0 deletions client/chain/chain_test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package chain
import (
"context"
"errors"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"time"

tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
Expand Down Expand Up @@ -286,3 +287,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

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

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L292-L293

Added lines #L292 - L293 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 297 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L296-L297

Added lines #L296 - L297 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 301 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L300-L301

Added lines #L300 - L301 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 305 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L304-L305

Added lines #L304 - L305 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 309 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L308-L309

Added lines #L308 - L309 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 313 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L312-L313

Added lines #L312 - L313 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 317 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L316-L317

Added lines #L316 - L317 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 321 in client/chain/chain_test_support.go

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L320-L321

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

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

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

View check run for this annotation

Codecov / codecov/patch

client/chain/chain_test_support.go#L324-L325

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

import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"os"

"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,73 @@
package main

import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/x/distribution/types"
"os"

"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))
}
75 changes: 75 additions & 0 deletions examples/chain/distribution/4_FundCommunityPool/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/types"
distriutiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"os"

"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 0e0b7a2

Please sign in to comment.