github.com/InjectiveLabs/sdk-go@v1.53.0/examples/chain/authz/1_MsgGrant/example.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/InjectiveLabs/sdk-go/client"
     9  
    10  	"github.com/InjectiveLabs/sdk-go/client/common"
    11  
    12  	chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
    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  	granter := senderAddress.String()
    60  	grantee := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
    61  	expireIn := time.Now().AddDate(1, 0, 0) // years months days
    62  
    63  	// GENERIC AUTHZ
    64  	// msgtype := "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder"
    65  	// msg := chainClient.BuildGenericAuthz(granter, grantee, msgtype, expireIn)
    66  
    67  	// TYPED AUTHZ
    68  	msg := chainClient.BuildExchangeAuthz(
    69  		granter,
    70  		grantee,
    71  		chainclient.CreateSpotLimitOrderAuthz,
    72  		chainClient.DefaultSubaccount(senderAddress).String(),
    73  		[]string{"0xe0dc13205fb8b23111d8555a6402681965223135d368eeeb964681f9ff12eb2a"},
    74  		expireIn,
    75  	)
    76  
    77  	// AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
    78  	err = chainClient.QueueBroadcastMsg(msg)
    79  
    80  	if err != nil {
    81  		fmt.Println(err)
    82  	}
    83  
    84  	time.Sleep(time.Second * 5)
    85  
    86  	gasFee, err := chainClient.GetGasFee()
    87  
    88  	if err != nil {
    89  		fmt.Println(err)
    90  		return
    91  	}
    92  
    93  	fmt.Println("gas fee:", gasFee, "INJ")
    94  }