forked from ethereum-attestation-service/eas-is-true
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from daqhris/fix-attestation-page
Updated EventAttendanceVerification and fetchPoaps API, add event_ids.json.
- Loading branch information
Showing
3 changed files
with
146 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"eventIds": [ | ||
"176334", | ||
"176328", | ||
"176329", | ||
"176330", | ||
"176331", | ||
"176332" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,95 @@ | ||
import axios from 'axios'; | ||
import { readFileSync } from 'fs'; | ||
import path from 'path'; | ||
|
||
const eventIdsPath = path.join(process.cwd(), 'event_ids.json'); | ||
const { eventIds } = JSON.parse(readFileSync(eventIdsPath, 'utf8')); | ||
|
||
export default async function handler(req, res) { | ||
console.log('Incoming request:', { | ||
method: req.method, | ||
url: req.url, | ||
headers: req.headers, | ||
query: req.query, | ||
body: req.body | ||
}); | ||
|
||
const { address } = req.query; | ||
const eventId = '16947'; // ETHGlobal Brussels 2024 event ID | ||
|
||
if (!address) { | ||
console.error('Address is missing in the request'); | ||
return res.status(400).json({ error: 'Address is required' }); | ||
} | ||
|
||
try { | ||
console.log(`Fetching POAP event data for event ID: ${eventId}`); | ||
const eventResponse = await axios.get(`https://api.poap.tech/events/id/${eventId}`, { | ||
headers: { | ||
'X-API-Key': process.env.POAP_API_KEY | ||
} | ||
}); | ||
|
||
console.log('POAP event API response:', eventResponse.data); | ||
|
||
if (eventResponse.status !== 200) { | ||
throw new Error(`Failed to fetch POAP event: ${eventResponse.statusText}`); | ||
} | ||
|
||
const eventData = eventResponse.data; | ||
|
||
console.log(`Verifying POAP ownership for address: ${address}`); | ||
const ownershipResponse = await axios.get(`https://api.poap.tech/actions/scan/${address}`, { | ||
console.log(`Fetching POAPs for address: ${address}`); | ||
const poapResponse = await axios.get(`https://api.poap.tech/actions/scan/${address}`, { | ||
headers: { | ||
'X-API-Key': process.env.POAP_API_KEY | ||
} | ||
}); | ||
|
||
console.log('POAP ownership API response:', ownershipResponse.data); | ||
console.log('POAP API response:', JSON.stringify(poapResponse.data, null, 2)); | ||
|
||
if (ownershipResponse.status !== 200) { | ||
throw new Error(`Failed to verify POAP ownership: ${ownershipResponse.statusText}`); | ||
if (poapResponse.status !== 200) { | ||
throw new Error(`Failed to fetch POAPs: ${poapResponse.statusText}`); | ||
} | ||
|
||
const ownedPOAPs = ownershipResponse.data; | ||
console.log('Owned POAPs:', ownedPOAPs); | ||
const allPoaps = poapResponse.data; | ||
console.log('Fetched POAPs:', JSON.stringify(allPoaps, null, 2)); | ||
|
||
const hasPOAP = ownedPOAPs.some(poap => poap.event.id === parseInt(eventId)); | ||
const requiredPoaps = allPoaps.filter(poap => eventIds.includes(poap.event.id.toString())); | ||
const missingEventIds = eventIds.filter(id => !requiredPoaps.some(poap => poap.event.id.toString() === id)); | ||
|
||
if (hasPOAP) { | ||
console.log(`POAP found for address ${address}`); | ||
res.status(200).json({ poaps: [eventData], message: `Proof successful! ${address} has attended ETHGlobal Brussels 2024.` }); | ||
if (requiredPoaps.length > 0) { | ||
console.log(`Required POAPs found for address ${address}:`, JSON.stringify(requiredPoaps, null, 2)); | ||
res.status(200).json({ | ||
poaps: requiredPoaps, | ||
missingEventIds: missingEventIds, | ||
message: `Found ${requiredPoaps.length} out of ${eventIds.length} required POAPs.` | ||
}); | ||
} else { | ||
console.log(`No POAP found for address ${address}`); | ||
res.status(200).json({ poaps: [], message: "No eligible POAP found for ETHGlobal Brussels 2024." }); | ||
console.log(`No required POAPs found for address ${address}`); | ||
res.status(200).json({ | ||
poaps: [], | ||
missingEventIds: eventIds, | ||
message: "No required POAPs found for this address." | ||
}); | ||
} | ||
} catch (error) { | ||
console.error("Error fetching POAP data:", error); | ||
console.error("Error details:", { | ||
console.error("Full error details:", { | ||
message: error.message, | ||
stack: error.stack, | ||
request: error.config ? { | ||
url: error.config.url, | ||
method: error.config.method, | ||
headers: error.config.headers, | ||
params: error.config.params | ||
} : 'No request config', | ||
response: error.response ? { | ||
status: error.response.status, | ||
statusText: error.response.statusText, | ||
headers: error.response.headers, | ||
data: error.response.data | ||
} : 'No response data' | ||
}); | ||
res.status(500).json({ error: 'Failed to fetch POAP data. Please try again.', details: error.message }); | ||
|
||
let errorMessage = 'Failed to fetch POAP data. Please try again.'; | ||
let statusCode = 500; | ||
|
||
if (error.response) { | ||
if (error.response.status === 404) { | ||
errorMessage = 'No POAPs found for this address.'; | ||
statusCode = 404; | ||
} else if (error.response.status === 401) { | ||
errorMessage = 'Unauthorized access to POAP data. Please check API credentials.'; | ||
statusCode = 401; | ||
} | ||
} else if (error.request) { | ||
errorMessage = 'No response received from POAP API. Please try again later.'; | ||
} | ||
|
||
res.status(statusCode).json({ error: errorMessage, details: error.message }); | ||
} | ||
} |