diff --git a/client/chain/chain.go b/client/chain/chain.go index 08214e4b..4f6f8301 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -13,6 +13,8 @@ import ( "sync/atomic" "time" + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types" + sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" @@ -286,6 +288,13 @@ type ChainClient interface { FetchIBCConnectionConsensusState(ctx context.Context, connectionId string, revisionNumber uint64, revisionHeight uint64) (*ibcconnectiontypes.QueryConnectionConsensusStateResponse, error) FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error) + // Permissions module + FetchAllNamespaces(ctx context.Context) (*permissionstypes.QueryAllNamespacesResponse, error) + FetchNamespaceByDenom(ctx context.Context, denom string, includeRoles bool) (*permissionstypes.QueryNamespaceByDenomResponse, error) + FetchAddressRoles(ctx context.Context, denom, address string) (*permissionstypes.QueryAddressRolesResponse, error) + FetchAddressesByRole(ctx context.Context, denom, role string) (*permissionstypes.QueryAddressesByRoleResponse, error) + FetchVouchersForAddress(ctx context.Context, address string) (*permissionstypes.QueryVouchersForAddressResponse, error) + Close() } @@ -322,6 +331,7 @@ type chainClient struct { ibcClientQueryClient ibcclienttypes.QueryClient ibcConnectionQueryClient ibcconnectiontypes.QueryClient ibcTransferQueryClient ibctransfertypes.QueryClient + permissionsQueryClient permissionstypes.QueryClient tendermintQueryClient cmtservice.ServiceClient tokenfactoryQueryClient tokenfactorytypes.QueryClient txClient txtypes.ServiceClient @@ -422,6 +432,7 @@ func NewChainClient( ibcClientQueryClient: ibcclienttypes.NewQueryClient(conn), ibcConnectionQueryClient: ibcconnectiontypes.NewQueryClient(conn), ibcTransferQueryClient: ibctransfertypes.NewQueryClient(conn), + permissionsQueryClient: permissionstypes.NewQueryClient(conn), tendermintQueryClient: cmtservice.NewServiceClient(conn), tokenfactoryQueryClient: tokenfactorytypes.NewQueryClient(conn), txClient: txtypes.NewServiceClient(conn), @@ -763,6 +774,9 @@ func (c *chainClient) BuildSignedTx(clientCtx client.Context, accNum, accSeq, in } func (c *chainClient) buildSignedTx(clientCtx client.Context, txf tx.Factory, msgs ...sdk.Msg) ([]byte, error) { + if c.network.ChainId != "injective-777" { + panic("This versions of Go SDK should only be used with Devnet environment") + } ctx := context.Background() if clientCtx.Simulate { simTxBytes, err := txf.BuildSimTx(msgs...) @@ -874,6 +888,10 @@ func (c *chainClient) broadcastTx( await bool, msgs ...sdk.Msg, ) (*txtypes.BroadcastTxResponse, error) { + if c.network.ChainId != "injective-777" { + panic("This versions of Go SDK should only be used with Devnet environment") + } + txBytes, err := c.buildSignedTx(clientCtx, txf, msgs...) if err != nil { err = errors.Wrap(err, "failed to build signed Tx") @@ -2592,3 +2610,51 @@ func (c *chainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnect return res, err } + +// Permissions module + +func (c *chainClient) FetchAllNamespaces(ctx context.Context) (*permissionstypes.QueryAllNamespacesResponse, error) { + req := &permissionstypes.QueryAllNamespacesRequest{} + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.AllNamespaces, req) + + return res, err +} + +func (c *chainClient) FetchNamespaceByDenom(ctx context.Context, denom string, includeRoles bool) (*permissionstypes.QueryNamespaceByDenomResponse, error) { + req := &permissionstypes.QueryNamespaceByDenomRequest{ + Denom: denom, + IncludeRoles: includeRoles, + } + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.NamespaceByDenom, req) + + return res, err +} + +func (c *chainClient) FetchAddressRoles(ctx context.Context, denom, address string) (*permissionstypes.QueryAddressRolesResponse, error) { + req := &permissionstypes.QueryAddressRolesRequest{ + Denom: denom, + Address: address, + } + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.AddressRoles, req) + + return res, err +} + +func (c *chainClient) FetchAddressesByRole(ctx context.Context, denom, role string) (*permissionstypes.QueryAddressesByRoleResponse, error) { + req := &permissionstypes.QueryAddressesByRoleRequest{ + Denom: denom, + Role: role, + } + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.AddressesByRole, req) + + return res, err +} + +func (c *chainClient) FetchVouchersForAddress(ctx context.Context, address string) (*permissionstypes.QueryVouchersForAddressResponse, error) { + req := &permissionstypes.QueryVouchersForAddressRequest{ + Address: address, + } + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.permissionsQueryClient.VouchersForAddress, req) + + return res, err +} diff --git a/client/chain/chain_test.go b/client/chain/chain_test.go index f3845e06..0016f37f 100644 --- a/client/chain/chain_test.go +++ b/client/chain/chain_test.go @@ -52,7 +52,7 @@ func createClient(senderAddress cosmtypes.AccAddress, cosmosKeyring keyring.Keyr } func TestDefaultSubaccount(t *testing.T) { - network := common.LoadNetwork("testnet", "lb") + network := common.LoadNetwork("devnet", "lb") senderAddress, cosmosKeyring, err := accountForTests() if err != nil { @@ -75,7 +75,7 @@ func TestDefaultSubaccount(t *testing.T) { } func TestGetSubaccountWithIndex(t *testing.T) { - network := common.LoadNetwork("testnet", "lb") + network := common.LoadNetwork("devnet", "lb") senderAddress, cosmosKeyring, err := accountForTests() if err != nil { diff --git a/client/chain/chain_test_support.go b/client/chain/chain_test_support.go index afacd061..1d8df620 100644 --- a/client/chain/chain_test_support.go +++ b/client/chain/chain_test_support.go @@ -5,6 +5,8 @@ import ( "errors" "time" + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types" + sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client/grpc/cmtservice" @@ -717,3 +719,25 @@ func (c *MockChainClient) FetchIBCConnectionConsensusState(ctx context.Context, func (c *MockChainClient) FetchIBCConnectionParams(ctx context.Context) (*ibcconnectiontypes.QueryConnectionParamsResponse, error) { return &ibcconnectiontypes.QueryConnectionParamsResponse{}, nil } + +// Permissions module + +func (c *MockChainClient) FetchAllNamespaces(ctx context.Context) (*permissionstypes.QueryAllNamespacesResponse, error) { + return &permissionstypes.QueryAllNamespacesResponse{}, nil +} + +func (c *MockChainClient) FetchNamespaceByDenom(ctx context.Context, denom string, includeRoles bool) (*permissionstypes.QueryNamespaceByDenomResponse, error) { + return &permissionstypes.QueryNamespaceByDenomResponse{}, nil +} + +func (c *MockChainClient) FetchAddressRoles(ctx context.Context, denom, address string) (*permissionstypes.QueryAddressRolesResponse, error) { + return &permissionstypes.QueryAddressRolesResponse{}, nil +} + +func (c *MockChainClient) FetchAddressesByRole(ctx context.Context, denom, role string) (*permissionstypes.QueryAddressesByRoleResponse, error) { + return &permissionstypes.QueryAddressesByRoleResponse{}, nil +} + +func (c *MockChainClient) FetchVouchersForAddress(ctx context.Context, address string) (*permissionstypes.QueryVouchersForAddressResponse, error) { + return &permissionstypes.QueryVouchersForAddressResponse{}, nil +} diff --git a/client/common/network.go b/client/common/network.go index 4714f997..e7b62672 100644 --- a/client/common/network.go +++ b/client/common/network.go @@ -2,7 +2,6 @@ package common import ( "context" - "crypto/tls" "fmt" "net" "net/http" @@ -256,101 +255,6 @@ func LoadNetwork(name, node string) Network { ExplorerCookieAssistant: &DisabledCookieAssistant{}, OfficialTokensListURL: DevnetTokensListURL, } - case "testnet": - validNodes := []string{"lb", "sentry"} - if !contains(validNodes, node) { - panic(fmt.Sprintf("invalid node %s for %s", node, name)) - } - - var lcdEndpoint, tmEndpoint, chainGrpcEndpoint, chainStreamGrpcEndpoint, exchangeGrpcEndpoint, explorerGrpcEndpoint string - var chainTLSCert, exchangeTLSCert, explorerTLSCert credentials.TransportCredentials - var chainCookieAssistant, exchangeCookieAssistant, explorerCookieAssistant CookieAssistant - if node == "lb" { - lcdEndpoint = "https://testnet.sentry.lcd.injective.network:443" - tmEndpoint = "https://testnet.sentry.tm.injective.network:443" - chainGrpcEndpoint = "testnet.sentry.chain.grpc.injective.network:443" - chainStreamGrpcEndpoint = "testnet.sentry.chain.stream.injective.network:443" - exchangeGrpcEndpoint = "testnet.sentry.exchange.grpc.injective.network:443" - explorerGrpcEndpoint = "testnet.sentry.explorer.grpc.injective.network:443" - chainTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - exchangeTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - explorerTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - chainCookieAssistant = &BareMetalLoadBalancedCookieAssistant{} - exchangeCookieAssistant = &BareMetalLoadBalancedCookieAssistant{} - explorerCookieAssistant = &BareMetalLoadBalancedCookieAssistant{} - } else if node == "sentry" { - lcdEndpoint = "https://testnet.lcd.injective.network:443" - tmEndpoint = "https://testnet.tm.injective.network:443" - chainGrpcEndpoint = "testnet.chain.grpc.injective.network:443" - chainStreamGrpcEndpoint = "testnet.chain.stream.injective.network:443" - exchangeGrpcEndpoint = "testnet.exchange.grpc.injective.network:443" - explorerGrpcEndpoint = "testnet.explorer.grpc.injective.network:443" - chainTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - exchangeTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - explorerTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - chainCookieAssistant = &DisabledCookieAssistant{} - exchangeCookieAssistant = &DisabledCookieAssistant{} - explorerCookieAssistant = &DisabledCookieAssistant{} - } - - return Network{ - LcdEndpoint: lcdEndpoint, - TmEndpoint: tmEndpoint, - ChainGrpcEndpoint: chainGrpcEndpoint, - ChainStreamGrpcEndpoint: chainStreamGrpcEndpoint, - ChainTLSCert: chainTLSCert, - ExchangeGrpcEndpoint: exchangeGrpcEndpoint, - ExchangeTLSCert: exchangeTLSCert, - ExplorerGrpcEndpoint: explorerGrpcEndpoint, - ExplorerTLSCert: explorerTLSCert, - ChainId: "injective-888", - FeeDenom: "inj", - Name: "testnet", - ChainCookieAssistant: chainCookieAssistant, - ExchangeCookieAssistant: exchangeCookieAssistant, - ExplorerCookieAssistant: explorerCookieAssistant, - OfficialTokensListURL: TestnetTokensListURL, - } - case "mainnet": - validNodes := []string{"lb"} - if !contains(validNodes, node) { - panic(fmt.Sprintf("invalid node %s for %s", node, name)) - } - var lcdEndpoint, tmEndpoint, chainGrpcEndpoint, chainStreamGrpcEndpoint, exchangeGrpcEndpoint, explorerGrpcEndpoint string - var chainTLSCert, exchangeTLSCert, explorerTLSCert credentials.TransportCredentials - var chainCookieAssistant, exchangeCookieAssistant, explorerCookieAssistant CookieAssistant - - lcdEndpoint = "https://sentry.lcd.injective.network" - tmEndpoint = "https://sentry.tm.injective.network:443" - chainGrpcEndpoint = "sentry.chain.grpc.injective.network:443" - chainStreamGrpcEndpoint = "sentry.chain.stream.injective.network:443" - exchangeGrpcEndpoint = "sentry.exchange.grpc.injective.network:443" - explorerGrpcEndpoint = "sentry.explorer.grpc.injective.network:443" - chainTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - exchangeTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - explorerTLSCert = credentials.NewServerTLSFromCert(&tls.Certificate{}) - chainCookieAssistant = &BareMetalLoadBalancedCookieAssistant{} - exchangeCookieAssistant = &BareMetalLoadBalancedCookieAssistant{} - explorerCookieAssistant = &BareMetalLoadBalancedCookieAssistant{} - - return Network{ - LcdEndpoint: lcdEndpoint, - TmEndpoint: tmEndpoint, - ChainGrpcEndpoint: chainGrpcEndpoint, - ChainStreamGrpcEndpoint: chainStreamGrpcEndpoint, - ChainTLSCert: chainTLSCert, - ExchangeGrpcEndpoint: exchangeGrpcEndpoint, - ExchangeTLSCert: exchangeTLSCert, - ExplorerGrpcEndpoint: explorerGrpcEndpoint, - ExplorerTLSCert: explorerTLSCert, - ChainId: "injective-1", - FeeDenom: "inj", - Name: "mainnet", - ChainCookieAssistant: chainCookieAssistant, - ExchangeCookieAssistant: exchangeCookieAssistant, - ExplorerCookieAssistant: explorerCookieAssistant, - OfficialTokensListURL: MainnetTokensListURL, - } default: panic(fmt.Sprintf("invalid network %s", name)) diff --git a/examples/chain/permissions/1_CreateNamespace/example.go b/examples/chain/permissions/1_CreateNamespace/example.go new file mode 100644 index 00000000..540d2a9b --- /dev/null +++ b/examples/chain/permissions/1_CreateNamespace/example.go @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + blockedAddress := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r" + namespace := permissionstypes.Namespace{ + Denom: "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test", + RolePermissions: []*permissionstypes.Role{ + { + Role: permissionstypes.EVERYONE, + Permissions: uint32(permissionstypes.Action_MINT | permissionstypes.Action_RECEIVE | permissionstypes.Action_BURN), + }, + { + Role: "blacklisted", + Permissions: 0, + }, + }, + AddressRoles: []*permissionstypes.AddressRoles{ + { + Address: blockedAddress, + Roles: []string{"blacklisted"}, + }, + }, + } + + msg := &permissionstypes.MsgCreateNamespace{ + Sender: senderAddress.String(), + Namespace: namespace, + } + + //AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg + response, err := chainClient.SyncBroadcastMsg(msg) + + if err != nil { + panic(err) + } + + str, _ := json.MarshalIndent(response, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/2_DeleteNamespace/example.go b/examples/chain/permissions/2_DeleteNamespace/example.go new file mode 100644 index 00000000..611f569e --- /dev/null +++ b/examples/chain/permissions/2_DeleteNamespace/example.go @@ -0,0 +1,74 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + namespaceDenom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + + msg := &permissionstypes.MsgDeleteNamespace{ + Sender: senderAddress.String(), + NamespaceDenom: namespaceDenom, + } + + //AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg + response, err := chainClient.SyncBroadcastMsg(msg) + + if err != nil { + panic(err) + } + + str, _ := json.MarshalIndent(response, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/3_UpdateNamespace/example.go b/examples/chain/permissions/3_UpdateNamespace/example.go new file mode 100644 index 00000000..2fd48eaf --- /dev/null +++ b/examples/chain/permissions/3_UpdateNamespace/example.go @@ -0,0 +1,78 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + namespaceDenom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + + msg := &permissionstypes.MsgUpdateNamespace{ + Sender: senderAddress.String(), + NamespaceDenom: namespaceDenom, + WasmHook: &permissionstypes.MsgUpdateNamespace_MsgSetWasmHook{NewValue: "inj19ld6swyldyujcn72j7ugnu9twafhs9wxlyye5m"}, + SendsPaused: &permissionstypes.MsgUpdateNamespace_MsgSetSendsPaused{NewValue: true}, + MintsPaused: &permissionstypes.MsgUpdateNamespace_MsgSetMintsPaused{NewValue: true}, + BurnsPaused: &permissionstypes.MsgUpdateNamespace_MsgSetBurnsPaused{NewValue: true}, + } + + //AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg + response, err := chainClient.SyncBroadcastMsg(msg) + + if err != nil { + panic(err) + } + + str, _ := json.MarshalIndent(response, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/4_UpdateNamespaceRoles/example.go b/examples/chain/permissions/4_UpdateNamespaceRoles/example.go new file mode 100644 index 00000000..62941e9d --- /dev/null +++ b/examples/chain/permissions/4_UpdateNamespaceRoles/example.go @@ -0,0 +1,91 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + namespaceDenom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + blockedAddress := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r" + + msg := &permissionstypes.MsgUpdateNamespaceRoles{ + Sender: senderAddress.String(), + NamespaceDenom: namespaceDenom, + RolePermissions: []*permissionstypes.Role{ + { + Role: permissionstypes.EVERYONE, + Permissions: uint32(permissionstypes.Action_RECEIVE), + }, + { + Role: "blacklisted", + Permissions: 0, + }, + }, + AddressRoles: []*permissionstypes.AddressRoles{ + { + Address: blockedAddress, + Roles: []string{"blacklisted"}, + }, + }, + } + + //AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg + response, err := chainClient.SyncBroadcastMsg(msg) + + if err != nil { + panic(err) + } + + str, _ := json.MarshalIndent(response, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/5_RevokeNamespaceRoles/example.go b/examples/chain/permissions/5_RevokeNamespaceRoles/example.go new file mode 100644 index 00000000..3bc62f57 --- /dev/null +++ b/examples/chain/permissions/5_RevokeNamespaceRoles/example.go @@ -0,0 +1,81 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + namespaceDenom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + blockedAddress := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r" + + msg := &permissionstypes.MsgRevokeNamespaceRoles{ + Sender: senderAddress.String(), + NamespaceDenom: namespaceDenom, + AddressRolesToRevoke: []*permissionstypes.AddressRoles{ + { + Address: blockedAddress, + Roles: []string{"blacklisted"}, + }, + }, + } + + //AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg + response, err := chainClient.SyncBroadcastMsg(msg) + + if err != nil { + panic(err) + } + + str, _ := json.MarshalIndent(response, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/6_ClaimVoucher/example.go b/examples/chain/permissions/6_ClaimVoucher/example.go new file mode 100644 index 00000000..3af4da11 --- /dev/null +++ b/examples/chain/permissions/6_ClaimVoucher/example.go @@ -0,0 +1,74 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + denom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + + msg := &permissionstypes.MsgClaimVoucher{ + Sender: senderAddress.String(), + Denom: denom, + } + + //AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg + response, err := chainClient.SyncBroadcastMsg(msg) + + if err != nil { + panic(err) + } + + str, _ := json.MarshalIndent(response, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/query/1_AllNamespaces/example.go b/examples/chain/permissions/query/1_AllNamespaces/example.go new file mode 100644 index 00000000..650477d2 --- /dev/null +++ b/examples/chain/permissions/query/1_AllNamespaces/example.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "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("devnet", "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) + } + + ctx := context.Background() + + res, err := chainClient.FetchAllNamespaces(ctx) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + +} diff --git a/examples/chain/permissions/query/2_NamespaceByDenom/example.go b/examples/chain/permissions/query/2_NamespaceByDenom/example.go new file mode 100644 index 00000000..69efeb11 --- /dev/null +++ b/examples/chain/permissions/query/2_NamespaceByDenom/example.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + namespaceDenom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + + ctx := context.Background() + + res, err := chainClient.FetchNamespaceByDenom(ctx, namespaceDenom, true) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/query/3_AddressRoles/example.go b/examples/chain/permissions/query/3_AddressRoles/example.go new file mode 100644 index 00000000..17fe06a0 --- /dev/null +++ b/examples/chain/permissions/query/3_AddressRoles/example.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + namespaceDenom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + address := senderAddress.String() + + ctx := context.Background() + + res, err := chainClient.FetchAddressRoles(ctx, namespaceDenom, address) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/query/4_AddressesByRole/example.go b/examples/chain/permissions/query/4_AddressesByRole/example.go new file mode 100644 index 00000000..ca2b3f7f --- /dev/null +++ b/examples/chain/permissions/query/4_AddressesByRole/example.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + namespaceDenom := "factory/inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r/inj_test" + role := "blacklisted" + + ctx := context.Background() + + res, err := chainClient.FetchAddressesByRole(ctx, namespaceDenom, role) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/permissions/query/5_VouchersForAddress/example.go b/examples/chain/permissions/query/5_VouchersForAddress/example.go new file mode 100644 index 00000000..fe7b9d96 --- /dev/null +++ b/examples/chain/permissions/query/5_VouchersForAddress/example.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "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("devnet", "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", + "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3", // 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) + } + + address := senderAddress.String() + + ctx := context.Background() + + res, err := chainClient.FetchVouchersForAddress(ctx, address) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +}