feat: Refactor actions to use tool functions for improved code clarity and maintainability

This commit is contained in:
Fahri Bilici
2024-12-28 16:24:57 +01:00
parent 9326da25b1
commit 378fea201d
30 changed files with 118 additions and 654 deletions

View File

@@ -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) {