Path: blob/main/docs/versioned_docs/version-v0.25/guide/03-blog/02-connect-blockchain.md
1722 views
------Create a blockchain client in Go
Learn how to connect your blockchain to an independent application with RPC.
After creating the blog blockchain in this tutorial you will learn how to connect to your blockchain from a separate client.
Use the blog blockchain
Navigate to a separate directory right next to the blog blockchain you built in the Build a Blog tutorial.
Creating a blockchain client
Create a new directory called blogclient on the same level as blog directory. As the name suggests, blogclient will contain a standalone Go program that acts as a client to your blog blockchain.
The command:
Shows just blog now. More results are listed when you have more directories here.
Create your blogclient directory first, change your current working directory, and initialize the new Go module.
The go.mod file is created inside your blogclient directory.
Your blockchain client has only two dependencies:
The
blogblockchaintypesfor message types and a query clientignitefor thecosmosclientblockchain client
The replace directive uses the package from the local blog directory and is specified as a relative path to the blogclient directory.
Cosmos SDK uses a custom version of the protobuf package, so use the replace directive to specify the correct dependency.
The blogclient will eventually have only two files:
main.gofor the main logic of the clientgo.modfor specifying dependencies.
Main logic of the client in main.go
Add the following code to your main.go file to make a connection to your blockchain from a separate app.
Read the comments in the code carefully to learn details about each line of code.
To learn more about the cosmosclient package, see the Go cosmosclient package documentation. Details are provided to learn how to use the Client type with Options and KeyringBackend.
Run the blockchain and the client
Make sure your blog blockchain is still running with ignite chain serve.
Install dependencies for your blogclient:
Run the blockchain client:
If successful, the results of running the command are printed to the terminal:
You can confirm the new post with using the blogd query blog posts command that you learned about in the previous chapter. The result looks similar to:
Congratulations, you have just created a post using a separate app.