GenLayer Builder Mission ยท Full Tutorial

Zero to
GenLayer
๐Ÿš€

Learn, Build, Deploy โ€” AI-powered smart contracts in minutes. No Solidity. No oracles. Just Python + AI.

5
Tutorial Steps
0
Install Required
โˆž
Possibilities
Quick Start Hub

Everything You Need

All the tools and resources to begin building on GenLayer in one place.

๐Ÿ–ฅ๏ธ

GenLayer Studio

Browser-based IDE. Write, deploy, and test your Intelligent Contracts without installing anything.

Open Studio โ†’
๐Ÿšฐ

Testnet Faucet

Get free GEN tokens for the Asimov testnet. Paste your wallet address and tokens arrive in seconds.

Get Tokens โ†’
๐Ÿ“š

Official Docs

Full API reference, guides, and deep dives into GenLayer's architecture and SDK.

Read Docs โ†’
โšก

Project Boilerplate

Pre-configured starter with Vite + genlayer-js. Fork it and you're already halfway done.

Fork on GitHub โ†’

Core Concepts

Two Ideas That Change Everything

Before writing code, understand the two breakthroughs that make GenLayer unique.

Concept 01

โš–๏ธ Optimistic Democracy

When your contract calls an AI model, a randomly-selected group of validators each run the contract independently โ€” each with their own AI (GPT, LLaMA, Mistralโ€ฆ).

One validator is the Leader: they run first and propose a result. Other validators verify it. If the majority agree, the result is accepted on-chain.

๐Ÿ’ก Analogy Like a court: one judge writes the verdict, a panel independently reviews. If the majority agree, it becomes law โ€” fast, fair, trustless.
```
Concept 02

๐Ÿ”€ Equivalence Principle

Five validators might get five differently-worded LLM responses. "Yes", "Definitely!", "Affirmative" โ€” all mean the same thing but aren't identical strings.

The Equivalence Principle defines what counts as agreement. You choose the rule in your contract.

๐Ÿ’ก Analogy Like grading exams: two students say the same thing differently. The rule defines whether they both pass.
```
Mode When to Use Example
strict_eq Byte-for-byte identical results required Boolean True/False, exact numbers
non_comparative Each validator checks their own result against a rule "Does the response contain 'YES'?"
comparative Second LLM call checks if two results mean the same thing Long-form text, subjective answers

Step-by-Step Tutorial

From Zero to Deployed

No terminal. No installs. Just your browser and 5 steps.

1
Setup

Open GenLayer Studio

Navigate to studio.genlayer.com. This is your full development environment โ€” a browser-based IDE that runs a GenLayer node in the cloud. No Docker, no npm, no configuration files.

โœ… You'll see A code editor on the left, contract state panel on the right, and transaction logs at the bottom. Test accounts come pre-funded with tokens โ€” no faucet needed.
```
2
Create

Create a New Contract File

In the left sidebar, click the "+" button next to "Contracts". Name your file YesNoOracle.py. The Studio will open a blank Python editor for you.

3
Code

Paste the Contract

Copy the complete contract from Section 4 below and paste it into the editor. Studio will syntax-highlight it immediately.

4
Deploy

Click "Deploy Contract"

In the right panel, click "Deploy Contract". Since our __init__ takes no arguments, just confirm. Watch the transaction status change: PENDING โ†’ ACCEPTED โ†’ FINALIZED.

๐ŸŽ‰ When finalized Your contract address appears at the top of the State panel (looks like 0x1a2b3cโ€ฆ). Copy it โ€” you'll need it for the frontend.
5
Test

Call a Function in Studio

In the "Execute Transaction" panel, select ask_question, type "Is water wet?" as the argument, and click "Send Transaction". Once finalized, call get_latest() as a view method to see the stored YES/NO result.

๐Ÿ’ก Pro tip The "Logs" tab in Studio shows each validator's AI reasoning in real time. Watch the consensus process unfold step by step.
```

The Intelligent Contract

Full Working Contract

A complete Python Intelligent Contract. Copy it, deploy it, and it works.

YesNoOracle.py
# { "Depends": "py-genlayer:test" }
# This header tells GenVM which SDK version to load.

from genlayer import *

class YesNoOracle(gl.Contract):
โ€โ€โ€
AI Yes/No Oracle โ€” Intelligent Contract
Users ask any question. AI answers YES or NO.
All answers are stored permanently on-chain.
โ€œโ€โ€

