| name | starknet-defi |
| description | Execute DeFi operations on Starknet including token swaps via avnu aggregator, DCA recurring buys, STRK staking, and lending/borrowing. Supports gasless transactions. |
| license | Apache-2.0 |
| metadata | {"author":"starknet-agentic","version":"1.0.0","org":"keep-starknet-strange"} |
| keywords | ["starknet","defi","swap","dca","staking","lending","avnu","ekubo","jediswap","zklend","nostra","aggregator","yield"] |
| allowed-tools | ["Bash","Read","Write","Glob","Grep","Task"] |
| user-invocable | true |
Starknet DeFi Skill
Execute DeFi operations on Starknet using avnu aggregator and native protocols.
When to Use
- Swaps, DCA orders, staking, and lending or borrowing flows on Starknet.
- Agent workflows that need AVNU routing, paymaster support, or protocol-specific DeFi execution.
When NOT to Use
- Plain wallet management without DeFi intent.
- Cairo contract authoring, deployment operations, or security auditing.
Quick Start
- Install the AVNU + starknet.js dependencies and point the skill at a funded Starknet account.
- Use skills catalog when the flow expands into wallet setup, deployment, or auditing.
Prerequisites
npm install starknet@^8.9.1 @avnu/avnu-sdk@^4.0.1
Token Swaps (avnu SDK v4)
Get Quote and Execute Swap
import { getQuotes, executeSwap, type QuoteRequest } from "@avnu/avnu-sdk";
import { Account, RpcProvider, ETransactionVersion } from "starknet";
const provider = new RpcProvider({ nodeUrl: process.env.STARKNET_RPC_URL });
const account = new Account({
provider,
address,
signer: privateKey,
transactionVersion: ETransactionVersion.V3,
});
import { fetchVerifiedTokenBySymbol } from '@avnu/avnu-sdk';
const eth = await fetchVerifiedTokenBySymbol('ETH');
const strk = await fetchVerifiedTokenBySymbol('STRK');
const quoteParams: QuoteRequest = {
sellTokenAddress: eth.address,
buyTokenAddress: strk.address,
sellAmount: BigInt(10 ** 17),
takerAddress: account.address,
};
const quotes = await getQuotes(quoteParams);
const bestQuote = quotes[0];
const result = await executeSwap({
provider: account,
quote: bestQuote,
slippage: 0.01,
executeApprove: true,
});
console.log("Tx:", result.transactionHash);
Quote Response Fields (SDK v4)
interface Quote {
quoteId: string;
sellTokenAddress: string;
buyTokenAddress: string;
sellAmount: bigint;
buyAmount: bigint;
sellAmountInUsd: number;
buyAmountInUsd: number;
priceImpact: number;
gasFeesInUsd: number;
routes: Array<{
name: string;
percent: number;
}>;
fee: {
avnuFees: bigint;
integratorFees: bigint;
};
}
Build Swap Calls (for multicall composition)
import { quoteToCalls } from "@avnu/avnu-sdk";
const calls = await quoteToCalls({
quote: bestQuote,
takerAddress: account.address,
slippage: 0.01,
includeApprove: true,
});
Gasless Swap (Pay Gas in Token) - SDK v4 + PaymasterRpc
import { getQuotes, executeSwap } from "@avnu/avnu-sdk";
import { PaymasterRpc } from "starknet";
const quotes = await getQuotes(quoteParams);
const bestQuote = quotes[0];
const paymaster = new PaymasterRpc({
nodeUrl: process.env.AVNU_PAYMASTER_URL || "https://starknet.paymaster.avnu.fi",
});
const result = await executeSwap({
provider: account,
quote: bestQuote,
slippage: 0.01,
executeApprove: true,
paymaster: {
active: true,
provider: paymaster,
params: {
feeMode: {
mode: "default",
gasToken: "0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8",
},
},
},
});
DCA (Dollar Cost Averaging)
Create DCA Order
import { executeCreateDca } from "@avnu/avnu-sdk";
import moment from "moment";
const dcaOrder = {
sellTokenAddress: usdcAddress,
buyTokenAddress: strkAddress,
totalAmount: parseUnits("100", 6),
numberOfOrders: 10,
frequency: moment.duration(1, "day"),
startAt: Math.floor(Date.now() / 1000),
};
const result = await executeCreateDca({
provider: account,
order: dcaOrder,
});
Check and Cancel DCA
import { getDcaOrders, executeCancelDca, DcaOrderStatus } from "@avnu/avnu-sdk";
const orders = await getDcaOrders({
traderAddress: account.address,
status: DcaOrderStatus.OPEN,
});
await executeCancelDca({
provider: account,
orderAddress: orders[0].orderAddress,
});
STRK Staking
Stake STRK
import { executeStake, getAvnuStakingInfo } from "@avnu/avnu-sdk";
const stakingInfo = await getAvnuStakingInfo();
const result = await executeStake({
provider: account,
poolAddress: stakingInfo.pools[0].address,
amount: parseUnits("100", 18),
});
Get User Staking Info
import { getUserStakingInfo } from "@avnu/avnu-sdk";
const userInfo = await getUserStakingInfo(TOKENS.STRK, account.address);
console.log("Staked:", userInfo.amount);
console.log("Unclaimed rewards:", userInfo.unclaimedRewards);
Claim Rewards
import { executeClaimRewards } from "@avnu/avnu-sdk";
await executeClaimRewards({
provider: account,
poolAddress: poolAddress,
restake: true,
});
Unstake
import { executeInitiateUnstake, executeUnstake } from "@avnu/avnu-sdk";
await executeInitiateUnstake({
provider: account,
poolAddress: poolAddress,
amount: parseUnits("50", 18),
});
await executeUnstake({
provider: account,
poolAddress: poolAddress,
});
Market Data
Token Prices
import { getPrices, fetchTokens, fetchVerifiedTokenBySymbol } from "@avnu/avnu-sdk";
const strk = await fetchVerifiedTokenBySymbol("STRK");
const prices = await getPrices([ethAddress, strkAddress, usdcAddress]);
const tokens = await fetchTokens({ page: 0, size: 20, tags: ["verified"] });
Protocol Reference
| Protocol | Operations | Notes |
|---|
| avnu | Swap aggregation, DCA, gasless | Best-price routing across all DEXs |
| Ekubo | AMM, concentrated liquidity | Highest TVL on Starknet |
| JediSwap | AMM, classic pools | V2 with concentrated liquidity |
| zkLend | Lending, borrowing | Variable and stable rates |
| Nostra | Lending, borrowing | Multi-asset pools |
Configuration
| Variable | Purpose | Default |
|---|
STARKNET_RPC_URL | Starknet JSON-RPC endpoint | Required |
STARKNET_ACCOUNT_ADDRESS | Agent's account address | Required |
STARKNET_PRIVATE_KEY | Agent's signing key | Required |
AVNU_BASE_URL | avnu API base URL | https://starknet.api.avnu.fi |
AVNU_PAYMASTER_URL | avnu paymaster URL | https://starknet.paymaster.avnu.fi |
AVNU_API_KEY | Optional avnu integrator key | None |
avnu URL Reference
| Network | API URL | Paymaster URL |
|---|
| Mainnet | https://starknet.api.avnu.fi | https://starknet.paymaster.avnu.fi |
| Sepolia | https://sepolia.api.avnu.fi | https://sepolia.paymaster.avnu.fi |
Error Handling
async function safeSwap(account, quote, slippage = 0.01) {
try {
return await executeSwap({
provider: account,
quote,
slippage,
executeApprove: true,
});
} catch (error) {
if (error.message?.includes("INSUFFICIENT_BALANCE")) {
throw new Error("Not enough tokens for swap");
}
if (error.message?.includes("SLIPPAGE") || error.message?.includes("Insufficient tokens received")) {
return await executeSwap({
provider: account,
quote,
slippage: slippage * 2,
executeApprove: true,
});
}
if (error.message?.includes("QUOTE_EXPIRED")) {
throw new Error("Quote expired. Please retry the operation.");
}
if (error.message?.includes("INSUFFICIENT_LIQUIDITY")) {
throw new Error("Insufficient liquidity. Try a smaller amount.");
}
throw error;
}
}
References