Skip to content

decentralized ai agent running on phala network that queries from the graph's api

Notifications You must be signed in to change notification settings

DefiDash/ai-agent-contract

Repository files navigation

DefiDash AI Agent Smart Contract

The backend AI Smart Contract built in typescript that is running on Phala Network and interacting with The Graph's API using Langchain for controlling the actions with Anthropic Claude

What does this code do?

  1. Exposes a public API interface for the frontend to interact with and hosted on Phala Network
    • API receives two values (key: string and chatQuery: string)
      • key is generated by setting secrets for a published agent
      • chatQuery is appended by the user everytime they make a request
  2. Uses The Graph to interact with Uniswap API
  3. Uses Claude in the background to create AI Agents
  4. Has two simplistic AI Agent that decides the course of action
    • one to decide the most appropriate API to call to get the data
    • one to decide the most appropriate visualization to display in the frontend

Why we use Phala Network?

Phala Network provides DeCC (Decentralized Cloud Computing) which allows users to upload their typescript project to be hosted and run on blockchain nodes as opposed to centralized providers like DigitalOcean, AWS and GCP. The nature of decentralized AI and high privacy suits the needs of typical web3 users, and is more comfortable for typical web3 users as well.

Why we use The Graph?

The Graph provides an indexing and querying service for Dapps' smart contract which effectively allows anybody to query the data exposed by the smart contract easily and efficiently without having to manually sort through the gigabytes of infomration on the blockchain. This efficiency is one of the key factors that we chose the Graph.

Getting Started

1. Get API Keys.

In order to start using the Graph's subgraphs, you have to get access to their API key:

  • Visit this website https://thegraph.com/studio/
  • Connect your wallet
  • Navigate into the Graph Studio, and also navigate into the API Keys tab
  • CLick the "Create API Keys" button, and enter the necessary details.
  • You can now access your API key.
2. Use the acquired keys as part of your API URL like the following:

Why Use the Uniswap V3 Subgraph?

As the dApp is geared towards showing DeFi-related data, the Uniswap subgraph would be the best choice because it provides comprehensive data on liquidity pools, swaps, token prices, and transaction volumes within the Uniswap V3 protocol, allowing users to track key metrics for various assets.

Querying the Subgraph

The dApp uses fetch for fetching the subgraph data. The format is as the following: ''' const requestObject = await fetch( url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query }), } ) const response = await requestObject.json(); '''

Running and Deployment

Environment Setup

You can always get started by cloning the repo

git clone git@github.com:DefiDash/ai-agent-contract.git

Before proceeding, you might want to setup the environment variabls

.env

THIRDWEB_API_KEY="XXXX"

secrets/main.json

{
    "claudeApiKey":"XXXX"
}  

Testing

npm run build # build the appplication
npm run test # test the application

Deploying

npm run publish-agent # publish the agent
npm run set-secrets # set the secrets (use the API here from the frontend)

About

decentralized ai agent running on phala network that queries from the graph's api

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published