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 2 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
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 @@
"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 @@
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 @@
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 @@
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 @@
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 @@
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) 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
}
Comment on lines +291 to +327
Copy link

Choose a reason for hiding this comment

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

The added methods for the distribution module in the MockChainClient struct are stubs returning empty responses. Ensure that these methods are adequately documented to indicate they are placeholders meant to be overridden or extended for more detailed testing scenarios.

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"
Copy link

Choose a reason for hiding this comment

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

Consider organizing imports by grouping standard library imports separately from third-party imports for better readability.

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

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"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"
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,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))
}
Comment on lines +17 to +74
Copy link

Choose a reason for hiding this comment

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

The script for withdrawing validator commission is well-structured and demonstrates the transaction broadcast process effectively. As with previous scripts, ensure that sensitive information, such as the private key, is securely managed and not hardcoded.

- "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
+ os.Getenv("PRIVATE_KEY"), // keyring will be used if pk not provided

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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))
}
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",
os.Getenv("PRIVATE_KEY"), // 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"
Copy link

Choose a reason for hiding this comment

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

Again, consider organizing imports by grouping standard library imports separately from third-party imports for better readability.

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

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"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"
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,
)
Comment on lines +24 to +32
Copy link

Choose a reason for hiding this comment

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

The security concern regarding hardcoding sensitive information is present here as well. Use environment variables or secure vaults instead.


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)
}
Comment on lines +68 to +72
Copy link

Choose a reason for hiding this comment

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

Similar to the previous files, consider handling errors from AsyncBroadcastMsg more gracefully than panicking, especially in production code.


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