Skip to content

Commit

Permalink
chore: add endpoints for snapshot subgraph and update api url
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed Jun 11, 2024
1 parent 3cb5123 commit 61ea0b0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/back/routes/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import handleAPI from 'decentraland-gatsby/dist/entities/Route/handle'
import routes from 'decentraland-gatsby/dist/entities/Route/routes'
import { Request } from 'express'

import { SnapshotSubgraph } from '../../clients/SnapshotSubgraph'
import { getDelegations as getSnapshotDelegations } from '../../entities/Snapshot/utils'
import { SnapshotService } from '../../services/SnapshotService'
import { SnapshotStatusService } from '../../services/SnapshotStatusService'
import {
validateAddress,
validateAddresses,
validateBlockNumber,
validateDates,
validateProposalFields,
validateProposalSnapshotId,
Expand All @@ -22,6 +26,8 @@ export default routes((router) => {
router.get('/snapshot/vp-distribution/:address/:proposalSnapshotId?', handleAPI(getVpDistribution))
router.post('/snapshot/scores', handleAPI(getScores))
router.get('/snapshot/proposal-scores/:proposalSnapshotId', handleAPI(getProposalScores))
router.post('/snapshot/delegations', handleAPI(getDelegations))
router.post('/snapshot/picked-by', handleAPI(getPickedBy))
})

async function getStatus() {
Expand Down Expand Up @@ -77,3 +83,20 @@ async function getProposalScores(req: Request<{ proposalSnapshotId?: string }>)
const proposalSnapshotId = validateProposalSnapshotId(req.params.proposalSnapshotId)
return await SnapshotService.getProposalScores(proposalSnapshotId)
}

async function getDelegations(req: Request) {
const { address, blockNumber } = req.body
validateAddress(address)
validateBlockNumber(blockNumber)
return getSnapshotDelegations(address, blockNumber)
}

async function getPickedBy(req: Request) {
const { addresses, space } = req.body
validateAddresses(addresses)
if (!space || typeof space !== 'string' || space.length === 0) {
throw new RequestError('Invalid snapshot space', RequestError.BadRequest)
}

return await SnapshotSubgraph.get().getPickedBy(addresses, space)
}
15 changes: 15 additions & 0 deletions src/back/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ export function validateAddress(address?: string) {
return address
}

export function validateAddresses(addresses?: string[]) {
if (!Array.isArray(addresses)) {
throw new RequestError(`Invalid addresses ${addresses}`, RequestError.BadRequest)
}
addresses.forEach((address) => {
validateAddress(address)
})
}

export const areValidAddresses = (addresses: string[]) =>
Array.isArray(addresses) && addresses.every((item) => isEthereumAddress(item))

Expand Down Expand Up @@ -154,3 +163,9 @@ export function validateEventTypesFilters(req: Request) {

return parsedEventTypes.data
}

export function validateBlockNumber(blockNumber?: unknown | null) {
if (blockNumber !== null && blockNumber !== undefined && typeof blockNumber !== 'number') {
throw new Error('Invalid blockNumber: must be null, undefined, or a number')
}
}
10 changes: 4 additions & 6 deletions src/clients/SnapshotSubgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ export class SnapshotSubgraph {
return delegations
}

async getPickedBy(variables: { address: string[]; space: string }) {
const { address, space } = variables

if (!address || !space) {
async getPickedBy(addresses: string[], space: string) {
if (!addresses || !space) {
return []
}

Expand All @@ -93,13 +91,13 @@ export class SnapshotSubgraph {
const body = await response.json()
return body?.data?.delegatedFrom || []
},
{ address, space },
{ address: addresses, space },
500
)

const pickedBy = new Map<string, Set<string>>()

for (const addr of address) {
for (const addr of addresses) {
const filteredDelegations = delegations.filter((deleg) => deleg.delegate === addr)
pickedBy.set(addr, new Set())

Expand Down
2 changes: 1 addition & 1 deletion src/entities/Snapshot/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export const SNAPSHOT_SPACE = process.env.GATSBY_SNAPSHOT_SPACE || ''
export const SNAPSHOT_ADDRESS = process.env.GATSBY_SNAPSHOT_ADDRESS || ''
export const SNAPSHOT_DURATION = Number(process.env.GATSBY_SNAPSHOT_DURATION || '')
export const SNAPSHOT_URL = process.env.GATSBY_SNAPSHOT_URL || 'https://testnet.snapshot.org/'
export const SNAPSHOT_QUERY_ENDPOINT = process.env.GATSBY_SNAPSHOT_QUERY_ENDPOINT || ''
export const SNAPSHOT_QUERY_ENDPOINT = `https://gateway-arbitrum.network.thegraph.com/api/${process.env.THE_GRAPH_API_KEY}/subgraphs/id/4YgtogVaqoM8CErHWDK8mKQ825BcVdKB8vBYmb4avAQo`
export const SNAPSHOT_API = process.env.GATSBY_SNAPSHOT_API || ''

0 comments on commit 61ea0b0

Please sign in to comment.