-
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.
Add endpoint for the Snapshot strategy
- Loading branch information
Showing
4 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/backend/api/[context]/snapshot/[blocknumber]/strategy-formatted-vote-weights.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,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(), | ||
]), | ||
) | ||
} |
1 change: 0 additions & 1 deletion
1
...xt]/snapshot/[blocknumber]/voteWeights.ts → ...t]/snapshot/[blocknumber]/vote-weights.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
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 |
---|---|---|
@@ -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" | ||
] | ||
} |
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 |
---|---|---|
@@ -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 |