Skip to content

AI Agent (daccAiAgent)

A function to create an AI Agent for blockchain interactions

The daccAiAgent function creates an AI-powered agent that can chat and perform blockchain actions on behalf of the user. The AI can execute transactions, check balances, and interact with smart contracts via natural language.

Import

import { daccAiAgent } from 'dacc-js';

Usage

import { daccAiAgent } from 'dacc-js';
import { optimismSepolia, baseSepolia } from 'viem/chains';
 
const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  // privateKey: '0xabc123...', // Optional
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia, baseSepolia],
  tokens: [
    {
      name: 'Token1',
      symbol: 'TOK1',
      chain: optimismSepolia,
      address: '0x123...',
    },
    {
      name: 'Token2',
      symbol: 'TOK2',
      chain: baseSepolia,
      address: '0x234...',
    },
  ],
});
 
// Chat with AI to perform transactions
const response = await ai.chat("Transfer 0.01 TOK1 to 0xRecipient...");
console.log('AI response:', response.text);
 
// Stream chat for real-time responses
const streamResponse = await ai.streamChat("What's my balance of Token2?");
for await (const chunk of streamResponse.textStream) {
  process.stdout.write(chunk);
}

Arguments

ParameterTypeDescription
daccPublickeystringThe encrypted wallet data from createDaccWallet.
passwordSecretkeystringThe password for decrypting the wallet.
privateKey0x${string}Optional: Direct private key (alternative to daccPublickey + password).
llmLlmConfigLLM configuration with baseURL, apiKey, and model.
chainsChain[]Array of supported chains (e.g., [optimismSepolia, baseSepolia]).
tokensTokenInfo[]Array of tokens with name, symbol, chain, and address.
systemPromptstringOptional: Custom system prompt (defaults to built-in prompt).

Return Value

The response result is an object containing {chat, streamChat} methods.

MethodParametersReturn TypeDescription
chatmessage: stringPromise<{text: string}>Chat with AI to perform blockchain actions.
streamChatmessage: stringPromise<{textStream: AsyncIterable<string>}>Stream chat with AI for real-time responses.

Parameters

daccPublickey

  • Type: string

The user's encrypted public key from createDaccWallet.

const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..', 
  passwordSecretkey: 'my+Password#123..', 
  // privateKey: '0xabc123...', //
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia],
  tokens: [{ name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' }],
});

passwordSecretkey

  • Type: string

The same password used when creating the wallet with createDaccWallet.

const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..', 
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia],
  tokens: [{ name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' }],
});

privateKey (optional)

  • Type: 0x${string}

Direct private key for wallet access.

const ai = await daccAiAgent({
  // daccPublickey: 'daccPublickey_0x123_XxX..', //
  // passwordSecretkey: 'my+Password#123..', //
  privateKey: '0xabc123...', 
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia],
  tokens: [{ name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' }],
});

llm

  • Type: { baseURL: string; apiKey: string; model: string }

LLM (Large Language Model) configuration for the AI provider.

const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  llm: {
    baseURL: 'https://openrouter.ai/api/v1', 
    apiKey: process.env.OPENROUTER_API_KEY!, 
    model: 'x-ai/grok-4.1-fast', 
  },
  chains: [optimismSepolia],
  tokens: [{ name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' }],
});

chains

  • Type: Chain[]

Array of supported blockchain networks from viem/chains.

import { optimismSepolia, baseSepolia } from 'viem/chains';
 
const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia, baseSepolia], 
  tokens: [
    { name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' },
    { name: 'Token2', symbol: 'TOK2', chain: baseSepolia, address: '0x234...' },
  ],
});

tokens

  • Type: TokenInfo[]

Array of token configurations with name, symbol, chain, and address.

const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia, baseSepolia],
  tokens: [ 
    { name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' }, 
    { name: 'Token2', symbol: 'TOK2', chain: baseSepolia, address: '0x234...' }, 
  ], 
});

systemPrompt (optional)

  • Type: string
  • Default: Built-in system prompt

Custom system prompt for the AI agent.

const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia],
  tokens: [{ name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' }],
  systemPrompt: 'You are a helpful blockchain assistant. Always confirm transactions before executing.', 
});

Examples

Chat with AI to send tokens

import { daccAiAgent } from 'dacc-js';
import { optimismSepolia } from 'viem/chains';
 
const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia],
  tokens: [
    { name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' },
  ],
});
 
// Ask AI to transfer tokens
const response = await ai.chat('Transfer 0.01 TOK1 to 0xRecipientAddress...');
console.log('AI response:', response.text);

Stream chat for balance inquiry

import { daccAiAgent } from 'dacc-js';
import { baseSepolia } from 'viem/chains';
 
const ai = await daccAiAgent({
  daccPublickey: 'daccPublickey_0x123_XxX..',
  passwordSecretkey: 'my+Password#123..',
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [baseSepolia],
  tokens: [
    { name: 'Token2', symbol: 'TOK2', chain: baseSepolia, address: '0x234...' },
  ],
});
 
// Stream chat for real-time balance inquiry
const streamResponse = await ai.streamChat("What's my balance of Token2?");
for await (const chunk of streamResponse.textStream) {
  process.stdout.write(chunk);
}

Use AI with direct private key

import { daccAiAgent } from 'dacc-js';
import { optimismSepolia } from 'viem/chains';
 
const ai = await daccAiAgent({
  privateKey: '0xabc123...',
  llm: {
    baseURL: 'https://openrouter.ai/api/v1',
    apiKey: process.env.OPENROUTER_API_KEY!,
    model: 'x-ai/grok-4.1-fast',
  },
  chains: [optimismSepolia],
  tokens: [
    { name: 'Token1', symbol: 'TOK1', chain: optimismSepolia, address: '0x123...' },
  ],
});
 
// Ask AI about available tokens
const tokensResponse = await ai.chat('What tokens do I have available?');
console.log('Available tokens:', tokensResponse.text);