Skip to content

Commit bb47a8f

Browse files
authored
Merge pull request #208 from InjectiveLabs/feat/add_distribution_module_examples
Feat/add distribution module examples
2 parents bd9ffbe + d64bc93 commit bb47a8f

File tree

17 files changed

+987
-0
lines changed

17 files changed

+987
-0
lines changed

.coderabbit.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
reviews:
2+
auto_review:
3+
base_branches:
4+
- "master"
5+
- "dev"
6+
- "feat/.*"
7+
chat:
8+
auto_reply: true

client/chain/chain.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"sync/atomic"
1515
"time"
1616

17+
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
18+
1719
"github.com/cosmos/cosmos-sdk/types/query"
1820

1921
"google.golang.org/grpc/credentials/insecure"
@@ -167,6 +169,17 @@ type ChainClient interface {
167169
FetchDenomsFromCreator(ctx context.Context, creator string) (*tokenfactorytypes.QueryDenomsFromCreatorResponse, error)
168170
FetchTokenfactoryModuleState(ctx context.Context) (*tokenfactorytypes.QueryModuleStateResponse, error)
169171

172+
// distribution module
173+
FetchValidatorDistributionInfo(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorDistributionInfoResponse, error)
174+
FetchValidatorOutstandingRewards(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorOutstandingRewardsResponse, error)
175+
FetchValidatorCommission(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorCommissionResponse, error)
176+
FetchValidatorSlashes(ctx context.Context, validatorAddress string, startingHeight uint64, endingHeight uint64, pagination *query.PageRequest) (*distributiontypes.QueryValidatorSlashesResponse, error)
177+
FetchDelegationRewards(ctx context.Context, delegatorAddress string, validatorAddress string) (*distributiontypes.QueryDelegationRewardsResponse, error)
178+
FetchDelegationTotalRewards(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegationTotalRewardsResponse, error)
179+
FetchDelegatorValidators(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorValidatorsResponse, error)
180+
FetchDelegatorWithdrawAddress(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorWithdrawAddressResponse, error)
181+
FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error)
182+
170183
Close()
171184
}
172185

@@ -202,6 +215,7 @@ type chainClient struct {
202215
wasmQueryClient wasmtypes.QueryClient
203216
chainStreamClient chainstreamtypes.StreamClient
204217
tokenfactoryQueryClient tokenfactorytypes.QueryClient
218+
distributionQueryClient distributiontypes.QueryClient
205219
subaccountToNonce map[ethcommon.Hash]uint32
206220

207221
closed int64
@@ -296,6 +310,7 @@ func NewChainClient(
296310
wasmQueryClient: wasmtypes.NewQueryClient(conn),
297311
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
298312
tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn),
313+
distributionQueryClient: distributiontypes.NewQueryClient(conn),
299314
subaccountToNonce: make(map[ethcommon.Hash]uint32),
300315
}
301316

