Comparing curve with pyth
curve
View full →Author
@0xinit
Stars
53
Repository
0xinit/cryptoskills
Curve Finance
Curve is the dominant AMM for pegged-asset swaps (stablecoins, wrapped tokens, LSTs). Its StableSwap invariant concentrates liquidity around peg, delivering 10-100x lower slippage than constant-product AMMs for like-kind assets. CryptoSwap (Tricrypto) extends this to volatile pairs. The protocol also issues crvUSD, a stablecoin backed by LLAMMA — a soft-liquidation mechanism that gradually converts collateral instead of instant liquidation. CRV emissions are directed to liquidity gauges via vote-escrowed CRV (veCRV).
All Curve pool contracts are written in Vyper. ABI encoding is identical to Solidity — viem works without modification.
What You Probably Got Wrong
Curve is one of the most commonly mis-integrated protocols. Each pool type has different interfaces, and token ordering is deployment-specific.
- Curve pools have DIFFERENT ABIs per pool type — StableSwap (2-pool, 3-pool), CryptoSwap, Tricrypto, Meta pools, and Factory pools all have different function signatures. There is NO universal pool ABI. Always read the specific pool's ABI from Etherscan or the Curve docs.
- Token indices are pool-specific and NOT sorted — The order depends on deployment, not address sorting. Always call
coins(i)to verify which token is at which index. Getting this wrong swaps the wrong token. exchange()uses token indices, not addresses — You passi(sell token index) andj(buy token index), not token addresses. Passing the wrong index silently swaps the wrong token pair.get_dy()returns the estimated output BEFORE fees — The actual received amount is slightly less. Useget_dy()for quoting but apply slippage tolerance on top.add_liquidity()amounts array length varies per pool — 2 for 2-pool, 3 for 3-pool, 4 for 4-pool. Passing the wrong array length causes a revert with no useful error message.exchange()vsexchange_underlying()— Plain pools useexchange(). Meta pools useexchange_underlying()to swap between the meta-asset and the underlying basepool tokens. Calling the wrong function reverts.- crvUSD uses LLAMMA (soft liquidation), NOT traditional liquidation — Positions are gradually converted between collateral and crvUSD as price moves through bands. There is no instant liquidation threshold. Health approaching 0 means bands are fully converted.
- Gauge voting requires veCRV (vote-escrowed CRV) — Lock CRV for 1-4 years to get voting power. Voting power decays linearly. You cannot transfer or sell veCRV.
remove_liquidity_one_coin()has high slippage for large withdrawals from imbalanced pools — The StableSwap invariant penalizes imbalanced withdrawals. Always simulate first.- Virtual price only goes up (monotonic) —
get_virtual_price()returns the LP token value in underlying. It increases from fees and never decreases. Useful for pricing LP positions but NOT for detecting exploits (it was manipulated in some reentrancy attacks on Vyper <0.3.1 pools).
Quick Start
Installation
npm install viem
Client Setup
import { createPublicClient, createWalletClient, http, type Address } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet } from "viem/chains";
const publicClient = createPublicClient({
chain: mainnet,
transport: http(process.env.RPC_URL),
});
const account = privateKeyToAccount(
process.env.PRIVATE_KEY as `0x${string}`
);
const walletClient = createWalletClient({
account,
chain: mainnet,
transport: http(process.env.RPC_URL),
});
Swap USDC to USDT on 3pool
const THREE_POOL = "0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7" as const;
// 3pool indices: 0 = DAI, 1 = USDC, 2 = USDT
// Always verify with coins(i) before swapping
const threePoolAbi = [
{
name: "exchange",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "i", type: "int128" },
{ name: "j", type: "int128" },
{ name: "dx", type: "uint256" },
{ name: "min_dy", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "get_dy",
type: "function",
stateMutability: "view",
inputs: [
{ name: "i", type: "int128" },
{ name: "j", type: "int128" },
{ name: "dx", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "coins",
type: "function",
stateMutability: "view",
inputs: [{ name: "i", type: "uint256" }],
outputs: [{ name: "", type: "address" }],
},
] as const;
const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" as const;
const USDT = "0xdAC17F958D2ee523a2206206994597C13D831ec7" as const;
const amountIn = 10_000_000000n; // 10,000 USDC (6 decimals)
// Verify token indices
const coin1 = await publicClient.readContract({
address: THREE_POOL,
abi: threePoolAbi,
functionName: "coins",
args: [1n],
});
if (coin1.toLowerCase() !== USDC.toLowerCase()) {
throw new Error(`Expected USDC at index 1, got ${coin1}`);
}
// Quote expected output
const expectedOut = await publicClient.readContract({
address: THREE_POOL,
abi: threePoolAbi,
functionName: "get_dy",
args: [1n, 2n, amountIn], // i=1 (USDC) -> j=2 (USDT)
});
// 0.1% slippage tolerance (stableswap pools have tight spreads)
const minDy = (expectedOut * 999n) / 1000n;
// Approve 3pool to spend USDC
const erc20Abi = [
{
name: "approve",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "spender", type: "address" },
{ name: "amount", type: "uint256" },
],
outputs: [{ name: "", type: "bool" }],
},
] as const;
const { request: approveRequest } = await publicClient.simulateContract({
address: USDC,
abi: erc20Abi,
functionName: "approve",
args: [THREE_POOL, amountIn],
account: account.address,
});
const approveHash = await walletClient.writeContract(approveRequest);
await publicClient.waitForTransactionReceipt({ hash: approveHash });
// Execute swap
const { request } = await publicClient.simulateContract({
address: THREE_POOL,
abi: threePoolAbi,
functionName: "exchange",
args: [1n, 2n, amountIn, minDy],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("Swap reverted");
Pool Types
StableSwap Pools (Pegged Assets)
The original Curve pool type. Optimized for assets that trade near 1:1 (stablecoins, wrapped tokens). Uses the StableSwap invariant which blends constant-sum and constant-product formulas, controlled by the amplification parameter A.
| Pool | Address | Coins | Indices |
|---|---|---|---|
| 3pool | 0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7 | DAI, USDC, USDT | 0, 1, 2 |
| stETH/ETH | 0xDC24316b9AE028F1497c275EB9192a3Ea0f67022 | ETH, stETH | 0, 1 |
| frxETH/ETH | 0xa1F8A6807c402E4A15ef4EBa36528A3FED24E577 | ETH, frxETH | 0, 1 |
Key parameters:
- A (amplification) — Higher A means tighter peg. 3pool uses A=2000. Ranges from 1 (constant product) to ~5000.
- Fee — Typically 0.01%-0.04% for stableswap pools. Read via
fee()(returns value in 1e10 precision, so4000000= 0.04%).
CryptoSwap Pools (Volatile Pairs)
Two-token pools for non-pegged assets using the CryptoSwap invariant. Internally re-pegs around the current price, providing concentrated liquidity that auto-rebalances.
| Pool | Address | Coins |
|---|---|---|
| tricrypto2 | 0xD51a44d3FaE010294C616388b506AcdA1bfAAE46 | USDT, WBTC, WETH |
Meta Pools
Pools that pair a single token against an existing basepool's LP token. For example, FRAX/3CRV pairs FRAX against the 3pool LP token, giving FRAX access to DAI/USDC/USDT liquidity.
// Meta pool: exchange_underlying() swaps between the meta-asset
// and any token in the basepool
const metaPoolAbi = [
{
name: "exchange_underlying",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "i", type: "int128" },
{ name: "j", type: "int128" },
{ name: "dx", type: "uint256" },
{ name: "min_dy", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
// LUSD/3CRV meta pool
// Underlying indices: 0 = LUSD, 1 = DAI, 2 = USDC, 3 = USDT
// exchange_underlying(0, 2, amount, minOut) swaps LUSD -> USDC
Factory Pools
User-deployed pools created through the Curve Factory. They follow the same interface as their pool type (StableSwap or CryptoSwap) but are created permissionlessly.
const CURVE_FACTORY = "0xB9fC157394Af804a3578134A6585C0dc9cc990d4" as const;
const factoryAbi = [
{
name: "pool_count",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "pool_list",
type: "function",
stateMutability: "view",
inputs: [{ name: "i", type: "uint256" }],
outputs: [{ name: "", type: "address" }],
},
] as const;
const poolCount = await publicClient.readContract({
address: CURVE_FACTORY,
abi: factoryAbi,
functionName: "pool_count",
});
Swapping
Basic Exchange (StableSwap)
All StableSwap pools use exchange(i, j, dx, min_dy) where i and j are token indices.
// Older pools (like 3pool) use int128 for indices
const stableSwapExchangeAbi = [
{
name: "exchange",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "i", type: "int128" },
{ name: "j", type: "int128" },
{ name: "dx", type: "uint256" },
{ name: "min_dy", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
// Newer factory pools may use uint256 for indices
const factoryExchangeAbi = [
{
name: "exchange",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "i", type: "uint256" },
{ name: "j", type: "uint256" },
{ name: "dx", type: "uint256" },
{ name: "min_dy", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
Exchange with ETH
ETH pools (stETH/ETH, frxETH/ETH) accept native ETH via msg.value. Pass ETH as value, not as an ERC-20 approval.
const STETH_POOL = "0xDC24316b9AE028F1497c275EB9192a3Ea0f67022" as const;
// stETH/ETH pool: 0 = ETH, 1 = stETH
const ethPoolExchangeAbi = [
{
name: "exchange",
type: "function",
stateMutability: "payable",
inputs: [
{ name: "i", type: "int128" },
{ name: "j", type: "int128" },
{ name: "dx", type: "uint256" },
{ name: "min_dy", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
const ethAmount = 1_000_000_000_000_000_000n; // 1 ETH
const expectedSteth = await publicClient.readContract({
address: STETH_POOL,
abi: threePoolAbi, // get_dy has same signature
functionName: "get_dy",
args: [0n, 1n, ethAmount],
});
const minSteth = (expectedSteth * 999n) / 1000n;
const { request } = await publicClient.simulateContract({
address: STETH_POOL,
abi: ethPoolExchangeAbi,
functionName: "exchange",
args: [0n, 1n, ethAmount, minSteth],
value: ethAmount, // send ETH with the call
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("Swap reverted");
Curve Router
For optimal routing across multiple pools, use the Curve Router. It finds the best path automatically.
const CURVE_ROUTER = "0xF0d4c12A5768D806021F80a262B4d39d26C58b8D" as const;
const routerAbi = [
{
name: "exchange",
type: "function",
stateMutability: "payable",
inputs: [
{ name: "_route", type: "address[11]" },
{ name: "_swap_params", type: "uint256[5][5]" },
{ name: "_amount", type: "uint256" },
{ name: "_min_dy", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "get_dy",
type: "function",
stateMutability: "view",
inputs: [
{ name: "_route", type: "address[11]" },
{ name: "_swap_params", type: "uint256[5][5]" },
{ name: "_amount", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
// Route encoding: alternating [token, pool, token, pool, ..., token]
// padded with zero addresses to length 11
// swap_params[i] = [i, j, swap_type, pool_type, n_coins]
// swap_type: 1 = exchange, 2 = exchange_underlying, 3 = exchange on underlying
// pool_type: 1 = stableswap, 2 = cryptoswap, 3 = tricrypto
Liquidity
Add Liquidity (Balanced)
Provide all tokens proportionally to minimize slippage.
const addLiquidityAbi = [
{
name: "add_liquidity",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "amounts", type: "uint256[3]" },
{ name: "min_mint_amount", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "calc_token_amount",
type: "function",
stateMutability: "view",
inputs: [
{ name: "amounts", type: "uint256[3]" },
{ name: "is_deposit", type: "bool" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
// Deposit 1000 of each stablecoin into 3pool
const amounts: readonly [bigint, bigint, bigint] = [
1000_000000000000000000n, // 1000 DAI (18 decimals)
1000_000000n, // 1000 USDC (6 decimals)
1000_000000n, // 1000 USDT (6 decimals)
];
// Estimate LP tokens received
const expectedLp = await publicClient.readContract({
address: THREE_POOL,
abi: addLiquidityAbi,
functionName: "calc_token_amount",
args: [amounts, true],
});
// 0.5% slippage on LP token mint
const minMintAmount = (expectedLp * 995n) / 1000n;
// Approve all three tokens to the pool
// (omitted for brevity — same pattern as swap approval)
const { request } = await publicClient.simulateContract({
address: THREE_POOL,
abi: addLiquidityAbi,
functionName: "add_liquidity",
args: [amounts, minMintAmount],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("Add liquidity reverted");
Add Liquidity (Single-Sided)
Deposit only one token. The pool rebalances internally, charging a small imbalance fee.
// Deposit 5000 USDC only into 3pool
const singleSidedAmounts: readonly [bigint, bigint, bigint] = [
0n, // 0 DAI
5000_000000n, // 5000 USDC
0n, // 0 USDT
];
const expectedLp = await publicClient.readContract({
address: THREE_POOL,
abi: addLiquidityAbi,
functionName: "calc_token_amount",
args: [singleSidedAmounts, true],
});
// Wider slippage for single-sided (imbalance fee applies)
const minMintAmount = (expectedLp * 990n) / 1000n;
const { request } = await publicClient.simulateContract({
address: THREE_POOL,
abi: addLiquidityAbi,
functionName: "add_liquidity",
args: [singleSidedAmounts, minMintAmount],
account: account.address,
});
Remove Liquidity (Proportional)
Withdraw all tokens proportionally — no slippage from imbalance.
const removeLiquidityAbi = [
{
name: "remove_liquidity",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "_amount", type: "uint256" },
{ name: "min_amounts", type: "uint256[3]" },
],
outputs: [{ name: "", type: "uint256[3]" }],
},
] as const;
const lpAmount = 3000_000000000000000000n; // 3000 LP tokens
const { request } = await publicClient.simulateContract({
address: THREE_POOL,
abi: removeLiquidityAbi,
functionName: "remove_liquidity",
args: [lpAmount, [0n, 0n, 0n]], // SET MIN AMOUNTS IN PRODUCTION
account: account.address,
});
Remove Liquidity (Single Coin)
Withdraw everything as a single token. Higher slippage for large amounts or imbalanced pools.
const removeOneCoinAbi = [
{
name: "remove_liquidity_one_coin",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "_token_amount", type: "uint256" },
{ name: "i", type: "int128" },
{ name: "_min_amount", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "calc_withdraw_one_coin",
type: "function",
stateMutability: "view",
inputs: [
{ name: "_token_amount", type: "uint256" },
{ name: "i", type: "int128" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
const lpToWithdraw = 1000_000000000000000000n; // 1000 LP tokens
// Estimate how much USDC we get
const expectedUsdc = await publicClient.readContract({
address: THREE_POOL,
abi: removeOneCoinAbi,
functionName: "calc_withdraw_one_coin",
args: [lpToWithdraw, 1n], // index 1 = USDC
});
const minUsdc = (expectedUsdc * 995n) / 1000n;
const { request } = await publicClient.simulateContract({
address: THREE_POOL,
abi: removeOneCoinAbi,
functionName: "remove_liquidity_one_coin",
args: [lpToWithdraw, 1n, minUsdc],
account: account.address,
});
Remove Liquidity (Imbalanced)
Withdraw specific amounts of each token.
const removeImbalanceAbi = [
{
name: "remove_liquidity_imbalance",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "amounts", type: "uint256[3]" },
{ name: "max_burn_amount", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
// Withdraw exactly 500 DAI and 500 USDC, no USDT
const withdrawAmounts: readonly [bigint, bigint, bigint] = [
500_000000000000000000n, // 500 DAI
500_000000n, // 500 USDC
0n, // 0 USDT
];
// Estimate LP tokens burned
const estimatedBurn = await publicClient.readContract({
address: THREE_POOL,
abi: addLiquidityAbi,
functionName: "calc_token_amount",
args: [withdrawAmounts, false], // false = withdrawal
});
// Allow 1% more LP burn than estimated
const maxBurnAmount = (estimatedBurn * 1010n) / 1000n;
const { request } = await publicClient.simulateContract({
address: THREE_POOL,
abi: removeImbalanceAbi,
functionName: "remove_liquidity_imbalance",
args: [withdrawAmounts, maxBurnAmount],
account: account.address,
});
crvUSD (LLAMMA)
crvUSD is Curve's stablecoin. Loans are backed by collateral deposited into LLAMMA (Lending-Liquidating AMM Algorithm). Instead of instant liquidation at a threshold, LLAMMA gradually converts collateral to crvUSD as the collateral price drops through user-defined bands. If the price recovers, it converts back.
Create a crvUSD Loan
// crvUSD Controller for WETH collateral
const CRVUSD_WETH_CONTROLLER = "0xA920De414eA4Ab66b97dA1bFE9e6EcA7d4219635" as const;
const controllerAbi = [
{
name: "create_loan",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "collateral", type: "uint256" },
{ name: "debt", type: "uint256" },
{ name: "N", type: "uint256" },
],
outputs: [],
},
{
name: "max_borrowable",
type: "function",
stateMutability: "view",
inputs: [
{ name: "collateral", type: "uint256" },
{ name: "N", type: "uint256" },
],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "health",
type: "function",
stateMutability: "view",
inputs: [
{ name: "user", type: "address" },
],
outputs: [{ name: "", type: "int256" }],
},
{
name: "user_state",
type: "function",
stateMutability: "view",
inputs: [
{ name: "user", type: "address" },
],
outputs: [
{ name: "collateral", type: "uint256" },
{ name: "stablecoin", type: "uint256" },
{ name: "debt", type: "uint256" },
{ name: "N", type: "uint256" },
],
},
] as const;
const WETH = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" as const;
const collateralAmount = 10_000000000000000000n; // 10 WETH
// N = number of bands (4-50). More bands = wider liquidation range = safer but lower LTV
const numBands = 10n;
// Check max borrowable amount
const maxDebt = await publicClient.readContract({
address: CRVUSD_WETH_CONTROLLER,
abi: controllerAbi,
functionName: "max_borrowable",
args: [collateralAmount, numBands],
});
// Borrow 80% of max for safety margin
const debtAmount = (maxDebt * 80n) / 100n;
// Approve WETH to controller
const { request: approveReq } = await publicClient.simulateContract({
address: WETH,
abi: erc20Abi,
functionName: "approve",
args: [CRVUSD_WETH_CONTROLLER, collateralAmount],
account: account.address,
});
await walletClient.writeContract(approveReq);
// Create loan
const { request } = await publicClient.simulateContract({
address: CRVUSD_WETH_CONTROLLER,
abi: controllerAbi,
functionName: "create_loan",
args: [collateralAmount, debtAmount, numBands],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("Loan creation reverted");
Monitor Loan Health
// Health > 0 means position is safe
// Health approaching 0 means bands are being converted (soft liquidation)
// Health < 0 means position can be hard-liquidated
const health = await publicClient.readContract({
address: CRVUSD_WETH_CONTROLLER,
abi: controllerAbi,
functionName: "health",
args: [account.address],
});
// Health is returned in 1e18 precision
// health = 100e18 means 100% above liquidation
const healthPercent = Number(health) / 1e18;
if (healthPercent < 10) {
console.warn(`Low health: ${healthPercent.toFixed(2)}% — consider repaying or adding collateral`);
}
// Read full user state
const [collateral, stablecoin, debt, bands] = await publicClient.readContract({
address: CRVUSD_WETH_CONTROLLER,
abi: controllerAbi,
functionName: "user_state",
args: [account.address],
});
Repay crvUSD Loan
const repayAbi = [
{
name: "repay",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "_d_debt", type: "uint256" },
],
outputs: [],
},
] as const;
const CRVUSD = "0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E" as const;
const repayAmount = 5000_000000000000000000n; // repay 5000 crvUSD
// Approve crvUSD to controller
const { request: approveReq } = await publicClient.simulateContract({
address: CRVUSD,
abi: erc20Abi,
functionName: "approve",
args: [CRVUSD_WETH_CONTROLLER, repayAmount],
account: account.address,
});
await walletClient.writeContract(approveReq);
const { request } = await publicClient.simulateContract({
address: CRVUSD_WETH_CONTROLLER,
abi: repayAbi,
functionName: "repay",
args: [repayAmount],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("Repay reverted");
Gauge System
Curve directs CRV emissions to liquidity providers via gauges. Deposit your LP tokens into a gauge to earn CRV rewards. Boost your rewards up to 2.5x by holding veCRV.
Deposit LP Tokens into Gauge
// 3pool gauge
const THREE_POOL_GAUGE = "0xbFcF63294aD7105dEa65aA58F8AE5BE2D9d0952A" as const;
const THREE_POOL_LP = "0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490" as const;
const gaugeAbi = [
{
name: "deposit",
type: "function",
stateMutability: "nonpayable",
inputs: [{ name: "_value", type: "uint256" }],
outputs: [],
},
{
name: "withdraw",
type: "function",
stateMutability: "nonpayable",
inputs: [{ name: "_value", type: "uint256" }],
outputs: [],
},
{
name: "claimable_tokens",
type: "function",
stateMutability: "nonpayable",
inputs: [{ name: "addr", type: "address" }],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
const lpAmount = 1000_000000000000000000n; // 1000 LP tokens
// Approve gauge to spend LP tokens
const { request: approveReq } = await publicClient.simulateContract({
address: THREE_POOL_LP,
abi: erc20Abi,
functionName: "approve",
args: [THREE_POOL_GAUGE, lpAmount],
account: account.address,
});
await walletClient.writeContract(approveReq);
// Deposit into gauge
const { request } = await publicClient.simulateContract({
address: THREE_POOL_GAUGE,
abi: gaugeAbi,
functionName: "deposit",
args: [lpAmount],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("Gauge deposit reverted");
Claim CRV Rewards
const MINTER = "0xd061D61a4d941c39E5453435B6345Dc261C2fcE0" as const;
const minterAbi = [
{
name: "mint",
type: "function",
stateMutability: "nonpayable",
inputs: [{ name: "gauge_addr", type: "address" }],
outputs: [],
},
] as const;
// Check claimable amount
const claimable = await publicClient.simulateContract({
address: THREE_POOL_GAUGE,
abi: gaugeAbi,
functionName: "claimable_tokens",
args: [account.address],
});
// Mint (claim) CRV rewards
const { request } = await publicClient.simulateContract({
address: MINTER,
abi: minterAbi,
functionName: "mint",
args: [THREE_POOL_GAUGE],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("CRV claim reverted");
Gauge Voting
Lock CRV for veCRV
Lock CRV tokens to receive vote-escrowed CRV (veCRV). Longer lock = more voting power. Lock duration: 1 week to 4 years. Voting power decays linearly toward the unlock date.
const CRV = "0xD533a949740bb3306d119CC777fa900bA034cd52" as const;
const VECRV = "0x5f3b5DfEb7B28CDbD7FAba78963EE202a494e2A2" as const;
const veCrvAbi = [
{
name: "create_lock",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "_value", type: "uint256" },
{ name: "_unlock_time", type: "uint256" },
],
outputs: [],
},
{
name: "increase_amount",
type: "function",
stateMutability: "nonpayable",
inputs: [{ name: "_value", type: "uint256" }],
outputs: [],
},
{
name: "increase_unlock_time",
type: "function",
stateMutability: "nonpayable",
inputs: [{ name: "_unlock_time", type: "uint256" }],
outputs: [],
},
{
name: "withdraw",
type: "function",
stateMutability: "nonpayable",
inputs: [],
outputs: [],
},
] as const;
const lockAmount = 10000_000000000000000000n; // 10,000 CRV
// Lock for 4 years (max voting power)
// unlock_time must be rounded down to the nearest week (Thursday 00:00 UTC)
const WEEK = 7n * 24n * 60n * 60n;
const FOUR_YEARS = 4n * 365n * 24n * 60n * 60n;
const now = BigInt(Math.floor(Date.now() / 1000));
const unlockTime = ((now + FOUR_YEARS) / WEEK) * WEEK;
// Approve CRV to veCRV
const { request: approveReq } = await publicClient.simulateContract({
address: CRV,
abi: erc20Abi,
functionName: "approve",
args: [VECRV, lockAmount],
account: account.address,
});
await walletClient.writeContract(approveReq);
// Create lock
const { request } = await publicClient.simulateContract({
address: VECRV,
abi: veCrvAbi,
functionName: "create_lock",
args: [lockAmount, unlockTime],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("veCRV lock reverted");
Vote on Gauge Weights
Direct CRV emissions to specific gauges. Votes persist until changed. Each veCRV holder gets 10,000 vote points (100%) to allocate across gauges.
const GAUGE_CONTROLLER = "0x2F50D538606Fa9EDD2B11E2446BEb18C9D5846bB" as const;
const gaugeControllerAbi = [
{
name: "vote_for_gauge_weights",
type: "function",
stateMutability: "nonpayable",
inputs: [
{ name: "_gauge_addr", type: "address" },
{ name: "_user_weight", type: "uint256" },
],
outputs: [],
},
{
name: "gauge_relative_weight",
type: "function",
stateMutability: "view",
inputs: [{ name: "addr", type: "address" }],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "vote_user_power",
type: "function",
stateMutability: "view",
inputs: [{ name: "user", type: "address" }],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
// Allocate 50% of voting power to 3pool gauge
// Weight is in basis points: 5000 = 50%
const { request } = await publicClient.simulateContract({
address: GAUGE_CONTROLLER,
abi: gaugeControllerAbi,
functionName: "vote_for_gauge_weights",
args: [THREE_POOL_GAUGE, 5000n],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("Gauge vote reverted");
Check Voting Power Usage
// Returns total weight used out of 10000 (100%)
const usedPower = await publicClient.readContract({
address: GAUGE_CONTROLLER,
abi: gaugeControllerAbi,
functionName: "vote_user_power",
args: [account.address],
});
const remainingBps = 10000n - usedPower;
Pool Discovery
MetaRegistry
The MetaRegistry aggregates all pool registries (main, factory, crypto factory) into a single interface.
const META_REGISTRY = "0xF98B45FA17DE75FB1aD0e7aFD971b0ca00e379fC" as const;
const metaRegistryAbi = [
{
name: "find_pool_for_coins",
type: "function",
stateMutability: "view",
inputs: [
{ name: "_from", type: "address" },
{ name: "_to", type: "address" },
],
outputs: [{ name: "", type: "address" }],
},
{
name: "find_pools_for_coins",
type: "function",
stateMutability: "view",
inputs: [
{ name: "_from", type: "address" },
{ name: "_to", type: "address" },
],
outputs: [{ name: "", type: "address[]" }],
},
{
name: "get_coins",
type: "function",
stateMutability: "view",
inputs: [{ name: "_pool", type: "address" }],
outputs: [{ name: "", type: "address[8]" }],
},
{
name: "get_balances",
type: "function",
stateMutability: "view",
inputs: [{ name: "_pool", type: "address" }],
outputs: [{ name: "", type: "uint256[8]" }],
},
] as const;
// Find the best pool for USDC -> USDT
const pool = await publicClient.readContract({
address: META_REGISTRY,
abi: metaRegistryAbi,
functionName: "find_pool_for_coins",
args: [USDC, USDT],
});
// Find ALL pools for a pair
const pools = await publicClient.readContract({
address: META_REGISTRY,
abi: metaRegistryAbi,
functionName: "find_pools_for_coins",
args: [USDC, USDT],
});
Reading Pool State
Virtual Price
Virtual price represents the LP token value in terms of the underlying asset. It only increases over time from trading fees.
const poolStateAbi = [
{
name: "get_virtual_price",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "balances",
type: "function",
stateMutability: "view",
inputs: [{ name: "i", type: "uint256" }],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "A",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ name: "", type: "uint256" }],
},
{
name: "fee",
type: "function",
stateMutability: "view",
inputs: [],
outputs: [{ name: "", type: "uint256" }],
},
] as const;
// Virtual price is 1e18 precision
const virtualPrice = await publicClient.readContract({
address: THREE_POOL,
abi: poolStateAbi,
functionName: "get_virtual_price",
});
// LP token value in USD (assuming underlying = $1)
const lpValueUsd = Number(virtualPrice) / 1e18;
// Pool balances per coin index
const [daiBalance, usdcBalance, usdtBalance] = await Promise.all([
publicClient.readContract({ address: THREE_POOL, abi: poolStateAbi, functionName: "balances", args: [0n] }),
publicClient.readContract({ address: THREE_POOL, abi: poolStateAbi, functionName: "balances", args: [1n] }),
publicClient.readContract({ address: THREE_POOL, abi: poolStateAbi, functionName: "balances", args: [2n] }),
]);
// Amplification parameter
const amplification = await publicClient.readContract({
address: THREE_POOL,
abi: poolStateAbi,
functionName: "A",
});
// Fee in 1e10 precision (4000000 = 0.04%)
const poolFee = await publicClient.readContract({
address: THREE_POOL,
abi: poolStateAbi,
functionName: "fee",
});
const feePercent = Number(poolFee) / 1e10 * 100;
Contract Addresses
Last verified: February 2026
See resources/contract-addresses.md for the full address table.
| Contract | Ethereum |
|---|---|
| 3pool | 0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7 |
| stETH/ETH | 0xDC24316b9AE028F1497c275EB9192a3Ea0f67022 |
| Tricrypto2 | 0xD51a44d3FaE010294C616388b506AcdA1bfAAE46 |
| CRV Token | 0xD533a949740bb3306d119CC777fa900bA034cd52 |
| veCRV | 0x5f3b5DfEb7B28CDbD7FAba78963EE202a494e2A2 |
| Curve Router | 0xF0d4c12A5768D806021F80a262B4d39d26C58b8D |
| MetaRegistry | 0xF98B45FA17DE75FB1aD0e7aFD971b0ca00e379fC |
| crvUSD | 0xf939E0A03FB07F59A73314E73794Be0E57ac1b4E |
Error Handling
| Error | Cause | Fix |
|---|---|---|
Exchange resulted in fewer coins than expected | Output below min_dy | Increase slippage tolerance or re-quote |
Exceeds allowance | Pool not approved to spend token | Call approve() with sufficient amount |
Insufficient funds | Balance below swap amount | Check balanceOf before calling exchange |
| Empty revert (Vyper) | Wrong function signature or invalid index | Verify ABI matches pool type, check coin indices |
dev: exceeds allowance | Vyper dev error for allowance check | Approve token to the correct pool address |
Lock expired | Trying to increase amount on expired veCRV lock | Withdraw first, then create new lock |
Withdraw old tokens first | Creating veCRV lock when one already exists | Call withdraw() on expired lock first |
Security
Slippage Protection
Never set min_dy to 0 in production. Always quote with get_dy() first.
const expectedOut = await publicClient.readContract({
address: poolAddress,
abi: threePoolAbi,
functionName: "get_dy",
args: [i, j, amountIn],
});
// For stableswap: 10-50 bps is reasonable
const minDy = (expectedOut * 9990n) / 10000n; // 10 bps
// For cryptoswap/volatile: 50-200 bps
const minDyCrypto = (expectedOut * 9950n) / 10000n; // 50 bps
USDT Approval Reset
USDT requires setting allowance to 0 before setting a new non-zero value.
async function approveUsdt(spender: Address, amount: bigint): Promise<void> {
const currentAllowance = await publicClient.readContract({
address: USDT,
abi: erc20Abi,
functionName: "allowance",
args: [account.address, spender],
});
if (currentAllowance > 0n && currentAllowance < amount) {
const { request: resetReq } = await publicClient.simulateContract({
address: USDT,
abi: erc20Abi,
functionName: "approve",
args: [spender, 0n],
account: account.address,
});
const resetHash = await walletClient.writeContract(resetReq);
await publicClient.waitForTransactionReceipt({ hash: resetHash });
}
const { request } = await publicClient.simulateContract({
address: USDT,
abi: erc20Abi,
functionName: "approve",
args: [spender, amount],
account: account.address,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
if (receipt.status !== "success") throw new Error("USDT approval failed");
}
Front-Running Mitigation
- Use tight
min_dyon every swap (quote + slippage) - Use Flashbots Protect RPC for mainnet transactions
- Large liquidity operations should use proportional add/remove to minimize extractable value
- For large single-sided deposits, split into multiple smaller transactions
References
pyth
View full →Author
@0xinit
Stars
53
Repository
0xinit/cryptoskills
Pyth Network Development Guide
Pyth Network is a decentralized oracle providing real-time price feeds for cryptocurrencies, equities, forex, and commodities. This guide covers integrating Pyth price feeds into Solana applications.
Overview
Pyth Network provides:
- Real-Time Price Feeds - 400ms update frequency with pull oracle model
- Confidence Intervals - Statistical uncertainty bounds for each price
- EMA Prices - Exponential moving average prices (~1 hour window)
- Multi-Asset Support - Crypto, equities, FX, commodities, indices
- On-Chain Integration - CPI for Solana programs
- Off-Chain Integration - HTTP and WebSocket APIs via Hermes
Program IDs
| Program | Address | Description |
|---|---|---|
| Solana Receiver | rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ | Posts price updates to Solana |
| Price Feed | pythWSnswVUd12oZpeFP8e9CVaEqJg25g1Vtc2biRsT | Stores price feed data |
Deployed on: Solana Mainnet, Devnet, Eclipse Mainnet/Testnet, Sonic networks
Popular Price Feed IDs
| Asset | Hex Feed ID |
|---|---|
| BTC/USD | 0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43 |
| ETH/USD | 0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace |
| SOL/USD | 0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d |
| USDC/USD | 0xeaa020c61cc479712813461ce153894a96a6c00b21ed0cfc2798d1f9a9e9c94a |
| USDT/USD | 0x2b89b9dc8fdf9f34709a5b106b472f0f39bb6ca9ce04b0fd7f2e971688e2e53b |
Full list: https://pyth.network/developers/price-feed-ids
Quick Start
Installation
# TypeScript/JavaScript
npm install @pythnetwork/hermes-client @pythnetwork/pyth-solana-receiver
# Rust (add to Cargo.toml)
# pyth-solana-receiver-sdk = "0.3.0"
Fetch Price (Off-Chain)
import { HermesClient } from "@pythnetwork/hermes-client";
const client = new HermesClient("https://hermes.pyth.network");
const priceIds = [
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD
];
const priceUpdates = await client.getLatestPriceUpdates(priceIds);
for (const update of priceUpdates.parsed) {
const price = update.price;
const displayPrice = Number(price.price) * Math.pow(10, price.expo);
console.log(`Price: $${displayPrice.toFixed(2)}`);
console.log(`Confidence: ±${Number(price.conf) * Math.pow(10, price.expo)}`);
}
Use Price On-Chain (Rust/Anchor)
use anchor_lang::prelude::*;
use pyth_solana_receiver_sdk::price_update::PriceUpdateV2;
#[derive(Accounts)]
pub struct UsePrice<'info> {
pub price_update: Account<'info, PriceUpdateV2>,
}
pub fn use_price(ctx: Context<UsePrice>) -> Result<()> {
let price_update = &ctx.accounts.price_update;
let clock = Clock::get()?;
// Get price no older than 60 seconds
let price = price_update.get_price_no_older_than(
&clock,
60, // max age in seconds
)?;
msg!("Price: {} × 10^{}", price.price, price.exponent);
msg!("Confidence: ±{}", price.conf);
Ok(())
}
Core Concepts
Price Structure
Each Pyth price contains:
| Field | Type | Description |
|---|---|---|
price | i64 | Price value in fixed-point format |
conf | u64 | Confidence interval (standard deviation) |
expo | i32 | Exponent for scaling (e.g., -8 means divide by 10^8) |
publish_time | i64 | Unix timestamp of price |
Converting to display price:
const displayPrice = price * Math.pow(10, expo);
// Example: price=19405100, expo=-2 → $194,051.00
Confidence Intervals
Confidence intervals represent the uncertainty in the reported price:
// Price is $50,000 ± $50 means:
// - 68% chance true price is between $49,950 - $50,050
// - Use confidence for risk management
const price = 50000;
const confidence = 50;
// Safe lower bound (conservative)
const safeLowerBound = price - confidence;
// Safe upper bound (conservative)
const safeUpperBound = price + confidence;
Best Practice: Reject prices with confidence > 2% of price:
const maxConfidenceRatio = 0.02; // 2%
const confidenceRatio = confidence / Math.abs(price);
if (confidenceRatio > maxConfidenceRatio) {
throw new Error("Price confidence too wide");
}
EMA Prices
Exponential Moving Average prices smooth out short-term volatility:
- ~1 hour averaging window (5921 Solana slots)
- Weighted by inverse confidence (tight confidence = more weight)
- Good for: liquidations, collateral valuation
- Available as
ema_priceandema_conf
// Use EMA for less volatile applications
const emaPrice = priceUpdate.emaPrice;
const emaConf = priceUpdate.emaConf;
Off-Chain Integration
Hermes Client
Hermes is the recommended way to fetch Pyth prices off-chain.
Public Endpoint: https://hermes.pyth.network
For production, get a dedicated endpoint from a Pyth data provider.
Fetching Latest Prices
import { HermesClient } from "@pythnetwork/hermes-client";
const client = new HermesClient("https://hermes.pyth.network");
// Single price
const btcPrice = await client.getLatestPriceUpdates([
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"
]);
// Multiple prices in one request
const prices = await client.getLatestPriceUpdates([
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH
"0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d", // SOL
]);
Streaming Real-Time Updates
import { HermesClient } from "@pythnetwork/hermes-client";
const client = new HermesClient("https://hermes.pyth.network");
const priceIds = [
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"
];
// Subscribe to real-time updates via SSE
const eventSource = await client.getPriceUpdatesStream(priceIds, {
parsed: true,
});
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Price update:", data);
};
eventSource.onerror = (error) => {
console.error("Stream error:", error);
eventSource.close();
};
// Close when done
// eventSource.close();
Posting Prices to Solana
import { PythSolanaReceiver } from "@pythnetwork/pyth-solana-receiver";
import { HermesClient } from "@pythnetwork/hermes-client";
import { Connection, Keypair } from "@solana/web3.js";
const connection = new Connection("https://api.mainnet-beta.solana.com");
const wallet = Keypair.fromSecretKey(/* your key */);
const hermesClient = new HermesClient("https://hermes.pyth.network");
const pythReceiver = new PythSolanaReceiver({ connection, wallet });
// Fetch price update data
const priceUpdateData = await hermesClient.getLatestPriceUpdates([
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"
]);
// Build transaction to post price
const transactionBuilder = pythReceiver.newTransactionBuilder();
await transactionBuilder.addPostPriceUpdates(priceUpdateData.binary.data);
// Add your program instruction that uses the price
// transactionBuilder.addInstruction(yourInstruction);
// Send transaction
const transactions = await transactionBuilder.buildVersionedTransactions({
computeUnitPriceMicroLamports: 50000,
});
for (const tx of transactions) {
const sig = await connection.sendTransaction(tx);
console.log("Transaction:", sig);
}
On-Chain Integration (Rust)
Setup
Add to Cargo.toml:
[dependencies]
pyth-solana-receiver-sdk = "0.3.0"
anchor-lang = "0.30.1"
Reading Price in Anchor Program
use anchor_lang::prelude::*;
use pyth_solana_receiver_sdk::price_update::{PriceUpdateV2, get_feed_id_from_hex};
declare_id!("YourProgramId...");
// BTC/USD price feed ID
const BTC_USD_FEED_ID: &str = "0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43";
#[program]
pub mod my_program {
use super::*;
pub fn check_price(ctx: Context<CheckPrice>) -> Result<()> {
let price_update = &ctx.accounts.price_update;
let clock = Clock::get()?;
// Verify this is the correct feed
let feed_id = get_feed_id_from_hex(BTC_USD_FEED_ID)?;
// Get price no older than 60 seconds
let price = price_update.get_price_no_older_than_with_custom_verification(
&clock,
60,
&feed_id,
ctx.accounts.price_update.to_account_info().owner,
)?;
msg!("BTC/USD Price: {} × 10^{}", price.price, price.exponent);
msg!("Confidence: ±{}", price.conf);
Ok(())
}
}
#[derive(Accounts)]
pub struct CheckPrice<'info> {
#[account(
constraint = price_update.to_account_info().owner == &pyth_solana_receiver_sdk::ID
)]
pub price_update: Account<'info, PriceUpdateV2>,
}
Using Price for Calculations
pub fn swap_with_oracle(
ctx: Context<SwapWithOracle>,
amount_in: u64,
) -> Result<()> {
let price_update = &ctx.accounts.price_update;
let clock = Clock::get()?;
// Get price with staleness check
let price = price_update.get_price_no_older_than(&clock, 30)?;
// Validate confidence (max 1% of price)
let conf_ratio = (price.conf as u128 * 10000) / (price.price.unsigned_abs() as u128);
require!(conf_ratio <= 100, ErrorCode::ConfidenceTooWide);
// Convert price to usable format
// price.price is in fixed-point with price.exponent
let price_scaled = if price.exponent >= 0 {
(price.price as u128) * 10_u128.pow(price.exponent as u32)
} else {
(price.price as u128) / 10_u128.pow((-price.exponent) as u32)
};
// Calculate output amount using oracle price
let amount_out = (amount_in as u128)
.checked_mul(price_scaled)
.ok_or(ErrorCode::MathOverflow)?
/ 1_000_000; // Adjust for decimals
msg!("Swap {} -> {} using price {}", amount_in, amount_out, price_scaled);
Ok(())
}
#[error_code]
pub enum ErrorCode {
#[msg("Price confidence interval too wide")]
ConfidenceTooWide,
#[msg("Math overflow")]
MathOverflow,
}
Multiple Price Feeds
#[derive(Accounts)]
pub struct Liquidation<'info> {
#[account(
constraint = collateral_price.to_account_info().owner == &pyth_solana_receiver_sdk::ID
)]
pub collateral_price: Account<'info, PriceUpdateV2>,
#[account(
constraint = debt_price.to_account_info().owner == &pyth_solana_receiver_sdk::ID
)]
pub debt_price: Account<'info, PriceUpdateV2>,
}
pub fn check_liquidation(ctx: Context<Liquidation>) -> Result<bool> {
let clock = Clock::get()?;
let collateral = ctx.accounts.collateral_price
.get_price_no_older_than(&clock, 60)?;
let debt = ctx.accounts.debt_price
.get_price_no_older_than(&clock, 60)?;
// Normalize to same exponent for comparison
let collateral_value = normalize_price(collateral.price, collateral.exponent);
let debt_value = normalize_price(debt.price, debt.exponent);
// Check if undercollateralized
let is_liquidatable = collateral_value < debt_value * 150 / 100; // 150% ratio
Ok(is_liquidatable)
}
fn normalize_price(price: i64, expo: i32) -> i128 {
let target_expo = -8; // Normalize to 8 decimals
let adjustment = expo - target_expo;
if adjustment >= 0 {
(price as i128) * 10_i128.pow(adjustment as u32)
} else {
(price as i128) / 10_i128.pow((-adjustment) as u32)
}
}
Best Practices
1. Always Check Staleness
// Don't use old prices - set appropriate max age
let max_age_seconds = 60;
let price = price_update.get_price_no_older_than(&clock, max_age_seconds)?;
2. Validate Confidence Intervals
// Reject prices with wide confidence (high uncertainty)
const MAX_CONF_BPS: u64 = 200; // 2%
let conf_bps = (price.conf as u128 * 10000) / (price.price.unsigned_abs() as u128);
require!(conf_bps <= MAX_CONF_BPS as u128, ErrorCode::ConfidenceTooWide);
3. Verify Account Ownership
// Always verify the price account is owned by Pyth
#[account(
constraint = price_update.to_account_info().owner == &pyth_solana_receiver_sdk::ID
)]
pub price_update: Account<'info, PriceUpdateV2>,
4. Use EMA for Sensitive Operations
// For liquidations, use EMA to avoid manipulation
let ema_price = price_update.get_ema_price_no_older_than(&clock, 60)?;
5. Handle Price Unavailability
try {
const price = await client.getLatestPriceUpdates([feedId]);
// Use price
} catch (error) {
// Fallback behavior or reject transaction
console.error("Price unavailable:", error);
}
6. Consider Frontrunning
- Adversaries may see price updates before your transaction
- Don't design logic that races against price updates
- Use appropriate slippage tolerances
Price Feed Types
Fixed Price Feed Accounts
- Maintained continuously by Pyth
- Fixed address per feed
- Always has most recent price
- Shared by all users (potential congestion)
Ephemeral Price Update Accounts
- Created per transaction
- Can specify shard ID for parallelization
- Rent can be recovered after use
- Better for high-throughput applications
// Use shard ID to avoid congestion
const transactionBuilder = pythReceiver.newTransactionBuilder({
shardId: Math.floor(Math.random() * 65536), // Random shard
});
Resources
Official Documentation
GitHub Repositories
NPM Packages
Rust Crates
Skill Structure
pyth/
├── SKILL.md # This file
├── resources/
│ ├── program-addresses.md # All program IDs and feed IDs
│ └── api-reference.md # SDK API reference
├── examples/
│ ├── price-feeds/
│ │ ├── fetch-price.ts # Basic price fetching
│ │ └── multiple-prices.ts # Multiple price feeds
│ ├── on-chain/
│ │ ├── anchor-integration.rs # Anchor program example
│ │ └── price-validation.rs # Price validation patterns
│ └── streaming/
│ └── real-time-updates.ts # WebSocket streaming
├── templates/
│ ├── pyth-client.ts # TypeScript client template
│ └── anchor-oracle.rs # Anchor program template
└── docs/
└── troubleshooting.md # Common issues and solutions
Pyth on EVM Chains
This skill covers Pyth integration for Solana applications using Anchor CPI. For EVM chain integration (Ethereum, Arbitrum, Base, Optimism, Polygon, and 50+ other chains), see the pyth-evm skill.
Key differences between Pyth Solana and Pyth EVM:
| Aspect | Pyth Solana (this skill) | Pyth EVM (pyth-evm skill) |
|---|---|---|
| Contract interface | Anchor CPI to Pyth program | Solidity IPyth interface |
| Price update | Pull from Pyth accumulator account | Submit bytes[] via updatePriceFeeds |
| Contract address | Single Pyth program on Solana | Varies per EVM chain |
| Gas/compute | Compute units | ~120-150K gas per feed update |
| SDK | @pythnetwork/pyth-solana-receiver | @pythnetwork/hermes-client v3.1.0 |
Price feed IDs (bytes32) are the same across all chains — a BTC/USD feed ID works on both Solana and Ethereum.
Related Skills
pyth-evm— Pyth oracle integration for EVM chains (Solidity + TypeScript)chainlink— Push oracle alternative on EVM chainsredstone— Another pull oracle for EVM chains