Skip to content

Commit

Permalink
feat: Fetch collections (#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
RabbitDoge authored and hachiojidev committed Sep 24, 2021
1 parent 4676091 commit c48e939
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 22 deletions.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ REACT_APP_NODE_4 = "https://nodes.pancakeswap.com/"
REACT_APP_GRAPH_API_PROFILE = "https://api.thegraph.com/subgraphs/name/pancakeswap/profile"
REACT_APP_GRAPH_API_PREDICTION = "https://api.thegraph.com/subgraphs/name/pancakeswap/prediction-v2"
REACT_APP_GRAPH_API_LOTTERY = "https://api.thegraph.com/subgraphs/name/pancakeswap/lottery"
REACT_APP_GRAPH_API_NFT_MARKET = "https://api.thegraph.com/subgraphs/name/chefnyan/nft-markets"

REACT_APP_SNAPSHOT_BASE_URL = "https://testnet.snapshot.org"
REACT_APP_SNAPSHOT_VOTING_API = "https://xtjyd0liqe.execute-api.ap-northeast-1.amazonaws.com/dev/api"
1 change: 1 addition & 0 deletions .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ REACT_APP_NODE_4 = "https://nodes.pancakeswap.com/"
REACT_APP_GRAPH_API_PROFILE = "https://api.thegraph.com/subgraphs/name/pancakeswap/profile"
REACT_APP_GRAPH_API_PREDICTION = "https://api.thegraph.com/subgraphs/name/pancakeswap/prediction-v2"
REACT_APP_GRAPH_API_LOTTERY = "https://api.thegraph.com/subgraphs/name/pancakeswap/lottery"
REACT_APP_GRAPH_API_NFT_MARKET = ""

REACT_APP_SNAPSHOT_BASE_URL = "https://hub.snapshot.org"
REACT_APP_SNAPSHOT_VOTING_API = "https://voting-api.pancakeswap.info/api"
1 change: 1 addition & 0 deletions src/config/constants/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export const GRAPH_API_PREDICTION_V1 = 'https://api.thegraph.com/subgraphs/name/

export const INFO_CLIENT = 'https://bsc.streamingfast.io/subgraphs/name/pancakeswap/exchange-v2'
export const BLOCKS_CLIENT = 'https://api.thegraph.com/subgraphs/name/pancakeswap/blocks'
export const GRAPH_API_NFTMARKET = process.env.REACT_APP_GRAPH_API_NFT_MARKET
2 changes: 2 additions & 0 deletions src/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import mint from './mint/reducer'
import lists from './lists/reducer'
import burn from './burn/reducer'
import multicall from './multicall/reducer'
import nftMarketReducer from './nftMarket/reducer'

const PERSISTED_KEYS: string[] = ['user', 'transactions', 'lists', 'profile', 'collectibles']

Expand All @@ -37,6 +38,7 @@ const store = configureStore({
voting: votingReducer,
lottery: lotteryReducer,
info: infoReducer,
nftMarket: nftMarketReducer,

// Exchange
user,
Expand Down
29 changes: 29 additions & 0 deletions src/state/nftMarket/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { request, gql } from 'graphql-request'
import { GRAPH_API_NFTMARKET } from 'config/constants/endpoints'

export const getCollections = async (): Promise<any[]> => {
try {
const res = await request(
GRAPH_API_NFTMARKET,
gql`
{
collections {
id
name
symbol
active
totalVolumeBNB
numberTokensListed
tradingFee
creatorFee
}
}
`,
)

return res.collections
} catch (error) {
console.error('Failed to fetch NFT collections', error)
return []
}
}
10 changes: 10 additions & 0 deletions src/state/nftMarket/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useEffect } from 'react'
import { useAppDispatch } from 'state'
import { fetchCollections } from './reducer'

export const useFetchCollections = () => {
const dispatch = useAppDispatch()
useEffect(() => {
dispatch(fetchCollections())
}, [dispatch])
}
15 changes: 0 additions & 15 deletions src/state/nftMarket/index.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/state/nftMarket/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import { getCollections } from './helpers'
import { State, Collection } from './types'

const initialState: State = {
data: {
collections: [],
users: [],
},
}

export const fetchCollections = createAsyncThunk<Collection[]>('nft/fetchFarmsPublicDataAsync', async () => {
const collections = await getCollections()
return collections
})

export const NftMarket = createSlice({
name: 'NftMarket',
initialState,
reducers: {},
extraReducers: (builder) => {
builder.addCase(fetchCollections.fulfilled, (state, action) => {
state.data.collections = action.payload
})
},
})

export default NftMarket.reducer
13 changes: 6 additions & 7 deletions src/state/nftMarket/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { BigNumberish } from 'ethers'
// Users -> Nft tokens IDs

export interface State {
collections: Collection[]
users: User[]
data: {
collections: Collection[]
users: User[]
}
}

export interface Transaction {
Expand All @@ -30,18 +32,15 @@ export interface NFT {
}

export interface Collection {
address: string
id: string
name: string
symbol: string
active: boolean
totalTrades: BigNumberish
totalVolumeBNB: BigNumberish
numberTokensListed: BigNumberish
nfts: NFT[]
creatorAddress: string
tradingFee: BigNumberish
creatorFee: BigNumberish
whitelistChecker: string
nfts: NFT[]
}

export interface User {
Expand Down
2 changes: 2 additions & 0 deletions src/views/nft/market/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'
import { useFetchCollections } from 'state/nftMarket/hooks'

const Market = () => {
useFetchCollections()
return <div>Market</div>
}

Expand Down

0 comments on commit c48e939

Please sign in to comment.