Skip to content

FraxLend Plugin

The FraxLend plugin enables your agent to interact with FraxLend protocol, providing comprehensive lending and borrowing capabilities. It supports various operations including lending, borrowing, withdrawing, and managing collateral on the Fraxtal network.

  1. Install the required packages:

    Terminal window
    npm i @iqai/plugin-fraxlend
  2. Create a .env file with required configuration:

    WALLET_PRIVATE_KEY=your-wallet-private-key
    OPENAI_API_KEY=your-openai-api-key

Here’s a complete example of setting up an agent with the FraxLend plugin:

import { AgentBuilder, ModelProviderName } from "@iqai/agent";
import { createFraxlendPlugin } from "@iqai/plugin-fraxlend";
import { fraxtal } from "viem/chains";
async function main() {
// Initialize FraxLend plugin
const fraxlendPlugin = await createFraxlendPlugin({
chain: fraxtal,
walletPrivateKey: process.env.WALLET_PRIVATE_KEY,
});
// Create agent with plugin
const agent = new AgentBuilder()
.withModelProvider(
ModelProviderName.OPENAI,
process.env.OPENAI_API_KEY
)
.withPlugin(fraxlendPlugin)
.build();
await agent.start();
}
main().catch(console.error);

The plugin provides the following operations:

  • Lending Management
    • Deposit assets
    • Withdraw assets
  • Borrowing Operations
    • Borrow assets
    • Repay borrowed amounts
  • Collateral Management
    • Add collateral
    • Remove collateral
  • Information Retrieval
    • Get lending statistics
    • View agent positions
    • Find pair addresses

Here’s how to interact with your FraxLend-enabled agent through natural language:

Query:

What's the pair address for FRAX-ETH pool with the highest APR?

The agent will process this to:

{
"assetSymbol": "FRAX",
"collateralSymbol": "ETH",
"sortByApr": "highest"
}

Query:

I want to lend 100 FRAX to the FRAX-ETH pool at 0x123...abc

Agent processes this as:

{
"pairAddress": "0x123...abc",
"amount": "100000000000000000000"
}

Query:

Borrow 1000 FRAX using 2 ETH as collateral from the pool at 0x123...abc

Agent structures this as:

{
"pairAddress": "0x123...abc",
"borrowAmount": "1000000000000000000000",
"collateralAmount": "2000000000000000000",
"receiver": "0x456...def"
}

Query:

Show my current FraxLend positions

The FraxLend plugin accepts these configuration parameters:

interface FraxLendPluginConfig {
chain: Chain; // Blockchain network configuration
walletPrivateKey: string; // Private key for transactions
}

Always implement proper error handling when using the plugin:

try {
const fraxlendPlugin = await createFraxlendPlugin({
chain: fraxtal,
walletPrivateKey: process.env.WALLET_PRIVATE_KEY,
});
} catch (error) {
console.error('Failed to initialize FraxLend plugin:', error);
}
  • Environment Variables: Always use environment variables for sensitive information like private keys
  • Transaction Validation: Double-check amounts and addresses before confirming transactions
  • Error Handling: Implement proper error handling for all operations
  • Gas Management: Consider gas costs for operations on the Fraxtal network
  • Invalid Pair Address: Ensure you’re using the correct pair address for the pool
  • Insufficient Balance: Verify you have enough tokens for the operation
  • Transaction Failures: Check gas settings and network status