Skip to content

Commit

Permalink
feat: get attestation by id and by schema id
Browse files Browse the repository at this point in the history
  • Loading branch information
mrisqid committed Aug 10, 2024
1 parent f81c795 commit e10b270
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 155 deletions.
52 changes: 28 additions & 24 deletions apps/haus/src/app/api/attestation/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
import { NextResponse } from 'next/server'
import { EAS } from '@ethereum-attestation-service/eas-sdk'
import { ethers } from 'ethers'
import { EAS_ADDRESS, PROVIDER } from '@/config'
import axios from 'axios'

const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY

const customStringify = (obj) => {
return JSON.stringify(obj, (key, value) => (typeof value === 'bigint' ? value.toString() : value))
}
const endpoint = 'https://optimism-sepolia.easscan.org/graphql'

export async function GET(request: Request, context: { params: { id: string } }) {
const query = `
query getAttestationById($where: AttestationWhereUniqueInput!) {
getAttestation(where: $where) {
decodedDataJson
attester
recipient
id
schemaId
}
}
`

try {
const attestationUID = context.params.id

if (!attestationUID) {
return NextResponse.json({ message: 'Attestation UID is required' }, { status: 400 })
}

// Initialize the sdk with the address of the EAS Schema contract address
const easInstance = new EAS(EAS_ADDRESS as string)

// Gets a default provider (in production use something else like infura/alchemy)
const provider = ethers.getDefaultProvider(PROVIDER)
const signer = new ethers.Wallet(PRIVATE_KEY as string, provider)

// Connects an ethers style provider/signingProvider to perform read/write functions.
easInstance.connect(signer)

const attestation = await easInstance.getAttestation(attestationUID)
console.log('Attestation detail: ', attestation)

const data = customStringify(attestation)

return NextResponse.json(data)
const response = await axios.post(
endpoint,
{
query,
variables: { where: { id: attestationUID } },
},
{
headers: {
'Content-Type': 'application/json',
},
},
)

return NextResponse.json(response.data)
} catch (error) {
console.error('Error handling GET request', error)
return NextResponse.json({ message: 'Something went wrong' }, { status: 500 })
Expand Down
54 changes: 52 additions & 2 deletions apps/haus/src/app/api/attestation/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { NextResponse } from 'next/server'
import { NextResponse, NextRequest } from 'next/server'
import { EAS, SchemaEncoder } from '@ethereum-attestation-service/eas-sdk'
import { ethers } from 'ethers'
import { EAS_ADDRESS, SCHEMA_UID, PROVIDER } from '@/config'
import axios from 'axios'
import { SCHEMA_UID, PROVIDER } from '@/config'

const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY
const EAS_ADDRESS = process.env.EAS_CONTRACT_ADDRESS

export async function POST(request: Request) {
try {
Expand Down Expand Up @@ -63,3 +65,51 @@ export async function POST(request: Request) {
return NextResponse.json({ message: 'Something went wrong' }, { status: 500 })
}
}

const endpoint = 'https://optimism-sepolia.easscan.org/graphql'

export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams
const query = `
query Attestations($where: AttestationWhereInput, $orderBy: [AttestationOrderByWithRelationInput!]) {
attestations(where: $where, orderBy: $orderBy) {
attester
id
recipient
schemaId
timeCreated
decodedDataJson
}
}
`

try {
// schema uid: ?uid=xxxxxxxxxxxxxxxxxxxxxx
const uid = searchParams.get('uid')

if (!uid) {
return NextResponse.json({ message: 'Schema UID is required' }, { status: 400 })
}

const response = await axios.post(
endpoint,
{
query,
variables: {
where: { schemaId: { equals: uid } },
orderBy: [{ timeCreated: 'desc' }],
},
},
{
headers: {
'Content-Type': 'application/json',
},
},
)

return NextResponse.json(response.data)
} catch (error) {
console.error('Error handling GET request', error)
return NextResponse.json({ message: 'Something went wrong' }, { status: 500 })
}
}
5 changes: 3 additions & 2 deletions apps/haus/src/app/hooks/useEAS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import { EAS } from '@ethereum-attestation-service/eas-sdk'
import { ethers } from 'ethers'
import { useEffect, useState } from 'react'
import { EAS_ADDRESS, PROVIDER } from '@/config'
import { PROVIDER } from '@/config'

export const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY
const EAS_ADDRESS = process.env.EAS_CONTRACT_ADDRESS

export const useEAS = () => {
const [eas, setEAS] = useState<EAS>()
Expand Down
3 changes: 2 additions & 1 deletion apps/haus/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export const EVENTS = [
},
]

export const EAS_ADDRESS = process.env.EAS_CONTRACT_ADDRESS
export const SCHEMA_UID = process.env.SCHEMA_UID
export const PROVIDER = process.env.EAS_PROVIDER_URL || 'https://sepolia.optimism.io'
export const QUERY_ENDPOINT =
process.env.QUERY_ENDPOINT_URL || 'https://optimism-sepolia.easscan.org/graphql'
Loading

0 comments on commit e10b270

Please sign in to comment.