Skip to content

Commit

Permalink
try to get tally data statically from exported round data first, use …
Browse files Browse the repository at this point in the history
…IPFS gateway if not available statically
  • Loading branch information
yuetloo committed Jul 6, 2024
1 parent de5c080 commit a562b5c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 16 additions & 3 deletions vue-app/src/api/tally.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { Contract } from 'ethers'

import { FundingRound } from './abi'
import { provider, ipfsGatewayUrl } from './core'
import { provider, chain } from './core'

// https://github.com/webpack/webpack/issues/7378#issuecomment-683891615
import type { Tally } from '@clrfund/common'
import { getIpfsUrl } from '@/utils/url'
import { getLeaderboardRoundInfo } from './round'

export { Tally }

export async function getTally(fundingRoundAddress: string): Promise<Tally> {
const fundingRound = new Contract(fundingRoundAddress, FundingRound, provider)
const tallyHash = await fundingRound.tallyHash()
const response = await fetch(`${ipfsGatewayUrl}/ipfs/${tallyHash}`)
return await response.json()

try {
// try to get the tally file statically first, if not found, try the IPFS gateway
const round = await getLeaderboardRoundInfo(fundingRoundAddress, chain.name)
if (round?.tally) {
return round.tally
} else {
throw new Error('No tally data, get from IPFS gateway')
}
} catch {
const response = await fetch(getIpfsUrl(tallyHash) || '')
return await response.json()
}
}
12 changes: 12 additions & 0 deletions vue-app/src/utils/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export enum ChainId {
export type ChainInfo = {
[chainId in ChainId]: {
label: string
name: string
currency: string
logo: string
isLayer2: boolean
Expand All @@ -29,6 +30,7 @@ export type ChainInfo = {
export const CHAIN_INFO: ChainInfo = {
[ChainId.MAINNET]: {
label: 'Mainnet',
name: 'mainnet',
currency: 'ETH',
logo: 'eth.svg',
isLayer2: false,
Expand All @@ -38,6 +40,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.SEPOLIA]: {
label: 'Sepolia',
name: 'sepolia',
currency: 'ETH',
logo: 'eth.svg',
isLayer2: false,
Expand All @@ -47,6 +50,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.HARDHAT]: {
label: 'Arbitrum Hardhat',
name: 'hardhat',
currency: 'AETH',
logo: 'arbitrum.svg',
isLayer2: true,
Expand All @@ -58,6 +62,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.ARBITRUM_ONE]: {
label: 'Arbitrum',
name: 'arbitrum',
currency: 'AETH',
logo: 'arbitrum.svg',
isLayer2: true,
Expand All @@ -69,6 +74,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.ARBITRUM_RINKEBY]: {
label: 'Arbitrum Rinkeby',
name: 'arbitrum-rinkeby',
currency: 'AETH',
logo: 'arbitrum.svg',
isLayer2: true,
Expand All @@ -80,6 +86,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.ARBITRUM_GOERLI]: {
label: 'Arbitrum Goerli',
name: 'arbitrum-goerli',
currency: 'AETH',
logo: 'arbitrum.svg',
isLayer2: true,
Expand All @@ -91,6 +98,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.ARBITRUM_SEPOLIA]: {
label: 'Arbitrum Sepolia',
name: 'arbitrum-sepolia',
currency: 'AETH',
logo: 'arbitrum.svg',
isLayer2: true,
Expand All @@ -102,6 +110,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.OPTIMISM]: {
label: 'Optimism',
name: 'optimism',
currency: 'OETH',
logo: 'optimism.svg',
isLayer2: true,
Expand All @@ -113,6 +122,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.OPTIMISM_SEPOLIA]: {
label: 'Optimism Sepolia',
name: 'optimism-sepolia',
currency: 'OETH',
logo: 'optimism.svg',
isLayer2: true,
Expand All @@ -124,6 +134,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.XDAI]: {
label: 'xDai',
name: 'xdai',
currency: 'xDai',
logo: 'xdai.svg',
isLayer2: false,
Expand All @@ -135,6 +146,7 @@ export const CHAIN_INFO: ChainInfo = {
},
[ChainId.POLYGON]: {
label: 'Polygon',
name: 'polygon',
currency: 'MATIC',
logo: 'polygon.svg',
isLayer2: false,
Expand Down

0 comments on commit a562b5c

Please sign in to comment.