Skip to content

Commit eeb2f16

Browse files
authored
Merge pull request #56 from MetaFactoryAI/feature/creditor-plugin
Implement creditor plugin to pull data from contribution tracker
2 parents 379cd45 + f48be2c commit eeb2f16

10 files changed

Lines changed: 8004 additions & 8 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ output/
44
cache/
55

66
.env
7+
8+
.vercel

api/declaration.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { VercelRequest, VercelResponse } from '@vercel/node';
2+
import { getDeclaration } from "@meta-cred/sc-plugin";
3+
import { allowCors } from "../utils/allowCors";
4+
5+
6+
const handler = async (req: VercelRequest, res: VercelResponse) => {
7+
const declaration = getDeclaration()
8+
9+
res.status(200).json(declaration);
10+
}
11+
12+
export default allowCors(handler);

api/graph.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { VercelRequest, VercelResponse } from '@vercel/node';
2+
import { createWeightedGraph } from "@meta-cred/sc-plugin";
3+
import { Gql, Chain, } from '../zeus';
4+
import { Rating } from "@meta-cred/sc-plugin/dist/types";
5+
import { allowCors } from "../utils/allowCors";
6+
7+
const { GRAPHQL_API_URL, GRAPHQL_ADMIN_SECRET } = process.env;
8+
9+
const handler = async (req: VercelRequest, res: VercelResponse) => {
10+
if (!GRAPHQL_API_URL || !GRAPHQL_ADMIN_SECRET) throw new Error("Missing ENV Vars");
11+
12+
const chain = Chain(GRAPHQL_API_URL, {
13+
headers: {
14+
'x-hasura-admin-secret': GRAPHQL_ADMIN_SECRET,
15+
}
16+
});
17+
18+
const data = await chain('query')({
19+
contributions: [{}, {
20+
id: true,
21+
title: true,
22+
created_at: true,
23+
date: true,
24+
author: {
25+
name: true,
26+
eth_address: true
27+
},
28+
contributors: [{}, {
29+
user: {
30+
name: true,
31+
eth_address: true,
32+
},
33+
contribution_share: true,
34+
}],
35+
votes: [{ where: { rating: { _nin: ["abstain"] } } }, {
36+
user: {
37+
name: true,
38+
eth_address: true,
39+
},
40+
rating: true,
41+
created_at: true
42+
}]
43+
}]
44+
});
45+
46+
// Filter out flagged contributions
47+
const contributions = data.contributions
48+
.filter(c => c.votes.length && !c.votes.find(v => v.rating === 'flag'))
49+
.map(c => ({
50+
...c,
51+
votes: c.votes.map(v => ({ ...v, rating: v.rating as Rating }))
52+
}))
53+
54+
55+
const weightedGraph = createWeightedGraph(contributions);
56+
57+
58+
res.status(200).json(weightedGraph);
59+
}
60+
61+
62+
export default allowCors(handler);

api/identities.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { VercelRequest, VercelResponse } from '@vercel/node';
2+
import { createIdentityProposals } from "@meta-cred/sc-plugin";
3+
import { Chain, } from '../zeus';
4+
import { allowCors } from "../utils/allowCors";
5+
6+
const { GRAPHQL_API_URL, GRAPHQL_ADMIN_SECRET } = process.env;
7+
8+
const handler = async (req: VercelRequest, res: VercelResponse) => {
9+
if (!GRAPHQL_API_URL || !GRAPHQL_ADMIN_SECRET) throw new Error("Missing ENV Vars");
10+
11+
const chain = Chain(GRAPHQL_API_URL, {
12+
headers: {
13+
'x-hasura-admin-secret': GRAPHQL_ADMIN_SECRET,
14+
}
15+
});
16+
17+
const { users } = await chain('query')({
18+
users: [{}, {
19+
id: true,
20+
name: true,
21+
eth_address: true,
22+
}]
23+
});
24+
25+
const proposals = createIdentityProposals(users);
26+
27+
28+
res.status(200).json(proposals);
29+
}
30+
31+
32+
export default allowCors(handler);

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
"license": "MIT",
88
"private": true,
99
"dependencies": {
10+
"@meta-cred/sc-plugin": "^0.1.4",
1011
"sourcecred": "0.10.0",
1112
"web3": "1.3.0",
1213
"web3-utils": "1.3.0"
1314
},
1415
"scripts": {
1516
"clean": "rimraf cache site",
1617
"clean-all": "yarn clean && rimraf output",
18+
"api-dev": "yarn vercel dev",
1719
"load": "dotenv sourcecred load",
1820
"go": "dotenv sourcecred go",
1921
"serve": "dotenv sourcecred serve",
@@ -24,6 +26,8 @@
2426
"dotenv-cli": "^4.0.0",
2527
"graphql": "^15.5.1",
2628
"graphql-request": "^3.4.0",
27-
"rimraf": "^3.0.2"
29+
"rimraf": "^3.0.2",
30+
"typescript": "^4.5.4",
31+
"vercel": "^23.1.2"
2832
}
2933
}

tsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"strict": true,
8+
"strictNullChecks": true,
9+
"forceConsistentCasingInFileNames": true,
10+
"noEmit": true,
11+
"esModuleInterop": true,
12+
"module": "esnext",
13+
"moduleResolution": "node",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"baseUrl": ".",
17+
},
18+
"include": ["**/*.ts"],
19+
"exclude": ["node_modules"]
20+
}

utils/allowCors.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { VercelRequest, VercelResponse } from '@vercel/node';
2+
3+
type VercelFunction = (req: VercelRequest, res: VercelResponse) => Promise<void>;
4+
5+
export const allowCors = (fn: VercelFunction): VercelFunction => async (req, res) => {
6+
res.setHeader('Access-Control-Allow-Credentials', 'true')
7+
res.setHeader('Access-Control-Allow-Origin', '*')
8+
// another common pattern
9+
// res.setHeader('Access-Control-Allow-Origin', req.headers.origin);
10+
res.setHeader('Access-Control-Allow-Methods', 'GET')
11+
res.setHeader(
12+
'Access-Control-Allow-Headers',
13+
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version'
14+
)
15+
if (req.method === 'OPTIONS') {
16+
res.status(200).end()
17+
return
18+
}
19+
return await fn(req, res)
20+
}

0 commit comments

Comments
 (0)