A Smart Contract Generator for Web3 Projects using Lyzr Automata
Published: 2024-04-26 11:55:56

Welcome to the future of smart contract development! In this blog post, we're excited to introduce you to our latest tool: the Smart Contract Generator powered by Lyzr Automata. Whether you're a seasoned blockchain developer or just stepping into the world of Web3 projects, our Smart Contract Generator is designed to streamline the process of creating robust and efficient smart contracts.

At its core, the Smart Contract Generator leverages the cutting-edge capabilities of Lyzr Automata to generate smart contracts tailored to your project's requirements. With just a few simple inputs, you can quickly generate high-quality smart contracts written in popular languages such as Solidity, Vyper, Ethereum, Rust, and Move.

**Setting Up the Environment**
**Imports:**

```
pip install lyzr_automata streamlit
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
from dotenv import load_dotenv
import os
from prompt import example

load_dotenv()
api = os.getenv("OPENAI_API_KEY")
```
Imports necessary libraries like streamlit, dotenv, PIL, and custom libraries for Lyzr Automata.
Loads environment variables using load_dotenv (likely containing the OpenAI API key).
load_dotenv() is called to load environment variables from a .env file (likely containing your OpenAI API key).
api = os.getenv("OPENAI_API_KEY") retrieves the API key from the environment variable.

**OpenAI Model Setup:**

```
open_ai_text_completion_model = OpenAIModel(
    api_key=api,
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.2,
        "max_tokens": 1500,
    },
)
```
The OpenAIModel class from lyzr_automata is used to create an instance of the OpenAI text completion model (open_ai_text_completion_model).
It sets the API key, model name ("gpt-4-turbo-preview"), temperature (0.2), and maximum output length (1500 tokens).

**User Input:**

```
contract_det = st.sidebar.text_area("Enter Your Contract Details", height=200)
contract_language = st.sidebar.selectbox("Enter Contract Language", ("Solidity", "Vyper", "Ethereum", "Rust", "Move"))
```
In the sidebar, a text area (contract_det) is created using st.sidebar.text_area for users to enter contract details.
A selectbox (contract_language) is created using st.sidebar.selectbox to allow users to choose the desired contract language (Solidity, Vyper, Ethereum, Rust, Move).

**Smart Contract Generation Function (smart_contract):**

```
def smart_contract(details,language):

    web3_agent = Agent(
            role="Blockchain expert",
            prompt_persona=f"Imagine you are an experienced Blockchain developer.Your Task is to generate Smart contract with given details."
        )

    prompt = f"""I am Building A project in {language}.The Description Of the project is below:
    description: {details}
    
    Your Task is to provide a code with all the requests above and no bugs.
    In your response provide all the code with notes and explanation of each function.
    
    Output:
    Smart Contract with code explanation
    [!Important] Do not Write code walk through or any thing else apart from the above
    """

    web3_task = Task(
        name="smart contract task",
        model=open_ai_text_completion_model,
        agent=web3_agent,
        instructions=prompt,
    )

    output = LinearSyncPipeline(
        name="Smart Contract Pipline",
        completion_message="Smart Contract Generated!!",
        tasks=[
              web3_task
        ],
    ).run()

    answer = output[0]['task_output']

    return answer

```
This function takes details (contract description) and language (chosen language) as input.
It creates a web3_agent object representing a "Blockchain expert" using the Agent class from lyzr_automata. The prompt persona defines the agent's role and task.
It constructs a prompt string that incorporates the user-provided details, language, and desired outputs. The prompt emphasizes generating code with explanations and avoiding walkthroughs.
A web3_task object is created using the Task class, specifying the name, model, agent, and instructions (the prompt).
A LinearSyncPipeline object is created, defining the pipeline name, completion message, and a list containing the web3_task.
The run method of the pipeline executes the task and retrieves the model's output.
The function returns the retrieved task output, which is the generated smart contract code.

**Output:**

```
if st.sidebar.button("Generate", type="primary"):
    solution = smart_contract(contract_det,contract_language)
    st.markdown(solution)
```
A button labeled "Generate" is created using st.sidebar.button. Clicking this button triggers the following steps:
It calls the smart_contract function with the entered details and chosen language.
The function returns the generated smart contract code (solution).
The code is displayed in the main content area using st.markdown.

try it now: [https://lyzr-smart-contract.streamlit.app/
](https://lyzr-smart-contract.streamlit.app/
)For more information explore the website: [Lyzr](https://www.lyzr.ai/)