Tutorial : How-to Cosmos Pt. 2- Building and Signing a Tx on Cosmos hub using Gaiad

KagemniKarimu
Lava Network
Published in
8 min readMay 16, 2023

Hello Cosmonauts! ⚛️
We return to our “Cosmos How-Tos” blog series 🎓🎓

If you did not see our previous tutorial in this series, it’s pretty essential that you go check it out. This series is intended to introduce someone to Cosmos with a hands-on approach. For those unaware and uninitiated, Cosmos is a network of interconnected appchains that focus on inter-blockchain communication with a common core. Popular projects such as Juno, Osmosis, Celestia and Lava all use Cosmos. Proverbially, Cosmos is one of the ‘Big Four’ for building decentralized applications (dApps) and Smart Contracts with NEAR, Solana, and Ethereum (EVM).

What’s covered in this tutorial — sending a transaction or Tx — is one of the essential features of participation in the Cosmos ecosystem. Most users only ever accomplish this through the use of a web wallet or dapp UI. Today, we’ll show how to do it from the terminal using a CLI named gaiad. Doing it this way will provide you hands-on insights on how a blockchain works and how transactions on the Cosmos network are formed and verified.

You’ll need the following to continue

✅ adminstrator access to a Linux machine with gaiad installed

✅ a private key which is loaded with Testnet funds and accessible via gaiad

Don’t worry! Both of these steps are well explained in Part 1 of this series!

1. Ensure your node is connected to an RPC Provider

The first step is to make sure you’re connected to RPC — if you’ve done our previous tutorial recently. All you’ll need to do is check the status!

gaiad status

🟢 If things worked right, you should see output like below!

This means that your RPC endpoint is reachable from your gaiad installation. Having properly configured RPC is an essential step to sending our first transaction. You can move onto the next step!

⚠️NOTE: if your RPC endpoint is unreachable, your terminal may hang. If so, press CTRL+C. You’ll need to return to the Lava gateway to get an RPC endpoint that you can use. Once you’ve retrieved a working RPC endpoint for the Cosmos Hub (Testnet), then you can continue onto the next step. Having a working RPC is essential to what we do next.

Once you’ve gotten your URL you can put it in gaiad with the following command, replacing the dummy URL with your own:

gaiad config node https://g.w.lavanet.xyz:443/gateway/cos5t/rpc-http/a43dd98c9ea68afa4f167969f48970bb

Then, type in the following command and see if your terminal hangs again.

gaiad status

After doing so, you should get input similar to what was displayed above(🟢) and you can now move onto our next step!

2. Verify your Cosmos Wallet address & Balance from the Command Line

As you may remember in the last tutorial, you will have created a Cosmos wallet using only command-line input.

An important step is to verify that this Cosmos wallet is still accessible from your machine. Type the following command:

gaiad keys list

This will list all the keys available to gaiad at the time of this command. You should get some output that displays your previous address by key_name. It should look like the following:

⚠️ If you did not get a print out of names, types, and addresses, please refer to the last tutorial which will guide you through the creation of a new address!

Next, we need to verify the balance of our address. Copy and paste the string of characters after it says address: and input the following command replacing my address with your own! We’ll use the query bank balances command which will give us information on all the available balances on chain at that address. An example of this should look like:

gaiad query bank balances cosmos15dgrn5rl8eah6s3lcjm5gjsn78w6kg5plmn6ml

This will return input reporting the specified address’s balance. Behind the scenes gaiad is really asking the RPC node for the address - but that’s neither here nor there!

As you can see, we verify that we have a uatom balance of 1000000 . If you recall, this is from when we used the CosmosHub Testnet faucet. Now that we have verified our balance, we can move onto sending our funds.

3. Generate a Tx JSON

It’s important to note that if you’re sending it — it must be going somewhere!

First, let’s make sure we’ve got the correct chain-id. This is akin to making sure we’re operating in the correct universe. You can check where gaiad is pointed using gaiad config chain-id. We want to identify the Theta testnet:

gaiad config chain-id theta-testnet-001

Our next step is to identify a sender and a receiver . We can use our pubkey address as the sender. If you don’t remember your address, you can always get it using the gaiad keys list command again. For a receiver, feel free to use my address cosmos15dgrn5rl8eah6s3lcjm5gjsn78w6kg5plmn6ml 😉. Of course, you’re always free to build a second key-pair using the same steps you made the first one. Once you have both a sender and receiver, build the transaction using the following format:

gaiad tx bank send <sender> <receiver> <amount> --fees=200uatom --generate-only > unsignedTx.json

