useSolanaClient
This commit is contained in:
@@ -1,28 +1,26 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect } from 'react'
|
||||
import { createSolanaRpc, createSolanaRpcSubscriptions } from '@solana/kit'
|
||||
import { useCluster } from '@solana/connector/react'
|
||||
import { useSolanaClient } from '@solana/connector/react'
|
||||
import { fetchMaybeEscrowAccount, subscribeEscrow } from '@descro/sdk'
|
||||
import type { Address, EscrowAccount } from '@descro/sdk'
|
||||
|
||||
export function useEscrowDetail(pda: Address | null) {
|
||||
const { cluster } = useCluster()
|
||||
const rpcUrl = cluster?.url ?? null
|
||||
const { client } = useSolanaClient()
|
||||
|
||||
const [account, setAccount] = useState<EscrowAccount | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!pda || !rpcUrl) {
|
||||
if (!pda || !client) {
|
||||
setAccount(null)
|
||||
return
|
||||
}
|
||||
|
||||
let cancelled = false
|
||||
setLoading(true)
|
||||
const rpc = createSolanaRpc(rpcUrl)
|
||||
const { rpc, rpcSubscriptions } = client
|
||||
|
||||
fetchMaybeEscrowAccount(rpc, pda)
|
||||
.then((maybeAcc) => {
|
||||
@@ -38,8 +36,6 @@ export function useEscrowDetail(pda: Address | null) {
|
||||
if (!cancelled) setLoading(false)
|
||||
})
|
||||
|
||||
const wsUrl = rpcUrl.replace('http', 'ws')
|
||||
const rpcSubscriptions = createSolanaRpcSubscriptions(wsUrl)
|
||||
const unsub = subscribeEscrow(rpcSubscriptions, pda, (updated) => {
|
||||
if (!cancelled) setAccount(updated)
|
||||
})
|
||||
@@ -48,7 +44,7 @@ export function useEscrowDetail(pda: Address | null) {
|
||||
cancelled = true
|
||||
unsub()
|
||||
}
|
||||
}, [pda, rpcUrl])
|
||||
}, [pda, client])
|
||||
|
||||
return { account, loading, error }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user