mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-06-07 15:11:47 +00:00
fix: drift user account info fetching
This commit is contained in:
@@ -254,7 +254,7 @@ export async function withdrawFromDriftUserAccount(
|
|||||||
|
|
||||||
const tx = new Transaction().add(...withdrawInstruction).add(
|
const tx = new Transaction().add(...withdrawInstruction).add(
|
||||||
ComputeBudgetProgram.setComputeUnitPrice({
|
ComputeBudgetProgram.setComputeUnitPrice({
|
||||||
microLamports: 0.000001 * 1000000 * 1000000,
|
microLamports: 0.000003 * 1000000 * 1000000,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
tx.recentBlockhash = latestBlockhash.blockhash;
|
tx.recentBlockhash = latestBlockhash.blockhash;
|
||||||
@@ -388,9 +388,11 @@ export async function doesUserHaveDriftAccount(agent: SolanaAgentKit) {
|
|||||||
agent.wallet.publicKey,
|
agent.wallet.publicKey,
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
await user.subscribe();
|
||||||
user.getActivePerpPositions();
|
user.getActivePerpPositions();
|
||||||
const userAccountExists = await user.exists();
|
const userAccountExists = await user.exists();
|
||||||
await cleanUp();
|
await cleanUp();
|
||||||
|
await user.unsubscribe();
|
||||||
return {
|
return {
|
||||||
hasAccount: userAccountExists,
|
hasAccount: userAccountExists,
|
||||||
account: user.userAccountPublicKey,
|
account: user.userAccountPublicKey,
|
||||||
|
|||||||
@@ -8,191 +8,191 @@ import { createOpenAI } from "@ai-sdk/openai";
|
|||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
function validateEnvironment(): void {
|
function validateEnvironment(): void {
|
||||||
const missingVars: string[] = [];
|
const missingVars: string[] = [];
|
||||||
const requiredVars = ["OPENAI_API_KEY", "RPC_URL", "SOLANA_PRIVATE_KEY"];
|
const requiredVars = ["OPENAI_API_KEY", "RPC_URL", "SOLANA_PRIVATE_KEY"];
|
||||||
|
|
||||||
requiredVars.forEach((varName) => {
|
requiredVars.forEach((varName) => {
|
||||||
if (!process.env[varName]) {
|
if (!process.env[varName]) {
|
||||||
missingVars.push(varName);
|
missingVars.push(varName);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (missingVars.length > 0) {
|
if (missingVars.length > 0) {
|
||||||
console.error("Error: Required environment variables are not set");
|
console.error("Error: Required environment variables are not set");
|
||||||
missingVars.forEach((varName) => {
|
missingVars.forEach((varName) => {
|
||||||
console.error(`${varName}=your_${varName.toLowerCase()}_here`);
|
console.error(`${varName}=your_${varName.toLowerCase()}_here`);
|
||||||
});
|
});
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
validateEnvironment();
|
validateEnvironment();
|
||||||
|
|
||||||
async function runAutonomousMode(interval = 10) {
|
async function runAutonomousMode(interval = 10) {
|
||||||
console.log("Starting autonomous mode...");
|
console.log("Starting autonomous mode...");
|
||||||
const openai = createOpenAI({
|
const openai = createOpenAI({
|
||||||
apiKey: process.env.OPENAI_API_KEY as string,
|
apiKey: process.env.OPENAI_API_KEY as string,
|
||||||
});
|
});
|
||||||
|
|
||||||
const solanaAgent = new SolanaAgentKit(
|
const solanaAgent = new SolanaAgentKit(
|
||||||
process.env.SOLANA_PRIVATE_KEY!,
|
process.env.SOLANA_PRIVATE_KEY!,
|
||||||
process.env.RPC_URL!,
|
process.env.RPC_URL!,
|
||||||
{
|
{
|
||||||
OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
|
OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const tools = createVercelAITools(solanaAgent);
|
const tools = createVercelAITools(solanaAgent);
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
const thought =
|
const thought =
|
||||||
"Be creative and do something interesting on the blockchain. " +
|
"Be creative and do something interesting on the blockchain. " +
|
||||||
"Choose an action or set of actions and execute it that highlights your abilities.";
|
"Choose an action or set of actions and execute it that highlights your abilities.";
|
||||||
|
|
||||||
const response = streamText({
|
const response = streamText({
|
||||||
prompt: thought,
|
prompt: thought,
|
||||||
tools,
|
tools,
|
||||||
model: openai("gpt-4o-mini"),
|
model: openai("gpt-4o-mini"),
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
system: `You are a helpful agent that can interact onchain using the Solana Agent Kit. You are
|
system: `You are a helpful agent that can interact onchain using the Solana Agent Kit. You are
|
||||||
empowered to interact onchain using your tools. If you ever need funds, you can request them from the
|
empowered to interact onchain using your tools. If you ever need funds, you can request them from the
|
||||||
faucet. If not, you can provide your wallet details and request funds from the user. If there is a 5XX
|
faucet. If not, you can provide your wallet details and request funds from the user. If there is a 5XX
|
||||||
(internal) HTTP error code, ask the user to try again later. If someone asks you to do something you
|
(internal) HTTP error code, ask the user to try again later. If someone asks you to do something you
|
||||||
can't do with your currently available tools, you must say so, and encourage them to implement it
|
can't do with your currently available tools, you must say so, and encourage them to implement it
|
||||||
themselves using the Solana Agent Kit, recommend they go to https://www.solanaagentkit.xyz for more information. Be
|
themselves using the Solana Agent Kit, recommend they go to https://www.solanaagentkit.xyz for more information. Be
|
||||||
concise and helpful with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested.`,
|
concise and helpful with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested.`,
|
||||||
maxSteps: 10,
|
maxSteps: 10,
|
||||||
});
|
});
|
||||||
|
|
||||||
for await (const textPart of response.textStream) {
|
for await (const textPart of response.textStream) {
|
||||||
process.stdout.write(textPart);
|
process.stdout.write(textPart);
|
||||||
}
|
}
|
||||||
console.log();
|
console.log();
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, interval * 1000));
|
await new Promise((resolve) => setTimeout(resolve, interval * 1000));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
console.error("Error:", error.message);
|
console.error("Error:", error.message);
|
||||||
}
|
}
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runChatMode() {
|
async function runChatMode() {
|
||||||
console.log("Starting chat mode... Type 'exit' to end.");
|
console.log("Starting chat mode... Type 'exit' to end.");
|
||||||
const openai = createOpenAI({
|
const openai = createOpenAI({
|
||||||
apiKey: process.env.OPENAI_API_KEY as string,
|
apiKey: process.env.OPENAI_API_KEY as string,
|
||||||
});
|
});
|
||||||
|
|
||||||
const solanaAgent = new SolanaAgentKit(
|
const solanaAgent = new SolanaAgentKit(
|
||||||
process.env.SOLANA_PRIVATE_KEY!,
|
process.env.SOLANA_PRIVATE_KEY!,
|
||||||
process.env.RPC_URL!,
|
process.env.RPC_URL!,
|
||||||
{
|
{
|
||||||
OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
|
OPENAI_API_KEY: process.env.OPENAI_API_KEY!,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const tools = createVercelAITools(solanaAgent);
|
const tools = createVercelAITools(solanaAgent);
|
||||||
console.log(tools);
|
console.log(tools);
|
||||||
|
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
output: process.stdout,
|
output: process.stdout,
|
||||||
});
|
});
|
||||||
|
|
||||||
const question = (prompt: string): Promise<string> =>
|
const question = (prompt: string): Promise<string> =>
|
||||||
new Promise((resolve) => rl.question(prompt, resolve));
|
new Promise((resolve) => rl.question(prompt, resolve));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
const userInput = await question("\nPrompt: ");
|
const userInput = await question("\nPrompt: ");
|
||||||
|
|
||||||
if (userInput.toLowerCase() === "exit") {
|
if (userInput.toLowerCase() === "exit") {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = streamText({
|
const response = streamText({
|
||||||
prompt: userInput,
|
prompt: userInput,
|
||||||
tools,
|
tools,
|
||||||
model: openai("gpt-4o-mini"),
|
model: openai("gpt-4o-mini"),
|
||||||
temperature: 0.7,
|
temperature: 0.7,
|
||||||
system: `You are a helpful agent that can interact onchain using the Solana Agent Kit. You are
|
system: `You are a helpful agent that can interact onchain using the Solana Agent Kit. You are
|
||||||
empowered to interact onchain using your tools. If you ever need funds, you can request them from the
|
empowered to interact onchain using your tools. If you ever need funds, you can request them from the
|
||||||
faucet. If not, you can provide your wallet details and request funds from the user. If there is a 5XX
|
faucet. If not, you can provide your wallet details and request funds from the user. If there is a 5XX
|
||||||
(internal) HTTP error code, ask the user to try again later. If someone asks you to do something you
|
(internal) HTTP error code, ask the user to try again later. If someone asks you to do something you
|
||||||
can't do with your currently available tools, you must say so, and encourage them to implement it
|
can't do with your currently available tools, you must say so, and encourage them to implement it
|
||||||
themselves using the Solana Agent Kit, recommend they go to https://www.solanaagentkit.xyz for more information. Be
|
themselves using the Solana Agent Kit, recommend they go to https://www.solanaagentkit.xyz for more information. Be
|
||||||
concise and helpful with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested.`,
|
concise and helpful with your responses. Refrain from restating your tools' descriptions unless it is explicitly requested.`,
|
||||||
maxSteps: 10,
|
maxSteps: 10,
|
||||||
});
|
});
|
||||||
|
|
||||||
for await (const textPart of response.textStream) {
|
for await (const textPart of response.textStream) {
|
||||||
process.stdout.write(textPart);
|
process.stdout.write(textPart);
|
||||||
}
|
}
|
||||||
console.log();
|
console.log();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
console.error("Error:", error.message);
|
console.error("Error:", error.message);
|
||||||
}
|
}
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
} finally {
|
} finally {
|
||||||
rl.close();
|
rl.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function chooseMode(): Promise<"chat" | "auto"> {
|
async function chooseMode(): Promise<"chat" | "auto"> {
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: process.stdin,
|
input: process.stdin,
|
||||||
output: process.stdout,
|
output: process.stdout,
|
||||||
});
|
});
|
||||||
|
|
||||||
const question = (prompt: string): Promise<string> =>
|
const question = (prompt: string): Promise<string> =>
|
||||||
new Promise((resolve) => rl.question(prompt, resolve));
|
new Promise((resolve) => rl.question(prompt, resolve));
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
console.log("\nAvailable modes:");
|
console.log("\nAvailable modes:");
|
||||||
console.log("1. chat - Interactive chat mode");
|
console.log("1. chat - Interactive chat mode");
|
||||||
console.log("2. auto - Autonomous action mode");
|
console.log("2. auto - Autonomous action mode");
|
||||||
|
|
||||||
const choice = (await question("\nChoose a mode (enter number or name): "))
|
const choice = (await question("\nChoose a mode (enter number or name): "))
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
rl.close();
|
rl.close();
|
||||||
|
|
||||||
if (choice === "1" || choice === "chat") {
|
if (choice === "1" || choice === "chat") {
|
||||||
return "chat";
|
return "chat";
|
||||||
} else if (choice === "2" || choice === "auto") {
|
} else if (choice === "2" || choice === "auto") {
|
||||||
return "auto";
|
return "auto";
|
||||||
}
|
}
|
||||||
console.log("Invalid choice. Please try again.");
|
console.log("Invalid choice. Please try again.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
console.log("Starting Agent...");
|
console.log("Starting Agent...");
|
||||||
const mode = await chooseMode();
|
const mode = await chooseMode();
|
||||||
|
|
||||||
if (mode === "chat") {
|
if (mode === "chat") {
|
||||||
await runChatMode();
|
await runChatMode();
|
||||||
} else {
|
} else {
|
||||||
await runAutonomousMode();
|
await runAutonomousMode();
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
console.error("Error:", error.message);
|
console.error("Error:", error.message);
|
||||||
}
|
}
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
main().catch((error) => {
|
main().catch((error) => {
|
||||||
console.error("Fatal error:", error);
|
console.error("Fatal error:", error);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user