This repository is a comprehensive, beginner-friendly guide to building your first C program on the Thru blockchain. Whether you are a seasoned developer or just starting out, this guide will walk you through every step of the process.
Start by cloning this repository and navigating into the project directory:
git clone https://github.com/namedfarouk/thru-c-tutorial.git
cd thru-c-tutorialOpen your terminal and run the following command to install the core build tools:
sudo apt update && sudo apt install -y build-essential pkg-config libssl-dev protobuf-compiler libclang-dev clang npmThru uses Buf for protocol buffers. Install it globally via npm:
sudo npm install -g @bufbuild/bufThe Thru CLI is built with Rust. Install the Rust toolchain:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Press 1 to proceed with the default installation
source $HOME/.cargo/envNow, install the official Thru command-line interface:
cargo install thru-cli --lockedNote: We use
--lockedto ensure we use the exact versions of dependencies intended by the developers, avoiding version conflicts.
mkdir -p ~/.thru/clithru-cli keys generate defaultBy default, the Thru CLI may be configured to use a local RPC endpoint (127.0.0.1), which will not work unless you are running a local Thru node.
For this guide, you should use the Thru Alphanet RPC.
nano ~/.thru/cli/config.yamlChange this:
rpc_base_url: http://127.0.0.1:8472To this:
rpc_base_url: https://rest.alphanet.thruput.orgSave and exit the file.
This command will set up your default account on the Thru network:
thru-cli account create defaultTo compile C code for the Thru VM (which runs on RISC-V), you need:
- System dependencies
- The Thru RISC-V toolchain
- The Thru C SDK
sudo apt update
sudo apt install -y \
build-essential \
pkg-config \
libssl-dev \
gcc-riscv64-unknown-elfthru-cli dev toolchain installthru-cli dev sdk install cls ~/.thru/sdk
which riscv64-unknown-elf-gccExpected output:
c toolchain
/usr/bin/riscv64-unknown-elf-gccThe Makefile tells the compiler how to build your project. Create a file named GNUmakefile in your root directory:
## Explicit configuration for the Thru SDK build
THRU_HOME := $(HOME)/.thru
BASEDIR := $(CURDIR)/build
THRU_C_SDK_DIR := $(THRU_HOME)/sdk/c/thru-sdk
THRU_RISCV_TOOLCHAIN := $(THRU_HOME)/sdk/toolchain
export THRU_HOME
export THRU_RISCV_TOOLCHAIN
export PATH := $(THRU_RISCV_TOOLCHAIN)/bin:$(PATH)
include $(THRU_C_SDK_DIR)/thru_c_program.mkThis file defines the "shape" of your data and instructions.
#ifndef TN_COUNTER_H
#define TN_COUNTER_H
#include <thru-sdk/c/tn_sdk.h>
#define TN_COUNTER_ERR_INVALID_INSTRUCTION 0x1000UL
#define TN_COUNTER_INSTRUCTION_CREATE (0U)
#define TN_COUNTER_INSTRUCTION_INCREMENT (1U)
typedef struct __attribute__((packed)) {
uint instruction_type;
ushort account_index;
uchar counter_program_seed[TN_SEED_SIZE];
uint proof_size;
} tn_counter_create_args_t;
typedef struct __attribute__((packed)) {
ulong counter_value;
} tn_counter_account_t;
#endifThis is where the actual "smart" logic of your program lives.
#include <thru-sdk/c/tn_sdk.h>
#include "tn_counter.h"
TSDK_ENTRYPOINT_FN void start(uchar const *data, ulong size) {
if (size < sizeof(uint)) tsdk_revert(TN_COUNTER_ERR_INVALID_INSTRUCTION);
uint const *type = (uint const *)data;
if (*type == TN_COUNTER_INSTRUCTION_CREATE) {
// Handle initialization
tsdk_return(TSDK_SUCCESS);
} else {
tsdk_revert(TN_COUNTER_ERR_INVALID_INSTRUCTION);
}
}...commands...
Thru currently operates on Alphanet. You may occasionally encounter errors such as:
service is currently unavailabletransaction rejectedParse failed
These are network-side issues and NOT caused by your setup. If this happens:
- Wait 1โ2 minutes
- Retry the command
โ ๏ธ Important
Before building, ensure the RISC-V toolchain is exposed to make:
export THRU_HOME=$HOME/.thru
export THRU_RISCV_TOOLCHAIN=$THRU_HOME/sdk/toolchain
export PATH=$THRU_RISCV_TOOLCHAIN/bin:$PATHrm -rf build
makeDeploy your compiled program to the Thru blockchain:
thru-cli program create my_counter ./build/thruvm/bin/tn_counter_program_c.binVisit scan.thru.org and paste your Program ID to see your program live on the blockchain!
If you found this guide helpful, feel free to reach out!
- X (Twitter): @namedfarouk
Thanks for using my guide! Built with โค๏ธ for the Thru Community.