```
# โ”€โ”€ State Variables (stored permanently on blockchain) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
questions:     gl.DynArray[str]   # Every question asked
answers:       gl.DynArray[bool]  # YES=True / NO=False
question_count: int              # Total questions answered

# โ”€โ”€ Constructor (called once at deployment) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def __init__(self):
    self.questions      = gl.DynArray[str]()
    self.answers        = gl.DynArray[bool]()
    self.question_count = 0

# โ”€โ”€ Write Method: ask a question โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
@gl.public.write
def ask_question(self, question: str) -> None:
    """Send a question. AI decides YES or NO. Stored on-chain."""

    # This inner function runs in GenLayer's AI sandbox.
    # Each validator executes it independently with their own LLM.
    def call_ai():
        prompt = f"""You are a concise Yes/No oracle.
```

The user asks: โ€œ{question}โ€
Rules:

- Answer ONLY with โ€œYESโ€ or โ€œNOโ€ โ€” nothing else.
- Base your answer on general knowledge and common sense.
  โ€œโ€โ€
  raw = gl.exec_prompt(prompt)
  # Parse to boolean so strict_eq works perfectly
  return โ€œYESโ€ in raw.strip().upper()
  
  ```
    # Equivalence Principle: all validators must return same boolean
    ai_answer: bool = gl.eq_principle_strict_eq(call_ai)
  
    # Store result permanently on-chain
    self.questions.append(question)
    self.answers.append(ai_answer)
    self.question_count += 1
  ```
  
  # โ”€โ”€ View Methods (read-only, free, no transaction needed) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
  @gl.public.view
  def get_answer(self, index: int) -> dict:
  if index >= self.question_count:
  raise Exception(โ€œIndex out of rangeโ€)
  return {
  โ€œquestionโ€: self.questions[index],
  โ€œanswerโ€:   โ€œYESโ€ if self.answers[index] else โ€œNOโ€,
  โ€œindexโ€:    index,
  }
  
  @gl.public.view
  def get_latest(self) -> dict:
  if self.question_count == 0:
  return {โ€œquestionโ€: โ€œNone yetโ€, โ€œanswerโ€: โ€œN/Aโ€, โ€œindexโ€: -1}
  return self.get_answer(self.question_count - 1)
  
  @gl.public.view
  def get_all_answers(self) -> list:
  return [self.get_answer(i) for i in range(self.question_count)]
  
  @gl.public.view
  def get_count(self) -> int:
  return self.question_count

Key Concepts in the Contract

gl.exec_prompt()

Sends your prompt to the validator's configured LLM. Each validator uses their own AI model โ€” GenLayer routes it automatically.

gl.eq_principle_strict_eq()

Wraps your AI function. Runs it in the non-deterministic sandbox and requires all validators to return the exact same boolean.

gl.DynArray[type]

A dynamic array that GenLayer serializes and stores on-chain. Survives between calls forever, unlike a regular Python list.

@gl.public.write vs .view

.write changes state and requires a transaction. .view is read-only and free โ€” no gas, no signing.


Frontend Interaction

Connect with genlayer-js

This is how your website talks to the blockchain. Three core functions you'll use in every GenLayer dApp.

๐Ÿ“ฆ Install npm install genlayer-js โ€” or fork the official boilerplate which has it pre-configured.
๐Ÿ“ค writeContract() โ€” Send a Question
// Sends a write transaction
const txHash = await client.writeContract({
  address:      CONTRACT_ADDRESS,
  functionName: 'ask_question',
  args:         [question],
  value:        0n,
});

// Wait for AI validators to agree
await client.waitForTransactionReceipt({
hash:     txHash,
status:   TransactionStatus.FINALIZED,
retries:  100,
interval: 3000,
});
๐Ÿ“– readContract() โ€” Get the Answer
// Free read โ€” no transaction needed
const result = await client.readContract({
address:      CONTRACT_ADDRESS,
functionName: โ€˜get_latestโ€™,
args:         [],
stateStatus:  โ€˜acceptedโ€™,
});

// Returns:
// { question: โ€œIs water wet?โ€,
//   answer: โ€œYESโ€, index: 0 }
oracle.js โ€” Full Setup
import { createClient, createAccount } from 'genlayer-js';
import { simulator } from 'genlayer-js/chains';
import { TransactionStatus } from 'genlayer-js/types';

// โ”€โ”€ Config โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
const CONTRACT_ADDRESS = โ€˜0xYOUR_CONTRACT_ADDRESS_HEREโ€™;

// โ”€โ”€ Create client connected to Studio simulator โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
const account = createAccount();
const client  = createClient({ chain: simulator, account });

// โ”€โ”€ Ask a question โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
async function askQuestion(question) {
await client.initializeConsensusSmartContract();
const txHash = await client.writeContract({
address: CONTRACT_ADDRESS, functionName: โ€˜ask_questionโ€™,
args: [question], value: 0n,
});
return client.waitForTransactionReceipt({
hash: txHash, status: TransactionStatus.FINALIZED,
retries: 100, interval: 3000,
});
}

// โ”€โ”€ Read the latest answer โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
async function getLatest() {
return client.readContract({
address: CONTRACT_ADDRESS, functionName: โ€˜get_latestโ€™,
args: [], stateStatus: โ€˜acceptedโ€™,
});
}

// โ”€โ”€ Read all answers โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
async function getAllAnswers() {
return client.readContract({
address: CONTRACT_ADDRESS, functionName: โ€˜get_all_answersโ€™,
args: [], stateStatus: โ€˜acceptedโ€™,
});
}

Architecture

How It All Flows

Trace a single question โ€” "Is coffee healthy?" โ€” all the way through the system.

๐Ÿ‘ค
User
types question
โ†’
๐ŸŒ
Frontend
genlayer-js
โ†’
๐Ÿ“œ
Contract
ask_question()
โ†’
โš™๏ธ
GenVM
AI sandbox
โ†’
๐Ÿค–
AI Models
LLM per validator
โ†’
๐Ÿ—ณ๏ธ
Validators
reach consensus
โ†’
โ›“๏ธ
Blockchain
stored forever
โ†’
โœ…
Result
YES or NO
Step 1โ€“2: Write Transaction User sends a question. genlayer-js signs and submits a writeContract transaction to the network.
Step 3โ€“5: AI Consensus Leader validator runs the prompt. Others verify independently. Majority agree on YES/NO boolean using strict_eq.
Step 6โ€“7: Finalized Answer committed to chain state. Frontend calls readContract (free) to display the result.

Live Demo

Try It: AI Oracle On-Chain

This is a real dApp โ€” not a mock. Questions go to a deployed YesNoOracle Intelligent Contract. AI validators reach consensus. Results live on the blockchain.

โš™๏ธ SETUP REQUIRED: Open this file, find CONTRACT_ADDRESS near the bottom, and paste your deployed contract address from GenLayer Studio. Then this demo goes live.
```
๐Ÿ”ฎ YesNoOracle โ€” GenLayer Simulator โ— SIMULATOR
Ask the Oracle anything
Results are stored on-chain permanently ยท powered by GenLayer Intelligent Contracts
โ—‹
idle
1
sending
2
consensus
3
finalized
โš  Error
Something went wrong.
Waiting for your first questionโ€ฆ
โ€”
Question asked
โ€”
โœ“ Finalized โ›“ On-chain tx: โ€”
On-chain history ยท this session
```
๐Ÿ’ก What's happening under the hood Each click calls writeContract() via genlayer-js. Multiple AI validators independently run gl.exec_prompt() with your question, then gl.eq_principle_strict_eq() ensures they agree on the same boolean before committing to chain state.

Learning Path

Where to Go Next

You've got the foundation. Here's the road ahead.

01 ๐ŸŒฑ

Beginner

Complete this tutorial. Deploy YesNoOracle. Understand Optimistic Democracy.

02 ๐Ÿ“œ

Contracts

Write contracts with web fetching. Use comparative equivalence. Add complex state.

03 ๐Ÿš€

Deploy

Deploy to Asimov Testnet. Connect MetaMask. Use the faucet for real testnet tokens.

04 ๐ŸŒ

Frontend

Build a full React app with genlayer-js. Handle wallet connections. Display live state.

05 ๐Ÿ—๏ธ

Build Your Own

Create something original. Submit to the Builder Portal. Join the GenLayer ecosystem.

Now You're Ready to Build ๐Ÿš€

You understand Optimistic Democracy, the Equivalence Principle, and you've deployed a real AI-powered smart contract. The rest is up to you.

๐Ÿ–ฅ Open Studio ๐Ÿ“š Read Docs ๐Ÿ† Builder Portal