Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Proposals API #212

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => {
const cursor: string | undefined = event?.queryStringParameters?.cursor;

const template = {
"@context": {
"@vocab": "http://daostar.org/",
},
"@context": "http://daostar.org/schemas",
type: "DAO",
name: name,
};
Expand Down
77 changes: 77 additions & 0 deletions Implementations/API/backend/functions/boardroom/getProposals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { APIGatewayProxyHandlerV2 } from "aws-lambda";
import { BoardroomKey, boardroomApiConfig } from "../config";
import fetch, { RequestInit } from "node-fetch";
import { utils } from "ethers";

function apiRequest(path: string, method: "GET" | "POST", data?: any) {
const payload: RequestInit = {
headers: {
"Content-Type": "application/json",
},
method,
redirect: "follow",
};
if (method === "POST") payload.body = JSON.stringify(data);
return fetch(path, payload).then((res) => res.json());
}

export const handler: APIGatewayProxyHandlerV2 = async (event) => {
const network = event?.pathParameters?.network;
if (!network) return { statusCode: 400, message: "Missing network" };

const path = boardroomApiConfig[network];
if (!path) return { statusCode: 400, message: "Missing config for network" };

const name = event?.pathParameters?.id;
if (!name) return { statusCode: 400, message: "Missing name" };

const cursor: string | undefined = event?.queryStringParameters?.cursor;

const template = {
"@context": "http://www.daostar.org/schemas",
type: "DAO",
name: name,
};

let queryPath =
path +
"/" +
name +
"/proposals" +
"?limit=50&key=" +
process.env.BOARDROOM_KEY;
if (cursor) {
queryPath += "&cursor=" + decodeURIComponent(cursor);
}

const res = (await apiRequest(queryPath, "GET")) as any;

if (!res.data) return { statusCode: 404, message: "DAO not found" };
const proposals = res.data;
const nextCursor = res.nextCursor;

const proposalsFormatted = proposals.map((a: any) => {
return {
"type": "proposal",
"id": a.id,
"name": a.title,
"status": a.currentState,
};
});

const transformed = {
proposals: proposalsFormatted,
...template,
nextCursor: encodeURIComponent(nextCursor),
};

return transformed
? {
statusCode: 200,
body: JSON.stringify(transformed),
}
: {
statusCode: 404,
body: JSON.stringify({ error: true }),
};
};
10 changes: 7 additions & 3 deletions Implementations/API/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,26 @@
"test": "vitest run"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.2",
"@serverless-stack/cli": "^1.18.4",
"@serverless-stack/resources": "^1.18.4",
"@tsconfig/node16": "^1.0.2",
"@types/aws-lambda": "^8.10.97",
"prettier": "^2.3.2",
"typescript": "^4.7.2",
"vitest": "^0.12.9",
"prettier": "^2.3.2"
"vitest": "^0.12.9"
},
"workspaces": [
"backend"
],
"dependencies": {
"@ethersproject/address": "^5.7.0",
"@ethersproject/shims": "^5.7.0",
"@serverless-stack/core": "^1.18.4",
"@snapshot-labs/snapshot.js": "^0.4.98",
"aws-lambda": "^1.0.7",
"chalk": "^5.3.0",
"ethers": "^5.7.2",
"fs-extra": "^11.2.0",
"node-fetch": "^3.2.6"
}
}
Loading