🚀Quick Start - Langchain

Transfer tokens on Solana with the help of Langchain and Vity Toolkit in less than 30 lines

In this guide you'll learn how to implement and use Vity Toolkit with Langchain in just a few lines of code. For the purpose of this example, we'll create an assistant that can transfer 0.01 USDC to another wallet on the Solana mainnet-beta.

Transfer tokens to another wallet on Solana

In this example, we will use LangChain Agent to transfer 0.01 USDC to another wallet on the Solana mainnet-beta using Vity Toolkit.

1

Install the package.

npm install vity-toolkit langchain @langchain/openai
2

Initialize Vity Toolkit and Langchain.

import { ChatOpenAI } from "@langchain/openai";
import { Action, LangchainToolkit } from "vity-toolkit";
import { createOpenAIFunctionsAgent, AgentExecutor } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";

const llm = new ChatOpenAI({ model: "gpt-4o" });
const toolKit = new LangchainToolkit({
    userPrivateKey: "<private_key>",
});
3

Get the required tools.

const tools = toolKit.getTools({ actions: [Action.SOLANA_WALLET_TRANSFER, Action.SOLANA_WALLET_GET_BALANCE, Action.SOLANA_WALLET_GET_MY_PUBLIC_KEY] });

If you don’t know which tool is exactly needed for your task, you can directly pass the App.SOLANA_WALLET app as a tool.

const tools = toolKit.getTools({ apps: [App.SOLANA_WALLET] });
4

Define the Agent.

const prompt = ChatPromptTemplate.fromMessages([
  ["system", "You are a helpful assistant"],
  ["placeholder", "{chat_history}"],
  ["human", "{input}"],
  ["placeholder", "{agent_scratchpad}"],
]);

const agent = await createOpenAIFunctionsAgent({
    llm,
    tools,
    prompt,
});

const agentExecutor = new AgentExecutor({ agent, tools, verbose: true });
5

Execute the Agent.

const response = await agentExecutor.invoke({ input: "Transfer 0.001 USDC to 8ueAQRiLExT7M9CNSkR11FDGbx2UyJqpSxCp1y43PWWs in mainnet" });
console.log(response);
Full code

Last updated