By following this beginner tutorial, you will end up with a simple blog app that is powered by the Cosmos SDK.
Let’s get started! The first step is to install the starport
CLI tool:
npm install -g @tendermint/starport
brew install tendermint/tap/starport
Alternatively, Starport can be built from source.
After starport
is installed, use it to create the initial app structure:
starport app github.com/example/blog
One of the main features of Starport is code generation. The command above has generated a directory structure with a working blockchain application. Starport can also add data types to your app with starport type
command. To see it in action, follow the poll application tutorial. In this guide, however, we’ll create those files manually to understand how it all works under the hood.
This blog app will store data in a persistent key-value store. Similarly to most key-value stores, you can retrieve, delete, update, and loop through keys to obtain the values you are interested in.
We’ll be creating a simple blog-like application, so let’s define the first type, the Post
.
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Post is a type containing Creator, Title, and ID
type Post struct {
Creator sdk.AccAddress `json:"creator" yaml:"creator"`
Title string `json:"title" yaml:"title"`
ID string `json:"id" yaml:"id"`
}
The code above defines the three properties of a post: Creator, Title and ID. The SDK provides useful types to represent things like addresses, so we use sdk.AccAddress
for Creator. A Title is stored as a string. Lastly, we generate unique global IDs for each post and also store them as strings.
Posts in our key-value store will look like this: