Getting Started with GenLayer
Welcome to the GenLayer getting started guide. This guide will help you set up the GenLayer environment, write your first Intelligent Contract, deploy it, and interact with it using the GenLayer Simulator. You'll also learn how to use the genlayer-project-boilerplate (opens in a new tab) to write end-to-end tests and build a frontend dApp with GenLayerJS.
Table of Contents
- Installation of the GenLayer CLI
- Launching the GenLayer Simulator
- Writing Intelligent Contracts
- Deploying and Interacting with Intelligent Contracts
- Writing End-to-End Tests with genlayer-project-boilerplate
- Building a Frontend dApp with GenLayerJS
Installation of the GenLayer CLI
The GenLayer CLI is used to set up the GenLayer Simulator and, in the future, mainnet and testnet environments.
Prerequisites
Ensure you have the following installed and updated:
Installation Steps
-
Install GenLayer CLI
Open your terminal and run:
npm install -g genlayer
-
Initialize GenLayer Environment
Run the following command to set up your development environment:
genlayer init
During initialization, you'll be prompted to select your preferred LLM provider(s) and enter any required API keys.
Example:
genlayer init
Optional Initialization Parameters
You can customize the initialization with the following options:
--numValidators <number>
: Specify the number of validators (default is 5).--branch <branch>
: Specify the branch of the GenLayer Simulator to use (default is "main").
Example:
genlayer init --numValidators 3 --branch staging
Launching the GenLayer Simulator
After initializing the environment, you can start the simulator by running:
genlayer up
This command launches the GenLayer Simulator using your existing configuration.
Optional Parameters:
--reset-validators
: Removes all current validators and creates new ones.--numValidators <number>
: Specify the number of validators to start.--branch <branch>
: Specify the branch to start the GenLayer Simulator from.
Example:
genlayer up --reset-validators --numValidators 3 --branch staging
Once the simulator is running, access it at http://localhost:8080/ (opens in a new tab).
Writing Intelligent Contracts
Intelligent Contracts in GenLayer are written in Python and extend the IContract
base class. They can process natural language inputs and interact with web data.
Creating a Simple Intelligent Contract
- Define Your Contract Class
from genlayer import IContract
class SimpleContract(IContract):
def __init__(self, initial_value):
self.value = initial_value
- Add Contract Methods
Add methods to interact with the contract:
def get_value(self):
return self.value
def set_value(self, new_value):
self.value = new_value
- Using LLMs and the Equivalence Principle
If you need to process natural language or web data:
def process_input(self, user_input):
prompt = f"""
...
User input: {user_input}
...
"""
result = await call_llm_with_principle(
prompt,
eq_principle="Example principle",
)
Deploying and Interacting with Intelligent Contracts
Deploying the Contract
-
Load Your Contract
In the GenLayer Simulator, navigate to the Contracts section and upload your contract file. -
Set Constructor Parameters
The simulator will automatically detect constructor parameters. Provide the required values. -
Deploy the Contract
Navigate to the Run and Deploy section and click the "Deploy" button. Upon successful deployment, the contract address will be displayed.
Interacting with the Contract
-
Read Methods
View the current state of the contract using Read Methods. These are methods that return data without modifying the state. -
Write Methods
Execute Write Methods to interact with your contract. Provide any required input parameters. -
Transaction Logs
Monitor transactions and validator consensus via the Node Logs at the bottom of the simulator.
Writing End-to-End Tests with genlayer-project-boilerplate
The genlayer-project-boilerplate (opens in a new tab) provides a starting point for writing end-to-end (e2e) tests for your Intelligent Contracts.
Setting Up the Boilerplate
- Clone the Repository
git clone https://github.com/yeagerai/genlayer-project-boilerplate.git
- Install Dependencies
Navigate to the project directory and install the required packages:
cd genlayer-project-boilerplate
npm install
- Configure Environment
Ensure the GenLayer Simulator is running. Update the configuration files if necessary.
Writing E2E Tests
-
Create Test Files
In thetest
directory, create test files using your pytest as a testing framework. -
Example Test
import pytest
from genlayer import GenLayer, Account
def test_simple_contract():
# Account
account_1 = create_new_account()
# Validators
result = post_request_localhost(
payload("sim_createRandomValidators", 5, 8, 12, ["openai"], ["gpt-4o"])
).json()
# Deploy contract
contract_code = open("contracts/my_contract.py", "r").read()
contract_address, transaction_response_deploy = deploy_intelligent_contract(
account_1,
contract_code,
"{value = 10}",
)
# Test initial state
initial_value = call_contract_method(
contract_address, account_1, "get_value", []
)
assert initial_value == 10
# Test state change
send_transaction_response = send_transaction(
account_1,
contract_address,
"set_value",
["20"],
)
# Test updated state
updated_value = call_contract_method(
contract_address, account_1, "get_value", []
)
assert updated_value == 20
Note on Deployment: For deployment, developers should currently use the GenLayer Simulator UI. Deploying through the CLI is not yet supported.
Building a Frontend dApp with GenLayerJS
You can build a frontend decentralized application (dApp) that interacts with your Intelligent Contract using GenLayerJS, a JavaScript SDK for GenLayer.
Installing GenLayerJS
npm install genlayer-js
Connecting to GenLayer
import { createClient } from 'genlayer-js';
const client = createClient(config);
Interacting with Contracts
- Read from a Contract
const value = await client.readContract({
address: contractAddress,
functionName: "get_value",
args: [],
});
- Update Contract State
const txHash = await client.writeContract({
address: contractAddress,
functionName: "set_value",
args: [newValue],
});
const receipt = await client.waitForTransactionReceipt({
hash: txHash,
status: "FINALIZED",
});
Running the Frontend
Ensure your frontend application is configured to connect to the GenLayer Simulator's RPC endpoint (http://localhost:4000/api (opens in a new tab)).
Start your frontend application:
npm run dev
Conclusion
You've now set up the GenLayer environment, written and deployed an Intelligent Contract, and interacted with it through both the simulator and a frontend dApp using GenLayerJS. Explore further by customizing your contracts, writing more comprehensive tests, and enhancing your dApp's functionality.