github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/docs/using-the-sdk/quick-start.md (about) 1 # Quick Start 2 3 This guide serves as a practical introduction to building blockchains with the Cosmos SDK. It shows how to scaffold the code for a basic blockchain node, build and run it. Several important concepts of the Cosmos SDK are introduced along the way. 4 5 ## Setup 6 7 ::: tip 8 To follow this guide, you need to [install golang](https://golang.org/doc/install) and set [your $GOPATH environment variable](https://golang.org/doc/code.html#GOPATH) 9 ::: 10 11 ::: warning 12 Make sure you are using the latest stable version of golang available on https://golang.org/dl/ 13 ::: 14 15 First, download the [`scaffold`](https://github.com/cosmos/scaffold) tool: 16 17 ```bash 18 git clone https://github.com/cosmos/scaffold 19 ``` 20 21 The `scaffold` tool lets you easily scaffold boilerplate Cosmos SDK applications. Once you have downloaded it, simply install it on your machine: 22 23 ```bash 24 cd scaffold 25 make 26 ``` 27 28 ## Create a Basic Cosmos SDK Blockchain 29 30 To create a basic Cosmos SDK application, simply type in the following command: 31 32 ```bash 33 scaffold app lvl-1 <username|org> <repo> 34 ``` 35 36 where `username|org` is the name of your github/gitlab/atlassian username or organisation, and `repo` the name of the distant repository you would push your application too. These arguments are used to configure the imports so that people can easily download and install your application once (if) you upload it. 37 38 The command above creates a starter application in a new folder named after the `repo` argument. This application contains the [basic logic most SDK applications](../intro/sdk-app-architecture.md) need as well as a set of standard [modules](../building-modules/intro.md) already hooked up. These include: 39 40 - [`auth`](../../x/auth/spec/): Accounts, signatures and fees. 41 - [`bank`](../../x/bank/spec/): Token transfers. 42 - [`staking`](../../x/staking/spec/): Proof-of-Stake logic, which is a way of managing validator set changes in public decentralised networks. Also includes delegation logic. 43 - [`slashing`](../../x/slashing/spec/): Slash validators that misebehave. Complementary to the `staking` module. 44 - [`distribution`](../../x/distribution/spec/): Distribution of rewards and fees earned by participants in the Proof-of-Stake system (delegators and validators). 45 - [`params`](../../x/params/spec/): Global parameter store of the application. 46 - [`supply`](../../x/supply/spec/): Handles global token supply of the application. Enables modules to hold tokens. 47 - [`genutil`](../../x/genutil) and [`genaccounts`](../../x/genaccounts): Utility modules to facilitate creation of genesis file. 48 49 Now, go into the application's folder. The structure should look like the following: 50 51 ``` 52 ├── app/ 53 │ ├── app.go 54 │ └── export.go 55 ├── cmd/ 56 │ ├── acli/ 57 │ │ └── main.go 58 │ ├── aud/ 59 │ │ └── main.go 60 ├── Makefile 61 ├── go.mod 62 └── x/ 63 ``` 64 65 where: 66 67 - `app.go` is the [main file](../basics/app-anatomy.md#core-application-file) defining the application logic. This is where the state is intantiated and modules are declared. This is also where the Cosmos SDK is imported as a dependency to help build the application. 68 - `export.go` is a helper file used to export the state of the application into a new genesis file. It is helpful when you want to upgrade your chain to a new (breaking) version. 69 - `acli/main.go` builds the command-line interface for your blockchain application. It enables end-users to create transactions and query the chain for information. 70 - `aud/main.go` builds the main [daemon client](../basics/app-anatomy.md#node-client) of the chain. It is used to run a full-node that will connect to peers and sync its local application state with the latest state of the network. 71 - `go.mod` helps manage dependencies. The two main dependencies used are the Cosmos SDK to help build the application, and Tendermint to replicate it. 72 - `x/` is the folder to place all the custom modules built specifically for the application. In general, most of the modules used in an application have already been built by third-party developers and only need to be imported in `app.go`. These modules do not need to be cloned into the application's `x/` folder. This is why the basic application shown above, which uses several modules, works despite having an empty `x/` folder. 73 74 ## Run your Blockchain 75 76 First, install the two main entrypoints of your blockchain, `aud` and `acli`: 77 78 ```bash 79 go mod tidy 80 make install 81 ``` 82 83 Make sure the clients are properly installed: 84 85 ```bash 86 acli --help 87 aud --help 88 ``` 89 90 Now that you have your daemon client `aud` and your command-line interface `acli` installed, go ahead and initialize your chain: 91 92 ```bash 93 aud init <node-moniker> --chain-id test 94 ``` 95 96 The command above creates all the configuration files needed for your node to run, as well as a default genesis file, which defines the initial state of the network. Before starting the chain, you need to populate the state with at least one account. To do so, first create a new [account](../basics/accounts.md) named `validator` (feel free to choose another name): 97 98 ```bash 99 acli keys add validator 100 ``` 101 102 Now that you have created a local account, go ahead and grant it `stake` tokens in your chain's genesis file. Doing so will also make sure your chain is aware of this account's existence: 103 104 ```bash 105 aud add-genesis-account $(acli keys show validator -a) 100000000stake 106 ``` 107 108 Now that your account has some tokens, you need to add a validator to your chain. Validators are special full-nodes that participate in the consensus process (implemented in the [underlying consensus engine](../intro/sdk-app-architecture.md#tendermint)) in order to add new blocks to the chain. Any account can declare its intention to become a validator operator, but only those with sufficient delegation get to enter the active set (for example, only the top 125 validator candidates with the most delegation get to be validators in the Cosmos Hub). For this guide, you will add your local node (created via the `init` command above) as a validator of your chain. Validators can be declared before a chain is first started via a special transaction included in the genesis file called a `gentx`: 109 110 ```bash 111 // create a gentx 112 aud gentx --name validator --amount 100000stake 113 114 // add the gentx to the genesis file 115 aud collect-gentxs 116 ``` 117 118 A `gentx` does three things: 119 120 1. Makes the `validator` account you created into a validator operator account (i.e. the account that controls the validator). 121 2. Self-delegates the provided `amount` of staking tokens. 122 3. Link the operator account with a Tendermint node pubkey that will be used for signing blocks. If no `--pubkey` flag is provided, it defaults to the local node pubkey created via the `aud init` command above. 123 124 For more on `gentx`, use the following command: 125 126 ```bash 127 aud gentx --help 128 ``` 129 130 Now that everyting is set up, you can finally start your node: 131 132 ```bash 133 aud start 134 ``` 135 136 You should see blocks come in. 137 138 ## Send Tokens and Increase Delegation 139 140 Now that your chain is running, it is time to try sending tokens from the first account you created to a second account. In a new terminal window, start by running the following query command: 141 142 ```bash 143 acli query account $(acli keys show validator -a) --chain-id test 144 ``` 145 146 You should see the current balance of the account you created, equal to the original balance of `stake` you granted it minus the amount you delegated via the `gentx`. Now, create a second account: 147 148 ```bash 149 acli keys add receiver 150 ``` 151 152 The command above creates a local key-pair that is not yet registered on the chain. An account is registered the first time it receives tokens from another account. Now, run the following command to send tokens to the second account: 153 154 ```bash 155 acli tx send $(acli keys show validator -a) $(acli keys show receiver -a) 1000stake --chain-id test 156 ``` 157 158 Check that the second account did receive the tokens: 159 160 ```bash 161 acli query account $(acli keys show receiver -a) --chain-id test 162 ``` 163 164 Finally, delegate some of the stake tokens sent to the `receiver` account to the validator: 165 166 ```bash 167 acli tx staking delegate $(acli keys show validator --bech val -a) 500stake --from receiver --chain-id test 168 ``` 169 170 Try to query the total delegations to `validator`: 171 172 ```bash 173 acli query staking delegations-to $(acli keys show validator --bech val -a) --chain-id test 174 ``` 175 176 You should see two delegations, the first one made from the `gentx`, and the second one you just performed from the `receiver` account. 177 178 ## Next 179 180 Congratulations on making it to the end of this short introduction guide! If you want to learn more, check out the following resources: 181 182 - [How to build a full SDK application from scratch](https://tutorials.cosmos.network/nameservice/tutorial/00-intro.html). 183 - [Read the Cosmos SDK Documentation](../intro/overview.md). 184