update app to use solana kit

This commit is contained in:
thesn10
2026-05-23 14:45:49 +02:00
parent c92e71fe81
commit 9b7391677c
13 changed files with 320 additions and 256 deletions

View File

@@ -1,40 +1,33 @@
'use client'
import { useState, useEffect, useRef, useMemo } from 'react'
import { PublicKey, Connection } from '@solana/web3.js'
import { useState, useEffect } from 'react'
import { createSolanaRpc, createSolanaRpcSubscriptions } from '@solana/kit'
import { useCluster } from '@solana/connector/react'
import { AnchorProvider } from '@coral-xyz/anchor'
import { EscrowClient, subscribeEscrow } from '@descro/sdk'
import type { EscrowAccount } from '@descro/sdk'
import { fetchMaybeEscrowAccount, subscribeEscrow } from '@descro/sdk'
import type { Address, EscrowAccount } from '@descro/sdk'
export function useEscrowDetail(pda: PublicKey | null) {
export function useEscrowDetail(pda: Address | null) {
const { cluster } = useCluster()
const connection = useMemo(
() => (cluster?.url ? new Connection(cluster.url) : null),
[cluster?.url],
)
const rpcUrl = cluster?.url ?? null
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 || !connection) {
if (!pda || !rpcUrl) {
setAccount(null)
return
}
let cancelled = false
setLoading(true)
const provider = new AnchorProvider(connection, {} as never, {})
const client = new EscrowClient(provider)
const rpc = createSolanaRpc(rpcUrl)
client
.fetchEscrow(pda)
.then((acc) => {
fetchMaybeEscrowAccount(rpc, pda)
.then((maybeAcc) => {
if (!cancelled) {
setAccount(acc)
setAccount(maybeAcc.exists ? maybeAcc.data : null)
setError(null)
}
})
@@ -45,17 +38,17 @@ export function useEscrowDetail(pda: PublicKey | null) {
if (!cancelled) setLoading(false)
})
const unsub = subscribeEscrow(connection, pda, (updated) => {
const wsUrl = rpcUrl.replace('http', 'ws')
const rpcSubscriptions = createSolanaRpcSubscriptions(wsUrl)
const unsub = subscribeEscrow(rpcSubscriptions, pda, (updated) => {
if (!cancelled) setAccount(updated)
})
unsubRef.current = unsub
return () => {
cancelled = true
unsub()
unsubRef.current = null
}
}, [pda?.toBase58(), connection])
}, [pda, rpcUrl])
return { account, loading, error }
}

View File

@@ -1,23 +1,17 @@
'use client'
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 { useState, useEffect, useCallback, useRef } from 'react'
import { createSolanaRpc } from '@solana/kit'
import { useWallet, useCluster } from '@solana/connector/react'
import { fetchEscrowsForWallet } from '@descro/sdk'
import type { EscrowAccountWithPda } from '@descro/sdk'
const POLL_INTERVAL_MS = 5000
export function useEscrows() {
const { address } = useTransactionSigner()
const { account: address } = useWallet()
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 rpcUrl = cluster?.url ?? null
const [escrows, setEscrows] = useState<EscrowAccountWithPda[]>([])
const [loading, setLoading] = useState(false)
@@ -25,23 +19,20 @@ export function useEscrows() {
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
const fetchEscrows = useCallback(async () => {
if (!publicKey || !connection) {
if (!address || !rpcUrl) {
setEscrows([])
return
}
try {
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()),
)
const rpc = createSolanaRpc(rpcUrl)
const results = await fetchEscrowsForWallet(rpc, address)
results.sort((a, b) => Number(b.account.escrowId - a.account.escrowId))
setEscrows(results)
setError(null)
} catch (err: unknown) {
setError(err instanceof Error ? err.message : String(err))
}
}, [publicKey, connection])
}, [address, rpcUrl])
useEffect(() => {
setLoading(true)