Skip to content

Commit

Permalink
Merge branch 'master' into metric-linking
Browse files Browse the repository at this point in the history
  • Loading branch information
leolower authored Apr 23, 2020
2 parents 2a2882a + bee0815 commit 190689e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/containers/Executive/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ function ExecutiveInfo(props) {
useEffect(() => {
if (excutivesData.data && excutivesData.data.spells) {
getMakerDaoData()
.then(({ executiveVotes }) => {
.then(({ spellsInfo }) => {
setData(
excutivesData.data.spells.map(spell => {
const proposal = executiveVotes.find(prop => {
const proposal = spellsInfo.find(prop => {
return prop.source.toLowerCase() === spell.id.toLowerCase()
})
return {
Expand Down
4 changes: 2 additions & 2 deletions src/containers/Vote/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ function VoteInfo(props: Props) {

useEffect(() => {
getMakerDaoData()
.then(({ executiveVotes }) => {
const vote = executiveVotes.find(el => {
.then(({ spellsInfo }) => {
const vote = spellsInfo.find(el => {
return el.source.toLowerCase() === voteId.toLowerCase()
})
setMakerData(vote)
Expand Down
4 changes: 2 additions & 2 deletions src/containers/VoterHistory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ function VoterHistory(props: Props) {
useEffect(() => {
if (historyData.data && historyData.data.executives) {
getMakerDaoData()
.then(({ executiveVotes }) => {
.then(({ spellsInfo }) => {
setExecutives(exs =>
exs.map(spell => {
const proposal = executiveVotes.find(prop => prop.source.toLowerCase() === spell.id.toLowerCase())
const proposal = spellsInfo.find(prop => prop.source.toLowerCase() === spell.id.toLowerCase())
return {
...spell,
...proposal,
Expand Down
46 changes: 17 additions & 29 deletions src/utils/makerdao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ import BigNumber from 'bignumber.js'
import { getUnixTime } from 'date-fns'
const Hash = require('ipfs-only-hash')

const network = 'mainnet'
const prod = 'https://cms-gov.makerfoundation.com'
const path = 'content/governance-dashboard'
const spellsPath = 'content/all-spells'
const rawUri = 'https://raw.githubusercontent.com/makerdao/community/master/governance/polls'

const POLLING_EMITTER = '0xF9be8F0945acDdeeDaA64DFCA5Fe9629D0CF8E5D' // mainnet

const MKR_SUPPLY_API = 'https://api.etherscan.io/api'
const PRECISION = new BigNumber('10').exponentiatedBy(18)

const check = async res => {
const check = async (res, resource) => {
if (!res.ok) {
throw new Error(`unable to fetch topics: ${res.status} - ${await res.text()}`)
throw new Error(`unable to fetch ${resource}: ${res.status} - ${await res.text()}`)
}
}

Expand All @@ -39,29 +38,14 @@ export const promiseRetry = ({ times = 3, fn, delay = 500, args = [] }) => {
)
}

const fetchNetwork = async (url, network = 'mainnet') => {
const fetchNetwork = async (url, resource, path, network = 'mainnet') => {
const res = await fetch(`${url}/${path}?network=${network}`)
await check(res)
await check(res, resource)
return await res.json()
}

const fetchTopics = async network => {
return fetchNetwork(prod, network)
}

function extractProposals(topics, network) {
const executiveTopics = topics.filter(t => t.govVote === false)
return executiveTopics.reduce((acc, topic) => {
const proposals = topic.proposals.map(({ source, ...otherProps }) => ({
...otherProps,
source: source.startsWith('{') ? JSON.parse(source)[network] : source,
active: topic.active,
govVote: topic.govVote,
topicKey: topic.key,
topicTitle: topic.topic,
}))
return acc.concat(proposals)
}, [])
const fetchSpells = async network => {
return fetchNetwork(prod, 'spells', spellsPath, network)
}

export const formatHistoricalPolls = topics => {
Expand Down Expand Up @@ -94,24 +78,28 @@ export const formatHistoricalPolls = topics => {
}

export async function getMakerDaoData() {
const topics = await promiseRetry({
fn: fetchTopics,
const allSpells = await promiseRetry({
fn: fetchSpells,
times: 4,
delay: 1,
})

const executiveVotes = extractProposals(topics, network)
const historicalPolls = formatHistoricalPolls(topics)
const spellsInfo = allSpells.map(({ source, title, proposal_blurb, about }) => ({
source,
title,
proposal_blurb,
about,
}))

return { executiveVotes, historicalPolls }
return { spellsInfo }
}

// Polls data
const fetchPollFromUrl = async url => {
let customUri = url
if (url.includes('github.com')) customUri = `${rawUri}/${url.substring(url.lastIndexOf('/') + 1)}`
const res = await fetch(customUri)
await check(res)
await check(res, 'topics')
const contentType = res.headers.get('content-type')
if (!contentType) return null
if (contentType.indexOf('application/json') !== -1) {
Expand Down

0 comments on commit 190689e

Please sign in to comment.