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

Feat/snapshot #66

Merged
merged 2 commits into from
May 9, 2023
Merged
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
4 changes: 4 additions & 0 deletions Implementations/API/backend/functions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export const gnosisGraphConfig: { [key: string]: any } = {

export const gnosisApiConfig: { [key: string]: any } = {
'1': 'https://safe-transaction-mainnet.safe.global/api/v1'
}

export const snapshotApiConfig: { [key: string]: any } = {
'1': 'https://hub.snapshot.org/graphql'
}
74 changes: 74 additions & 0 deletions Implementations/API/backend/functions/snapshot/getMembers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { APIGatewayProxyHandlerV2 } from 'aws-lambda'
import { snapshotApiConfig } from 'functions/config'
import fetch from 'node-fetch'

function apiRequest(path: string, method: string, data: any) {
return fetch(path, {
headers: {
'Content-Type': 'application/json',
},
method,
redirect: 'follow',
body: JSON.stringify(data),
}).then((res) => res.json())
}

export const handler: APIGatewayProxyHandlerV2 = async (event) => {

console.log({graphConfig: snapshotApiConfig})
const path = snapshotApiConfig['1']
if (!path) return { statusCode: 400, message: 'Missing config for network' }

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

const template = {
'@context': {
'@vocab': 'http://daostar.org/',
},
type: 'DAO',
name: eventId,
}

const query = `query GetMembers($dao: String!) {
space(id: $dao) {
id
name
about
network
symbol
members
}
}`


const data = {
query,
variables: { dao: eventId },
}
console.log({data})

const res = (await apiRequest(path, 'POST', data)) as any
console.log({ res, space: res.data.space })

if (!res.data.space) return {statusCode: 404, message: 'DAO not found'}
const members = res.data.space.members

console.log({ members })

const membersFormatted = members.map((m: string) => {
return { id: m, type: 'EthereumAddress' }
})

const transformed = { members: membersFormatted, ...template }

return transformed
? {
statusCode: 200,
body: JSON.stringify(transformed),
}
: {
statusCode: 404,
body: JSON.stringify({ error: true }),
}
}
114 changes: 114 additions & 0 deletions Implementations/API/backend/functions/snapshot/getProposals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { APIGatewayProxyHandlerV2 } from 'aws-lambda'
import { snapshotApiConfig } from 'functions/config'
import fetch from 'node-fetch'

function apiRequest(path: string, method: string, data: any) {
return fetch(path, {
headers: {
'Content-Type': 'application/json',
},
method,
redirect: 'follow',
body: JSON.stringify(data),
}).then((res) => res.json())
}

const calculateSatatus = (
processed: boolean,
cancelled: boolean,
passed: boolean,
votingStarts: number,
votingEnds: number,
graceEnds: number,
sponsored: boolean
) => {
const now = Date.now()
if (processed) {
return 'processed'
} else if (passed) {
return 'passed'
} else if (cancelled) {
return 'cancelled'
} else if (now >= votingEnds && graceEnds > now) {
return 'grace'
} else if (now >= votingStarts && votingEnds > now) {
return 'voting'
} else if (sponsored) {
return 'sponsored'
} else {
return 'submitted'
}
}

export const handler: APIGatewayProxyHandlerV2 = async (event) => {
console.log({graphConfig: snapshotApiConfig})
const path = snapshotApiConfig['1']
if (!path) return { statusCode: 400, message: 'Missing config for network' }

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

const template = {
'@context': {
'@vocab': 'http://daostar.org/',
},
type: 'DAO',
name: eventId,
}

const query = `query GetProposals($dao: String!) {
proposals (
where: {
space_in: [$dao],
},
orderBy: "created",
orderDirection: desc
) {
id
title
body
choices
start
end
snapshot
state
author
space {
id
name
}
}
}`

const data = {
query,
variables: { dao: eventId },
}

const res = (await apiRequest(path, 'POST', data)) as any

if (!(res.data.proposals && res.data.proposals.length > 0)) return { statusCode: 404, message: 'DAO not found' }
console.log({ data: res.data.proposals })
const proposals = res.data.proposals

const proposalsFormatted = proposals.map((p: any) => {
const status = p.state
return {
id: p.id,
type: 'proposal',
status: status,
}
})

const transformed = { proposals: proposalsFormatted, ...template }

return transformed
? {
statusCode: 200,
body: JSON.stringify(transformed),
}
: {
statusCode: 404,
body: JSON.stringify({ error: true }),
}
}
2 changes: 2 additions & 0 deletions Implementations/API/stacks/MyStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export function MyStack({ stack }: StackContext) {
'GET /gnosis/activities/{network}/{id}': 'functions/gnosis/getActivityLogs.handler',
'GET /daodao/members/{network}/{id}': 'functions/daodao/getMembers.handler',
'GET /daodao/proposals/{network}/{id}': 'functions/daodao/getProposals.handler',
'GET /snapshot/members/{id}': 'functions/snapshot/getMembers.handler',
'GET /snapshot/proposals/{id}': 'functions/snapshot/getProposals.handler',
},
customDomain: {
domainName: 'services.daostar.org',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const FRAMEWORK_URIs = {
membersURI: (address, network) => `https://services.daostar.org/api/v1/gnosis/members/${network}/${address}`,
proposalsURI: (address, network) => `https://services.daostar.org/api/v1/gnosis/proposals/${network}/${address}`,
},
snapshot: {
membersURI: (address, network) => `https://services.daostar.org/api/v1/snapshot/members/${address}`,
proposalsURI: (address, network) => `https://services.daostar.org/api/v1/snapshot/proposals/${address}`,
},
}

export default FRAMEWORK_URIs;
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ const RegistrationForm = ({ toggleRegScreen, setRegistrationData }) => {
{ label: 'Moloch', value: 'molochv2' },
{ label: 'Safe', value: 'safe' },
{ label: 'DAODAO', value: 'daodao' },
{ label: 'Snapshot', value: 'snapshot' },
]}
/>
)
Expand Down Expand Up @@ -230,7 +231,7 @@ const RegistrationForm = ({ toggleRegScreen, setRegistrationData }) => {
<Divider vertical={true} />
<div className="wizard-row wizard-row-flex">
<FormGroup label="Contract address">{EthNetworksSelect}</FormGroup>
<InputGroup fill placeholder="Enter DAO contract address" value={daoContractAddress} onChange={onChangeDaoContractAddress} />
<InputGroup fill placeholder="Enter DAO address or id (eg ENS for snapshot)" value={daoContractAddress} onChange={onChangeDaoContractAddress} />
</div>
<div className="wizard-row">
<FormGroup label="Name" labelFor="name" fill>
Expand Down