-
Notifications
You must be signed in to change notification settings - Fork 313
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lend and withdraw asset with lulo, not only usdc
- Loading branch information
1 parent
805ff71
commit 67d44e1
Showing
9 changed files
with
194 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Action } from "../types/action"; | ||
import { SolanaAgentKit } from "../agent"; | ||
import { z } from "zod"; | ||
import { withdrawAsset } from "../tools"; | ||
|
||
const withdrawAssetAction: Action = { | ||
name: "WITHDRAW_ASSET", | ||
similes: [ | ||
"withdraw usdc", | ||
"withdraw with lulo", | ||
], | ||
description: "Withdraw SPL tokens using Lulo protocol", | ||
examples: [ | ||
[ | ||
{ | ||
input: { | ||
mintAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", | ||
amount: 100, | ||
}, | ||
output: { | ||
status: "success", | ||
signature: "4xKpN2...", | ||
message: "Successfully withdraw 100 USDC", | ||
}, | ||
explanation: "Withdraw 100 USDC on Lulo", | ||
}, | ||
], | ||
], | ||
schema: z.object({ | ||
mintAddress: z.string().describe("SPL Mint address"), | ||
amount: z.number().positive().describe("Amount to lend"), | ||
}), | ||
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => { | ||
try { | ||
const mintAddress = input.mintAddress as string; | ||
const amount = input.amount as number; | ||
|
||
const response = await withdrawAsset(agent, mintAddress, amount); | ||
|
||
return { | ||
status: "success", | ||
signature: response, | ||
message: `Successfully withdraw ${amount} of token ${mintAddress}`, | ||
}; | ||
} catch (error: any) { | ||
return { | ||
status: "error", | ||
message: `Withdraw failed: ${error.message}`, | ||
}; | ||
} | ||
}, | ||
}; | ||
|
||
export default withdrawAssetAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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 withdrawAsset( | ||
agent: SolanaAgentKit, | ||
mintAddress: string, | ||
amount: number, | ||
): Promise<string> { | ||
try { | ||
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": process.env.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}`); | ||
} | ||
} |