What You’ll Build
By the end of this tutorial, you will:- Initialize a blockchain with Proof of Authority consensus
- Create and fund user accounts
- Deploy a smart contract written in assembly
- Call the contract and produce blocks
- Explore the blockchain state
Prerequisites
Make sure you have:- Rust 1.70+ installed
- The Minichain binary built (
cargo build --release) - Basic understanding of blockchain concepts
Complete Workflow
-
Initialize the Blockchain
Create a new blockchain with one authority node:
Output:This creates:
- A genesis block with height 0
- Authority keypairs in
data/keys/ - Configuration file at
data/config.json - Blockchain database in
data/directory
--forceflag: -
Create User Accounts
Generate keypairs for two users, Alice and Bob:
List all accounts to verify:Each account consists of:
- Ed25519 keypair (private/public keys)
- Derived address (Blake3 hash of public key)
- JSON file stored in
data/keys/
-
Fund the Accounts
Only authority accounts can mint new tokens. Fund Alice and Bob:
Replace the addresses with the actual addresses shown by
account list. Check Alice’s balance: -
Write a Smart Contract
Create a simple counter contract that increments a storage value.
Save this as
counter.asm:What it does:- Loads storage slot 0 into register R1
- Adds 1 to the counter
- Stores the new value back to slot 0
- Halts execution
-
Deploy the Contract
Compile and deploy the contract using Alice’s account:
Output:Save the contract address - you’ll need it for calling the contract!
-
Produce a Block
The transaction is now in the mempool. Produce a block to execute it:
Output:The contract is now deployed and ready to use!
-
Call the Contract
Call the counter contract to increment the value:
Produce a block to execute the call:Call it again from Bob’s account:The counter storage slot now holds the value
2(incremented twice). -
Explore the Blockchain
List recent blocks:
View block details:Output:Check contract account:Check Alice’s updated balance:Notice that Alice’s balance has decreased by the gas cost of deployment and execution.
Transaction Flow Summary
The complete transaction flow in Minichain:Gas Costs Reference
Key gas costs for the operations used:| Operation | Gas Cost | Description |
|---|---|---|
| Transfer | 21,000 | Base transaction cost |
| Deploy (per byte) | 200 | Contract deployment |
| LOADI | 2 | Load immediate value |
| ADD | 2 | Arithmetic operation |
| SLOAD | 100 | Storage read |
| SSTORE (set) | 20,000 | Storage write (new value) |
| SSTORE (reset) | 5,000 | Storage write (existing slot) |
| HALT | 0 | Stop execution |
Next Steps
Now that you’ve completed a full workflow:- Learn to write more complex contracts in Writing Your First Contract
- Master debugging techniques in Debugging Contracts
- Understand testing patterns in Testing Guide
- Explore the VM instruction set
Troubleshooting
Insufficient Balance Error
If you see “Insufficient balance”, make sure:- You’ve minted enough tokens to the account
- The gas limit is not excessively high
- You haven’t already spent the tokens
Transaction Pending
Remember that transactions must be included in a block:- Always run
block produceafter submitting transactions - Check the mempool with
block listto see pending transactions
Contract Not Found
If calling a contract fails:- Verify the contract address is correct
- Ensure you’ve produced a block after deployment
- Check that the address is a contract with
account info