use solana connector
This commit is contained in:
@@ -1,30 +1,35 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useRef } from 'react'
|
||||
import { PublicKey } from '@solana/web3.js'
|
||||
import { useConnection } from '@solana/wallet-adapter-react'
|
||||
import { useState, useEffect, useRef, useMemo } from 'react'
|
||||
import { PublicKey, Connection } from '@solana/web3.js'
|
||||
import { useCluster } from '@solana/connector/react'
|
||||
import { AnchorProvider } from '@coral-xyz/anchor'
|
||||
import { EscrowClient, subscribeEscrow } from '@descro/sdk'
|
||||
import type { EscrowAccount } from '@descro/sdk'
|
||||
|
||||
export function useEscrowDetail(pda: PublicKey | null) {
|
||||
const { connection } = useConnection()
|
||||
const { cluster } = useCluster()
|
||||
const connection = useMemo(
|
||||
() => (cluster?.url ? new Connection(cluster.url) : null),
|
||||
[cluster?.url],
|
||||
)
|
||||
|
||||
const [account, setAccount] = useState<EscrowAccount | null>(null)
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const unsubRef = useRef<(() => void) | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!pda) {
|
||||
if (!pda || !connection) {
|
||||
setAccount(null)
|
||||
return
|
||||
}
|
||||
|
||||
let cancelled = false
|
||||
|
||||
setLoading(true)
|
||||
const provider = new AnchorProvider(connection, {} as any, {})
|
||||
const provider = new AnchorProvider(connection, {} as never, {})
|
||||
const client = new EscrowClient(provider)
|
||||
|
||||
client
|
||||
.fetchEscrow(pda)
|
||||
.then((acc) => {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback, useRef } from 'react'
|
||||
import { useWallet, useConnection } from '@solana/wallet-adapter-react'
|
||||
import { useState, useEffect, useCallback, useRef, useMemo } from 'react'
|
||||
import { Connection, PublicKey } from '@solana/web3.js'
|
||||
import { useTransactionSigner, useCluster } from '@solana/connector/react'
|
||||
import { AnchorProvider } from '@coral-xyz/anchor'
|
||||
import { EscrowClient } from '@descro/sdk'
|
||||
import type { EscrowAccountWithPda } from '@descro/sdk'
|
||||
@@ -9,24 +10,31 @@ import type { EscrowAccountWithPda } from '@descro/sdk'
|
||||
const POLL_INTERVAL_MS = 5000
|
||||
|
||||
export function useEscrows() {
|
||||
const { publicKey } = useWallet()
|
||||
const { connection } = useConnection()
|
||||
const { address } = useTransactionSigner()
|
||||
const { cluster } = useCluster()
|
||||
|
||||
const publicKey = useMemo(() => (address ? new PublicKey(address) : null), [address])
|
||||
const connection = useMemo(
|
||||
() => (cluster?.url ? new Connection(cluster.url) : null),
|
||||
[cluster?.url],
|
||||
)
|
||||
|
||||
const [escrows, setEscrows] = useState<EscrowAccountWithPda[]>([])
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
|
||||
|
||||
const fetchEscrows = useCallback(async () => {
|
||||
if (!publicKey) {
|
||||
if (!publicKey || !connection) {
|
||||
setEscrows([])
|
||||
return
|
||||
}
|
||||
try {
|
||||
const provider = new AnchorProvider(connection, {} as any, {})
|
||||
const provider = new AnchorProvider(connection, {} as never, {})
|
||||
const client = new EscrowClient(provider)
|
||||
const results = await client.fetchEscrowsForWallet(publicKey)
|
||||
results.sort((a, b) =>
|
||||
Number(b.account.escrowId.toString()) - Number(a.account.escrowId.toString())
|
||||
results.sort(
|
||||
(a, b) => Number(b.account.escrowId.toString()) - Number(a.account.escrowId.toString()),
|
||||
)
|
||||
setEscrows(results)
|
||||
setError(null)
|
||||
@@ -38,9 +46,7 @@ export function useEscrows() {
|
||||
useEffect(() => {
|
||||
setLoading(true)
|
||||
fetchEscrows().finally(() => setLoading(false))
|
||||
|
||||
intervalRef.current = setInterval(fetchEscrows, POLL_INTERVAL_MS)
|
||||
|
||||
return () => {
|
||||
if (intervalRef.current !== null) clearInterval(intervalRef.current)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user