Getting started

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

  1. Installation of the GenLayer CLI
  2. Launching the GenLayer Simulator
  3. Writing Intelligent Contracts
  4. Deploying and Interacting with Intelligent Contracts
  5. Writing End-to-End Tests with genlayer-project-boilerplate
  6. 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

  1. Install GenLayer CLI

    Open your terminal and run:

    npm install -g genlayer
  2. 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

  1. Define Your Contract Class
  from genlayer import IContract
 
  class SimpleContract(IContract):
      def __init__(self, initial_value):
          self.value = initial_value
  1. 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
  1. 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

  1. Load Your Contract
    In the GenLayer Simulator, navigate to the Contracts section and upload your contract file.

  2. Set Constructor Parameters
    The simulator will automatically detect constructor parameters. Provide the required values.

  3. 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

  1. Read Methods
    View the current state of the contract using Read Methods. These are methods that return data without modifying the state.

  2. Write Methods
    Execute Write Methods to interact with your contract. Provide any required input parameters.

  3. 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

  1. Clone the Repository
  git clone https://github.com/yeagerai/genlayer-project-boilerplate.git
  1. Install Dependencies
    Navigate to the project directory and install the required packages:
  cd genlayer-project-boilerplate
  npm install
  1. Configure Environment
    Ensure the GenLayer Simulator is running. Update the configuration files if necessary.

Writing E2E Tests

  1. Create Test Files
    In the test directory, create test files using your pytest as a testing framework.

  2. 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

  1. Read from a Contract
  const value = await client.readContract({
      address: contractAddress,
      functionName: "get_value",
      args: [],
  });
  1. 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.