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.

OperationDescription
SwapsBest-price token exchanges across all DEXs
DCAAutomated dollar-cost averaging orders
StakingSTRK staking with reward claiming
GaslessPay 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

ProtocolOperations
AVNUSwap aggregation, DCA, gasless transactions
EkuboAMM with concentrated liquidity
JediSwapClassic AMM pools
zkLendLending and borrowing
NostraMulti-asset lending pools

API Endpoints

NetworkAVNU APIPaymaster
Mainnethttps://starknet.api.avnu.fihttps://starknet.paymaster.avnu.fi
Sepoliahttps://sepolia.api.avnu.fihttps://sepolia.paymaster.avnu.fi

Next Steps