@@ -1494,3 +1509,69 @@ type OrderCancelData struct {
14941509
OrderHash string
14951510
Cid string
14961511
}
1512+
1513+
// Distribution module
1514+
func (c *chainClient) FetchValidatorDistributionInfo(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorDistributionInfoResponse, error) {
1515+
req := &distributiontypes.QueryValidatorDistributionInfoRequest{
1516+
ValidatorAddress: validatorAddress,
1517+
}
1518+
return c.distributionQueryClient.ValidatorDistributionInfo(ctx, req)
1519+
}
1520+
1521+
func (c *chainClient) FetchValidatorOutstandingRewards(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorOutstandingRewardsResponse, error) {
1522+
req := &distributiontypes.QueryValidatorOutstandingRewardsRequest{
1523+
ValidatorAddress: validatorAddress,
1524+
}
1525+
return c.distributionQueryClient.ValidatorOutstandingRewards(ctx, req)
1526+
}
1527+
1528+
func (c *chainClient) FetchValidatorCommission(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorCommissionResponse, error) {
1529+
req := &distributiontypes.QueryValidatorCommissionRequest{
1530+
ValidatorAddress: validatorAddress,
1531+
}
1532+
return c.distributionQueryClient.ValidatorCommission(ctx, req)
1533+
}
1534+
1535+
func (c *chainClient) FetchValidatorSlashes(ctx context.Context, validatorAddress string, startingHeight uint64, endingHeight uint64, pagination *query.PageRequest) (*distributiontypes.QueryValidatorSlashesResponse, error) {
1536+
req := &distributiontypes.QueryValidatorSlashesRequest{
1537+
ValidatorAddress: validatorAddress,
1538+
StartingHeight: startingHeight,
1539+
EndingHeight: endingHeight,
1540+
Pagination: pagination,
1541+
}
1542+
return c.distributionQueryClient.ValidatorSlashes(ctx, req)
1543+
}
1544+
1545+
func (c *chainClient) FetchDelegationRewards(ctx context.Context, delegatorAddress string, validatorAddress string) (*distributiontypes.QueryDelegationRewardsResponse, error) {
1546+
req := &distributiontypes.QueryDelegationRewardsRequest{
1547+
DelegatorAddress: delegatorAddress,
1548+
ValidatorAddress: validatorAddress,
1549+
}
1550+
return c.distributionQueryClient.DelegationRewards(ctx, req)
1551+
}
1552+
1553+
func (c *chainClient) FetchDelegationTotalRewards(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegationTotalRewardsResponse, error) {
1554+
req := &distributiontypes.QueryDelegationTotalRewardsRequest{
1555+
DelegatorAddress: delegatorAddress,
1556+
}
1557+
return c.distributionQueryClient.DelegationTotalRewards(ctx, req)
1558+
}
1559+
1560+
func (c *chainClient) FetchDelegatorValidators(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorValidatorsResponse, error) {
1561+
req := &distributiontypes.QueryDelegatorValidatorsRequest{
1562+
DelegatorAddress: delegatorAddress,
1563+
}
1564+
return c.distributionQueryClient.DelegatorValidators(ctx, req)
1565+
}
1566+
1567+
func (c *chainClient) FetchDelegatorWithdrawAddress(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorWithdrawAddressResponse, error) {
1568+
req := &distributiontypes.QueryDelegatorWithdrawAddressRequest{
1569+
DelegatorAddress: delegatorAddress,
1570+
}
1571+
return c.distributionQueryClient.DelegatorWithdrawAddress(ctx, req)
1572+
}
1573+
1574+
func (c *chainClient) FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error) {
1575+
req := &distributiontypes.QueryCommunityPoolRequest{}
1576+
return c.distributionQueryClient.CommunityPool(ctx, req)
1577+
}

client/chain/chain_test_support.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"time"
77

8+
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
9+
810
tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
911

