forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-token-verification.ts
More file actions
89 lines (76 loc) · 2.32 KB
/
Copy pathuse-token-verification.ts
File metadata and controls
89 lines (76 loc) · 2.32 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
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
'use client'
import { useState, useEffect, useCallback } from 'react'
import { getTokenMetadata } from '@/lib/contract'
import { isVerifiedToken, isFavoriteToken, toggleFavoriteToken } from '@/lib/stellar'
import type { TokenInfo } from '@/types/stream'
export interface TokenVerificationResult {
isValid: boolean
isVerified: boolean
isFavorite: boolean
metadata: TokenInfo | null
warning: string | null
loading: boolean
error: string | null
toggleFavorite: () => void
}
export function useTokenVerification(
address: string,
): TokenVerificationResult {
const [metadata, setMetadata] = useState<TokenInfo | null>(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState<string | null>(null)
const verified = isVerifiedToken(address)
const favorite = isFavoriteToken(address)
const verify = useCallback(async () => {
if (!address || address.length === 0) {
setMetadata(null)
return
}
setLoading(true)
setError(null)
try {
const info = await getTokenMetadata(address)
if (!info) {
setError('Invalid token address or contract')
setMetadata(null)
return
}
setMetadata(info)
if (info.decimals === 0) {
setError('Token has 0 decimals - may not be a valid SEP-41 token')
} else if (!info.symbol || info.symbol.length === 0) {
setError('Token has no symbol - may not be a valid SEP-41 token')
}
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to verify token'
setError(message)
setMetadata(null)
} finally {
setLoading(false)
}
}, [address])
useEffect(() => {
verify()
}, [verify])
const toggleFavorite = useCallback(() => {
toggleFavoriteToken(address)
}, [address])
let warning: string | null = null
if (metadata && !verified) {
if (metadata.decimals === 0 || !metadata.symbol) {
warning = 'This token appears to be invalid or malformed. Verify the address is correct before creating a stream.'
} else {
warning = 'This token is not verified. Only proceed if you trust this token address.'
}
}
return {
isValid: error === null && metadata !== null,
isVerified: verified,
isFavorite: favorite,
metadata,
warning,
loading,
error,
toggleFavorite,
}
}