Skip to content

coinbase/onchain-agent-demo-backend

Repository files navigation

Onchain Agent Backend Demo

Token-creation

A web app that enables onchain interactions through a conversational UI using AgentKit, a collaboration between CDP SDK and OnchainKit.

Overview

This project features a Python backend designed to work seamlessly with CDP's AgentKit backend. Together, they enable the creation of an AI agent capable of performing onchain operations on Base. The agent uses GPT-4 for natural language understanding and AgentKit for onchain interactions.

Modules

  • The agent module contains functions for interacting with the onchain agent.
    • initialize_agent creates (or loads) an agent from CDP Wallet Data.
    • run_agent invokes the agent.
    • handle_action_agent handles agent actions - in our demo, we just save the addresses of deployed NFTs and ERC-20s to a SQLite database, but you can customize this behavior for your application.
  • The agent.custom_actions module contains an example for adding custom actions to the agent.
    • get_latest_block is a custom action we've added that retrieves the latest Base Sepolia block information for the agent.
    • You can add additional custom actions to this module, following our example.

Key Features

  • AI-Powered Chat Interface: Interactive chat interface for natural language interactions onchain
  • Onchain Operations: Ability to perform various blockchain operations through Agentkit:
    • Deploy and interact with ERC-20 tokens
    • Create and manage NFTs
    • Check wallet balances
    • Request funds from faucet
  • Real-time Updates: Server-Sent Events (SSE) for streaming responses
  • Multi-language Support: Built-in language selector for internationalization
  • Responsive Design: Modern UI built with Tailwind CSS
  • Wallet Integration: Secure wallet management through CDP Agentkit

Tech Stack

  • Backend: Python with Flask
  • AI/ML: LangChain, GPT-4
  • Blockchain: Coinbase Developer Platform (CDP) Agentkit

Prerequisites

Environment Setup

Create a .env file with the following variables:

CDP_API_KEY_NAME= # Create an API key at https://portal.cdp.coinbase.com/projects/api-keys
CDP_API_KEY_PRIVATE_KEY="" # Create an API key at https://portal.cdp.coinbase.com/projects/api-keys
OPENAI_API_KEY= # Get an API key from OpenAI - https://platform.openai.com/docs/quickstart
NETWORK_ID=base-sepolia

Installation

  1. Install dependencies:
poetry install
  1. Start the development server:
poetry run python index.py

This will start the Python backend server.

Agent Wallet

Note: when running your agent from the first time, the SDK will automatically generate a wallet for you. The wallet information will be logged to the console, and saved to the wallets table in the SQLite DB.

Important: This is for demo purposes, and we do not recommend this approach for a production application. Please save your wallet information somewhere safe.

Agents will by default be re-instantiated through the SQLite DB.

You can also instantiate your agent from the following environment variables.

CDP_WALLET_ID="",
CDP_WALLET_SEED=""

API Usage

The application exposes a chat endpoint that accepts natural language commands for blockchain interactions:

curl -X POST http://localhost:5000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"input": "deploy a new ERC-20 token", "conversation_id": 0}'

Retrieve a list of NFTs deployed by the agent:

curl http://localhost:5000/nfts 

Retrieve a list of ERC-20s deployed by the agent:

curl http://localhost:5000/tokens

Deploying to Replit

Steps:

  • Sign up for a Replit account, or login to your existing one.
  • Navigate to the template links, and click Use Template on the top right hand side.
  • Under Secrets in Workspace Features, add the environment variables below.
    • Tip: You can click Edit as JSON and copy the values below in.
  • Click Deploy in the top right.
    • Tip: Deploy your backend first, as you'll need the deployment URL for the frontend's NEXT_PUBLIC_API_URL environment variable.

Backend

{
  "CDP_API_KEY_NAME": "get this from https://portal.cdp.coinbase.com/projects/api-keys",
  "CDP_API_KEY_PRIVATE_KEY": "get this from https://portal.cdp.coinbase.com/projects/api-keys",
  "OPENAI_API_KEY": "get this from https://platform.openai.com/api-keys",
  "NETWORK_ID": "base-sepolia"
}

Important: Replit resets the SQLite template on every deployment, before sending funds to your agent or using it on Mainnet be sure to read Agent Wallet and save your wallet ID and seed in a safe place.

Frontend

{
  "NEXT_PUBLIC_API_URL": "your backend deployment URL here"
}

License

See LICENSE.md for details.

Contributing

See CONTRIBUTING.md for guidelines on how to contribute to this project.

About

No description, website, or topics provided.

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages