|
| 1 | +import axios from 'axios' |
| 2 | +import { checksumAddress, isAddress } from 'viem' |
| 3 | + |
| 4 | +import { CHAIN_ID } from 'src/typings' |
| 5 | + |
| 6 | +interface AttestationResponse { |
| 7 | + data: { |
| 8 | + attestations: Array<{ |
| 9 | + attester: string |
| 10 | + recipient: string |
| 11 | + decodedDataJson: string |
| 12 | + }> |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +interface DecodedData { |
| 17 | + name: string |
| 18 | + type: string |
| 19 | + value: { |
| 20 | + type: string |
| 21 | + value: string |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const ATTESTATION_SCHEMA_UID = `0x9ee9a1bfbf4f8f9b977c6b30600d6131d2a56d0be8100e2238a057ea8b18be7e` |
| 26 | + |
| 27 | +const ATTESTATION_URL: Record<CHAIN_ID, string> = { |
| 28 | + [CHAIN_ID.ETHEREUM]: 'https://easscan.org/graphql', |
| 29 | + [CHAIN_ID.OPTIMISM]: 'https://optimism.easscan.org/graphql', |
| 30 | + [CHAIN_ID.SEPOLIA]: 'https://sepolia.easscan.org/graphql', |
| 31 | + [CHAIN_ID.OPTIMISM_SEPOLIA]: 'https://optimism-sepolia.easscan.org/graphql', |
| 32 | + [CHAIN_ID.BASE]: 'https://base.easscan.org/graphql', |
| 33 | + [CHAIN_ID.BASE_SEPOLIA]: 'https://base-sepolia.easscan.org/graphql', |
| 34 | + [CHAIN_ID.ZORA]: '', |
| 35 | + [CHAIN_ID.ZORA_SEPOLIA]: '', |
| 36 | + [CHAIN_ID.FOUNDRY]: '', |
| 37 | +} |
| 38 | + |
| 39 | +export async function getPropDates( |
| 40 | + daoTreasuryAddress: string, |
| 41 | + chainId: CHAIN_ID, |
| 42 | + propId: string |
| 43 | +): Promise<Array<any>> { |
| 44 | + // Input validation |
| 45 | + if (!daoTreasuryAddress || !isAddress(daoTreasuryAddress)) { |
| 46 | + return [] |
| 47 | + } |
| 48 | + |
| 49 | + const attestationUrl = ATTESTATION_URL[chainId] |
| 50 | + if (!attestationUrl) { |
| 51 | + return [] |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + const query = ` |
| 56 | + query Attestations { |
| 57 | + attestations( |
| 58 | + where: { |
| 59 | + schemaId: { equals: "${ATTESTATION_SCHEMA_UID}" } |
| 60 | + recipient: { equals: "${checksumAddress(daoTreasuryAddress)}" } |
| 61 | + } |
| 62 | + ) { |
| 63 | + attester |
| 64 | + recipient |
| 65 | + decodedDataJson |
| 66 | + } |
| 67 | + } |
| 68 | +` |
| 69 | + |
| 70 | + try { |
| 71 | + const response = await axios.post<AttestationResponse>( |
| 72 | + attestationUrl, |
| 73 | + { query }, |
| 74 | + { |
| 75 | + headers: { |
| 76 | + 'Content-Type': 'application/json', |
| 77 | + }, |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + const attestations = response?.data?.data?.attestations |
| 82 | + |
| 83 | + if (!attestations?.length) { |
| 84 | + return [] |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + // Get decoded data from each attestation |
| 89 | + const decodedData = attestations.map(attestation => |
| 90 | + JSON.parse(attestation.decodedDataJson) as DecodedData[] |
| 91 | + ) |
| 92 | + |
| 93 | + const propDates = decodedData.map(decoded => ({ |
| 94 | + propId: Number(decoded.find(d => d.name === 'propId')?.value.value), |
| 95 | + replyTo: decoded.find(d => d.name === 'replyTo')?.value.value, |
| 96 | + response: decoded.find(d => d.name === 'response')?.value.value, |
| 97 | + milestoneId: Number(decoded.find(d => d.name === 'milestoneId')?.value.value) |
| 98 | + })) |
| 99 | + |
| 100 | + return propDates.filter(date => Number(date.propId) == Number(propId)) |
| 101 | + } catch (parseError) { |
| 102 | + console.error('Error parsing attestation data:', parseError) |
| 103 | + return [] |
| 104 | + } |
| 105 | + } catch (error) { |
| 106 | + console.error('Error fetching attestations:', error) |
| 107 | + return [] |
| 108 | + } |
| 109 | +} |
0 commit comments