1012
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
@@ -286,3 +288,40 @@ func (c *MockChainClient) FetchDenomsFromCreator(ctx context.Context, creator st
286288
func (c *MockChainClient) FetchTokenfactoryModuleState(ctx context.Context) (*tokenfactorytypes.QueryModuleStateResponse, error) {
287289
return &tokenfactorytypes.QueryModuleStateResponse{}, nil
288290
}
291+
292+
// Distribution module
293+
func (c *MockChainClient) FetchValidatorDistributionInfo(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorDistributionInfoResponse, error) {
294+
return &distributiontypes.QueryValidatorDistributionInfoResponse{}, nil
295+
}
296+
297+
func (c *MockChainClient) FetchValidatorOutstandingRewards(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorOutstandingRewardsResponse, error) {
298+
return &distributiontypes.QueryValidatorOutstandingRewardsResponse{}, nil
299+
}
300+
301+
func (c *MockChainClient) FetchValidatorCommission(ctx context.Context, validatorAddress string) (*distributiontypes.QueryValidatorCommissionResponse, error) {
302+
return &distributiontypes.QueryValidatorCommissionResponse{}, nil
303+
}
304+
305+
func (c *MockChainClient) FetchValidatorSlashes(ctx context.Context, validatorAddress string, startingHeight uint64, endingHeight uint64, pagination *query.PageRequest) (*distributiontypes.QueryValidatorSlashesResponse, error) {
306+
return &distributiontypes.QueryValidatorSlashesResponse{}, nil
307+
}
308+
309+
func (c *MockChainClient) FetchDelegationRewards(ctx context.Context, delegatorAddress string, validatorAddress string) (*distributiontypes.QueryDelegationRewardsResponse, error) {
310+
return &distributiontypes.QueryDelegationRewardsResponse{}, nil
311+
}
312+
313+
func (c *MockChainClient) FetchDelegationTotalRewards(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegationTotalRewardsResponse, error) {
314+
return &distributiontypes.QueryDelegationTotalRewardsResponse{}, nil
315+
}
316+
317+
func (c *MockChainClient) FetchDelegatorValidators(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorValidatorsResponse, error) {
318+
return &distributiontypes.QueryDelegatorValidatorsResponse{}, nil
319+
}
320+
321+
func (c *MockChainClient) FetchDelegatorWithdrawAddress(ctx context.Context, delegatorAddress string) (*distributiontypes.QueryDelegatorWithdrawAddressResponse, error) {
322+
return &distributiontypes.QueryDelegatorWithdrawAddressResponse{}, nil
323+
}
324+
325+
func (c *MockChainClient) FetchCommunityPool(ctx context.Context) (*distributiontypes.QueryCommunityPoolResponse, error) {
326+
return &distributiontypes.QueryCommunityPoolResponse{}, nil
327+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/cosmos/cosmos-sdk/x/distribution/types"
9+
10+
"github.com/InjectiveLabs/sdk-go/client"
11+
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
12+
"github.com/InjectiveLabs/sdk-go/client/common"
13+
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
14+
)
15+
16+
func main() {
17+
network := common.LoadNetwork("testnet", "lb")
18+
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
24+
os.Getenv("HOME")+"/.injectived",
25+
"injectived",
26+
"file",
27+
"inj-user",
28+
"12345678",
29+
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
30+
false,
31+
)
32+
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
clientCtx, err := chainclient.NewClientContext(
38+
network.ChainId,
39+
senderAddress.String(),
40+
cosmosKeyring,
41+
)
42+
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
48+
49+
chainClient, err := chainclient.NewChainClient(
50+
clientCtx,
51+
network,
52+
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
53+
)
54+
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
withdrawAddress := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
60+
61+
msg := &types.MsgSetWithdrawAddress{
62+
DelegatorAddress: senderAddress.String(),
63+
WithdrawAddress: withdrawAddress,
64+
}
65+
66+
//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
67+
response, err := chainClient.AsyncBroadcastMsg(msg)
68+
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
str, _ := json.MarshalIndent(response, "", " ")
74+
fmt.Print(string(str))
75+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/cosmos/cosmos-sdk/x/distribution/types"
9+
10+
"github.com/InjectiveLabs/sdk-go/client"
11+
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
12+
"github.com/InjectiveLabs/sdk-go/client/common"
13+
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
14+
)
15+
16+
func main() {
17+
network := common.LoadNetwork("testnet", "lb")
18+
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
19+
if err != nil {
20+
panic(err)
21+
}
22+
23+
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
24+
os.Getenv("HOME")+"/.injectived",
25+
"injectived",
26+
"file",
27+
"inj-user",
28+
"12345678",
29+
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
30+
false,
31+
)
32+
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
clientCtx, err := chainclient.NewClientContext(
38+
network.ChainId,
39+
senderAddress.String(),
40+
cosmosKeyring,
41+
)
42+
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
48+
49+
chainClient, err := chainclient.NewChainClient(
50+
clientCtx,
51+
network,
52+
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
53+
)
54+
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
validatorAddress := "injvaloper1ultw9r29l8nxy5u6thcgusjn95vsy2caw722q5"
60+
61+
msg := &types.MsgWithdrawValidatorCommission{
62+
ValidatorAddress: validatorAddress,
63+
}
64+
65+
//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
66+
response, err := chainClient.AsyncBroadcastMsg(msg)
67+
68+
if err != nil {
69+
panic(err)
70+
}
71+
72+
str, _ := json.MarshalIndent(response, "", " ")
73+
fmt.Print(string(str))
74+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
8+
"github.com/cosmos/cosmos-sdk/types"
9+
distriutiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
10+
11+
"github.com/InjectiveLabs/sdk-go/client"
12+
chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
13+
"github.com/InjectiveLabs/sdk-go/client/common"
14+
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
15+
)
16+
17+
func main() {
18+
network := common.LoadNetwork("testnet", "lb")
19+
tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
25+
os.Getenv("HOME")+"/.injectived",
26+
"injectived",
27+
"file",
28+
"inj-user",
29+
"12345678",
30+
"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
31+
false,
32+
)
33+
34+
if err != nil {
35+
panic(err)
36+
}
37+
38+
clientCtx, err := chainclient.NewClientContext(
39+
network.ChainId,
40+
senderAddress.String(),
41+
cosmosKeyring,
42+
)
43+
44+
if err != nil {
45+
panic(err)
46+
}
47+
48+
clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
49+
50+
chainClient, err := chainclient.NewChainClient(
51+
clientCtx,
52+
network,
53+
common.OptionGasPrices(client.DefaultGasPriceWithDenom),
54+
)
55+
56+
if err != nil {
57+
panic(err)
58+
}
59+
60+
amount := types.NewCoin("inj", types.NewInt(1))
61+
62+
msg := &distriutiontypes.MsgFundCommunityPool{
63+
Amount: []types.Coin{amount},
64+
Depositor: senderAddress.String(),
65+
}
66+
67+
//AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
68+
response, err := chainClient.AsyncBroadcastMsg(msg)
69+
70+
if err != nil {
71+
panic(err)
72+
}
73+
74+
str, _ := json.MarshalIndent(response, "", " ")
75+
fmt.Print(string(str))
76+
}

0 commit comments

Comments
 (0)