mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-31 23:26:50 +00:00
feat: langchain
This commit is contained in:
83
src/agent/index.ts
Normal file
83
src/agent/index.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
||||
import bs58 from "bs58";
|
||||
import {
|
||||
request_faucet_funds,
|
||||
deploy_token,
|
||||
deploy_collection,
|
||||
get_balance,
|
||||
mintCollectionNFT,
|
||||
transfer,
|
||||
trade,
|
||||
registerDomain
|
||||
} from "../tools";
|
||||
import { CollectionOptions } from "../types";
|
||||
import { DEFAULT_OPTIONS } from "../constants";
|
||||
|
||||
/**
|
||||
* Main class for interacting with Solana blockchain
|
||||
* Provides a unified interface for token operations, NFT management, and trading
|
||||
*
|
||||
* @class SolanaAgentKit
|
||||
* @property {Connection} connection - Solana RPC connection
|
||||
* @property {Keypair} wallet - Wallet keypair for signing transactions
|
||||
* @property {PublicKey} wallet_address - Public key of the wallet
|
||||
*/
|
||||
export class SolanaAgentKit {
|
||||
public connection: Connection;
|
||||
public wallet: Keypair;
|
||||
public wallet_address: PublicKey;
|
||||
|
||||
constructor(
|
||||
private_key: string,
|
||||
rpc_url = "https://api.mainnet-beta.solana.com"
|
||||
) {
|
||||
this.connection = new Connection(rpc_url);
|
||||
this.wallet = Keypair.fromSecretKey(bs58.decode(private_key));
|
||||
this.wallet_address = this.wallet.publicKey;
|
||||
}
|
||||
|
||||
// Tool methods
|
||||
async requestFaucetFunds() {
|
||||
return request_faucet_funds(this);
|
||||
}
|
||||
|
||||
async deployToken(
|
||||
decimals: number = DEFAULT_OPTIONS.TOKEN_DECIMALS,
|
||||
initialSupply?: number
|
||||
) {
|
||||
return deploy_token(this, decimals, initialSupply);
|
||||
}
|
||||
|
||||
async deployCollection(options: CollectionOptions) {
|
||||
return deploy_collection(this, options);
|
||||
}
|
||||
|
||||
async getBalance(token_address?: PublicKey) {
|
||||
return get_balance(this, token_address);
|
||||
}
|
||||
|
||||
async mintNFT(
|
||||
collectionMint: PublicKey,
|
||||
metadata: Parameters<typeof mintCollectionNFT>[2],
|
||||
recipient?: PublicKey
|
||||
) {
|
||||
return mintCollectionNFT(this, collectionMint, metadata, recipient);
|
||||
}
|
||||
|
||||
async transfer(to: PublicKey, amount: number, mint?: PublicKey) {
|
||||
return transfer(this, to, amount, mint);
|
||||
}
|
||||
|
||||
async registerDomain(name: string, spaceKB?: number) {
|
||||
return registerDomain(this, name, spaceKB);
|
||||
}
|
||||
|
||||
async trade(
|
||||
outputMint: PublicKey,
|
||||
inputAmount: number,
|
||||
inputMint?: PublicKey,
|
||||
slippageBps: number = DEFAULT_OPTIONS.SLIPPAGE_BPS
|
||||
) {
|
||||
return trade(this, outputMint, inputAmount, inputMint, slippageBps);
|
||||
}
|
||||
}
|
||||
81
src/index.ts
81
src/index.ts
@@ -1,78 +1,7 @@
|
||||
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
|
||||
import bs58 from "bs58";
|
||||
import {
|
||||
request_faucet_funds,
|
||||
deploy_token,
|
||||
deploy_collection,
|
||||
get_balance,
|
||||
mintCollectionNFT,
|
||||
transfer,
|
||||
trade,
|
||||
} from "./tools";
|
||||
import { CollectionOptions } from "./types";
|
||||
import { DEFAULT_OPTIONS } from "./constants";
|
||||
import { SolanaAgentKit } from './agent'; // Move the SolanaAgentKit class to src/agent.ts
|
||||
import { createSolanaTools } from './langchain';
|
||||
|
||||
/**
|
||||
* Main class for interacting with Solana blockchain
|
||||
* Provides a unified interface for token operations, NFT management, and trading
|
||||
*
|
||||
* @class SolanaAgentKit
|
||||
* @property {Connection} connection - Solana RPC connection
|
||||
* @property {Keypair} wallet - Wallet keypair for signing transactions
|
||||
* @property {PublicKey} wallet_address - Public key of the wallet
|
||||
*/
|
||||
export class SolanaAgentKit {
|
||||
public connection: Connection;
|
||||
public wallet: Keypair;
|
||||
public wallet_address: PublicKey;
|
||||
export { SolanaAgentKit, createSolanaTools };
|
||||
|
||||
constructor(
|
||||
private_key: string,
|
||||
rpc_url = "https://api.mainnet-beta.solana.com"
|
||||
) {
|
||||
this.connection = new Connection(rpc_url);
|
||||
this.wallet = Keypair.fromSecretKey(bs58.decode(private_key));
|
||||
this.wallet_address = this.wallet.publicKey;
|
||||
}
|
||||
|
||||
// Tool methods
|
||||
async requestFaucetFunds() {
|
||||
return request_faucet_funds(this);
|
||||
}
|
||||
|
||||
async deployToken(
|
||||
decimals: number = DEFAULT_OPTIONS.TOKEN_DECIMALS,
|
||||
initialSupply?: number
|
||||
) {
|
||||
return deploy_token(this, decimals, initialSupply);
|
||||
}
|
||||
|
||||
async deployCollection(options: CollectionOptions) {
|
||||
return deploy_collection(this, options);
|
||||
}
|
||||
|
||||
async getBalance(token_address?: PublicKey) {
|
||||
return get_balance(this, token_address);
|
||||
}
|
||||
|
||||
async mintNFT(
|
||||
collectionMint: PublicKey,
|
||||
metadata: Parameters<typeof mintCollectionNFT>[2],
|
||||
recipient?: PublicKey
|
||||
) {
|
||||
return mintCollectionNFT(this, collectionMint, metadata, recipient);
|
||||
}
|
||||
|
||||
async transfer(to: PublicKey, amount: number, mint?: PublicKey) {
|
||||
return transfer(this, to, amount, mint);
|
||||
}
|
||||
|
||||
async trade(
|
||||
outputMint: PublicKey,
|
||||
inputAmount: number,
|
||||
inputMint?: PublicKey,
|
||||
slippageBps: number = DEFAULT_OPTIONS.SLIPPAGE_BPS
|
||||
) {
|
||||
return trade(this, outputMint, inputAmount, inputMint, slippageBps);
|
||||
}
|
||||
}
|
||||
// Optional: Export types that users might need
|
||||
export * from './types';
|
||||
181
src/langchain/index.ts
Normal file
181
src/langchain/index.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
import { Tool } from "langchain/tools";
|
||||
import { SolanaAgentKit } from "../index";
|
||||
import { PublicKey } from "@solana/web3.js";
|
||||
|
||||
export class SolanaBalanceTool extends Tool {
|
||||
name = "solana_balance";
|
||||
description = "Get the balance of a Solana wallet or token account. Input can be a token address or empty for SOL balance.";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const tokenAddress = input ? new PublicKey(input) : undefined;
|
||||
const balance = await this.solanaKit.getBalance(tokenAddress);
|
||||
return `Balance: ${balance}`;
|
||||
} catch (error: any) {
|
||||
return `Error getting balance: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaTransferTool extends Tool {
|
||||
name = "solana_transfer";
|
||||
description = "Transfer tokens or SOL to another address. Input should be JSON string with: {to: string, amount: number, mint?: string}";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const { to, amount, mint } = JSON.parse(input);
|
||||
const recipient = new PublicKey(to);
|
||||
const mintAddress = mint ? new PublicKey(mint) : undefined;
|
||||
|
||||
await this.solanaKit.transfer(recipient, amount, mintAddress);
|
||||
return `Successfully transferred ${amount} to ${to}`;
|
||||
} catch (error: any) {
|
||||
return `Error making transfer: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaDeployTokenTool extends Tool {
|
||||
name = "solana_deploy_token";
|
||||
description = "Deploy a new SPL token. Input should be JSON string with: {decimals?: number, initialSupply?: number}";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const { decimals = 9, initialSupply } = JSON.parse(input);
|
||||
const result = await this.solanaKit.deployToken(decimals, initialSupply);
|
||||
return `Token deployed successfully. Mint address: ${result.mint.toString()}`;
|
||||
} catch (error: any) {
|
||||
return `Error deploying token: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaDeployCollectionTool extends Tool {
|
||||
name = "solana_deploy_collection";
|
||||
description = "Deploy a new NFT collection. Input should be JSON with: {name: string, uri: string, royaltyBasisPoints?: number, creators?: Array<{address: string, percentage: number}>}";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const options = JSON.parse(input);
|
||||
const result = await this.solanaKit.deployCollection(options);
|
||||
return `Collection deployed successfully. Address: ${result.collectionAddress.toString()}`;
|
||||
} catch (error: any) {
|
||||
return `Error deploying collection: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaMintNFTTool extends Tool {
|
||||
name = "solana_mint_nft";
|
||||
description = "Mint a new NFT in a collection. Input should be JSON with: {collectionMint: string, metadata: {name: string, symbol: string, uri: string}, recipient?: string}";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const { collectionMint, metadata, recipient } = JSON.parse(input);
|
||||
const recipientPubkey = recipient ? new PublicKey(recipient) : undefined;
|
||||
const result = await this.solanaKit.mintNFT(
|
||||
new PublicKey(collectionMint),
|
||||
metadata,
|
||||
recipientPubkey
|
||||
);
|
||||
return `NFT minted successfully. Mint address: ${result.mint.toString()}`;
|
||||
} catch (error: any) {
|
||||
return `Error minting NFT: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaTradeTool extends Tool {
|
||||
name = "solana_trade";
|
||||
description = "Swap tokens using Jupiter Exchange. Input should be JSON with: {outputMint: string, inputAmount: number, inputMint?: string, slippageBps?: number}";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const { outputMint, inputAmount, inputMint, slippageBps } = JSON.parse(input);
|
||||
const tx = await this.solanaKit.trade(
|
||||
new PublicKey(outputMint),
|
||||
inputAmount,
|
||||
inputMint ? new PublicKey(inputMint) : undefined,
|
||||
slippageBps
|
||||
);
|
||||
return `Trade executed successfully. Transaction: ${tx}`;
|
||||
} catch (error: any) {
|
||||
return `Error executing trade: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaRequestFundsTool extends Tool {
|
||||
name = "solana_request_funds";
|
||||
description = "Request SOL from Solana faucet (devnet/testnet only)";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(_input: string): Promise<string> {
|
||||
try {
|
||||
await this.solanaKit.requestFaucetFunds();
|
||||
return "Successfully requested faucet funds";
|
||||
} catch (error: any) {
|
||||
return `Error requesting funds: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class SolanaRegisterDomainTool extends Tool {
|
||||
name = "solana_register_domain";
|
||||
description = "Register a .sol domain name. Input should be JSON with: {name: string, spaceKB?: number}";
|
||||
|
||||
constructor(private solanaKit: SolanaAgentKit) {
|
||||
super();
|
||||
}
|
||||
|
||||
async _call(input: string): Promise<string> {
|
||||
try {
|
||||
const { name, spaceKB = 1 } = JSON.parse(input);
|
||||
const tx = await this.solanaKit.registerDomain(name, spaceKB);
|
||||
return `Domain registered successfully. Transaction: ${tx}`;
|
||||
} catch (error: any) {
|
||||
return `Error registering domain: ${error.message}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Updated createSolanaTools function
|
||||
export function createSolanaTools(solanaKit: SolanaAgentKit) {
|
||||
return [
|
||||
new SolanaBalanceTool(solanaKit),
|
||||
new SolanaTransferTool(solanaKit),
|
||||
new SolanaDeployTokenTool(solanaKit),
|
||||
new SolanaDeployCollectionTool(solanaKit),
|
||||
new SolanaMintNFTTool(solanaKit),
|
||||
new SolanaTradeTool(solanaKit),
|
||||
new SolanaRequestFundsTool(solanaKit),
|
||||
new SolanaRegisterDomainTool(solanaKit),
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user