Skip to content

Commit

Permalink
Add endpoint for the Snapshot strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
asgeir-s committed May 26, 2023
1 parent fc4dd73 commit 100fefa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// given a list of addresses, fetch the vote weight for each address from the snapshot in the database
// will be used by the strategy to calculate the score

import type { VercelRequest, VercelResponse } from "@vercel/node"
import { createDelegationSnapshot } from "../../../../lib/updateSnapshot"
import {
checkIfSnapshotExists,
getDelegationSnapshot,
getVoteWeightSnapshot,
} from "../../../../lib/services/storage/db"
import * as R from "ramda"
import { BigNumber } from "ethers"

export default async function getVoteWeightsForSnapshot(
request: VercelRequest,
response: VercelResponse,
) {
const context = request.query.context as string
const mainChainBlockNumber: number | null =
request.query.blocknumber === "latest"
? null
: Number(request.query.blocknumber)

const addresses = request.body.addresses as string[]

console.log({
context,
mainChainBlockNumber,
addresses,
})

if (mainChainBlockNumber != null) {
if (!(await checkIfSnapshotExists(context, mainChainBlockNumber))) {
await createDelegationSnapshot(context, mainChainBlockNumber)
}
}

const voteWeights = await getVoteWeightSnapshot(context, mainChainBlockNumber)
console.log({ voteWeights })

const relevantVoteWeights = R.innerJoin(
(record, address) => record.to_address === address,
voteWeights,
addresses,
)

response
.status(200)
.json(
relevantVoteWeights.map((record) => [
record.to_address,
BigNumber.from(record.delegated_amount)
.add(BigNumber.from(record.to_address_own_amount))
.toString(),
]),
)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// given a list of addresses, fetch the vote weight for each address from the snapshot in the database
// will be used by the strategy to calculate the score

import type { VercelRequest, VercelResponse } from "@vercel/node"
import { createDelegationSnapshot } from "../../../../lib/updateSnapshot"
Expand Down
5 changes: 3 additions & 2 deletions packages/backend/example-requests/delegates-scores.http
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Get the voting power delegated to each delegate (provided in the request body).
// Returned in the format required by the "API POST strategy" (https://github.com/snapshot-labs/snapshot-strategies/tree/master/src/strategies/api-post)

POST http://localhost:3000/api/delegates/scores?space=gnosis.eth
POST http://localhost:3000/api/mydao.eth/snapshot/8979656/strategy-formatted-vote-weights
content-type: application/json

{
"addresses": [
"0x6cc5b30Cd0A93C1F85C7868f5F2620AB8c458190",
"0x7ef021f62E3E7975FBC21d3202C5A1F19D53bB47",
"0x1E1A51E25f2816335cA436D65e9Af7694BE232ad"
"0x1E1A51E25f2816335cA436D65e9Af7694BE232ad",
"0xD028d504316FEc029CFa36bdc3A8f053F6E5a6e4"
]
}
2 changes: 1 addition & 1 deletion packages/backend/example-requests/snapshot-context.http
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Recomputes the vote weights for all delegates across all Snapshot Spaces.

GET http://localhost:3000/api/gnosis.eth/snapshot/8979656/create
GET http://localhost:3000/api/mydao.eth/snapshot/8979656/create

// 8500949 - has one delegation set on goerli in gnosis.eth
// 8979655 - has more

0 comments on commit 100fefa

Please sign in to comment.