forked from pancakeswap/pancake-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseGetWalletNfts.ts
119 lines (98 loc) · 3.05 KB
/
useGetWalletNfts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { useWeb3React } from '@web3-react/core'
import { useEffect, useReducer } from 'react'
import { getPancakeRabbitContract } from 'utils/contractHelpers'
import makeBatchRequest from 'utils/makeBatchRequest'
const pancakeRabbitsContract = getPancakeRabbitContract()
export type NftMap = {
[key: number]: {
tokenUri: string
tokenIds: number[]
}
}
type Action = { type: 'set_nfts'; data: NftMap } | { type: 'reset' } | { type: 'refresh'; timestamp: number }
type State = {
isLoading: boolean
nfts: NftMap
lastUpdated: number
}
const initialState: State = {
isLoading: true,
nfts: {},
lastUpdated: Date.now(),
}
const reducer = (state: State, action: Action) => {
switch (action.type) {
case 'set_nfts':
return {
...initialState,
isLoading: false,
nfts: action.data,
}
case 'refresh':
return {
...initialState,
lastUpdated: action.timestamp,
}
case 'reset':
return {
...initialState,
isLoading: false,
}
default:
return state
}
}
const useGetWalletNfts = () => {
const [state, dispatch] = useReducer(reducer, initialState)
const { account } = useWeb3React()
const { lastUpdated } = state
useEffect(() => {
const fetchNfts = async () => {
try {
const balanceOf = await pancakeRabbitsContract.methods.balanceOf(account).call()
if (balanceOf > 0) {
const getTokenIdAndBunnyId = async (index: number) => {
try {
const { tokenOfOwnerByIndex, getBunnyId, tokenURI } = pancakeRabbitsContract.methods
const tokenId = await tokenOfOwnerByIndex(account, index).call()
const [bunnyId, tokenUri] = await makeBatchRequest([getBunnyId(tokenId).call, tokenURI(tokenId).call])
return [Number(bunnyId), Number(tokenId), tokenUri]
} catch (error) {
return null
}
}
const tokenIdPromises = []
for (let i = 0; i < balanceOf; i++) {
tokenIdPromises.push(getTokenIdAndBunnyId(i))
}
const tokenIdsOwnedByWallet = await Promise.all(tokenIdPromises)
const nfts: NftMap = tokenIdsOwnedByWallet.reduce((accum, association) => {
if (!association) {
return accum
}
const [bunnyId, tokenId, tokenUri] = association
return {
...accum,
[bunnyId]: {
tokenUri,
tokenIds: accum[bunnyId] ? [...accum[bunnyId].tokenIds, tokenId] : [tokenId],
},
}
}, {})
dispatch({ type: 'set_nfts', data: nfts })
} else {
// Reset it in case of wallet change
dispatch({ type: 'reset' })
}
} catch (error) {
dispatch({ type: 'reset' })
}
}
if (account) {
fetchNfts()
}
}, [account, lastUpdated, dispatch])
const refresh = () => dispatch({ type: 'refresh', timestamp: Date.now() })
return { ...state, refresh }
}
export default useGetWalletNfts