DeFi Operations
This guide covers DeFi operations for AI agents on Starknet using the AVNU aggregator. For complete API details and code examples, see the DeFi Skill documentation.
Overview
AVNU aggregates liquidity across all Starknet DEXs (Ekubo, JediSwap, etc.) to find optimal routes for every swap. Combined with paymaster support, agents can execute DeFi operations in any token.
| Operation | Description |
|---|---|
| Swaps | Best-price token exchanges across all DEXs |
| DCA | Automated dollar-cost averaging orders |
| Staking | STRK staking with reward claiming |
| Gasless | Pay fees in any token via paymaster |
Token Swaps
Get Quote and Execute
// Via MCP tools
const quote = await mcpClient.callTool({
name: "starknet_get_quote",
arguments: {
sellToken: "ETH",
buyToken: "STRK",
amount: "0.1",
}
});
const result = await mcpClient.callTool({
name: "starknet_swap",
arguments: {
sellToken: "ETH",
buyToken: "STRK",
amount: "0.1",
slippage: 0.01, // 1%
}
});Gasfree Swaps
Two gasfree modes are available:
Sponsored Mode (dApp pays gas):
// Requires AVNU_PAYMASTER_API_KEY environment variable
const result = await mcpClient.callTool({
name: "starknet_swap",
arguments: {
sellToken: "ETH",
buyToken: "STRK",
amount: "0.1",
gasfree: true, // Gas is fully sponsored
}
});Token Mode (pay gas in ERC-20):
// Without API key, specify gasToken
const result = await mcpClient.callTool({
name: "starknet_swap",
arguments: {
sellToken: "ETH",
buyToken: "STRK",
amount: "0.1",
gasfree: true,
gasToken: "USDC", // Pay gas in USDC
}
});Gasfree Configuration
Sponsored mode requires AVNU_PAYMASTER_API_KEY. Without it, gasfree uses token mode where gas is paid in the specified gasToken (or the sell token by default).
Best Price Routing
AVNU automatically finds the optimal route across Ekubo, JediSwap, and other DEXs. You don't need to specify which exchange to use.
DCA (Dollar Cost Averaging)
Set up recurring purchases to reduce timing risk:
import { executeCreateDca } from "@avnu/avnu-sdk";
import moment from "moment";
await executeCreateDca({
provider: account,
order: {
sellTokenAddress: usdcAddress,
buyTokenAddress: strkAddress,
totalAmount: BigInt(100 * 10**6), // 100 USDC
numberOfOrders: 10, // 10 orders of 10 USDC each
frequency: moment.duration(1, "day"),
startAt: Math.floor(Date.now() / 1000),
},
});STRK Staking
Stake STRK to earn rewards:
import { executeStake, getAvnuStakingInfo } from "@avnu/avnu-sdk";
// Get pool info
const stakingInfo = await getAvnuStakingInfo();
// Stake
await executeStake({
provider: account,
poolAddress: stakingInfo.pools[0].address,
amount: BigInt(100 * 10**18), // 100 STRK
});Unstaking Cooldown
STRK has a 21-day unstaking cooldown. Plan accordingly when managing agent liquidity.
Supported Protocols
| Protocol | Operations |
|---|---|
| AVNU | Swap aggregation, DCA, gasless transactions |
| Ekubo | AMM with concentrated liquidity |
| JediSwap | Classic AMM pools |
| zkLend | Lending and borrowing |
| Nostra | Multi-asset lending pools |
API Endpoints
| Network | AVNU API | Paymaster |
|---|---|---|
| Mainnet | https://starknet.api.avnu.fi | https://starknet.paymaster.avnu.fi |
| Sepolia | https://sepolia.api.avnu.fi | https://sepolia.paymaster.avnu.fi |
Next Steps
- DeFi Skill Reference - Complete API with DCA management, staking rewards, and error handling
- Wallet Management - Token transfers and balance checking
- MCP Server Setup - Configure your AI assistant