Skip to content

Commit e06a213

Browse files
authored
Merge pull request #44 from DimensionDev/dao-space
feat: add dao space twitter project
2 parents c9501e1 + 407cf07 commit e06a213

File tree

5 files changed

+98
-12
lines changed

5 files changed

+98
-12
lines changed

public/dao/specific-list.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"spaceId": "cakevote.eth",
4+
"spaceName": "PancakeSwap",
5+
"twitterHandler": "PancakeSwap"
6+
}
7+
]

src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import * as process from 'process'
22
import { FungibleToken, NonFungibleCollection, NonFungibleToken } from './type'
33
import { CoinGecko } from './providers/coingecko'
4-
import { initFolder, mergePublicFileToOutput, writeCollectionsToFile, writeTokensToFile } from './utils'
4+
import { initFolder, mergePublicFileToOutput, writeCollectionsToFile, writeTokensToFile, writeDAOToFile } from './utils'
55
import { CoinMarketCap } from './providers/coinmarketcap'
66
import { NFTScanCollection, NFTScanToken } from './providers/NFTScan'
7+
import { DAO } from './providers/dao'
78

89
const coinGeckoAPI = new CoinGecko()
910
const nftScanTokenAPI = new NFTScanToken()
1011
const nftScanCollectionAPI = new NFTScanCollection()
1112
const cmcAPI = new CoinMarketCap()
13+
const daoAPI = new DAO()
1214

1315
const fungibleProviders = [coinGeckoAPI, cmcAPI]
1416
const nonFungibleTokenProviders = [nftScanTokenAPI]
1517
const nonFungibleCollectionProviders = [nftScanCollectionAPI]
1618

1719
async function main() {
1820
await initFolder()
19-
await initFolder()
2021

21-
// Fetch fungible token
2222
for (const p of fungibleProviders) {
2323
let fungibleTokens: FungibleToken[] = []
2424
console.log(`Fetch the data from ${p.getProviderName()}`)
@@ -41,9 +41,6 @@ async function main() {
4141
}
4242
}
4343

44-
await mergePublicFileToOutput('fungible-tokens')
45-
46-
// Fetch nonFungible token
4744
for (const p of nonFungibleTokenProviders) {
4845
let nonFungibleTokens: NonFungibleToken[] = []
4946
console.log(`Fetch the data from ${p.getProviderName()}`)
@@ -61,9 +58,7 @@ async function main() {
6158
await writeTokensToFile(p.getProviderName(), 'non-fungible-tokens', nonFungibleTokens)
6259
}
6360
}
64-
await mergePublicFileToOutput('non-fungible-tokens')
6561

66-
// Fetch nonFungible Collections
6762
for (const p of nonFungibleCollectionProviders) {
6863
let nonFungibleCollections: NonFungibleCollection[] = []
6964
console.log(`Fetch the data from ${p.getProviderName()}`)
@@ -81,10 +76,15 @@ async function main() {
8176
await writeCollectionsToFile(p.getProviderName(), nonFungibleCollections)
8277
}
8378
}
84-
await mergePublicFileToOutput('non-fungible-collections')
8579

86-
// merge nft lucky drop file
80+
const spaces = await daoAPI.getSpaces()
81+
await writeDAOToFile(spaces)
82+
83+
await mergePublicFileToOutput('non-fungible-collections')
84+
await mergePublicFileToOutput('non-fungible-tokens')
8785
await mergePublicFileToOutput('nft-lucky-drop')
86+
await mergePublicFileToOutput('fungible-tokens')
87+
await mergePublicFileToOutput('dao')
8888

8989
console.log('Generate success!')
9090
process.exit(0)

src/providers/dao.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { DaoProvider, Space } from '../type'
2+
import axios from 'axios'
3+
import { uniqBy } from 'lodash'
4+
5+
interface RawSpace {
6+
id: string,
7+
name: string,
8+
avatar: string,
9+
twitter: string,
10+
followersCount: number,
11+
validation: { name: string }
12+
}
13+
14+
export class DAO implements DaoProvider {
15+
async getSpaces(): Promise<Space[]> {
16+
const allSettled = await Promise.allSettled(Array.from(Array(14)).map(async (x, i) => {
17+
return axios.post<{
18+
data: {
19+
spaces: RawSpace[]
20+
}
21+
}>('https://hub.snapshot.org/graphql', {
22+
operationName: "Spaces",
23+
query: `
24+
query Spaces {
25+
spaces(
26+
first: 1000,
27+
skip: ${i * 1000},
28+
orderDirection: asc
29+
) {
30+
id
31+
name
32+
avatar
33+
twitter
34+
followersCount
35+
validation {
36+
params
37+
name
38+
}
39+
}
40+
}
41+
`,
42+
variables: null
43+
})
44+
}))
45+
46+
const rawSpaces = allSettled.flatMap(
47+
(x) => (x.status === 'fulfilled' ? x.value.data.data.spaces ?? undefined : undefined)
48+
).filter(x => x) as RawSpace[]
49+
50+
return uniqBy(rawSpaces.filter(x => x.avatar && x.followersCount > 499 && x.validation.name !== 'any' && x.twitter).map(x => ({
51+
spaceId: x.id,
52+
spaceName: x.name,
53+
twitterHandler: x.twitter
54+
}) as Space), x => x.spaceId)
55+
}
56+
}

src/type.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ export interface NonFungibleCollection {
160160
}
161161
}
162162

