forked from Heliobond/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseVault.ts
More file actions
63 lines (54 loc) · 2.12 KB
/
Copy pathuseVault.ts
File metadata and controls
63 lines (54 loc) · 2.12 KB
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
'use client'
import { useCallback, useEffect, useState } from 'react'
import { HB_DATA } from '../data'
import { fetchSharePrice, fetchTotalAssets } from './vault'
import { useWallet } from './WalletProvider'
export interface VaultState {
fetchedAt: Date | null
sharePrice: number
totalAssets: number
loading: boolean
error: string | null
refresh: () => void
}
export function useVault(): VaultState {
const { address, isDemo, network: walletNetwork } = useWallet()
// Allow explicit override via env var, otherwise use wallet's network, fallback to public
const network = (process.env.NEXT_PUBLIC_STELLAR_NETWORK?.toLowerCase() ||
walletNetwork?.toLowerCase() ||
'public') as 'public' | 'testnet'
const [sharePrice, setSharePrice] = useState(HB_DATA.pool.sharePrice)
const [totalAssets, setTotalAssets] = useState(HB_DATA.pool.totalAssets)
const [loading, setLoading] = useState(!!process.env.NEXT_PUBLIC_VAULT_CONTRACT_ID && !isDemo)
const [error, setError] = useState<string | null>(null)
const [fetchedAt, setFetchedAt] = useState<Date | null>(new Date())
const [tick, setTick] = useState(0)
const refresh = useCallback(() => setTick((t) => t + 1), [])
useEffect(() => {
const contractId = process.env.NEXT_PUBLIC_VAULT_CONTRACT_ID
if (!contractId || isDemo || !address) {
setLoading(false)
if (!fetchedAt) setFetchedAt(new Date())
return
}
setLoading(true)
setError(null)
Promise.all([fetchSharePrice(network), fetchTotalAssets(network)])
.then(([price, assets]) => {
setSharePrice(price)
setTotalAssets(assets)
setFetchedAt(new Date())
})
.catch((e: unknown) => {
setError(e instanceof Error ? e.message : 'Could not read vault')
setFetchedAt(new Date())
})
.finally(() => setLoading(false))
}, [address, isDemo, tick, network])
useEffect(() => {
if (!process.env.NEXT_PUBLIC_VAULT_CONTRACT_ID || isDemo) return
const id = setInterval(() => refresh(), 30000)
return () => clearInterval(id)
}, [isDemo, refresh])
return { sharePrice, totalAssets, loading, error, fetchedAt, refresh }
}