mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-06-09 07:36:45 +00:00
feat: Refactor actions to use tool functions for improved code clarity and maintainability
This commit is contained in:
@@ -2,6 +2,7 @@ import { Action } from "../types/action";
|
||||
import { SolanaAgentKit } from "../agent";
|
||||
import { z } from "zod";
|
||||
import { VersionedTransaction } from "@solana/web3.js";
|
||||
import { lendAsset } from "../tools";
|
||||
|
||||
const lendAssetAction: Action = {
|
||||
name: "solana_lend_asset",
|
||||
@@ -38,55 +39,11 @@ const lendAssetAction: Action = {
|
||||
try {
|
||||
const amount = input.amount as number;
|
||||
|
||||
const response = await fetch(
|
||||
`https://blink.lulo.fi/actions?amount=${amount}&symbol=USDC`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
account: agent.wallet.publicKey.toBase58(),
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
return {
|
||||
status: "error",
|
||||
message: `Failed to get lending transaction: ${response.statusText}`
|
||||
};
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Deserialize the transaction
|
||||
const luloTxn = VersionedTransaction.deserialize(
|
||||
Buffer.from(data.transaction, "base64")
|
||||
);
|
||||
|
||||
// Get a recent blockhash and set it
|
||||
const { blockhash } = await agent.connection.getLatestBlockhash();
|
||||
luloTxn.message.recentBlockhash = blockhash;
|
||||
|
||||
// Sign and send transaction
|
||||
luloTxn.sign([agent.wallet]);
|
||||
const signature = await agent.connection.sendTransaction(luloTxn, {
|
||||
preflightCommitment: "confirmed",
|
||||
maxRetries: 3,
|
||||
});
|
||||
|
||||
// Wait for confirmation
|
||||
const latestBlockhash = await agent.connection.getLatestBlockhash();
|
||||
await agent.connection.confirmTransaction({
|
||||
signature,
|
||||
blockhash: latestBlockhash.blockhash,
|
||||
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
|
||||
});
|
||||
const response = await lendAsset(agent, amount);
|
||||
|
||||
return {
|
||||
status: "success",
|
||||
signature,
|
||||
signature: response,
|
||||
message: `Successfully lent ${amount} USDC`
|
||||
};
|
||||
} catch (error: any) {
|
||||
|
||||
Reference in New Issue
Block a user