descro playgound & sdk

This commit is contained in:
thesn10
2026-05-18 22:02:41 +02:00
parent 84a2ec4200
commit 2a89352212
37 changed files with 8775 additions and 12088 deletions

View File

@@ -0,0 +1,56 @@
'use client'
import { useState, useEffect, useRef } from 'react'
import { PublicKey } from '@solana/web3.js'
import { useConnection } from '@solana/wallet-adapter-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 [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) {
setAccount(null)
return
}
let cancelled = false
setLoading(true)
const provider = new AnchorProvider(connection, {} as any, {})
const client = new EscrowClient(provider)
client
.fetchEscrow(pda)
.then((acc) => {
if (!cancelled) {
setAccount(acc)
setError(null)
}
})
.catch((err: unknown) => {
if (!cancelled) setError(err instanceof Error ? err.message : String(err))
})
.finally(() => {
if (!cancelled) setLoading(false)
})
const unsub = subscribeEscrow(connection, pda, (updated) => {
if (!cancelled) setAccount(updated)
})
unsubRef.current = unsub
return () => {
cancelled = true
unsub()
unsubRef.current = null
}
}, [pda?.toBase58(), connection])
return { account, loading, error }
}

View File

@@ -0,0 +1,50 @@
'use client'
import { useState, useEffect, useCallback, useRef } from 'react'
import { useWallet, useConnection } from '@solana/wallet-adapter-react'
import { AnchorProvider } from '@coral-xyz/anchor'
import { EscrowClient } from '@descro/sdk'
import type { EscrowAccountWithPda } from '@descro/sdk'
const POLL_INTERVAL_MS = 5000
export function useEscrows() {
const { publicKey } = useWallet()
const { connection } = useConnection()
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) {
setEscrows([])
return
}
try {
const provider = new AnchorProvider(connection, {} as any, {})
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())
)
setEscrows(results)
setError(null)
} catch (err: unknown) {
setError(err instanceof Error ? err.message : String(err))
}
}, [publicKey, connection])
useEffect(() => {
setLoading(true)
fetchEscrows().finally(() => setLoading(false))
intervalRef.current = setInterval(fetchEscrows, POLL_INTERVAL_MS)
return () => {
if (intervalRef.current !== null) clearInterval(intervalRef.current)
}
}, [fetchEscrows])
return { escrows, loading, error, refresh: fetchEscrows }
}