317 lines
11 KiB
TypeScript
317 lines
11 KiB
TypeScript
/**
|
||
* API Testing Script
|
||
* Tests all oracle integrations to verify they work
|
||
*
|
||
* Usage: npx tsx scripts/test-apis.ts
|
||
*/
|
||
|
||
import { getChainlinkPrice, CHAINLINK_FEEDS } from '../lib/oracle/chainlink'
|
||
import { getCurveTokenPrice, getCurveSupportedChains } from '../lib/oracle/curve'
|
||
import { getDexScreenerPair, searchDexScreenerPairs } from '../lib/oracle/dexscreener'
|
||
import { getESPNScoreboard, getESPNGame, ESPN_SPORTS } from '../lib/oracle/espn'
|
||
import { fetchExternalAPI, testAPIConfiguration } from '../lib/oracle/external-api'
|
||
|
||
console.log('๐งช Testing PollCelo Oracle APIs...\n')
|
||
|
||
async function testChainlink() {
|
||
console.log('โ'.repeat(80))
|
||
console.log('1๏ธโฃ CHAINLINK PRICE FEEDS')
|
||
console.log('โ'.repeat(80))
|
||
|
||
try {
|
||
// Test BTC/USD
|
||
console.log('\n๐ Testing BTC/USD feed...')
|
||
const btcPrice = await getChainlinkPrice(CHAINLINK_FEEDS['BTC/USD'])
|
||
|
||
if (btcPrice) {
|
||
console.log(`โ
BTC/USD: $${btcPrice.price.toLocaleString()}`)
|
||
console.log(` Decimals: ${btcPrice.decimals}`)
|
||
console.log(` Updated: ${new Date(btcPrice.updatedAt * 1000).toLocaleString()}`)
|
||
} else {
|
||
console.log('โ Failed to get BTC price')
|
||
}
|
||
|
||
// Test ETH/USD
|
||
console.log('\n๐ Testing ETH/USD feed...')
|
||
const ethPrice = await getChainlinkPrice(CHAINLINK_FEEDS['ETH/USD'])
|
||
|
||
if (ethPrice) {
|
||
console.log(`โ
ETH/USD: $${ethPrice.price.toLocaleString()}`)
|
||
console.log(` Decimals: ${ethPrice.decimals}`)
|
||
console.log(` Updated: ${new Date(ethPrice.updatedAt * 1000).toLocaleString()}`)
|
||
} else {
|
||
console.log('โ Failed to get ETH price')
|
||
}
|
||
|
||
// Test CELO/USD
|
||
console.log('\n๐ Testing CELO/USD feed...')
|
||
const celoPrice = await getChainlinkPrice(CHAINLINK_FEEDS['CELO/USD'])
|
||
|
||
if (celoPrice) {
|
||
console.log(`โ
CELO/USD: $${celoPrice.price.toFixed(4)}`)
|
||
console.log(` Decimals: ${celoPrice.decimals}`)
|
||
console.log(` Updated: ${new Date(celoPrice.updatedAt * 1000).toLocaleString()}`)
|
||
} else {
|
||
console.log('โ Failed to get CELO price')
|
||
}
|
||
|
||
console.log('\nโ
Chainlink tests passed!')
|
||
return true
|
||
} catch (error: any) {
|
||
console.error('\nโ Chainlink test failed:', error.message)
|
||
return false
|
||
}
|
||
}
|
||
|
||
async function testCurve() {
|
||
console.log('\nโ'.repeat(80))
|
||
console.log('2๏ธโฃ CURVE FINANCE API')
|
||
console.log('โ'.repeat(80))
|
||
|
||
try {
|
||
// Test supported chains
|
||
console.log('\n๐ Fetching supported chains...')
|
||
const chains = await getCurveSupportedChains()
|
||
|
||
if (chains.length > 0) {
|
||
console.log(`โ
Found ${chains.length} supported chains`)
|
||
console.log(` Chains: ${chains.slice(0, 5).join(', ')}${chains.length > 5 ? '...' : ''}`)
|
||
} else {
|
||
console.log('โ ๏ธ No chains returned (API might be down)')
|
||
}
|
||
|
||
// Test token price on Celo
|
||
console.log('\n๐ Testing cUSD price on Celo...')
|
||
const cUSDAddress = '0x765DE816845861e75A25fCA122bb6898B8B1282a'
|
||
const price = await getCurveTokenPrice('celo', cUSDAddress)
|
||
|
||
if (price) {
|
||
console.log(`โ
cUSD price: $${price.price.toFixed(4)}`)
|
||
console.log(` Source: ${price.source}`)
|
||
console.log(` Timestamp: ${new Date(price.timestamp).toLocaleString()}`)
|
||
} else {
|
||
console.log('โ ๏ธ Price not available (might not be indexed by Curve)')
|
||
}
|
||
|
||
console.log('\nโ
Curve tests completed!')
|
||
return true
|
||
} catch (error: any) {
|
||
console.error('\nโ Curve test failed:', error.message)
|
||
return false
|
||
}
|
||
}
|
||
|
||
async function testDexScreener() {
|
||
console.log('\nโ'.repeat(80))
|
||
console.log('3๏ธโฃ DEXSCREENER API')
|
||
console.log('โ'.repeat(80))
|
||
|
||
try {
|
||
// Test popular pair (ETH/USDC on Uniswap V3)
|
||
console.log('\n๐ Testing ETH/USDC pair on Uniswap V3...')
|
||
const ethUsdcPair = '0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640'
|
||
const pair = await getDexScreenerPair(ethUsdcPair)
|
||
|
||
if (pair) {
|
||
console.log(`โ
Pair: ${pair.baseToken.symbol}/${pair.quoteToken.symbol}`)
|
||
console.log(` Price: $${parseFloat(pair.priceUsd).toFixed(2)}`)
|
||
console.log(` Volume 24h: $${pair.volume24h.toLocaleString()}`)
|
||
console.log(` Liquidity: $${pair.liquidity.usd.toLocaleString()}`)
|
||
console.log(` Price Change 24h: ${pair.priceChange.h24.toFixed(2)}%`)
|
||
console.log(` Chain: ${pair.chainId}`)
|
||
console.log(` DEX: ${pair.dexId}`)
|
||
} else {
|
||
console.log('โ Failed to get pair data')
|
||
}
|
||
|
||
// Test search by token
|
||
console.log('\n๐ Searching for USDC pairs...')
|
||
const usdcAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
|
||
const pairs = await searchDexScreenerPairs(usdcAddress)
|
||
|
||
if (pairs.length > 0) {
|
||
console.log(`โ
Found ${pairs.length} pairs with USDC`)
|
||
console.log(` Top 3 pairs:`)
|
||
pairs.slice(0, 3).forEach((p, i) => {
|
||
console.log(` ${i + 1}. ${p.baseToken.symbol}/${p.quoteToken.symbol} on ${p.dexId}`)
|
||
})
|
||
} else {
|
||
console.log('โ ๏ธ No pairs found')
|
||
}
|
||
|
||
console.log('\nโ
DexScreener tests passed!')
|
||
return true
|
||
} catch (error: any) {
|
||
console.error('\nโ DexScreener test failed:', error.message)
|
||
return false
|
||
}
|
||
}
|
||
|
||
async function testESPN() {
|
||
console.log('\nโ'.repeat(80))
|
||
console.log('4๏ธโฃ ESPN SPORTS API')
|
||
console.log('โ'.repeat(80))
|
||
|
||
try {
|
||
// Test NFL scoreboard
|
||
console.log('\n๐ Testing NFL scoreboard...')
|
||
const nflGames = await getESPNScoreboard('football/nfl')
|
||
|
||
if (nflGames.length > 0) {
|
||
console.log(`โ
Found ${nflGames.length} NFL games`)
|
||
console.log(` Recent games:`)
|
||
nflGames.slice(0, 3).forEach((game, i) => {
|
||
const comp = game.competitions[0]
|
||
const home = comp.competitors.find(c => c.homeAway === 'home')
|
||
const away = comp.competitors.find(c => c.homeAway === 'away')
|
||
console.log(` ${i + 1}. ${away?.team.displayName} @ ${home?.team.displayName}`)
|
||
console.log(` Score: ${away?.score} - ${home?.score}`)
|
||
console.log(` Status: ${game.status.type.description}`)
|
||
})
|
||
} else {
|
||
console.log('โ ๏ธ No games found (might be off-season)')
|
||
}
|
||
|
||
// Test NBA scoreboard
|
||
console.log('\n๐ Testing NBA scoreboard...')
|
||
const nbaGames = await getESPNScoreboard('basketball/nba')
|
||
|
||
if (nbaGames.length > 0) {
|
||
console.log(`โ
Found ${nbaGames.length} NBA games`)
|
||
console.log(` Recent games:`)
|
||
nbaGames.slice(0, 3).forEach((game, i) => {
|
||
const comp = game.competitions[0]
|
||
const home = comp.competitors.find(c => c.homeAway === 'home')
|
||
const away = comp.competitors.find(c => c.homeAway === 'away')
|
||
console.log(` ${i + 1}. ${away?.team.displayName} @ ${home?.team.displayName}`)
|
||
console.log(` Score: ${away?.score} - ${home?.score}`)
|
||
})
|
||
} else {
|
||
console.log('โ ๏ธ No games found')
|
||
}
|
||
|
||
// Test Soccer (EPL)
|
||
console.log('\n๐ Testing Soccer (EPL) scoreboard...')
|
||
const soccerGames = await getESPNScoreboard('soccer', undefined, 'eng.1')
|
||
|
||
if (soccerGames.length > 0) {
|
||
console.log(`โ
Found ${soccerGames.length} EPL games`)
|
||
console.log(` Recent matches:`)
|
||
soccerGames.slice(0, 3).forEach((game, i) => {
|
||
const comp = game.competitions[0]
|
||
const home = comp.competitors.find(c => c.homeAway === 'home')
|
||
const away = comp.competitors.find(c => c.homeAway === 'away')
|
||
console.log(` ${i + 1}. ${home?.team.displayName} vs ${away?.team.displayName}`)
|
||
console.log(` Score: ${home?.score} - ${away?.score}`)
|
||
})
|
||
} else {
|
||
console.log('โ ๏ธ No games found')
|
||
}
|
||
|
||
console.log('\nโ
ESPN tests completed!')
|
||
return true
|
||
} catch (error: any) {
|
||
console.error('\nโ ESPN test failed:', error.message)
|
||
return false
|
||
}
|
||
}
|
||
|
||
async function testExternalAPIs() {
|
||
console.log('\nโ'.repeat(80))
|
||
console.log('5๏ธโฃ EXTERNAL APIs')
|
||
console.log('โ'.repeat(80))
|
||
|
||
try {
|
||
// Test CoinGecko (no key required)
|
||
console.log('\n๐ Testing CoinGecko API...')
|
||
const coinGeckoConfig = {
|
||
apiEndpoint: 'https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd',
|
||
jsonPath: 'bitcoin.usd',
|
||
apiMethod: 'GET' as const,
|
||
}
|
||
|
||
const coinGeckoData = await fetchExternalAPI(coinGeckoConfig)
|
||
console.log(`โ
CoinGecko response:`)
|
||
console.log(` Bitcoin: $${coinGeckoData.bitcoin.usd.toLocaleString()}`)
|
||
console.log(` Ethereum: $${coinGeckoData.ethereum.usd.toLocaleString()}`)
|
||
|
||
// Test Exchange Rate API (no key required)
|
||
console.log('\n๐ Testing Exchange Rate API (USD/TRY)...')
|
||
const forexConfig = {
|
||
apiEndpoint: 'https://api.exchangerate.host/latest?base=USD&symbols=TRY',
|
||
jsonPath: 'rates.TRY',
|
||
apiMethod: 'GET' as const,
|
||
}
|
||
|
||
const forexData = await fetchExternalAPI(forexConfig)
|
||
console.log(`โ
Exchange rate:`)
|
||
console.log(` 1 USD = ${forexData.rates.TRY.toFixed(2)} TRY`)
|
||
|
||
// Test weather API (requires key, will fail gracefully)
|
||
console.log('\n๐ Testing Weather API (public endpoint)...')
|
||
const weatherConfig = {
|
||
apiEndpoint: 'https://wttr.in/Istanbul?format=j1',
|
||
jsonPath: 'current_condition.0.temp_C',
|
||
apiMethod: 'GET' as const,
|
||
}
|
||
|
||
try {
|
||
const weatherData = await fetchExternalAPI(weatherConfig)
|
||
console.log(`โ
Weather in Istanbul:`)
|
||
console.log(` Temperature: ${weatherData.current_condition[0].temp_C}ยฐC`)
|
||
} catch (error) {
|
||
console.log('โ ๏ธ Weather API test skipped (requires different endpoint)')
|
||
}
|
||
|
||
console.log('\nโ
External API tests completed!')
|
||
return true
|
||
} catch (error: any) {
|
||
console.error('\nโ External API test failed:', error.message)
|
||
return false
|
||
}
|
||
}
|
||
|
||
async function runAllTests() {
|
||
console.log('๐ Starting comprehensive API tests...\n')
|
||
|
||
const results = {
|
||
chainlink: false,
|
||
curve: false,
|
||
dexscreener: false,
|
||
espn: false,
|
||
external: false,
|
||
}
|
||
|
||
results.chainlink = await testChainlink()
|
||
results.curve = await testCurve()
|
||
results.dexscreener = await testDexScreener()
|
||
results.espn = await testESPN()
|
||
results.external = await testExternalAPIs()
|
||
|
||
// Summary
|
||
console.log('\nโ'.repeat(80))
|
||
console.log('๐ TEST SUMMARY')
|
||
console.log('โ'.repeat(80))
|
||
console.log(`\n1. Chainlink: ${results.chainlink ? 'โ
PASSED' : 'โ FAILED'}`)
|
||
console.log(`2. Curve: ${results.curve ? 'โ
PASSED' : 'โ FAILED'}`)
|
||
console.log(`3. DexScreener: ${results.dexscreener ? 'โ
PASSED' : 'โ FAILED'}`)
|
||
console.log(`4. ESPN: ${results.espn ? 'โ
PASSED' : 'โ FAILED'}`)
|
||
console.log(`5. External APIs: ${results.external ? 'โ
PASSED' : 'โ FAILED'}`)
|
||
|
||
const passed = Object.values(results).filter(r => r).length
|
||
const total = Object.keys(results).length
|
||
|
||
console.log(`\n${passed}/${total} tests passed`)
|
||
|
||
if (passed === total) {
|
||
console.log('\n๐ All APIs are working correctly!')
|
||
} else {
|
||
console.log('\nโ ๏ธ Some APIs failed. Check the logs above for details.')
|
||
}
|
||
|
||
console.log('\n' + 'โ'.repeat(80))
|
||
}
|
||
|
||
// Run tests
|
||
runAllTests().catch(console.error)
|