Skip to content

Commit

Permalink
Merge pull request #19 from gnosis/specific-address-api
Browse files Browse the repository at this point in the history
Address New Endpoint Issues
  • Loading branch information
nginnever authored Jun 5, 2023
2 parents 100fefa + be68a14 commit f2e2236
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 3,739 deletions.
36 changes: 36 additions & 0 deletions packages/backend/api/[context]/latest/delegates/topDelegates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as R from "ramda"
import type { VercelRequest, VercelResponse } from "@vercel/node"
import { db, getDelegationSnapshot } from "../../../../lib/services/storage/db"
import { BigNumber, ethers } from "ethers"

export default async function getTopDelegates(
request: VercelRequest,
response: VercelResponse,
) {
// const context = request.query.context as string
const space = request.query.space as string
const block = 8979657

const stats = await db
.selectFrom("delegation_snapshot")
.where("context", "=", space)
.where("main_chain_block_number", "=", block)
.select(["to_address"])
.execute()

if (stats.length === 0) {
console.log("No delegations found for space context", space)
}

console.log(stats)
const finalArray = stats.map(function (obj) {
return obj.to_address;
});
console.log(finalArray)
const test = R.countBy(R.toLower)(finalArray)
console.log(test)

response.status(200).json({
success: "true",
})
}
34 changes: 34 additions & 0 deletions packages/backend/api/[context]/latest/delegators/set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// return all of the delegated addresses that a specific delegator address is delegating to and the amount delegated for each delegate per delegator.

import type { VercelRequest, VercelResponse } from "@vercel/node"
import { db, getDelegationSnapshot } from "../../../../lib/services/storage/db"
import { BigNumber, ethers } from "ethers"

export default async function getDelegationSet(
request: VercelRequest,
response: VercelResponse,
) {
const context = request.query.context as string
const space = request.query.space as string
const delegatorAddress = ethers.utils.getAddress(
request.query.delegatorAddress as string,
)

const delegates = await db
.selectFrom("delegation_snapshot")
.where("context", "=", space)
.where("main_chain_block_number", "is", null)
.where("from_address", "=", delegatorAddress)
.select(["to_address", "delegated_amount", "to_address_own_amount"])
.execute()

if (delegates.length === 0) {
console.log("No delegates found for delegatorAddress", delegatorAddress)
}

response.status(200).json({
success: "true",
delegates,
numberOfDelegates: delegates.length
})
}
41 changes: 41 additions & 0 deletions packages/backend/api/[context]/latest/spacestats/allstats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// return global stats for a given snapshot space.

import type { VercelRequest, VercelResponse } from "@vercel/node"
import { db, getDelegationSnapshot } from "../../../../lib/services/storage/db"
import { BigNumber, ethers } from "ethers"

export default async function getSpaceStats(
request: VercelRequest,
response: VercelResponse,
) {
const context = request.query.context as string
const space = request.query.space as string

const stats = await db
.selectFrom("delegation_snapshot")
.where("context", "=", space)
.where("main_chain_block_number", "is", null)
.select(["from_address", "delegated_amount", "to_address_own_amount"])
.execute()

if (stats.length === 0) {
console.log("No delegations found for space context", space)
}

// total unique delegations TODO: find select distinct in kinsly
const unique = [...new Set(stats.map(item => item.from_address))];
// total delegated vote weight
// total non-unique delegations
const globalStats = stats.reduce((acc, stat) => {
acc.totalVoteWeight = acc.totalVoteWeight.add(BigNumber.from(stat.delegated_amount))
acc.totalDelegations++
return acc
}, { totalVoteWeight:BigNumber.from(0), totalDelegations:0 })

response.status(200).json({
success: "true",
totalVoteWeight: globalStats.totalVoteWeight.toString(),
totalDelegations: globalStats.totalDelegations,
totalUniqueDelegators: unique.length
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// return global stats for a given snapshot space.

import type { VercelRequest, VercelResponse } from "@vercel/node"
import { db, getDelegationSnapshot } from "../../../../lib/services/storage/db"
import { BigNumber, ethers } from "ethers"

export default async function totalDelegatees(
request: VercelRequest,
response: VercelResponse,
) {
const context = request.query.context as string
const space = request.query.space as string

const stats = await db
.selectFrom("delegation_snapshot")
.where("context", "=", space)
.where("main_chain_block_number", "is", null)
.select(["to_address"])
.distinct()
.execute()

if (stats.length === 0) {
console.log("No delegatees found for space context", space)
}

response.status(200).json({
success: "true",
totalUniqueDelegatees: stats.length
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// return global stats for a given snapshot space.

import type { VercelRequest, VercelResponse } from "@vercel/node"
import { db, getDelegationSnapshot } from "../../../../lib/services/storage/db"
import { BigNumber, ethers } from "ethers"

export default async function totalDelegators(
request: VercelRequest,
response: VercelResponse,
) {
const context = request.query.context as string
const space = request.query.space as string

const stats = await db
.selectFrom("delegation_snapshot")
.where("context", "=", space)
.where("main_chain_block_number", "is", null)
.select(["from_address"])
.distinct()
.execute()

if (stats.length === 0) {
console.log("No delegators found for space context", space)
}

response.status(200).json({
success: "true",
totalUniqueDelegators: stats.length
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// return the total vote weight across all snapshot spaces.

import type { VercelRequest, VercelResponse } from "@vercel/node"
import { db, getDelegationSnapshot } from "../../../../lib/services/storage/db"
import { BigNumber, ethers } from "ethers"

export default async function getTotalVoteWeight(
request: VercelRequest,
response: VercelResponse,
) {
const spaces = ["gnosis.eth", "gnosisdaotest.eth"]

const total = await spaces.reduce(async (acc, space) => {
const accFirst = await acc
const stats = await db
.selectFrom("delegation_snapshot")
.where("context", "=", space)
.where("main_chain_block_number", "is", null)
.select(["delegated_amount", "to_address_own_amount"])
.execute()

const spaceTotal = stats.reduce((innerAcc, stat) => {
return innerAcc.add(BigNumber.from(stat.delegated_amount))
}, BigNumber.from(0))

return accFirst.add(spaceTotal)
}, Promise.resolve(BigNumber.from(0)))

response.status(200).json({
success: "true",
totalSpaceVotingWeight: total.toString()
})
}
1 change: 1 addition & 0 deletions packages/backend/example-requests/delegate-delegators.http
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
GET http://localhost:3000/api/delegate/delegators?space=gnosis.eth&delegateAddress=0xDE1e8A7E184Babd9F0E3af18f40634e9Ed6F0905
content-type: application/json

curl -v "http://localhost:3000/api/delegate/delegators" -H "Content-Type: application/json" -d "{"space":"gnosis.eth","delegateAddress":"0xDE1e8A7E184Babd9F0E3af18f40634e9Ed6F0905"}"
1 change: 1 addition & 0 deletions packages/backend/example-requests/delegator-set.http
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -v -X GET "http://localhost:3000/api/context/latest/delegators/set?space=gnosis.eth&delegatorAddress=0x6cc5b30Cd0A93C1F85C7868f5F2620AB8c458190"
1 change: 1 addition & 0 deletions packages/backend/example-requests/space-allstats.http
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -v -X GET "http://localhost:3000/api/context/latest/spacestats/allstats?space=gnosis.eth"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -v -X GET "http://localhost:3000/api/context/latest/spacestats/totalVoteWeight"
Loading

0 comments on commit f2e2236

Please sign in to comment.