celo-poll/lib/oracle/dexscreener.ts
2025-12-08 16:31:30 +03:00

180 lines
4.3 KiB
TypeScript

/**
* DexScreener Price Oracle
* Uses DexScreener API to get real-time DEX prices
* https://docs.dexscreener.com/api/reference
*/
import axios from 'axios'
const DEXSCREENER_API_BASE = 'https://api.dexscreener.com/latest'
export interface DexScreenerConfig {
chain: string // e.g., 'celo', 'ethereum', 'bsc'
pairAddress: string
targetPrice?: number
comparisonType?: 'above' | 'below' | 'between'
minPrice?: number
maxPrice?: number
}
export interface DexScreenerPairData {
chainId: string
dexId: string
pairAddress: string
baseToken: {
address: string
name: string
symbol: string
}
quoteToken: {
address: string
name: string
symbol: string
}
priceNative: string
priceUsd: string
volume24h: number
liquidity: {
usd: number
base: number
quote: number
}
fdv: number
priceChange: {
h24: number
h6: number
h1: number
m5: number
}
}
/**
* Get pair data by address
*/
export async function getDexScreenerPair(
pairAddress: string
): Promise<DexScreenerPairData | null> {
try {
const response = await axios.get(
`${DEXSCREENER_API_BASE}/dex/pairs/${pairAddress}`
)
if (response.data.pair) {
return response.data.pair as DexScreenerPairData
}
return null
} catch (error) {
console.error(`Failed to get DexScreener pair data:`, error)
return null
}
}
/**
* Search for pairs by token address
*/
export async function searchDexScreenerPairs(
tokenAddress: string
): Promise<DexScreenerPairData[]> {
try {
const response = await axios.get(
`${DEXSCREENER_API_BASE}/dex/tokens/${tokenAddress}`
)
return (response.data.pairs || []) as DexScreenerPairData[]
} catch (error) {
console.error(`Failed to search DexScreener pairs:`, error)
return []
}
}
/**
* Get top pairs for a specific chain
*/
export async function getDexScreenerTopPairs(
chain: string,
limit: number = 10
): Promise<DexScreenerPairData[]> {
try {
const response = await axios.get(
`${DEXSCREENER_API_BASE}/dex/pairs/${chain}`
)
return (response.data.pairs || []).slice(0, limit) as DexScreenerPairData[]
} catch (error) {
console.error(`Failed to get DexScreener top pairs:`, error)
return []
}
}
/**
* Verify poll result using DexScreener data
*/
export async function verifyDexScreenerPoll(
config: DexScreenerConfig,
pollEndTime: number
): Promise<{ correct: boolean; optionIndex: number; proof: any }> {
try {
const pairData = await getDexScreenerPair(config.pairAddress)
if (!pairData) {
throw new Error('Failed to fetch pair data from DexScreener')
}
const currentPrice = parseFloat(pairData.priceUsd)
let correctOptionIndex = -1
// Determine correct option based on comparison type
if (config.comparisonType === 'above' && config.targetPrice) {
correctOptionIndex = currentPrice > config.targetPrice ? 0 : 1
} else if (config.comparisonType === 'below' && config.targetPrice) {
correctOptionIndex = currentPrice < config.targetPrice ? 0 : 1
} else if (
config.comparisonType === 'between' &&
config.minPrice &&
config.maxPrice
) {
if (currentPrice >= config.minPrice && currentPrice <= config.maxPrice) {
correctOptionIndex = 0
} else if (currentPrice < config.minPrice) {
correctOptionIndex = 1
} else {
correctOptionIndex = 2
}
}
return {
correct: correctOptionIndex >= 0,
optionIndex: correctOptionIndex,
proof: {
source: 'dexscreener',
price: currentPrice,
priceChange24h: pairData.priceChange.h24,
volume24h: pairData.volume24h,
liquidity: pairData.liquidity.usd,
timestamp: Date.now(),
pairAddress: config.pairAddress,
baseToken: pairData.baseToken.symbol,
quoteToken: pairData.quoteToken.symbol,
},
}
} catch (error) {
console.error('DexScreener verification failed:', error)
throw error
}
}
/**
* Popular pair addresses for quick reference
*/
export const POPULAR_PAIRS = {
celo: {
'CELO/cUSD': '0x', // Add actual pair address
'CELO/USDC': '0x',
},
ethereum: {
'ETH/USDC': '0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640', // Uniswap V3
'WBTC/ETH': '0xcbcdf9626bc03e24f779434178a73a0b4bad62ed', // Uniswap V3
},
}