mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-19 23:26:45 +00:00
fix: lend using lulo
This commit is contained in:
@@ -10,61 +10,51 @@ import { SolanaAgentKit } from "../agent";
|
||||
*/
|
||||
export async function lendAsset(
|
||||
agent: SolanaAgentKit,
|
||||
amount: number,
|
||||
amount: number
|
||||
): Promise<string> {
|
||||
try {
|
||||
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(),
|
||||
}),
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Deserialize the transaction
|
||||
const luloTxn = VersionedTransaction.deserialize(
|
||||
Buffer.from(data.transaction, "base64"),
|
||||
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);
|
||||
|
||||
const signature = await agent.connection.sendTransaction(luloTxn, {
|
||||
preflightCommitment: "confirmed",
|
||||
maxRetries: 3,
|
||||
});
|
||||
|
||||
// Wait for confirmation using the latest strategy
|
||||
const latestBlockhash = await agent.connection.getLatestBlockhash();
|
||||
await agent.connection.confirmTransaction({
|
||||
signature,
|
||||
blockhash: latestBlockhash.blockhash,
|
||||
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
|
||||
});
|
||||
|
||||
return signature;
|
||||
} catch (error: any) {
|
||||
throw new Error(`Lending failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch lending details for agent
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param LULO_API_KEY Valid API key for Lulo
|
||||
* @returns Lending account details
|
||||
*/
|
||||
export async function getLendingDetails(
|
||||
agent: SolanaAgentKit,
|
||||
LULO_API_KEY = "",
|
||||
): Promise<LuloAccountDetailsResponse> {
|
||||
try {
|
||||
if (!LULO_API_KEY) {
|
||||
throw new Error("Missing Lulo API key");
|
||||
}
|
||||
|
||||
const response = await fetch(`${LULO_API}/account`, {
|
||||
headers: {
|
||||
"x-wallet-pubkey": agent.wallet.publicKey.toBase58(),
|
||||
"x-api-key": LULO_API_KEY,
|
||||
},
|
||||
});
|
||||
|
||||
const { data } = await response.json();
|
||||
|
||||
return data as LuloAccountDetailsResponse;
|
||||
} catch (error: any) {
|
||||
throw new Error(`Failed to fetch lending details: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user