-
Notifications
You must be signed in to change notification settings - Fork 13
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 #19 from gnosis/specific-address-api
Address New Endpoint Issues
- Loading branch information
Showing
11 changed files
with
208 additions
and
3,739 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
packages/backend/api/[context]/latest/delegates/topDelegates.ts
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,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", | ||
}) | ||
} |
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,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
41
packages/backend/api/[context]/latest/spacestats/allstats.ts
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,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 | ||
}) | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/backend/api/[context]/latest/spacestats/totalDelegatees.ts
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,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 | ||
}) | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/backend/api/[context]/latest/spacestats/totalDelegators.ts
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,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 | ||
}) | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/backend/api/[context]/latest/spacestats/totalVoteWeight.ts
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,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() | ||
}) | ||
} |
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 @@ | ||
curl -v -X GET "http://localhost:3000/api/context/latest/delegators/set?space=gnosis.eth&delegatorAddress=0x6cc5b30Cd0A93C1F85C7868f5F2620AB8c458190" |
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 @@ | ||
curl -v -X GET "http://localhost:3000/api/context/latest/spacestats/allstats?space=gnosis.eth" |
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 @@ | ||
curl -v -X GET "http://localhost:3000/api/context/latest/spacestats/totalVoteWeight" |
Oops, something went wrong.