163+
export interface Space {
164+
spaceId: string
165+
spaceName: string
166+
twitterHandler: string
167+
}
168+
163169
export interface FungibleTokenProvider {
164170
getTopTokens(): Promise<FungibleToken[]>
165171
getProviderName(): SourceType
@@ -174,3 +180,7 @@ export interface NonFungibleCollectionProvider {
174180
getCollections(): Promise<NonFungibleCollection[]>
175181
getProviderName(): SourceType
176182
}
183+
184+
export interface DaoProvider {
185+
getSpaces(): Promise<Space[]>
186+
}

src/utils/file.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
22
import fs from 'node:fs/promises'
3-
import { FungibleToken, NonFungibleCollection, NonFungibleToken, SourceType } from '../type'
3+
import { FungibleToken, NonFungibleCollection, NonFungibleToken, SourceType, Space } from '../type'
44

55
// @ts-ignore
66
export const getOutputDir = (type) => path.join(process.env.PWD, `output/${type}`)
@@ -12,6 +12,7 @@ export async function initFolder() {
1212
await createIfNotExist(getOutputDir('non-fungible-tokens'))
1313
await createIfNotExist(getOutputDir('non-fungible-collections'))
1414
await createIfNotExist(getOutputDir('nft-lucky-drop'))
15+
await createIfNotExist(getOutputDir('dao'))
1516
}
1617

1718
export async function writeTokensToFile(
@@ -42,8 +43,20 @@ export async function writeCollectionsToFile(provider: SourceType, tokens: Fungi
4243
)
4344
}
4445

46+
export async function writeDAOToFile(spaces: Space[]) {
47+
if (!spaces.length) throw new Error(`Forbid writing the empty data of DAO to output`)
48+
49+
await fs.writeFile(
50+
path.join(getOutputDir('dao'), `spaces.json`),
51+
JSON.stringify(spaces, undefined, 2),
52+
{
53+
encoding: 'utf-8',
54+
},
55+
)
56+
}
57+
4558
export async function mergePublicFileToOutput(
46-
type: 'fungible-tokens' | 'non-fungible-tokens' | 'non-fungible-collections' | 'nft-lucky-drop',
59+
type: 'fungible-tokens' | 'non-fungible-tokens' | 'non-fungible-collections' | 'nft-lucky-drop' | 'dao',
4760
) {
4861
const src = path.join(`${getPublicDir(type)}`, 'specific-list.json')
4962
const dest = path.join(`${getOutputDir(type)}`, 'specific-list.json')

0 commit comments

Comments
 (0)