2025-12-08 16:31:30 +03:00

160 lines
4.2 KiB
TypeScript

/**
* Curve Finance Price Oracle
* Uses Curve's price feed API to get token prices
* https://prices.curve.finance/feeds-docs
*/
import axios from 'axios'
const CURVE_API_BASE = 'https://prices.curve.finance'
export interface CurvePriceConfig {
chain: string // e.g., 'ethereum', 'celo', 'polygon'
tokenAddress: string
targetPrice?: number
comparisonType?: 'above' | 'below' | 'between'
minPrice?: number
maxPrice?: number
}
export interface CurvePriceData {
price: number
timestamp: number
source: string
}
/**
* Get supported chains from Curve
*/
export async function getCurveSupportedChains(): Promise<string[]> {
try {
const response = await axios.get(`${CURVE_API_BASE}/v1/chains/`)
return response.data.data.map((chain: any) => chain.name)
} catch (error) {
console.error('Failed to get Curve supported chains:', error)
return []
}
}
/**
* Get current USD price for a token on a chain
*/
export async function getCurveTokenPrice(
chain: string,
tokenAddress: string
): Promise<CurvePriceData | null> {
try {
const response = await axios.get(
`${CURVE_API_BASE}/v1/usd_price/${chain}/${tokenAddress}`
)
return {
price: response.data.data.usd_price,
timestamp: Date.now(),
source: 'curve',
}
} catch (error) {
console.error(`Failed to get Curve price for ${tokenAddress}:`, error)
return null
}
}
/**
* Get price history for a token
*/
export async function getCurvePriceHistory(
chain: string,
tokenAddress: string,
fromTimestamp?: number,
toTimestamp?: number
): Promise<any[]> {
try {
const params = new URLSearchParams()
if (fromTimestamp) params.append('start', fromTimestamp.toString())
if (toTimestamp) params.append('end', toTimestamp.toString())
const response = await axios.get(
`${CURVE_API_BASE}/v1/usd_price/${chain}/${tokenAddress}/history?${params}`
)
return response.data.data
} catch (error) {
console.error(`Failed to get Curve price history:`, error)
return []
}
}
/**
* Verify poll result using Curve price feed
*/
export async function verifyCurvePoll(
config: CurvePriceConfig,
pollEndTime: number
): Promise<{ correct: boolean; optionIndex: number; proof: any }> {
try {
// Get price at or near poll end time
const currentPrice = await getCurveTokenPrice(config.chain, config.tokenAddress)
if (!currentPrice) {
throw new Error('Failed to fetch price from Curve')
}
let correctOptionIndex = -1
// Determine correct option based on comparison type
if (config.comparisonType === 'above' && config.targetPrice) {
correctOptionIndex = currentPrice.price > config.targetPrice ? 0 : 1
} else if (config.comparisonType === 'below' && config.targetPrice) {
correctOptionIndex = currentPrice.price < config.targetPrice ? 0 : 1
} else if (
config.comparisonType === 'between' &&
config.minPrice &&
config.maxPrice
) {
if (
currentPrice.price >= config.minPrice &&
currentPrice.price <= config.maxPrice
) {
correctOptionIndex = 0
} else if (currentPrice.price < config.minPrice) {
correctOptionIndex = 1
} else {
correctOptionIndex = 2
}
}
return {
correct: correctOptionIndex >= 0,
optionIndex: correctOptionIndex,
proof: {
source: 'curve',
price: currentPrice.price,
timestamp: currentPrice.timestamp,
chain: config.chain,
tokenAddress: config.tokenAddress,
},
}
} catch (error) {
console.error('Curve verification failed:', error)
throw error
}
}
/**
* Common token addresses on different chains
*/
export const CURVE_TOKENS = {
ethereum: {
WETH: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
USDC: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
USDT: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
DAI: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
WBTC: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
},
celo: {
CELO: '0x471EcE3750Da237f93B8E339c536989b8978a438',
cUSD: '0x765DE816845861e75A25fCA122bb6898B8B1282a',
cEUR: '0xD8763CBa276a3738E6DE85b4b3bF5FDed6D6cA73',
},
}