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

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"cosmossdk.io/math"
     8  	"github.com/InjectiveLabs/sdk-go/client"
     9  	"github.com/InjectiveLabs/sdk-go/client/common"
    10  
    11  	oracletypes "github.com/InjectiveLabs/sdk-go/chain/oracle/types"
    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  	price := []math.LegacyDec{math.LegacyMustNewDecFromStr("100")}
    60  	base := []string{"BAYC"}
    61  	quote := []string{"WETH"}
    62  
    63  	msg := &oracletypes.MsgRelayPriceFeedPrice{
    64  		Sender: senderAddress.String(),
    65  		Price:  price,
    66  		Base:   base,
    67  		Quote:  quote,
    68  	}
    69  
    70  	// AsyncBroadcastMsg, SyncBroadcastMsg, QueueBroadcastMsg
    71  	result, err := chainClient.SyncBroadcastMsg(msg)
    72  
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  
    77  	fmt.Printf("Broadcast result: %s\n", result)
    78  }