mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-24 07:36:45 +00:00
lend and withdraw asset with lulo, not only usdc
This commit is contained in:
@@ -1 +1,3 @@
|
||||
export * from "./lend";
|
||||
export * from "./lulo_lend";
|
||||
export * from "./lulo_withdraw";
|
||||
|
||||
64
src/tools/lulo/lulo_lend.ts
Normal file
64
src/tools/lulo/lulo_lend.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { VersionedTransaction } from "@solana/web3.js";
|
||||
import { SolanaAgentKit } from "../../index";
|
||||
|
||||
/**
|
||||
* Lend tokens for yields using Lulo
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param mintAddress SPL Mint address
|
||||
* @param amount Amount to lend
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
export async function luloLend(
|
||||
agent: SolanaAgentKit,
|
||||
mintAddress: string,
|
||||
amount: number,
|
||||
): Promise<string> {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`https://api.flexlend.fi/generate/account/deposit?priorityFee=50000`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-wallet-pubkey": agent.wallet.publicKey.toBase58(),
|
||||
"x-api-key": process.env.FLEXLEND_API_KEY!
|
||||
},
|
||||
body: JSON.stringify({
|
||||
owner: agent.wallet.publicKey.toBase58(),
|
||||
mintAddress: mintAddress,
|
||||
depositAmount: amount.toString(),
|
||||
}),
|
||||
},
|
||||
);
|
||||
const { data: { transactionMeta } } = await response.json()
|
||||
|
||||
// Deserialize the transaction
|
||||
const luloTxn = VersionedTransaction.deserialize(
|
||||
Buffer.from(transactionMeta[0].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 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}`);
|
||||
}
|
||||
}
|
||||
69
src/tools/lulo/lulo_withdraw.ts
Normal file
69
src/tools/lulo/lulo_withdraw.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { VersionedTransaction } from "@solana/web3.js";
|
||||
import { SolanaAgentKit } from "../../index";
|
||||
|
||||
/**
|
||||
* Withdraw tokens for yields using Lulo
|
||||
* @param agent SolanaAgentKit instance
|
||||
* @param mintAddress SPL Mint address
|
||||
* @param amount Amount to withdraw
|
||||
* @returns Transaction signature
|
||||
*/
|
||||
export async function luloWithdraw(
|
||||
agent: SolanaAgentKit,
|
||||
mintAddress: string,
|
||||
amount: number,
|
||||
): Promise<string> {
|
||||
try {
|
||||
if (!agent.config.FLEXLEND_API_KEY) {
|
||||
throw new Error("Lulo API key not found in agent configuration");
|
||||
}
|
||||
|
||||
const response = await fetch(
|
||||
`https://api.flexlend.fi/generate/account/withdraw?priorityFee=50000`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-wallet-pubkey": agent.wallet.publicKey.toBase58(),
|
||||
"x-api-key": agent.config.FLEXLEND_API_KEY
|
||||
},
|
||||
body: JSON.stringify({
|
||||
owner: agent.wallet.publicKey.toBase58(),
|
||||
mintAddress: mintAddress,
|
||||
depositAmount: amount,
|
||||
}),
|
||||
},
|
||||
);
|
||||
|
||||
const { data: { transactionMeta } } = await response.json()
|
||||
|
||||
// Deserialize the transaction
|
||||
const luloTxn = VersionedTransaction.deserialize(
|
||||
Buffer.from(transactionMeta[0].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 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}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user