226 lines
6.3 KiB
TypeScript
226 lines
6.3 KiB
TypeScript
/**
|
|
* Unified Oracle Verification Service
|
|
* Handles all verification types and determines poll winners automatically
|
|
*/
|
|
|
|
import { verifyChainlinkPoll, ChainlinkConfig } from './chainlink'
|
|
import { verifyCurvePoll, CurvePriceConfig } from './curve'
|
|
import { verifyDexScreenerPoll, DexScreenerConfig } from './dexscreener'
|
|
import { verifyExternalAPIPoll, ExternalAPIConfig } from './external-api'
|
|
import { verifyESPNPoll, ESPNConfig } from './espn'
|
|
|
|
export type VerificationType = 'chainlink' | 'curve' | 'dexscreener' | 'api' | 'espn' | 'manual'
|
|
|
|
export interface VerificationResult {
|
|
success: boolean
|
|
correctOptionId?: string
|
|
optionIndex?: number
|
|
proof?: any
|
|
error?: string
|
|
timestamp: number
|
|
}
|
|
|
|
export interface PollVerificationConfig {
|
|
verificationType: VerificationType
|
|
verificationConfig:
|
|
| ChainlinkConfig
|
|
| CurvePriceConfig
|
|
| DexScreenerConfig
|
|
| ExternalAPIConfig
|
|
| ESPNConfig
|
|
| Record<string, any>
|
|
}
|
|
|
|
/**
|
|
* Main verification function - routes to appropriate oracle
|
|
*/
|
|
export async function verifyPoll(
|
|
pollId: string,
|
|
options: Array<{ id: string; text: string }>,
|
|
verificationConfig: PollVerificationConfig,
|
|
pollEndTime: number
|
|
): Promise<VerificationResult> {
|
|
const startTime = Date.now()
|
|
|
|
try {
|
|
let result: { correct: boolean; optionIndex: number; proof: any }
|
|
|
|
// Route to appropriate verification method
|
|
switch (verificationConfig.verificationType) {
|
|
case 'chainlink':
|
|
result = await verifyChainlinkPoll(
|
|
verificationConfig.verificationConfig as ChainlinkConfig,
|
|
pollEndTime
|
|
)
|
|
break
|
|
|
|
case 'curve':
|
|
result = await verifyCurvePoll(
|
|
verificationConfig.verificationConfig as CurvePriceConfig,
|
|
pollEndTime
|
|
)
|
|
break
|
|
|
|
case 'dexscreener':
|
|
result = await verifyDexScreenerPoll(
|
|
verificationConfig.verificationConfig as DexScreenerConfig,
|
|
pollEndTime
|
|
)
|
|
break
|
|
|
|
case 'api':
|
|
result = await verifyExternalAPIPoll(
|
|
verificationConfig.verificationConfig as ExternalAPIConfig,
|
|
pollEndTime
|
|
)
|
|
break
|
|
|
|
case 'espn':
|
|
result = await verifyESPNPoll(
|
|
verificationConfig.verificationConfig as ESPNConfig,
|
|
pollEndTime
|
|
)
|
|
break
|
|
|
|
case 'manual':
|
|
// Manual verification - skip automated verification
|
|
return {
|
|
success: false,
|
|
error: 'Manual verification required',
|
|
timestamp: Date.now(),
|
|
}
|
|
|
|
default:
|
|
throw new Error(`Unknown verification type: ${verificationConfig.verificationType}`)
|
|
}
|
|
|
|
// Map option index to option ID
|
|
const correctOptionId = options[result.optionIndex]?.id
|
|
|
|
if (!correctOptionId) {
|
|
throw new Error(`Invalid option index: ${result.optionIndex}`)
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
correctOptionId,
|
|
optionIndex: result.optionIndex,
|
|
proof: {
|
|
...result.proof,
|
|
verificationType: verificationConfig.verificationType,
|
|
verificationTime: Date.now(),
|
|
verificationDuration: Date.now() - startTime,
|
|
},
|
|
timestamp: Date.now(),
|
|
}
|
|
} catch (error: any) {
|
|
console.error(`Verification failed for poll ${pollId}:`, error)
|
|
|
|
return {
|
|
success: false,
|
|
error: error.message || 'Verification failed',
|
|
timestamp: Date.now(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validate verification config before creating poll
|
|
*/
|
|
export async function validateVerificationConfig(
|
|
config: PollVerificationConfig
|
|
): Promise<{ valid: boolean; error?: string }> {
|
|
try {
|
|
switch (config.verificationType) {
|
|
case 'chainlink':
|
|
const clConfig = config.verificationConfig as ChainlinkConfig
|
|
if (!clConfig.feedAddress) {
|
|
return { valid: false, error: 'Chainlink feed address required' }
|
|
}
|
|
break
|
|
|
|
case 'curve':
|
|
const curveConfig = config.verificationConfig as CurvePriceConfig
|
|
if (!curveConfig.chain || !curveConfig.tokenAddress) {
|
|
return { valid: false, error: 'Curve chain and token address required' }
|
|
}
|
|
break
|
|
|
|
case 'dexscreener':
|
|
const dexConfig = config.verificationConfig as DexScreenerConfig
|
|
if (!dexConfig.pairAddress) {
|
|
return { valid: false, error: 'DexScreener pair address required' }
|
|
}
|
|
break
|
|
|
|
case 'api':
|
|
const apiConfig = config.verificationConfig as ExternalAPIConfig
|
|
if (!apiConfig.apiEndpoint || !apiConfig.jsonPath) {
|
|
return { valid: false, error: 'API endpoint and JSON path required' }
|
|
}
|
|
break
|
|
|
|
case 'espn':
|
|
const espnConfig = config.verificationConfig as ESPNConfig
|
|
if (!espnConfig.sport) {
|
|
return { valid: false, error: 'ESPN sport required' }
|
|
}
|
|
if (!espnConfig.gameId && (!espnConfig.homeTeam || !espnConfig.awayTeam)) {
|
|
return { valid: false, error: 'Either gameId or team names required' }
|
|
}
|
|
break
|
|
|
|
case 'manual':
|
|
// Manual verification always valid
|
|
break
|
|
|
|
default:
|
|
return { valid: false, error: 'Unknown verification type' }
|
|
}
|
|
|
|
return { valid: true }
|
|
} catch (error: any) {
|
|
return { valid: false, error: error.message }
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get human-readable description of verification config
|
|
*/
|
|
export function getVerificationDescription(
|
|
config: PollVerificationConfig
|
|
): string {
|
|
switch (config.verificationType) {
|
|
case 'chainlink':
|
|
return 'Verified using Chainlink on-chain price feed'
|
|
|
|
case 'curve':
|
|
const curveConfig = config.verificationConfig as CurvePriceConfig
|
|
return `Verified using Curve price on ${curveConfig.chain}`
|
|
|
|
case 'dexscreener':
|
|
return 'Verified using DexScreener real-time DEX data'
|
|
|
|
case 'api':
|
|
const apiConfig = config.verificationConfig as ExternalAPIConfig
|
|
return `Verified using external API: ${new URL(apiConfig.apiEndpoint).hostname}`
|
|
|
|
case 'espn':
|
|
const espnConfig = config.verificationConfig as ESPNConfig
|
|
return `Verified using ESPN sports data: ${espnConfig.sport}`
|
|
|
|
case 'manual':
|
|
return 'Manually verified by poll creator'
|
|
|
|
default:
|
|
return 'Unknown verification method'
|
|
}
|
|
}
|
|
|
|
// Re-export all oracle functions
|
|
export * from './chainlink'
|
|
export * from './curve'
|
|
export * from './dexscreener'
|
|
export * from './external-api'
|
|
export * from './espn'
|