<sender> field will accept either your address. I use cosmos15dgrn5rl8eah6s3lcjm5gjsn78w6kg5plmn6mlwhich is the pubkey of my wallet titled test1.
<receiver> field is another public key address to send to. You can use cosmos15dgrn5rl8eah6s3lcjm5gjsn78w6kg5plmn6ml which is the public address I created at test1! I’ll use cosmos1qxpu7dc0vz2cha0rucdezevejvcwltw4ytqyd2 which is another address I created at test2.
amount field is parsed as number, then denomination, for short hand <INTdenom> i.e., 1000uatom.

With our --fees=<amount>tag, we need to specify the amount of fees that will be provided to cover the transaction we are building. It carries this same <INTdenom> format as the amount we send. We’ll choose 200 uatom, which should be sufficient to send at the time of this tutorial.

It is this --generate-only flag which is crucial. --generate-only let’s gaiad know that we only want to create some output for our transaction, not actually send it! We could send straightaway but this gives us a chance to inspect the transaction that we are performing. We use > unSignedTx.json to dump the output into a JSON file in the current directory.

If you drop the --generate-only tag, gaiad will prompt you to sign automatically, so you can move onto step 6.

Otherwise, try to cat unSignedTx.json to ensure that the file has been generated by your command. It should contain tx info!

4. Sign the TX JSON and validate the signature

In order to verify that the transaction is coming from you, you will need to cryptographically sign the Tx JSON that we just generated — this is a fairly simple process which involves identifying the chain-id and the key that you wish to use. Note that if you used the key_name to build the transaction, you can simply double up here. My sign example is below

gaiad tx sign --chain-id="theta-testnet-001" --from=test1 unsignedTx.json &> signedTx.json

This will take the unsignedTx.json and put it in a signedTx json signed by test1. gaiad will automatically validate the signatures after signing. After a short pause, go ahead and cat signedTx.json. You should get output similar to the following:

Look closely at this message. Notice that as opposed to before — our transaction indicates that there are signatures that have been directly signed. This is a crucial feature of a successful broadcast and one that it’s important to understand. Cryptographic signatures are the basis for a secure blockchain.

5. Broadcast the TX JSON using RPC

Now, the money!! Let’s send our transaction right away. 💸 Type the following:

gaiad tx broadcast signedTx.json

Once we do, somewhere in the backdrop, an RPC node (perhaps one served through the Lava gateway) is processing our request. gaiad will attempt to sync and return the txHash after your transaction broadcasts! Make sure you copy it into your clipboard. You will need it for the next step.

6. Verify the transaction went through

The final step is to check the receiver address we used in order to verify the funds went through. We do this to ensure the funds went exactly where we sent them!

This step is technically optional, as the broadcast command returns a CheckTx response by default. There are two ways we can accomplish this:

✅We can query the receiver address that we used earlier to see if the balance has changed. Note that blockchains take a moment to make consensus — our transaction may not post instantly.

gaiad query bank balances <receiver>

✅Secondarily, we can double-check the tx-hash to see if there is any information on chain about the exchange that took place. If we’re too hasty, we may get a tx not found error!

gaiad query tx <tx-hash>

Did the balance update? 😄You should be able to easily tell whether the transaction did indeed occur. Try comparing balances on both <sender> and <receiver> addresses. You can even try gaiad query account and see what else you might learn. Once you have verified, that’s it! You’ve just sent your first transaction on a blockchain from the command line.

😅For next time, we’ll show how to do this same process of building and sending a transaction, except programmatically using Cosmjs. We look forward to seeing you in Pt 3 of our series!

Further Resources

🚪https://gateway.lavanet.xyz

📚 https://docs.lavanet.xyz/tutorials

💻How to Cosmos Part 1

⚛️gaiad tutorials

About the Author🧑🏿‍💻

KagemniKarimu is current Developer Relations Engineer for Lava Network and former Developer Relations at Skynet Labs. He’s a self-proclaimed Rubyist, new Rust learner, and friendly Web3 enthusiast who entertains all conversations about tech. Follow him on Twitter or say hi to him on Lava’s Discord where he can be found lurking.

About Lava 🌋

Lava is a decentralized network of top-tier API providers that developers can subscribe to for access to any blockchain. Providers are rewarded for their quality of service, meaning apps and users get maximum speed, data integrity and uptime. RPC requests are routed to multiple endpoints at once, meaning anyone can privately query or send transactions on-chain, while being matched to the most performant and reliable providers.

We help developers build web3 apps on any chain, with reliable, performant and decentralized node infrastructure.

--

--